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

246
LINES

< > BotCompany Repo | #1011485 // Web+WebNode older version (with size)

JavaX fragment (include)

sclass WebNode {
  Web web;
  new L<Lisp> labels;
  O visInfo; // visualisation info
  
  *() {}
  *(Web *web) { web.fireNewNode(this); }
  
  void addLabel(S label) { addLabel(web.parseLabel(label)); }
  
  void addLabel(Lisp label) {
    if (setAdd(labels, label) && web != null) {
      web.index(label, this);
      ++web.size;
    }
  }
  
  void addLabels(Collection<Lisp> l) { for (Lisp lbl : l) addLabel(lbl); }
  void addStrings(Collection<S> l) { for (S lbl : l) addLabel(lbl); }
  
  bool hasLabel(Lisp label) { ret labels.contains(label); }
  bool hasLabel(S label) { ret labels.contains(web.parseLabel(label)); }
  
  toString { ret l(labels) == 1 ? str(first(labels)) : str(labels); }
  
  int count() { ret 1 + l(labels); }
  S text() { ret web.unparseLabel(first(labels)); }
  L<S> texts() { ret web.unparseLabels(labels); }
  
  void relation(S arrow, S b) {
    web.getRelation(this, web.node(b)).addLabel(arrow);
  }
  
  Lisp parseLabel(S l) { ret web.parseLabel(l); }
  S unparseLabel(Lisp l) { ret web.unparseLabel(l); }
}

sclass WebRelation extends WebNode {
  WebNode a, b;
  
  *() {}
  *(Web web, WebNode a, WebNode b) { super(web); this.a = a; this.b = b; }
}

