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

108
LINES

< > BotCompany Repo | #1009512 // Diagrams From Text v1 [OK]

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

Download Jar. Uses 4489K of libraries. Click here for Pure Java version (9411L/68K).

!7

sS text = [[
  One node "A".
  One node "B".
  B to the right of A.
]];

static int interval = 250;
static new TreeSet<S> nodes;
static SS withImage = caseInsensitiveMap();
static new LinkedHashSet<Relation> relations;
static new LinkedHashSet<Relation> maybes;
static Map<S, Pt> coordinates = caseInsensitiveMap();
static Canvas canvas;
static JCheckBox cbMaybe;
static JTextArea ta;
static CirclesAndLines cal;
static ReliableSingleThread thread = new(f doIt);
sS textRendered;

p-subst-noconsole { load('text); doIt(); }

svoid doIt { loading {
  if (ta != null) { text = getText(ta); save('text); }
  textRendered = text;
  clearAll(nodes, withImage, relations, maybes, coordinates);
  for (S s : sentences(text)) {
    new Matches m;
    if "One node *|a node *"
      nodes.add($1);
    else if "One node * with image" {
      nodes.add($1);
      withImage.put($1, $1);
    } else if "One node * with image *" {
      nodes.add($1);
      withImage.put($1, $2);
    } else if "* to the right of *"
      relations.add(Relation("to the right of", node($1), node($2)));
    else if "An arrow from * to *"
      relations.add(Relation("An arrow from * to *", node($1), node($2));
    else if "An arrow * from * to *"
      relations.add(Relation("An arrow * from * to *", $1, node($2), node($3)));
  }
  print("Got nodes: " + sfu(nodes));
  print("Got relations: " + sfu(relations));
  makeCoordinates();
  print("Got coordinates: " + sfu(coordinates));
  bool first = canvas == null;
  CirclesAndLines newCAL = calFromCoordinates(coordinates, withImage, relations);
  canvas = showCAL_2(cal, newCAL, canvas);
  if (first) cal = newCAL;
  if (first) swing {
    increaseFrameWidth(canvas, 300);
    addToFrame(canvas, withTopMargin(jcenteredline(
      cbMaybe = onChange(jcheckbox(cbMaybe, "Strict interpretation"), f trigger), jbutton("Render again", f trigger))));
    setSplitPaneLater(addToWindowSplitRight_aggressive(canvas, ta = typeWriterTextArea(text)), 0.6f);
    //onUpdate(ta, f triggerLater);
    
    awtEvery(canvas, interval, new Runnable {
      S lastText;
      
      public void run() {
        S text = getText(ta);
        if (eq(text, lastText)) {
          if (neq(text, textRendered))
            trigger();
        } else
          lastText = text;
      }
    });
  }
}}

svoid trigger { thread.trigger(); }

svoid makeCoordinates {
  for (S node : nodes) coordinates.put(node, pt(random(100), random(100)));
  processRules();
  if (isChecked(cbMaybe)) {
    migrateList(maybes, relations);
    processRules();
  }
}

svoid processRules {
  for (Relation r : cloneList(relations)) {
    if (r.is("to the right of")) {
      maybes.add(Relation("on the same height as", r.a(), r.b()));
      Pt pa = coordinates.get(r.a()), pb = coordinates.get(r.b());
      if (pa != null && pb != null)
        pa.x = max(pa.x, pb.x+10);
    }
    if (r.is("on the same height as")) {
      Pt pa = coordinates.get(r.a()), pb = coordinates.get(r.b());
      if (pa != null && pb != null)
        pa.y = pb.y;
    }
  }
}

// find node by first line of description
static S node(S s) {
  if (s == null || nodes.contains(s)) ret s;
  S node = firstStartingWithIC(nodes, s + "\n");
  //print(struct(s) + " => " + struct(node) + " - " + sfu(nodes));
  ret node != null ? node : s;
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1009512
Snippet name: Diagrams From Text v1 [OK]
Eternal ID of this version: #1009512/56
Text MD5: d7f499071ee84a4bf8953a5a1a6dcac8
Transpilation MD5: 6aa5ced100649db580903c429e6a8988
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-08-26 23:11:09
Source code size: 3406 bytes / 108 lines
Pitched / IR pitched: No / No
Views / Downloads: 589 / 1559
Version history: 55 change(s)
Referenced in: [show references]