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

118
LINES

< > BotCompany Repo | #1007792 // webToCAL

JavaX fragment (include)

1  
!include once #1007377 // Traits for CAL
2  
3  
please include function webToCAL_visStructures.
4  
5  
!include once #1010264 // x and y for WebNode
6  
7  
sclass WebToCAL {
8  
  Web web;
9  
  BufferedImage img = newBufferedImage(20, 20, Color.white);
10  
  new CirclesAndLines cal;
11  
  new Map<WebNode, Circle> map;
12  
  //new Map<Pair<WebNode>, Line> lines;
13  
  
14  
  S makeLabel(Lisp lisp) {
15  
    ifndef noCLParse
16  
      if (web.useCLParse) ret conceptLanguageToEnglish(clUnparse(lisp));
17  
    endifndef
18  
    ret or(lispHead(lisp), "[null]";
19  
  }
20  
  
21  
  void convert(final Web web) {
22  
    this.web = web;
23  
    cal.title = web.title;
24  
    
25  
    L<WebNode> nodes = web.nodes;
26  
    nodes = listMinusSet(nodes, web_relationObjects(web));
27  
    for (WebNode node : nodes) {
28  
      double x = node.x, y = node.y;
29  
      if (x == 0 && y == 0) {
30  
        x = cal_randomCircleCoord();
31  
        y = cal_randomCircleCoord();
32  
      }
33  
      //print("Circle " + node.text() + " pos: " + x + "/" + y);
34  
      
35  
      S text = makeLabel(first(node.labels()));
36  
      S quickvis = null;
37  
      if (node.visInfo() instanceof S) quickvis = (S) node.visInfo();
38  
      if (empty(quickvis)) quickvis = webToCAL_visMap.get(text);
39  
      
40  
      Circle c;
41  
      if (nempty(quickvis))
42  
        c = cal.circle_autoVis(text, quickvis, x, y);
43  
      else
44  
        c = cal.circle(img, x, y, text);
45  
      map.put(node, c);
46  
      for (Lisp l : dropFirst(node.labels()))
47  
        c.addTrait(makeLabel(l));
48  
    }
49  
    
50  
    for (Pair<WebNode> p : web_relations(web)) {
51  
      WebNode rel = web.getRelation(p);
52  
      if (empty(rel.labels())) continue;
53  
      Circle a = map.get(p.a), b = map.get(p.b);
54  
      if (a == null || b == null) {
55  
        warn("webToCAL: Circle not found");
56  
        continue;
57  
      }
58  
      if (a == b) continue; // TODO
59  
      /*Line line = lines.get(p);
60  
      if (line == null)
61  
        lines.put(p,*/
62  
      Line line = cal.arrow(a, makeLabel(first(rel.labels())), b);
63  
      for (Lisp l : dropFirst(rel.labels()))
64  
        line.addTrait(makeLabel(l));
65  
    }
66  
    
67  
    cal.onUserMadeCircle = voidfunc(Circle c) {
68  
      map.put(web.newNode(c.text), c);
69  
    };
70  
    
71  
    cal.onDeleteCircle = voidfunc(Circle c) {
72  
      web.removeNode(reverseLookup(map, c));
73  
    };
74  
    
75  
    cal.onDeleteLine = voidfunc(Line l) {
76  
      web.removeRelation(reverseLookup(map, l.a), reverseLookup(map, l.b));
77  
    };
78  
    
79  
    cal.onUserMadeArrow = voidfunc(Arrow a) {
80  
      web.getRelation(reverseLookup(map, a.a), reverseLookup(map, a.b)).addLabel(a.text);
81  
    };
82  
    
83  
    cal.onLayoutChange = voidfunc(Circle c) {
84  
      WebNode node = reverseLookup(map, c);
85  
      //print("layout change: " + c.text + " " + (node != null));
86  
      updateLayout(node);
87  
    };
88  
    
89  
    cal.onFullLayoutChange = r {
90  
      //print("full layout change");
91  
      for (WebNode web : keys(map))
92  
        updateLayout(web);
93  
    };
94  
  }
95  
  
96  
  void updateLayout(WebNode node) {
97  
    if (node == null) ret;
98  
    Circle c = map.get(node);
99  
    if (c == null) ret;
100  
    node.x = c.x;
101  
    node.y = c.y; // TODO: persist?
102  
  }
103  
}
104  
105  
static CirclesAndLines webToCAL(WebNode node) {
106  
  ret webToCAL(node.web);
107  
}
108  
109  
static CirclesAndLines webToCAL(Web web) {
110  
  new WebToCAL wtc;
111  
  wtc.convert(web);
112  
  ret wtc.cal;
113  
}
114  
115  
static void webToCAL(Web web, CirclesAndLines cal, Canvas canvas) {
116  
  copyCAL(webToCAL(web), cal);
117  
  canvas.update();
118  
}

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: #1007792
Snippet name: webToCAL
Eternal ID of this version: #1007792/41
Text MD5: 5610e5fe1fd696bf1baad31624378938
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-11-27 22:44:05
Source code size: 3395 bytes / 118 lines
Pitched / IR pitched: No / No
Views / Downloads: 710 / 690
Version history: 40 change(s)
Referenced in: [show references]