-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDaggerGraph.js
More file actions
555 lines (478 loc) · 17.8 KB
/
Copy pathDaggerGraph.js
File metadata and controls
555 lines (478 loc) · 17.8 KB
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
'use strict'
const DaggerBase = require('./DaggerBase.js').DaggerBase;
const DaggerSignal = require('./DaggerBase.js').DaggerSignal;
const DaggerTypes = require('./DaggerTypes.js');
var _nodeComparer = function(n1, n2) {
if(n1._ordinal[n1.currentTSystemEval] < n2._ordinal[n2.currentTSystemEval])
return -1;
if(n1._ordinal[n1.currentTSystemEval] > n2._ordinal[n2.currentTSystemEval])
return 1;
return 0;
}
let _recurseCalculateTopology = function(Me, level, node, touchedSet, topology_system) {
let retv = new Set();
if(node == null) {
// this was from an exported output pin, so return an empty set
return retv;
}
// set our precedence if level is larger than current value
node._ordinal[topology_system] = Math.max(level, node.ordinal(topology_system));
Me._maxOrdinal[topology_system] = Math.max(Me._maxOrdinal[topology_system], node._ordinal[topology_system]);
// recurse through all our output pins
let allOutPins = node.outputPins(topology_system).allPins;
for(let output of allOutPins) {
let ipins = output.connectedTo;
for(let p2 of ipins) {
// recurse through all it's connected pins
retv = new Set([...retv, ...(_recurseCalculateTopology(Me, level + 1, p2.parentNode, touchedSet, topology_system))]);
}
}
// add this node to the touched Set (we don't want to be included in our own _descendents Set)
touchedSet.add(node);
// recreate our _descendents List from the new set
for(let snode of retv) {
// if(!node._descendents[topology_system].includes(snode)) {
if(node._descendents[topology_system].indexOf(snode) < 0) {
node._descendents[topology_system].push(snode);
}
}
// sort the _descendents List by the Ordinal number. We need to set the current eval so the
// qsort knows which topology system we're computing
for(let tn of node._descendents[topology_system]) {
tn._currentTSystemEval = topology_system;
}
node._descendents[topology_system].sort(_nodeComparer);
// now add this node to the newset.
// We do this here because we don't want to include ourself in the _descendents List
retv.add(node);
return retv;
}
/**
* Class that represents a collection of DaggerNodes interconnected via DaggerBasePins
* @extends DaggerBase
*/
class DaggerGraph extends DaggerBase {
/**
* DaggerGraph ctor
* @param {Number} topologycount
*/
constructor(topologycount = 1) {
super();
this._nodes = [];
this._subGraphCount = [];
this._maxOrdinal = [];
this._topologycount = topologycount;
for(let i = 0; i < DaggerTypes.MaxTopologyCount; i++) {
this._subGraphCount[i] = 0;
this._maxOrdinal[i] = 0;
}
// signals
this.pinsDisconnected = new DaggerSignal();
this.pinsConnected = new DaggerSignal();
this.nodeRemoved = new DaggerSignal();
this.nodeAdded = new DaggerSignal();
this.topologyChanged = new DaggerSignal();
// init topology
this.calculateTopology();
this._topologyEnabled = true;
}
/**
* Get list of all nodes that have no connected input pins with the given topology
* @param {Number} topology_system
*/
topLevelNodes(topology_system = 0) {
let retv = [];
for(let node of this._nodes) {
if(node.isTopLevel(topology_system))
{
retv.push(node);
}
}
return retv;
}
get enableTopology() {
return this._topologyEnabled;
}
set enableTopology(enabled) {
if(enabled == this._topologyEnabled) {
return;
}
// if we are re-enabling the topology, calculate it now
this._topologyEnabled = enabled;
this.calculateTopology();
}
/**
* Called internally every time an action alters the topology of the graph.
*/
calculateTopology() {
if(!this._topologyEnabled)
return;
for(let t = 0; t < this._topologycount; t++) {
this._maxOrdinal[t] = 0;
// reset all presidence values and subgraph affiliations to -1
for(let i = 0; i < this._nodes.length; i++) {
let node = this._nodes[i];
node._ordinal[t] = -1;
node._subgraphAffiliation[t] = -1;
node._descendents[t] = [];
}
// get the top level nodes
let tnodes = this.topLevelNodes(t);
// Create a List of Sets. Each Set will hold a List of all Nodes that a top level Node touches on it's way to bottom level Nodes.
// We'll in turn use these Sets to calculate subgraph affiliations.
let touchedSetList = [];
for(let i = 0; i < tnodes.length; i++) {
let node = tnodes[i];
// top level nodes always have an Ordinal of 0
node._ordinal[t] = 0;
// create a "touched" Set
let touchedSet = new Set();
let allOutPins = node.outputPins(t).allPins;
for(let output of allOutPins) {
let connectedToPins = output.connectedTo;
for(let inpin of connectedToPins) {
// recurse through all it's connected pins
let newset = _recurseCalculateTopology(this, 1, inpin.parentNode, touchedSet, t);
// recreate our _descendents List from the newset
for(let setnode of newset) {
// if(!node._descendents[t].includes(setnode)) {
if(node._descendents[t].indexOf(setnode) < 0) {
node._descendents[t].push(setnode);
}
}
// sort the _descendents List by the Ordinal number
for(let tn of node._descendents[t]) {
tn._currentTSystemEval = t;
}
node._descendents[t].sort(_nodeComparer);
}
}
// all top level nodes touch themselves <Let's keep it professional here>
touchedSet.add(node);
// since we always have at least one subgraph, just add the first touched set to the list of sets
if(i == 0) {
touchedSetList.push(touchedSet);
} else {
// if not the first Set, see if we can merge this Set with an existing touched Set
let merged = false;
for(let u = 0; u < touchedSetList.length; u++) {
// get the intersection of touchedSet and touchedSetList[u]
let intersection = new Set([...touchedSet].filter(x => touchedSetList[u].has(x)));
// if the Cardinalty of the intersected Set is NOT zero, then these two Sets share the same subgraph
if(intersection.length > 0) {
// merge the new Set with the stored one
touchedSetList[u] = new Set([...touchedSetList[u], ...touchedSet]);
merged = true;
break;
}
}
if(!merged) {
// we didn't find a Stored Set to merge with, so store this Set
touchedSetList.push(touchedSet);
}
}
}
// scrub through all the sets and mark the Subgraph tag of each node
for(let i = 0; i < touchedSetList.length; i++) {
for(let node of touchedSetList[i]) {
node._subgraphAffiliation[t] = i;
}
}
// let the graph know how many subgraphs it has
this._subGraphCount[t] = touchedSetList.length;
}
// anounce the topology has changed
this.graphTopologyChanged();
this.topologyChanged.emit();
}
/**
* Get all the nodes in the graph
* @returns {array}
*/
get nodes() {
return this._nodes.slice();
}
/**
* Get the highest ordinal for the given topology system
* @param {Number} topology_system
* @returns {Number}
*/
maxOrdinal(topology_system = 0) {
return this._maxOrdinal[topology_system];
}
/**
* Get the number of subgraphs in the DaggerGraph
* @param {Number} topology_system
* @returns {Number}
*/
subGraphCount(topology_system = 0) {
return this._subGraphCount[topology_system]
}
/**
* Called before two pins are disconnected to test if they are currently allowed to disconnect. Override to
* provide logic for cases when pins are not allowed to disconnect (ie, data is being processed). If the method
* returns false, the pins will fail to disconnect.
* @param {DaggerBasePin} connectFrom
* @param {DaggerBasePin} connectTo
* @returns {boolean}
*/
beforePinsConnected(connectFrom, connectTo) {
return true;
}
/**
* Called after two pins are connected to test if either needs to be cloned.
* @param {DaggerBasePin} connectFrom
* @param {DaggerBasePin} connectTo
*/
afterPinsConnected(connectFrom, connectTo) {
// see if we should clone the output pin
if(connectFrom.parentNode.shouldClonePin(connectFrom)) {
if(!connectFrom.parentNode.clonePin(connectFrom)) {
// emitError("failed to autoclone pin");
}
}
// see if we should clone the input pin
if(connectTo.parentNode.shouldClonePin(connectTo)) {
if(!connectTo.parentNode.clonePin(connectTo)) {
// emitError("failed to autoclone pin");
}
}
}
/**
* Get list of all nodes that have no connected input pins
* @param {Number} topology_system
* @returns {array}
*/
topLevelNodes(topology_system = 0) {
let retv = [];
for(let node of this._nodes) {
if(node.isTopLevel(topology_system)) {
retv.push(node);
}
}
return retv;
}
/**
* Get list of all nodes that have no connected output pins
* @param {Number} topology_system
* @returns {array}
*/
bottomLevelNodes(topology_system = 0)
{
let retv = [];
for(let node of this._nodes) {
if(node.isBottomLevel(topology_system)) {
retv.push(node);
}
}
return retv;
}
/**
* Returns a list of nodes in a certain subgraph with the given topology system
* @param {Number} index
* @param {Number} topology_system
* @returns {array}
*/
getSubGraphNodes(index, topology_system = 0) {
let retv = [];
if(index > this._subGraphCount[topology_system] - 1) {
// emitError("Subgraph index out of range");
return retv;
}
for(let node of this._nodes) {
if(index == node.subgraphAffiliation(topology_system)) {
retv.push(node);
}
}
return retv;
}
/**
* Return an array containing arrays of nodes for each subgraph in the graph
* @param {Number} topology_system
* @returns {array}
*/
getSubGraphs(topology_system = 0) {
let retv = [];
for(let i = 0; i < this._subGraphCount[topology_system]; i++) {
retv.push(getSubGraphNodes(i, topology_system));
}
return retv;
}
/**
* Get a list of all nodes with the given name.
* @param {string} name
* @returns {array}
*/
getNodesWithName(name) {
let retv = [];
for(let node of this._nodes) {
if(node.name == name) {
retv.push(node);
}
}
return retv;
}
/**
* Find and return a pin that has the given instanceID
* @param {string} pinInstanceID
* @returns {DaggerBasePin}
*/
getPinWithInstanceID(pinInstanceID) {
for(let node of this._nodes) {
for(let i = 0; i < this._topologycount; i++) {
// look in the input pins
let retv = node.inputPins(i).getPinWithInstanceID(pinInstanceID);
if(retv) {
return retv;
}
// look in the output pins
retv = node.outputPins(i).getPinWithInstanceID(pinInstanceID);
if(retv) {
return retv;
}
}
}
return null;
}
/**
* Find and return a node with the given instanceID
* @param {string} nodeInstanceID
* @returns {DaggerNode}
*/
getNodeWithInstanceID(nodeInstanceID) {
for(let node of this._nodes) {
if(node.instanceID == nodeInstanceID)
return node;
}
return null;
}
/**
* Get an array of all DaggerInputPins that are connected
* @param {number} topology_system
* @returns {array}
*/
allConnections(topology_system = 0) {
let retv = [];
for(let node of this._nodes) {
let pins = node.inputPins(topology_system).allPins;
for(let pin of pins) {
if(pin && pin.isConnected) {
retv.push(ipin);
}
}
}
return retv;
}
/**
* Remove a node from the graph. If the node has any connections, they are disconnected first
* @param {DaggerNode} node
* @returns {boolean}
*/
removeNode(node) {
if(!node) {
return false;
}
if(this.beforeNodeRemoved(node)) {
// try to disconnect all pins
if(!node.disconnectAllPins()) {
// emitError("failed to remove node");
return false;
}
// remove and destroy the node
this._nodes.splice(this._nodes.indexOf(node), 1);;
node.purgeAll();
this.nodeRemoved.emit(node.instanceUUID);
// mark node as delete later so the signal can be processed with a live pointer to it
node._parentGraph = null; // this node is no longer ours
// recalc topology
this.calculateTopology();
return true;
}
return false;
}
/**
* Add a node to the graph
* @param {DaggerNode} node
* @returns {boolean}
*/
addNode(node, calculate = false) {
if(node.parentGraph != null) {
// emitError("QDaggerNode is already associated with a QDaggerGraph");
return null;
}
node.beforeAddedToGraph.emit(); // tell the node to signal that the node is about to be added to a graph
node._parentGraph = this;
this._nodes.push(node);
if(calculate) {
this.calculateTopology();
} else {
// adding a single node before connections has an easily determined
// effect on the topology
for(let t = 0; t < this._topologycount; t++) {
this._subGraphCount[t] ++;
this._maxOrdinal[t] = Math.max(1, this._maxOrdinal[t]);
node._subgraphAffiliation[t] = this.subGraphCount(t) + 1;
node._ordinal[t] = 0;
node._descendents[t] = [];
}
}
this.nodeAdded.emit(node);
node.addedToGraph(); // tell the node to signal that it was added to a graph
node.afterAddedToGraph.emit();
return node;
}
addNodes(nodes) {
let i = 0;
for(i = 0; i < nodes.length; i++) {
let node = nodes[i];
if(node.parentGraph != null) {
// emitError("QDaggerNode is already associated with a QDaggerGraph");
return null;
}
}
for(i = 0; i < nodes.length; i++) {
let node = nodes[i];
node.beforeAddedToGraph.emit(); // tell the node to signal that the node is about to be added to a graph
node._parentGraph = this;
this._nodes.push(node);
this.nodeAdded.emit(node);
node.addedToGraph(); // tell the node to signal that it was added to a graph
node.afterAddedToGraph.emit();
}
this.calculateTopology();
return nodes;
}
beforeNodeRemoved(node) {
return true;
}
beforePinsDisconnected(connectFrom, connectTo) {
return true;
}
afterPinsDisconnected(connectFrom, connectTo) {
// see if we should clone the output pin
if(connectFrom.parentNode.shouldRemoveClonePin(connectFrom)) {
if(!connectFrom.parentNode.removeClonePin(connectFrom)) {
// emitError("failed to remove autocloned pin");
}
}
// see if we should clone the input pin
if(connectTo.parentNode.shouldRemoveClonePin(connectTo)){
if(!connectTo.parentNode.removeClonePin(connectTo)) {
// emitError("failed to remove autocloned pin");
}
}
}
onPinsDisconnected(disconnectOutput, disconnectInput) {
this.calculateTopology();
this.pinsDisconnected.emit(disconnectOutput.instanceID, disconnectInput.instanceID);
}
onPinsConnected(connectFrom, connectTo) {
this.calculateTopology();
this.pinsConnected.emit(connectFrom, connectTo);
}
// called when topology has changed.
graphTopologyChanged() {}
}
module.exports = {
DaggerGraph: DaggerGraph
};