!7 static CirclesAndLines cal; 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; } void addTraits(L l) { setAddAll(traits(), l); } S textForRender() { L traits = traits(); if (l(traits) <= 1) ret text; ret text + " [" + join(", ", dropFirst(traits)) + "]"; } } p-pretty { cal = new CirclesAndLines; Circle a = cal.circle_autoVis("PLAYER", "Chess Player", 0.25, 0.75); Circle b = cal.circle_autoVis("PLAYER", "Chess Player", 0.75, 0.75); cal.arrow(a, "VS", b); cal.circle_autoVis("MAGNUS CARLSEN", 0.25, 0.25); cal.circle_autoVis("HIKARU NAKAMURA", 0.75, 0.25); Canvas canvas = cal.show(700, 600); nu(Thinker, +cal, +canvas, think := f think).init(); } sbool think() { Pair p = findTwoRandomNodes(); if (p != null && nodesAttract(p.a, p.b)) mergeNodes(p.a, p.b); true; } static Pair findTwoRandomNodes() { ret selectRandomPair(cal.circles); } sbool nodesAttract(Circle a, Circle b) { if (nodesDiverge(a, b)) false; for (S x : a.traits()) for (S y : b.traits()) if (traitsAttract(x, y)) true; false; } sbool nodesDiverge(Circle a, Circle b) { for (S x : a.traits()) for (S y : b.traits()) if (traitsDiverge(x, y)) true; false; } sbool traitsAttract(S a, S b) { ret isPair(a, b, "player", "magnus carlsen") || isPair(a, b, "player", "hikaru nakamura"); } sbool traitsDiverge(S a, S b) { ret isPair(a, b, "magnus carlsen", "hikaru nakamura"); } sbool isPair(S a, S b, S x, S y) { ret eqic(a, x) && eqic(b, y) || eqic(a, y) && eqic(b, x); } svoid mergeNodes(Circle a, Circle b) { b.addTraits(a.traits()); // TODO: change image? for (Line l : cal.lines) if (l.a == a || l.b == a) fail("todo"); cal.circles.remove(a); } sclass Thinker { double delay = 1; // 1 second bool continueOnError = true; CirclesAndLines cal; Canvas canvas; JTextArea textArea; JButton btnThink, btnStartOver; volatile bool thinking; S status, initialStatus = "Set up."; int step; bool inited; O think; // func -> bool O resetGraph; // runnable Throwable error; void init { if (inited) ret; inited = true; addControls(); thread "Think" { Thinker.this.run(); } } void addControls { swing { addToWindowRight(canvas, withMargin(vstackWithSpacing( jMaxWidth(167, jMinHeight(180, jscroll(textArea = setFontSize(16, makeBold(wordWrapTypeWriterTextArea()))))), btnThink = fatButton(100, "Think!", r { thinkOrStop() }), btnStartOver = fatButton(60, "Start over", r { startOver() }) ))); increaseFrameWidth(canvas, 180); status = initialStatus; status(); } } void startOver { cal.lock.lock(); try { step = 0; status = initialStatus; status(); callF(resetGraph); } finally { cal.lock.unlock(); } canvas.update(); } void run { repeat { sleepSeconds(delay); if (thinking) pcall { print("Thinking"); bool t; time { t = think(); } if (!t) { thinking(false); print("Done"); } else { print("Updating"); canvas.update(); } } } } void thinking(bool b) { thinking = b; status(); setText(btnThink, b ? "
Stop
thinking
" : "Think!"); } void thinkOrStop { thinking(!thinking); } bool think() { if (think == null) false; cal.lock.lock(); try { step++; status(); ret !isFalse(callF(think)); } catch e { printStackTrace(e); error = getInnerException(e); ret continueOnError; } finally { cal.lock.unlock(); } } JButton fatButton(int h, S text, O action) { JButton btn = jbutton(text, action); btn.setFont(sansSerifBold(16)); ret jMinSize(167, h, btn); } void status { setText(textArea, status + "\n\n" + (thinking ? "Thinking...\n\n" : "") + (step == 0 ? "No thoughts made yet." : "Thoughts made: " + step) + (error == null ? "" : "\n\nERROR: " + exceptionToStringShort(error))); } }