sclass Web {
  new L<WebNode> nodes; // Relations are also nodes
  Map<Pair<WebNode>, WebRelation> relations = new HashMap;
  new MultiMap<Lisp, WebNode> index; // label -> node
  //Map<S, L<WebNode>> pots = new Map;
  transient Lock lock = reentrantLock(true);
  int size;
  //int flags; // TODO
  bool useCLParse = true;
  bool labelsToUpper;
  S title, globalID = aGlobalID();
  S source;
  bool unverified;
  long created = nowUnlessLoading();
  static new L onNewNode; // L<voidfunc(WebNode)>
  static new L onNewLabel; // L<voidfunc(WebNode, Lisp)>
  
  static int F_useCLParse = 1;
  static int F_labelsToupper = 2;
  static int F_unverified = 3;
  
  /*bool potNotEmpty(S pot) { ret nempty(getPot(pot)); }
  
  L<WebNode> clearPot(S pot) {
    L<WebNode> l = getPot(pot);
    L<WebNode> l2 = cloneList(l);
    l.clear();
    ret l2;
  }*/
  
  /*L<WebNode> getPot(S pot) {
    L<WebNode> l = pots.get(pot);
    if (l == null)
      pots.put(pot, l = cloneList(nodes));
    ret l;
  }*/
  
  void relation(WebNode a, S arrow, WebNode b) {
    getRelation(a, b).addLabel(arrow);
  }
  
  Pair<WebNode> relation(S a, S arrow, S b) {
    ret relation(lisp(arrow, a, b));
  }
  
  Pair<WebNode> relation(Lisp l) {
    if (l(l) == 1) {
      findNode(l.get(0)).addLabel(lisp("wvuyakuvuelmxpwp", l.head));
      null;
    } else if (l(l) == 2) {
      S a = lisp2label(l.get(0)), b = lisp2label(l.get(1));
      if (l.is("fgvvrzypbkqomktd")) { // X is Y.
        findNode(a).addLabel(b);
        findNode(b).addLabel(a);
      }
      WebNode na = findNode(a), nb = findNode(b);
      getRelation(na, nb).addLabel(l.head);
      ret pair(na, nb);
    }
    null;
  }
  
  void relations(L<Lisp> l) {
    for (Lisp li : l) relation(li);
  }
  
  WebRelation getRelation(S a, S b) {
    ret getRelation(findNode(a), findNode(b));
  }
  
  WebRelation getRelation(WebNode a, WebNode b) {
    ret getRelation(pair(a, b));
  }
  
  WebRelation relation(WebNode a, WebNode b) { ret getRelation(a, b); }
  WebRelation relation(Pair<WebNode> p) { ret getRelation(p); }
  
  WebRelation getRelationOpt(WebNode a, WebNode b) {
    ret relations.get(pair(a, b));
  }
  
  WebRelation getRelation(Pair<WebNode> p) {
    WebRelation r = relations.get(p);
    if (r == null) relations.put(p, r = _newRelation(p.a, p.b));
    ret r;
  }
  
  WebRelation _newRelation(WebNode a, WebNode b) {
    WebRelation r = new WebRelation(this, a, b);
    nodes.add(r); ++size;
    //for (L<WebNode> l : values(pots)) l.add(r);
    ret r;
  }
  
  WebNode newNode() {
    WebNode node = new WebNode(this);
    nodes.add(node); ++size;
    //for (L<WebNode> l : values(pots)) l.add(node);
    ret node;
  }
  
  WebNode newNode(S s) {
    WebNode node = newNode();
    node.addLabel(parseLabel(s));
    ret node;
  }
  
  WebNode node(S s) { ret findNode(s); }
  WebNode node(Lisp l) { ret findNode(l); }

  WebNode findNode(S s) {
    ret findNode(parseLabel(s));
  }
  
  WebNode findNode(Lisp l) {
    WebNode n = findNodeOpt(l);
    ret n != null ? n : newNode(l);
  }
  
  WebNode findNodeOpt(Lisp l) {
    ret first(index.get(l));
    /*for (WebNode n : nodes)
      if (n.labels.contains(l))
        ret n;
    null;*/
  }
  
  WebNode newNode(S... labels) {
    WebNode n = newNode();
    for (S label : labels) n.addLabel(label);
    ret n;
  }
  
  WebNode newNode(Lisp... labels) {
    WebNode n = newNode();
    for (Lisp label : labels) n.addLabel(label);
    ret n;
  }
  
  toString { ret webToString(this); }
  
  int count() {
    ret size;
    /*int count = 0;
    for (WebNode n : nodes) count += n.count();
    for (WebNode n : values(relations)) count += n.count();
    ret count;*/
  }
  
  void index(Lisp label, WebNode n) {
    index.put(label, n);
    fireNewLabel(n, label);
  }
  
  void clear {
    size = 0;
    clearAll(nodes, relations, index/*, pots*/);
  }
  
  Lisp parseLabel(S s) {
    if (useCLParse) ret clParse(s);
    ret lisp(labelsToUpper ? upper(s) : s);
  }
  
  S unparseLabel(Lisp l) {
    ret useCLParse ? clUnparse(l) : lispHead(l);
  }
  
  L<Lisp> parseLabels(L<S> l) {
    ret map(func(S s) { parseLabel(s) }, l);
  }
  
  L<S> unparseLabels(L<Lisp> l) {
    ret map(func(Lisp l) { unparseLabel(l) }, l);
  }
  
  void fireNewNode(WebNode node) { for (O f : onNewNode) pcallF(f, node); }
  void fireNewLabel(WebNode node, Lisp label) { for (O f : onNewLabel) pcallF(f, node, label); }
  
  void removeNode(WebNode n) {
    if (n == null || !nodes.contains(n)) ret;
    
    n.web = null;
    
    L<Pair<WebNode>> relationsToDelete = pairList_lookupAnySide(keys(relations), n);
    
    for (Lisp label : n.labels)
      index.remove(label, n);
    nodes.remove(n);

    for (Pair<WebNode> p : relationsToDelete)
      removeRelation(p.a, p.b);
  }
  
  void removeRelation(WebNode a, WebNode b) {
    Pair<WebNode> p = pair(a, b);
    WebNode r = relations.get(p);
    if (r == null) ret;
    relations.remove(p);
    removeNode(r);
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

-
Snippet ID: #1011485
Snippet name: Web+WebNode older version (with size)
Eternal ID of this version: #1011485/1
Text MD5: 275fd889f99d5a99a374fad9923fd874
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-10-30 02:35:42
Source code size: 6398 bytes / 246 lines
Pitched / IR pitched: No / No
Views / Downloads: 471 / 540
Referenced in: