-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDaggerNode.js
More file actions
441 lines (392 loc) · 13.5 KB
/
Copy pathDaggerNode.js
File metadata and controls
441 lines (392 loc) · 13.5 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
'use strict'
const DaggerBase = require('./DaggerBase.js').DaggerBase;
const DaggerSignal = require('./DaggerBase.js').DaggerSignal;
const DaggerTypes = require('./DaggerTypes.js');
const DaggerBasePin = require('./DaggerBasePin.js').DaggerBasePin;
const DaggerPinCollection = require('./DaggerPinCollection.js').DaggerPinCollection;
/**
* Class that represents a node (or vertex) which is the fundamental unit of which DaggerGraphs are formed. A DaggerNode
* can be shared with multiple topologies with each topology having two DaggerPinCollections per node (one for each direction).
* @extends DaggerBase
*/
class DaggerNode extends DaggerBase {
/**
* DaggerNode ctor
*/
constructor() {
super();
// the current topology that is being calculated in calculateTopology
this._currentTSystemEval = -1;;
this._name = "DaggerNode";
this._parentGraph = null;
this._descendents = [];
this._subgraphAffiliation = [];
this._ordinal = [];
this._outputPins = [];
this._inputPins = [];
// signals
this.afterAddedToGraph = new DaggerSignal();
this.beforeAddedToGraph = new DaggerSignal();
this.pinCloned = new DaggerSignal();
this.nameChanged = new DaggerSignal();
for(let i = 0; i < DaggerTypes.MaxTopologyCount; i++) {
this._subgraphAffiliation[i] = -1;
this._ordinal[i] = -1;
this._inputPins[i] = new DaggerPinCollection(this, DaggerBasePin.PinDirection.Input, i);
this._outputPins[i] = new DaggerPinCollection(this, DaggerBasePin.PinDirection.Output, i);
}
}
/**
* Get the graph this node belongs to
* @returns {DaggerGraph}
*/
get parentGraph() {
return this._parentGraph;
}
/**
* Get the order of causality for the given topology that this node represents.
* @param {number} topology_system
* @returns {number}
*/
ordinal(topology_system = 0) {
return this._ordinal[topology_system];
}
/**
* Get the index of the subgraph this node belongs to.
* @param {number} topology_system
* @returns {number}
*/
subgraphAffiliation(topology_system = 0) {
return this._subgraphAffiliation[topology_system];
}
/**
* Get the name for this node.
*/
get name() {
return this._name;
}
/**
* Set the name for this node.
*/
set name(newName) {
this._name = newName;
this.nameChanged.emit(newName);
}
/**
* Get the DaggerPinCollection for the input pins of the given topology.
* @param {number} topology_system
* @returns {DaggerPinCollection}
*/
inputPins(topology_system = 0) {
return this._inputPins[topology_system];
}
/**
* Get the DaggerPinCollection for the output pins of the given topology.
* @param {number} topology_system
* @returns {DaggerPinCollection}
*/
outputPins(topology_system = 0) {
return this._outputPins[topology_system];
}
/**
* Get the list of nodes that the 'cause' of this node will 'effect'
* @param {number} topology_system
* @returns {array}
*/
descendents(topology_system = 0) {
return this._descendents[topology_system].slice();
}
/**
* Get the list of nodes that 'cause' this node.
* @param {number} topology_system
* @returns {array}
*/
ascendents(topology_system = 0) {
// any node in my subgraph that contains me as a descendent is one of my ascendents
let retv = [];
if(this.parentGraph) {
/*
let mysubgraph = this._subgraphAffiliation[topology_system];
let sl = this.parentGraph.getSubGraphNodes(mysubgraph, topology_system);
for(let n of sl) {
if(this != n && n.descendents(topology_system).includes(this)) {
retv.push(n);
}
}
*/
let all = this.parentGraph.nodes;
for(let i = 0; i < all.length; i++) {
if(all[i] != this && all[i].descendents(topology_system).indexOf(this) >= 0) {
retv.push(all[i]);
}
}
}
return retv;
}
/**
* Get if this node has no connected input pins for the given topology.
* @param {number} topology_system
* @returns {boolean}
*/
isTopLevel(topology_system = 0) {
let inpin = null;
let allpins = this._inputPins[topology_system].allPins;
for(let ipin of allpins) {
if(ipin.isConnected) {
// ignore exported pins
if(ipin.connectedTo.parentNode !== null) {
inpin = ipin;
break;
}
}
}
return inpin == null;
}
/**
* Get if this node has no connected output pins for the given topology.
* @param {number} topology_system
* @returns {boolean}
*/
isBottomLevel(topology_system = 0) {
let allpins = this._outputPins[topology_system].allPins;
for(let opin of allpins) {
if(opin.isConnected) {
return false;
}
}
return true;
}
/**
* Disconnect all this node's pins on all of it's topologies.
*/
disconnectAllPins() {
for(let i = 0; i < this._parentGraph._topologycount; i++) {
// go in reverse order to prevent autocloned pins from throwing off their collection iteration
let alloutput = this._outputPins[i].allPins;
for(let u = alloutput.length - 1; u > -1; u--) {
let opin = alloutput[u];
if(!opin.disconnectAll(false)) {
// this->emitError("Failed to disconnect output pin at disconnectAllPins");
return false;
}
}
let allinput = this._inputPins[i].allPins;
for(let u = allinput.length - 1; u > -1; u--) {
let ipin = allinput[u];
if(!ipin.disconnectPin(false)) {
// this->emitError("Failed to disconnect input pin at disconnectAllPins");
return false;
}
}
}
return true;
}
/**
* Find and return the DaggerOutputPin with the given name.
* @param {string} withName
* @param {number} topology_system
* @returns {DaggerOutputPin}
*/
getDaggerOutputPin(withName, topology_system = 0) {
return this._outputPins[topology_system].pin(withName);
}
/**
* Find and return the DaggerInput with the given name.
* @param {string} withName
* @param {number} topology_system
* @returns {DaggerOutputPin}
*/
getDaggerInputPin(withName, topology_system = 0) {
return this._inputPins[topology_system].pin(withName);
}
/**
* Returns true if this node has no input pins.
* @param {number} topology_system
*/
isTrueSource(topology_system = 0) {
return this._inputPins[topology_system].allPins.length === 0;
}
/**
* Returns true if this node has no output pins.
* @param {number} topology_system
*/
isTrueDest(topology_system = 0) {
return this._outputPins[topology_system].allPins.length === 0;
}
// get/set the current topology system that is being evaluated in calculateTopology
get currentTSystemEval() {
return this._currentTSystemEval;
}
set currentTSystemEval(system) {
this._currentTSystemEval = system;
}
/**
* Called by DaggerGraph after pins are connected to see if a pin should be cloned. When subclassing DaggerNode
* the subclass SHOULD call super.shouldClonePin.
* @param {DaggerBasePin} pin
* @returns {boolean}
*/
shouldClonePin(pin) {
if(pin.autoCloneMaster) {
if(pin.isInputPin) {
let toMax = pin.autoCloneMaster.maxAutoClone;
if(toMax != 0) {
if(toMax === -1 || (pin.autoCloneMaster.autoCloneCount < toMax)) {
return true;
}
}
} else {
// ensure there is only one connection if the pin is multiconnect
if(pin.connectedTo.length === 1) {
let toMax = pin.autoCloneMaster.maxAutoClone;
if(toMax != 0) {
if(toMax == -1 || (pin.autoCloneMaster.autoCloneCount < toMax)) {
return true;
}
}
}
}
}
return false;
}
// should only be used by deserialization - calling directly can result in duplicate pin names.
forceCloneWithName(pin, pinName) {
let retv = this.clonePin(pin);
if(retv) {
// we need to change the pin name and the name collection map
let parentCollection = pin.parent;
// if we can't change the name, we need to remove the clone
if(!parentCollection.setPinName(retv, pinName)) {
this.removeClonePin(pin);
retv = null;
}
}
return retv;
}
/**
* rename a given pin. Fails if pin is not flagged with canRename. Rarely should a pin be renamed.
* @param {DaggerBasePin} pin
* @param {string} pinName
* @returns {boolean}
*/
renamePin(pin, pinName) {
if(!pin.canRename) {
return false;
}
let parentCollection = pin.parent;
return parentCollection.setPinName(pin, pinName);
}
/**
* called by DaggerGraph after pins are connected to clone a pin
* if forceAutoCloneMaster is not null, the pin will be cloned from forceAutoCloneMaster instead of
* it's own autoCloneMaster property.
* @param {DaggerBasePin} pin
* @param {DaggerBasePin} forceAutoCloneMaster
* @returns {DaggerBasePin}
*/
clonePin(pin, forceAutoCloneMaster = null) {
if(pin.isInputPin) {
let input = forceAutoCloneMaster ? forceAutoCloneMaster : pin.autoCloneMaster;
if(!input) {
return null;
}
let clonedInput = input._clone();
if(clonedInput) {
clonedInput.cloned(input);
let parentCollection = pin.parent;
if(parentCollection.addPin(clonedInput, "")) {
this.pinCloned.emit(clonedInput);
clonedInput.onCloned();
return clonedInput;
} else {
return null;
}
}
} else {
let output = forceAutoCloneMaster ? forceAutoCloneMaster : pin.autoCloneMaster;
if(!output) {
return null;
}
let clonedOutput = output._clone();
if(clonedOutput) {
clonedOutput.cloned(output);
let parentCollection = pin.parent;
if(parentCollection.addPin(clonedOutput, "")) {
this.pinCloned.emit(clonedOutput);
clonedOutput.onCloned();
return clonedOutput;
} else {
return null;
}
}
}
return null;
}
/**
* Called by DaggerGraph after pins are disconnected to determine if an autocloned pin should be removed
* @param {DaggerBasePin} pin
* @returns {boolean}
*/
shouldRemoveClonePin(pin) {
// does this have an autoclone master?
if(pin.autoCloneMaster) {
return !pin.isConnected;
}
return false;
}
/**
* Called by DaggerGraph to remove a cloned pin (The pin that is removed might not be the one that is requested).
* @param {DaggerBasePin} pin
* @returns {boolean}
*/
removeClonePin(pin) {
let parentCollection = pin.parent;
if(pin.autoCloneMaster !== pin) {
// this was a cloned pin
return parentCollection.removePin(pin);
} else {
// the master clone was disconnected, so find the unconnected non-master clone and remove it
let all = parentCollection.allNonConnectedPins;
for(let tpin of all) {
if(tpin !== pin && tpin.autoCloneMaster == pin.autoCloneMaster) {
return parentCollection.removePin(tpin);
}
}
}
return false;
}
/**
* Override to allow a node to be able to decide if a pin can actually be removed. Also usefull to detect that a pin is
* about to be removed.
* @param {DaggerBasePin} pin
* @returns {boolean}
*/
canRemovePin(pin) {
return true;
}
/**
* Override to add logic for a node to respond to being parented to a graph
*/
addedToGraph() {}
/**
* Clean up when object hierarchy is unraveled. Subclasses should always super.
*/
purgeAll() {
super.purgeAll();
for(let i = 0; i < DaggerTypes.MaxTopologyCount; i++) {
if(this._inputPins[i]) {
this._inputPins[i].purgeAll();
}
if(this._outputPins[i]) {
this._outputPins[i].purgeAll();
}
this._descendents[i] = [];
}
this._outputPins = null;
this._inputPins = null;
this._descendents = null;
}
}
module.exports = {
DaggerNode: DaggerNode
};