Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

142
LINES

< > BotCompany Repo | #1007323 - Is Donald Good Or Bad? [WORKS]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 3874K of libraries. Click here for Pure Java version (5061L/35K/132K).

!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<S> traits;
  bool hasTrait(S t) { ret containsIC(traits(), t); }
  L<S> traits() { if (text != null && neq(first(traits), text)) traits.add(0, text); ret traits; }
  
  S textForRender() {
    L<S> 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 ? "<html><center>Stop<br>thinking</center></html>" : "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));
}

Author comment

Began life as a copy of #1007312

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv

No comments. add comment

Snippet ID: #1007323
Snippet name: Is Donald Good Or Bad? [WORKS]
Eternal ID of this version: #1007323/63
Text MD5: 524a198dfbff67e986f0e33f55feeef8
Transpilation MD5: bec7b2072da0adac7f26f40755cb22a0
Author: stefan
Category: javax / gui
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-03-16 01:37:29
Source code size: 3596 bytes / 142 lines
Pitched / IR pitched: No / No
Views / Downloads: 517 / 793
Version history: 62 change(s)
Referenced in: [show references]