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)

1  
sclass WebNode {
2  
  Web web;
3  
  new L<Lisp> labels;
4  
  O visInfo; // visualisation info
5  
  
6  
  *() {}
7  
  *(Web *web) { web.fireNewNode(this); }
8  
  
9  
  void addLabel(S label) { addLabel(web.parseLabel(label)); }
10  
  
11  
  void addLabel(Lisp label) {
12  
    if (setAdd(labels, label) && web != null) {
13  
      web.index(label, this);
14  
      ++web.size;
15  
    }
16  
  }
17  
  
18  
  void addLabels(Collection<Lisp> l) { for (Lisp lbl : l) addLabel(lbl); }
19  
  void addStrings(Collection<S> l) { for (S lbl : l) addLabel(lbl); }
20  
  
21  
  bool hasLabel(Lisp label) { ret labels.contains(label); }
22  
  bool hasLabel(S label) { ret labels.contains(web.parseLabel(label)); }
23  
  
24  
  toString { ret l(labels) == 1 ? str(first(labels)) : str(labels); }
25  
  
26  
  int count() { ret 1 + l(labels); }
27  
  S text() { ret web.unparseLabel(first(labels)); }
28  
  L<S> texts() { ret web.unparseLabels(labels); }
29  
  
30  
  void relation(S arrow, S b) {
31  
    web.getRelation(this, web.node(b)).addLabel(arrow);
32  
  }
33  
  
34  
  Lisp parseLabel(S l) { ret web.parseLabel(l); }
35  
  S unparseLabel(Lisp l) { ret web.unparseLabel(l); }
36  
}
37  
38  
sclass WebRelation extends WebNode {
39  
  WebNode a, b;
40  
  
41  
  *() {}
42  
  *(Web web, WebNode a, WebNode b) { super(web); this.a = a; this.b = b; }
43  
}
44  
45  
sclass Web {
46  
  new L<WebNode> nodes; // Relations are also nodes
47  
  Map<Pair<WebNode>, WebRelation> relations = new HashMap;
48  
  new MultiMap<Lisp, WebNode> index; // label -> node
49  
  //Map<S, L<WebNode>> pots = new Map;
50  
  transient Lock lock = reentrantLock(true);
51  
  int size;
52  
  //int flags; // TODO
53  
  bool useCLParse = true;
54  
  bool labelsToUpper;
55  
  S title, globalID = aGlobalID();
56  
  S source;
57  
  bool unverified;
58  
  long created = nowUnlessLoading();
59  
  static new L onNewNode; // L<voidfunc(WebNode)>
60  
  static new L onNewLabel; // L<voidfunc(WebNode, Lisp)>
61  
  
62  
  static int F_useCLParse = 1;
63  
  static int F_labelsToupper = 2;
64  
  static int F_unverified = 3;
65  
  
66  
  /*bool potNotEmpty(S pot) { ret nempty(getPot(pot)); }
67  
  
68  
  L<WebNode> clearPot(S pot) {
69  
    L<WebNode> l = getPot(pot);
70  
    L<WebNode> l2 = cloneList(l);
71  
    l.clear();
72  
    ret l2;
73  
  }*/
74  
  
75  
  /*L<WebNode> getPot(S pot) {
76  
    L<WebNode> l = pots.get(pot);
77  
    if (l == null)
78  
      pots.put(pot, l = cloneList(nodes));
79  
    ret l;
80  
  }*/
81  
  
82  
  void relation(WebNode a, S arrow, WebNode b) {
83  
    getRelation(a, b).addLabel(arrow);
84  
  }
85  
  
86  
  Pair<WebNode> relation(S a, S arrow, S b) {
87  
    ret relation(lisp(arrow, a, b));
88  
  }
89  
  
90  
  Pair<WebNode> relation(Lisp l) {
91  
    if (l(l) == 1) {
92  
      findNode(l.get(0)).addLabel(lisp("wvuyakuvuelmxpwp", l.head));
93  
      null;
94  
    } else if (l(l) == 2) {
95  
      S a = lisp2label(l.get(0)), b = lisp2label(l.get(1));
96  
      if (l.is("fgvvrzypbkqomktd")) { // X is Y.
97  
        findNode(a).addLabel(b);
98  
        findNode(b).addLabel(a);
99  
      }
100  
      WebNode na = findNode(a), nb = findNode(b);
101  
      getRelation(na, nb).addLabel(l.head);
102  
      ret pair(na, nb);
103  
    }
104  
    null;
105  
  }
106  
  
107  
  void relations(L<Lisp> l) {
108  
    for (Lisp li : l) relation(li);
109  
  }
110  
  
111  
  WebRelation getRelation(S a, S b) {
112  
    ret getRelation(findNode(a), findNode(b));
113  
  }
114  
  
115  
  WebRelation getRelation(WebNode a, WebNode b) {
116  
    ret getRelation(pair(a, b));
117  
  }
118  
  
119  
  WebRelation relation(WebNode a, WebNode b) { ret getRelation(a, b); }
120  
  WebRelation relation(Pair<WebNode> p) { ret getRelation(p); }
121  
  
122  
  WebRelation getRelationOpt(WebNode a, WebNode b) {
123  
    ret relations.get(pair(a, b));
124  
  }
125  
  
126  
  WebRelation getRelation(Pair<WebNode> p) {
127  
    WebRelation r = relations.get(p);
128  
    if (r == null) relations.put(p, r = _newRelation(p.a, p.b));
129  
    ret r;
130  
  }
131  
  
132  
  WebRelation _newRelation(WebNode a, WebNode b) {
133  
    WebRelation r = new WebRelation(this, a, b);
134  
    nodes.add(r); ++size;
135  
    //for (L<WebNode> l : values(pots)) l.add(r);
136  
    ret r;
137  
  }
138  
  
139  
  WebNode newNode() {
140  
    WebNode node = new WebNode(this);
141  
    nodes.add(node); ++size;
142  
    //for (L<WebNode> l : values(pots)) l.add(node);
143  
    ret node;
144  
  }
145  
  
146  
  WebNode newNode(S s) {
147  
    WebNode node = newNode();
148  
    node.addLabel(parseLabel(s));
149  
    ret node;
150  
  }
151  
  
152  
  WebNode node(S s) { ret findNode(s); }
153  
  WebNode node(Lisp l) { ret findNode(l); }
154  
155  
  WebNode findNode(S s) {
156  
    ret findNode(parseLabel(s));
157  
  }
158  
  
159  
  WebNode findNode(Lisp l) {
160  
    WebNode n = findNodeOpt(l);
161  
    ret n != null ? n : newNode(l);
162  
  }
163  
  
164  
  WebNode findNodeOpt(Lisp l) {
165  
    ret first(index.get(l));
166  
    /*for (WebNode n : nodes)
167  
      if (n.labels.contains(l))
168  
        ret n;
169  
    null;*/
170  
  }
171  
  
172  
  WebNode newNode(S... labels) {
173  
    WebNode n = newNode();
174  
    for (S label : labels) n.addLabel(label);
175  
    ret n;
176  
  }
177  
  
178  
  WebNode newNode(Lisp... labels) {
179  
    WebNode n = newNode();
180  
    for (Lisp label : labels) n.addLabel(label);
181  
    ret n;
182  
  }
183  
  
184  
  toString { ret webToString(this); }
185  
  
186  
  int count() {
187  
    ret size;
188  
    /*int count = 0;
189  
    for (WebNode n : nodes) count += n.count();
190  
    for (WebNode n : values(relations)) count += n.count();
191  
    ret count;*/
192  
  }
193  
  
194  
  void index(Lisp label, WebNode n) {
195  
    index.put(label, n);
196  
    fireNewLabel(n, label);
197  
  }
198  
  
199  
  void clear {
200  
    size = 0;
201  
    clearAll(nodes, relations, index/*, pots*/);
202  
  }
203  
  
204  
  Lisp parseLabel(S s) {
205  
    if (useCLParse) ret clParse(s);
206  
    ret lisp(labelsToUpper ? upper(s) : s);
207  
  }
208  
  
209  
  S unparseLabel(Lisp l) {
210  
    ret useCLParse ? clUnparse(l) : lispHead(l);
211  
  }
212  
  
213  
  L<Lisp> parseLabels(L<S> l) {
214  
    ret map(func(S s) { parseLabel(s) }, l);
215  
  }
216  
  
217  
  L<S> unparseLabels(L<Lisp> l) {
218  
    ret map(func(Lisp l) { unparseLabel(l) }, l);
219  
  }
220  
  
221  
  void fireNewNode(WebNode node) { for (O f : onNewNode) pcallF(f, node); }
222  
  void fireNewLabel(WebNode node, Lisp label) { for (O f : onNewLabel) pcallF(f, node, label); }
223  
  
224  
  void removeNode(WebNode n) {
225  
    if (n == null || !nodes.contains(n)) ret;
226  
    
227  
    n.web = null;
228  
    
229  
    L<Pair<WebNode>> relationsToDelete = pairList_lookupAnySide(keys(relations), n);
230  
    
231  
    for (Lisp label : n.labels)
232  
      index.remove(label, n);
233  
    nodes.remove(n);
234  
235  
    for (Pair<WebNode> p : relationsToDelete)
236  
      removeRelation(p.a, p.b);
237  
  }
238  
  
239  
  void removeRelation(WebNode a, WebNode b) {
240  
    Pair<WebNode> p = pair(a, b);
241  
    WebNode r = relations.get(p);
242  
    if (r == null) ret;
243  
    relations.remove(p);
244  
    removeNode(r);
245  
  }
246  
}

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: 472 / 541
Referenced in: [show references]