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

118
LINES

< > BotCompany Repo | #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, web_relationObjects(web));
    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 : web_relations(web)) {
      WebNode rel = web.getRelation(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  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: 783 / 765
Version history: 40 change(s)
Referenced in: [show references]