forked from rulespace/rulespace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.js
480 lines (439 loc) · 10 KB
/
graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// every node object should be unique (object identity)
export class Graph
{
constructor()
{
this._nodes = new Set();
this._fw = new Map();
this._bw = new Map();
}
nodes() // returns iter
{
return this._nodes;
}
*edges() // returns iter
{
for (const [from, tos] of this._fw.entries())
{
for (const to of tos)
{
yield [from, to];
}
}
}
predecessors(node)
{
return this._bw.get(node);
}
successors(node)
{
return this._fw.get(node);
}
addNode(node)
{
if (this._nodes.has(node))
{
return;
}
this._nodes.add(node);
this._fw.set(node, new Set());
this._bw.set(node, new Set());
}
addEdge(from, to)
{
this.addNode(from);
this.addNode(to);
this._fw.get(from).add(to);
this._bw.get(to).add(from);
}
nodeSmooth(nodeRemoveF)
{
for (const node of this.nodes())
{
if (nodeRemoveF(node))
{
const preds = this.predecessors(node);
const succs = this.successors(node);
for (const pred of preds)
{
const predOutgoing = this._fw.get(pred);
predOutgoing.delete(node);
for (const succ of succs)
{
predOutgoing.add(succ);
this._bw.get(succ).add(pred);
}
}
for (const succ of succs)
{
this._bw.get(succ).delete(node);
}
this._nodes.delete(node);
this._fw.delete(node);
this._bw.delete(node);
}
}
}
replaceNode(node, newNode) // TODO: untested
{
const preds = this.predecessors(node);
const succs = this.successors(node);
for (const pred of preds)
{
const predOutgoing = this._fw.get(pred);
predOutgoing.delete(node);
predOutgoing.add(newNode);
}
for (const succ of succs)
{
const succIncoming = this._bw.get(succ);
succIncoming.delete(node);
succIncoming.add(newNode);
}
this._nodes.delete(node);
this._fw.delete(node);
this._bw.delete(node);
this.addNode(newNode);
const newOutgoing = this._fw.get(newNode);
for (const succ of succs)
{
newOutgoing.add(succ);
}
const newIncoming = this._bw.get(newNode);
for (const pred of preds)
{
newIncoming.add(pred);
}
}
mapNodes(f)
{
const nodeMap = new Map();
for (const node of this.nodes())
{
nodeMap.set(node, f(node));
}
function mapNode(n)
{
return nodeMap.get(n);
}
const fwMapped = new Map();
const bwMapped = new Map();
for (const [source, dests] of this._fw)
{
fwMapped.set(mapNode(source), new Set([...dests].map(mapNode)));
}
for (const [dest, sources] of this._bw)
{
bwMapped.set(mapNode(dest), new Set([...sources].map(mapNode)));
}
const g = new Graph();
g._nodes = nodeMap.values();
g._fw = fwMapped;
g._bw = bwMapped;
return g;
}
// every node object requires a numerical and unique id
toDot(nodeLabeler = n => String(n))
{
let sb = `digraph G {\nnode [style=filled,fontname="Roboto Condensed"];\n`;
for (const node of this.nodes())
{
sb += `${node.id} [shape=box label="${nodeLabeler(node)}"];\n`;
}
for (const edge of this.edges())
{
sb += `${edge[0].id} -> ${edge[1].id};\n`
}
sb += "}";
return sb;
}
}
export function visitNodes(instance, tuples_, visitTuple, visitProduct, visitProductGb, visitGroupBy)
{
const wl = [...tuples_];
const seen = new Set();
while (wl.length > 0)
{
const tuple = wl.pop();
if (seen.has(tuple))
{
continue;
}
seen.add(tuple);
if (!visitTuple(tuple))
{
continue;
}
for (const product of instance.outProducts(tuple))
{
if (seen.has(product))
{
continue;
}
seen.add(product);
if (!visitProduct(product))
{
continue;
}
const outTuple = instance.outTuple(product);
if (outTuple !== null)
{
wl.push(outTuple);
}
}
for (const productGB of instance.outProductsGroupBy(tuple))
{
if (seen.has(productGB))
{
continue;
}
seen.add(productGB);
if (!visitProductGb(productGB))
{
continue;
}
const groupby = instance.outGroupBy(productGB);
if (!seen.has(groupby))
{
seen.add(groupby);
if (!visitGroupBy(groupby))
{
continue;
}
const outTuple = instance.outTuple(groupby);
if (outTuple !== null)
{
wl.push(outTuple);
}
}
}
}
}
export function visitLinks(instance, tuples_, visitTuple2Product, visitProduct2Tuple)
{
const wl = [...tuples_];
const seen = new Set();
while (wl.length > 0)
{
const tuple = wl.pop();
if (seen.has(tuple))
{
continue;
}
seen.add(tuple);
// const t = getTag(tuple);
for (const product of instance.outProducts(tuple))
{
// sb += `${t} -> ${getTag(product)};\n`;
if (seen.has(product))
{
continue;
}
if (!visitTuple2Product(tuple, product))
{
continue;
}
seen.add(product);
// const p = getTag(product);
const outTuple = instance.outProduct(product);
if (outTuple !== null)
{
// sb += `${p} -> ${getTag(outTuple)};\n`;
if (!visitProduct2Tuple(product, outTuple))
{
continue;
}
wl.push(outTuple);
}
}
// for (const productGB of instance.outProductsGroupBy(tuple))
// {
// //sb += `${t} -> ${getTag(productGB)};\n`;
// if (seen.has(productGB))
// {
// continue;
// }
// seen.add(productGB);
// if (!visitProductGb(productGB))
// {
// continue;
// }
// // const p = getTag(productGB);
// const groupby = instance.outGroupBy(outgb);
// // const gb = getTag(groupby);
// // sb += `${p} -> ${gb};\n`;
// if (!seen.has(groupby))
// {
// seen.add(groupby);
// if (!visitGroupBy(groupby))
// {
// continue;
// }
// const outTuple = instance.outTuple(groupby);
// if (outTuple !== null)
// {
// // sb += `${gb} -> ${getTag(outTuple)};\n`;
// wl.push(outTuple);
// }
// }
// }
}
}
export function instance2graph(instance)
{
const tuples = [...instance.rootTuples()];
const nodes = new Map();
let tag = 0;
function getValue(value)
{
if (value instanceof Function)
{
return '<function>';
}
if (value._outproducts)
{
return getTuple(value);
}
if (typeof value === 'string')
{
return `\\"${value}\\"`
}
return value;
}
function getTuple(tuple)
{
const x = nodes.get(tuple);
if (x !== undefined)
{
return x;
}
const t = {id: tag++, type: 'tuple', name: tuple.name(), values: tuple.values().map(getValue)};
nodes.set(tuple, t);
return t;
}
function getProduct(product)
{
const x = nodes.get(product);
if (x !== undefined)
{
return x;
}
const p = {id: tag++, type: 'product'};
nodes.set(product, p);
return p;
}
function getProductGb(product)
{
const x = nodes.get(product);
if (x !== undefined)
{
return x;
}
const p = {id: tag++, type: 'productGb'};
nodes.set(product, p);
return p;
}
function getGroupBy(groupBy)
{
const x = nodes.get(groupBy);
if (x !== undefined)
{
return x;
}
const gb = {id: tag++, type: 'groupBy'};
nodes.set(groupBy, gb);
return gb;
}
const g = new Graph();
// add all root tuples individually (in case they have no outgoing edges)
tuples.forEach(t => g.addNode(getTuple(t)));
visitNodes(instance, tuples,
tuple => {
const t = getTuple(tuple);
instance.outProducts(tuple).forEach(product => g.addEdge(t, getProduct(product)));
instance.outProductsGroupBy(tuple).forEach(product => g.addEdge(t, getProductGb(product)));
return true;
},
product => {
g.addEdge(getProduct(product), getTuple(instance.outTuple(product)));
return true;
},
productGb => {
g.addEdge(getProductGb(productGb), getGroupBy(instance.outGroupBy(productGb)));
return true;
},
groupBy => {
const outTuple = instance.outTuple(groupBy);
if (outTuple !== null)
{
g.addEdge(getGroupBy(groupBy), getTuple(outTuple));
}
return true;
});
return g;
}
export function pgraph2dot(g)
{
function valueLabel(value)
{
if (value?.type === 'tuple')
{
return tupleLabel(value);
}
return value;
}
function tupleLabel(tuple)
{
return `[${tuple.name} ${tuple.values.map(valueLabel).join(' ')}]`;
}
function gbLabel(gb)
{
return gb;
}
function productLabel(product)
{
return product.constructor.name;
}
function productGBLabel(product)
{
return product.constructor.name;
}
function getTag(obj)
{
return obj.id;
}
let sb = `digraph G {\nnode [style=filled,fontname="Roboto Condensed"];\n`;
for (const node of g.nodes())
{
if (node.type === 'tuple')
{
sb += `${getTag(node)} [shape=box label="${tupleLabel(node)}"];\n`;
}
else if (node.type === 'product')
{
sb += `${getTag(node)} [label="${productLabel(node)}" color="0.650 0.200 1.000"];\n`;
}
else if (node.type === 'productGb')
{
sb += `${getTag(node)} [label="${productGBLabel(node)}"];\n`;
}
else if (node.type === 'groupBy')
{
sb += `${getTag(node)} [label="${gbLabel(node)}" color="0.650 0.200 1.000"];\n`;
}
else
{
throw new Error(`cannot handle type ${node.type}`);
}
}
for (const edge of g.edges())
{
sb += `${getTag(edge[0])} -> ${getTag(edge[1])};\n`
}
sb += "}";
return sb;
}
export function instance2dot(instance)
{
return pgraph2dot(instance2graph(instance));
}