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

268
LINES

< > BotCompany Repo | #1007689 // Web + WebRelation v2 (LIVE)

JavaX fragment (include)

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

Author comment

Began life as a copy of #1007660

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv, xrpafgyirdlv

No comments. add comment

Snippet ID: #1007689
Snippet name: Web + WebRelation v2 (LIVE)
Eternal ID of this version: #1007689/120
Text MD5: 51b9d081713679dd5416144f5a429e2e
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-20 13:17:41
Source code size: 6765 bytes / 268 lines
Pitched / IR pitched: No / No
Views / Downloads: 1020 / 5133
Version history: 119 change(s)
Referenced in: [show references]