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