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).

1  
!7
2  
3  
sS text = [[
4  
  One node "A".
5  
  One node "B".
6  
  B to the right of A.
7  
]];
8  
9  
static int interval = 250;
10  
static new TreeSet<S> nodes;
11  
static SS withImage = caseInsensitiveMap();
12  
static new LinkedHashSet<Relation> relations;
13  
static new LinkedHashSet<Relation> maybes;
14  
static Map<S, Pt> coordinates = caseInsensitiveMap();
15  
static Canvas canvas;
16  
static JCheckBox cbMaybe;
17  
static JTextArea ta;
18  
static CirclesAndLines cal;
19  
static ReliableSingleThread thread = new(f doIt);
20  
sS textRendered;
21  
22  
p-subst-noconsole { load('text); doIt(); }
23  
24  
svoid doIt { loading {
25  
  if (ta != null) { text = getText(ta); save('text); }
26  
  textRendered = text;
27  
  clearAll(nodes, withImage, relations, maybes, coordinates);
28  
  for (S s : sentences(text)) {
29  
    new Matches m;
30  
    if "One node *|a node *"
31  
      nodes.add($1);
32  
    else if "One node * with image" {
33  
      nodes.add($1);
34  
      withImage.put($1, $1);
35  
    } else if "One node * with image *" {
36  
      nodes.add($1);
37  
      withImage.put($1, $2);
38  
    } else if "* to the right of *"
39  
      relations.add(Relation("to the right of", node($1), node($2)));
40  
    else if "An arrow from * to *"
41  
      relations.add(Relation("An arrow from * to *", node($1), node($2));
42  
    else if "An arrow * from * to *"
43  
      relations.add(Relation("An arrow * from * to *", $1, node($2), node($3)));
44  
  }
45  
  print("Got nodes: " + sfu(nodes));
46  
  print("Got relations: " + sfu(relations));
47  
  makeCoordinates();
48  
  print("Got coordinates: " + sfu(coordinates));
49  
  bool first = canvas == null;
50  
  CirclesAndLines newCAL = calFromCoordinates(coordinates, withImage, relations);
51  
  canvas = showCAL_2(cal, newCAL, canvas);
52  
  if (first) cal = newCAL;
53  
  if (first) swing {
54  
    increaseFrameWidth(canvas, 300);
55  
    addToFrame(canvas, withTopMargin(jcenteredline(
56  
      cbMaybe = onChange(jcheckbox(cbMaybe, "Strict interpretation"), f trigger), jbutton("Render again", f trigger))));
57  
    setSplitPaneLater(addToWindowSplitRight_aggressive(canvas, ta = typeWriterTextArea(text)), 0.6f);
58  
    //onUpdate(ta, f triggerLater);
59  
    
60  
    awtEvery(canvas, interval, new Runnable {
61  
      S lastText;
62  
      
63  
      public void run() {
64  
        S text = getText(ta);
65  
        if (eq(text, lastText)) {
66  
          if (neq(text, textRendered))
67  
            trigger();
68  
        } else
69  
          lastText = text;
70  
      }
71  
    });
72  
  }
73  
}}
74  
75  
svoid trigger { thread.trigger(); }
76  
77  
svoid makeCoordinates {
78  
  for (S node : nodes) coordinates.put(node, pt(random(100), random(100)));
79  
  processRules();
80  
  if (isChecked(cbMaybe)) {
81  
    migrateList(maybes, relations);
82  
    processRules();
83  
  }
84  
}
85  
86  
svoid processRules {
87  
  for (Relation r : cloneList(relations)) {
88  
    if (r.is("to the right of")) {
89  
      maybes.add(Relation("on the same height as", r.a(), r.b()));
90  
      Pt pa = coordinates.get(r.a()), pb = coordinates.get(r.b());
91  
      if (pa != null && pb != null)
92  
        pa.x = max(pa.x, pb.x+10);
93  
    }
94  
    if (r.is("on the same height as")) {
95  
      Pt pa = coordinates.get(r.a()), pb = coordinates.get(r.b());
96  
      if (pa != null && pb != null)
97  
        pa.y = pb.y;
98  
    }
99  
  }
100  
}
101  
102  
// find node by first line of description
103  
static S node(S s) {
104  
  if (s == null || nodes.contains(s)) ret s;
105  
  S node = firstStartingWithIC(nodes, s + "\n");
106  
  //print(struct(s) + " => " + struct(node) + " - " + sfu(nodes));
107  
  ret node != null ? node : s;
108  
}

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: 779 / 1888
Version history: 55 change(s)
Referenced in: [show references]