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

118
LINES

< > TinyBrain | #1007792 - webToCAL

JavaX fragment (include)

!include once #1007377 // Traits for CAL

please include function webToCAL_visStructures.

!include once #1010264 // x and y for WebNode

sclass WebToCAL {
  Web web;
  BufferedImage img = newBufferedImage(20, 20, Color.white);
  new CirclesAndLines cal;
  new Map<WebNode, Circle> map;
  //new Map<Pair<WebNode>, Line> lines;
  
  S makeLabel(Lisp lisp) {
    ifndef noCLParse
      if (web.useCLParse) ret conceptLanguageToEnglish(clUnparse(lisp));
    endifndef
    ret or(lispHead(lisp), "[null]";
  }
  
  void convert(final Web web) {
    this.web = web;
    cal.title = web.title;
    
    L<WebNode> nodes = web.nodes;
    nodes = listMinusSet(nodes, values(web.relations));
    for (WebNode node : nodes) {
      double x = node.x, y = node.y;
      if (x == 0 && y == 0) {
        x = cal_randomCircleCoord();
        y = cal_randomCircleCoord();
      }
      //print("Circle " + node.text() + " pos: " + x + "/" + y);
      
      S text = makeLabel(first(node.labels()));
      S quickvis = null;
      if (node.visInfo instanceof S) quickvis = (S) node.visInfo;
      if (empty(quickvis)) quickvis = webToCAL_visMap.get(text);
      
      Circle c;
      if (nempty(quickvis))
        c = cal.circle_autoVis(text, quickvis, x, y);
      else
        c = cal.circle(img, x, y, text);
      map.put(node, c);
      for (Lisp l : dropFirst(node.labels()))
        c.addTrait(makeLabel(l));
    }
    
    for (Pair<WebNode> p : keys(web.relations)) {
      WebNode rel = web.relations.get(p);
      if (empty(rel.labels())) continue;
      Circle a = map.get(p.a), b = map.get(p.b);
      if (a == null || b == null) {
        warn("webToCAL: Circle not found");
        continue;
      }
      if (a == b) continue; // TODO
      /*Line line = lines.get(p);
      if (line == null)
        lines.put(p,*/
      Line line = cal.arrow(a, makeLabel(first(rel.labels())), b);
      for (Lisp l : dropFirst(rel.labels()))
        line.addTrait(makeLabel(l));
    }
    
    cal.onUserMadeCircle = voidfunc(Circle c) {
      map.put(web.newNode(c.text), c);
    };
    
    cal.onDeleteCircle = voidfunc(Circle c) {
      web.removeNode(reverseLookup(map, c));
    };
    
    cal.onDeleteLine = voidfunc(Line l) {
      web.removeRelation(reverseLookup(map, l.a), reverseLookup(map, l.b));
    };
    
    cal.onUserMadeArrow = voidfunc(Arrow a) {
      web.getRelation(reverseLookup(map, a.a), reverseLookup(map, a.b)).addLabel(a.text);
    };
    
    cal.onLayoutChange = voidfunc(Circle c) {
      WebNode node = reverseLookup(map, c);
      //print("layout change: " + c.text + " " + (node != null));
      updateLayout(node);
    };
    
    cal.onFullLayoutChange = r {
      //print("full layout change");
      for (WebNode web : keys(map))
        updateLayout(web);
    };
  }
  
  void updateLayout(WebNode node) {
    if (node == null) ret;
    Circle c = map.get(node);
    if (c == null) ret;
    node.x = c.x;
    node.y = c.y; // TODO: persist?
  }
}

static CirclesAndLines webToCAL(WebNode node) {
  ret webToCAL(node.web);
}

static CirclesAndLines webToCAL(Web web) {
  new WebToCAL wtc;
  wtc.convert(web);
  ret wtc.cal;
}

static void webToCAL(Web web, CirclesAndLines cal, Canvas canvas) {
  copyCAL(webToCAL(web), cal);
  canvas.update();
}

download  show line numbers  debug dex   

Travelled to 3 computer(s): cfunsshuasjs, onxytkatvevr, tvejysmllsmz

No comments. add comment

Snippet ID: #1007792
Snippet name: webToCAL
Eternal ID of this version: #1007792/38
Text MD5: 1f428a4d254272ebbb350dcbc9a0bab3
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-02 03:19:55
Source code size: 3391 bytes / 118 lines
Pitched / IR pitched: No / No
Views / Downloads: 136 / 139
Version history: 37 change(s)
Referenced in: [show]