!7 !include #1007303 // CirclesAndLines static CirclesAndLines cal; static Circle a, b, c, d; static Canvas canvas; static JButton btnThink, btnStartOver; static JTextArea textArea; volatile sbool thinking; static int step; static S status, initial = "I wonder: Is Donald good or bad?"; extend Base { new L traits; bool hasTrait(S t) { ret containsIC(traits(), t); } L traits() { if (text != null && neq(first(traits), text)) traits.add(0, text); ret traits; } S textForRender() { L traits = traits(); if (l(traits) <= 1) ret text; ret text + " [" + join(", ", dropFirst(traits)) + "]"; } } p { substance("Magellan"); cal = new CirclesAndLines; a = cal.addCircle(#1007291, 0.25, 0.25, "DONALD"); b = cal.addCircle(#1007292, 0.75, 0.25, "BOMBS"); c = cal.addCircle(#1007314, 0.25, 0.715, "GOOD"); d = cal.addCircle(#1007319, 0.75, 0.8, "BAD"); addArrows(); canvas = cal.showAsFrame(1000, 600); swing { addToWindowRight(canvas, withMargin(vstackWithSpacing( jMaxWidth(167, jMinHeight(180, jscroll(textArea = setFontSize(16, makeBold(wordWrapTypeWriterTextArea()))))), btnThink = fatButton(100, "Think!", f action), btnStartOver = fatButton(60, "Start over", f startOver)))); status = initial; status(); } hideConsole(); repeat { sleepSeconds(2); if (thinking) pcall { print("Thinking"); bool t; time { t = think(); } if (!t) { thinking(false); print("Done"); } else { print("Updating"); canvas.update(); } } } } svoid startOver { cal.lock.lock(); step = 0; status = initial; status(); try { for (Circle c : cal.circles) c.traits.clear(); cal.lines.clear(); addArrows(); } finally { cal.lock.unlock(); } canvas.update(); } svoid addArrows { cal.addArrow(a, b, "MAKES"); cal.addArrow(b, d, "ARE"); } svoid thinking(bool b) { thinking = b; status(); setText(btnThink, b ? "
Stop
thinking
" : "Think!"); } svoid action { thinking(!thinking); } // for (X is Y): addTrait(X, Y); // for (X makes bad): addLink(X is bad); sbool think() { cal.lock.lock(); try { step++; status(); bool change = false; for (Line l : cloneList(cal.lines)) { if (l.hasTrait("is") || l.hasTrait("are")) { int n = l(l.a.traits()); // l.a.traits().addAll(l.b.traits()); // funny version setAddAll(l.a.traits(), l.b.traits()); change |= l(l.a.traits()) > n; } if (eqic(l.text, "makes") && l.b.hasTrait("bad")) { change |= addLink(l.a, "IS", cal.findCircle("bad")); } } if (a.hasTrait("bad")) status = "Donald is bad!"; else if (a.hasTrait("good")) status = "Donald is good!"; status(); ret change; } finally { cal.lock.unlock(); } } sbool addLink(Circle a, S link, Circle b) { Arrow arrow = cal.findArrow(a, b); if (arrow == null) { arrow = cal.addArrow(a, b, link); true; } else { if (arrow.hasTrait(link)) false; arrow.traits().add(link); true; } } static JButton fatButton(int h, S text, O action) { JButton btn = jbutton(text, action); btn.setFont(sansSerifBold(16)); ret jMinSize(167, h, btn); } svoid status { setText(textArea, status + "\n\n" + (thinking ? "Thinking...\n\n" : "") + (step == 0 ? "No thoughts made yet." : "Thoughts made: " + step)); }