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