-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathgraph.js
More file actions
1012 lines (909 loc) · 29.2 KB
/
graph.js
File metadata and controls
1012 lines (909 loc) · 29.2 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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var C = require("./constants.js");
var Mapping = require("./../processing/mapping.js");
require("../../utils/array.js");
var Base = require("../base.js");
var ChannelNode = require("../processing/channel-node.js").ChannelNode;
var Utils = require("../utils/utils.js");
//----------------------------------------------------------------------------------------------------------------------
// GraphNode
//----------------------------------------------------------------------------------------------------------------------
/**
* Base class for other graph nodes
* @constructor
* @abstract
* @param {Graph} graph Reference to parent graph
*/
var GraphNode = function(){
/**
* All nodes that add a dependency to this node
* @type array<GraphNode>
**/
this._parents = [];
};
//----------------------------------------------------------------------------------------------------------------------
// InputNode
//----------------------------------------------------------------------------------------------------------------------
/**
* An InputNode include an DataEntry, a name and other information
* This class mirrors XML3D elements such as <float3>, <int> or <texture>
*
* @constructor
* @param {Graph} graph
* @extends {GraphNode}
*/
var InputNode = function(){
GraphNode.call(this);
/**
* Name of the input node
* @type {string}
* @private
*/
this._name = "";
/**
* Sequence key
* @type {number}
* @private
*/
this._key = 0;
/**
* DataEntry node that holds the value
* @type {DataEntry}
*/
this._data = null;
/**
* If this nodes is a parameter within a <dataflow>
* this is set to the name of the parameter, otherwise null
* @type {null|String}
* @private
*/
this._paramName = null;
/**
* Experimental! Apply different override logic in order
* to propagate global parameters to the source of the graph
* Could be used for instance for LOD concepts, where the
* renderer propagates the distance along the graph
* @type {boolean}
* @private
*/
this._paramGlobal = false;
/**
* Cache listener for DataEntry
* @see {InputNode.onDataChange}
*/
this._dataListener = this.onDataChange.bind(this);
};
Base.createClass(InputNode, GraphNode);
/**
* Propagate events from DataEntry to parent nodes
* @param {Object} newValue
* @param {C.DATA_ENTRY_STATE} notification
*/
InputNode.prototype.onDataChange = function(newValue, notification) {
var downNote;
switch(notification){
case C.DATA_ENTRY_STATE.CHANGED_VALUE: downNote = C.RESULT_STATE.CHANGED_DATA_VALUE; break;
case C.DATA_ENTRY_STATE.LOAD_START: downNote = C.RESULT_STATE.IMAGE_LOAD_START; break;
case C.DATA_ENTRY_STATE.LOAD_END: downNote = C.RESULT_STATE.IMAGE_LOAD_END; break;
case C.DATA_ENTRY_STATE.CHANGED_SIZE_TYPE: downNote = C.RESULT_STATE.CHANGED_STRUCTURE; break;
case C.DATA_ENTRY_STATE.CHANGED_SIZE: downNote = C.RESULT_STATE.CHANGED_DATA_SIZE; break;
default: downNote = C.RESULT_STATE.CHANGED_DATA_SIZE; break;
}
notifyParentsOnChanged(this,downNote);
};
Object.defineProperty(InputNode.prototype, "name", {
/** @param {string} v */
set: function(v){
this._name = v;
notifyParentsOnChanged(this, C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
},
/** @return {string} */
get: function(){ return this._name; }
});
Object.defineProperty(InputNode.prototype, "key", {
/** @param {number} v */
set: function(v){
this._key = v;
notifyParentsOnChanged(this, C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
},
/** @return {number} */
get: function(){ return this._key; }
});
Object.defineProperty(InputNode.prototype, "paramName", {
/** @param {string} v */
set: function(v){
this._paramName = v;
notifyParentsOnChanged(this, C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
},
/** @return {string} */
get: function(){ return this._paramName; }
});
Object.defineProperty(InputNode.prototype, "paramGlobal", {
/** @param {boolean} v */
set: function(v){
this._paramGlobal = v;
notifyParentsOnChanged(this, C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
},
/** @return {boolean} */
get: function(){ return this._paramGlobal; }
});
Object.defineProperty(InputNode.prototype, "data", {
/** @param {DataEntry} dataEntry */
set: function(dataEntry){
var prevDataLoading = false;
if(this._data) {
prevDataLoading = this._data._loading;
this._data.removeListener(this._dataListener);
}
this._data = dataEntry;
if(this._data) {
this._data.addListener(this._dataListener);
}
if(prevDataLoading != this._data._loading){
notifyParentsOnChanged(this, this._data._loading ? C.RESULT_STATE.IMAGE_LOAD_START :
C.RESULT_STATE.IMAGE_LOAD_END);
}
Base._flushResultCallbacks();
},
/** @return {DataEntry} */
get: function(){ return this._data; }
});
/**
* Getter for local parameter name, returns null if this is a global
* parameter
*
* @returns {null|String}
* @private
*/
InputNode.prototype._getParamNames = function(){
return this._paramGlobal ? null : this._paramName;
};
/**
* Getter for global parameter name, returns null if this is not a global
* parameter
*
* @returns {null|String}
* @private
*/
InputNode.prototype._getGlobalParamNames = function(){
return this._paramGlobal ? this._paramName : null;
};
//----------------------------------------------------------------------------------------------------------------------
// DataNode
//----------------------------------------------------------------------------------------------------------------------
var c_xflowNodeId = 0;
function getXflowNodeId(){
return ++c_xflowNodeId;
}
/**
* The DataNode is the central structure of an Xflow Graph.
* It is used to build a data composition graph as well as a data flow.
* It mirror the <data> element of XML3D
*
* @constructor
* @param {Graph} graph Context graph
* @param {boolean} isDataFlow is this node a dataflow
* @extends {GraphNode}
*/
var DataNode = function(isDataFlow){
GraphNode.call(this);
/**
* Marker, if this data node is expecting data. Xflow
* is not monitoring any load events. This must be set
* from external
* @type {boolean}
*/
this._loading = false;
/**
* Experimental! Priority. How important is this data?
* 0: Very important
* @type {number}
*/
this._loadLevel = 0;
/**
* Used for loading events: If progress level
* reaches infinity, loading events are triggered
* @type {Number}
*/
this._progressLevel = Infinity;
/**
* Globally unique id
*/
this.id = getXflowNodeId();
/**
* Is this node a proto node
* @type {boolean}
*/
this._isProtoNode = isDataFlow;
/**
* Children. InputNodes and DataNodes (as found in DOM)
* @type {Array}
*/
this._children = [];
/**
* The DataNode that has been reference via src
* @type {DataNode}
*/
this._sourceNode = null;
/**
* Field to attach custom data
* @type {null|Object}
*/
this._userData = null;
/**
* The filter type of this node (keep, rename, remove ...)
* @type {DATA_FILTER_TYPE}
*/
this._filterType = C.DATA_FILTER_TYPE.NONE;
/**
* Define the mapping
* @type {Mapping.Mapping}
*/
this._filterMapping = null;
/**
* String identifier for operator
* TODO: Operator class
* @type {string|Object}
*/
this._computeOperator = "";
/**
* True, if compute is a dataflow reference
* @type {boolean}
*/
this._computeUsesDataflow = false;
/**
* Mapping for input of operator,
* e.g. (position, texcoord) or ({position: pos, texcoord: uv})
* @type {Mapping}
*/
this._computeInputMapping = null;
/**
* Mapping for output of operator,
* e.g. (position, texcoord) = ... or {position: pos, texcoord: uv} = ...
* @type {Mapping}
*/
this._computeOutputMapping = null;
/**
* If dataflow node has been resolved, this
* entry is set
* @type {DataNode}
*/
this._dataflowNode = null;
/**
* Internal (optimized) version of this data node
* @type {ChannelNode}
*/
this._channelNode = new ChannelNode(this);
/**
* Map of cached channel nodes for dataflow instances with varying
* input arguments (specialized nodes)
* TODO: Use WeakMap?
* @type {Object.<string, ChannelNode>}
*/
this._substitutionNodes = {};
/**
* Cached version of local param names collected from
* children
* @type {Array.<string>}
*/
this._paramNames = null;
/**
* Cached version of global param names collected from
* children
* @type {Array.<string>}
*/
this._globalParamNames = null;
/**
* Platform, this data node should be executed on
* TODO: This should be implicit, not explicit
* @type {null}
*/
this._platform = null;
/**
* Observers of the node's C.RESULT_STATE
* @type {Array}
*/
this._listeners = [];
/**
* Observers of the node's progress level
* @type {Array}
*/
this._loadListeners = [];
};
Base.createClass(DataNode, GraphNode);
Object.defineProperty(DataNode.prototype, "sourceNode", {
/** @param {?DataNode} newSourceNode */
set: function(newSourceNode){
replaceNodeInHierarchy(this, "_sourceNode", newSourceNode);
updateProgressLevel(this);
this.notify(C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
},
/** @return {?DataNode} */
get: function(){ return this._sourceNode; }
});
Object.defineProperty(DataNode.prototype, "dataflowNode", {
/** @param {?DataNode} newDataflowNode */
set: function(newDataflowNode){
if(newDataflowNode && !this._computeUsesDataflow) {
throw new Error("Cannot set dataflowNode when compute doesn't use dataflow.");
}
replaceNodeInHierarchy(this, "_dataflowNode", newDataflowNode);
updateProgressLevel(this);
this.notify(C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
},
/** @return {?DataNode} */
get: function(){ return this._dataflowNode; }
});
Object.defineProperty(DataNode.prototype, "userData", {
/** @param {?DataNode} v */
set: function(v){
this._userData = v;
},
/** @return {?DataNode} */
get: function(){ return this._userData; }
});
/**
* Set (from external) if more data is expected.
* @param {boolean} loading
*/
DataNode.prototype.setLoading = function(loading){
if(this._loading != loading){
this._loading = loading;
this._channelNode.setStructureOutOfSync();
this._channelNode.loading = loading;
for (var sub in this._substitutionNodes) {
var subNode = this._substitutionNodes[sub];
subNode.setStructureOutOfSync();
subNode.loading = loading;
}
updateProgressLevel(this);
Base._flushResultCallbacks();
}
};
/**
* Returns if this or any child node is loading
* @returns {boolean}
*/
DataNode.prototype.isSubtreeLoading = function(){
return this._progressLevel == 0;
};
/**
* @returns {Number}
*/
DataNode.prototype.getProgressLevel = function(){
return this._progressLevel;
};
Object.defineProperty(DataNode.prototype, "filterType", {
/** @param {C.DATA_FILTER_TYPE} v */
set: function(v){
this._filterType = v;
this.notify( C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
},
/** @return {C.DATA_FILTER_TYPE} */
get: function(){ return this._filterType; }
});
Object.defineProperty(DataNode.prototype, "filterMapping", {
/** @param {Mapping} v */
set: function(v){
swapMapping(this, "_filterMapping", v);
this.notify( C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
},
/** @return {Mapping} */
get: function(){ return this._filterMapping; }
});
Object.defineProperty(DataNode.prototype, "computeOperator", {
/** @param {string} v */
set: function(v){
this._computeOperator = v;
this._computeUsesDataflow = false;
this.notify( C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
},
/** @return {string} */
get: function(){ return this._computeUsesDataflow ? null : this._computeOperator; }
});
Object.defineProperty(DataNode.prototype, "computeDataflowUrl", {
/** @param {string} v */
set: function(v){
this._computeOperator = v;
this._computeUsesDataflow = true;
this.notify( C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
},
/** @return {string} */
get: function(){ return this._computeUsesDataflow ? this._computeOperator : null; }
});
Object.defineProperty(DataNode.prototype, "computeInputMapping", {
/** @param {Mapping} v */
set: function(v){
swapMapping(this, "_computeInputMapping", v);
this.notify( C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
},
/** @return {Mapping} */
get: function(){ return this._computeInputMapping; }
});
Object.defineProperty(DataNode.prototype, "computeOutputMapping", {
/** @param {Mapping} v */
set: function(v){
swapMapping(this, "_computeOutputMapping", v);
this.notify( C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
},
/** @return {Mapping} */
get: function(){ return this._computeOutputMapping; }
});
//noinspection JSUnusedGlobalSymbols
DataNode.prototype.isProtoNode = function(){
return this._isProtoNode;
};
/**
* @param {GraphNode} child
*/
DataNode.prototype.appendChild = function(child){
this._children.push(child);
addParent(this, child);
updateProgressLevel(this);
this.notify( C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
};
/**
* @param {GraphNode} child
*/
DataNode.prototype.removeChild = function(child){
Array.erase(this._children, child);
removeParent(this, child);
updateProgressLevel(this);
this.notify( C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
};
/**
* @param {GraphNode} child
* @param {GraphNode} beforeNode
*/
DataNode.prototype.insertBefore = function(child, beforeNode){
var idx = this._children.indexOf(beforeNode);
if(idx == -1)
this._children.push(child);
else
this._children.splice(idx, 0, child);
addParent(this, child);
// TODO: Next three calls on all structural changes. Add Method
updateProgressLevel(this);
this.notify( C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
};
/**
* clear all references and remove children
*/
DataNode.prototype.clear = function(){
for(var i =0; i < this._children.length; ++i){
removeParent(this, this._children[i]);
}
this._children = [];
this._dataflowNode && this._dataflowNode.removeParent(this);
this._dataflowNode = null;
this._channelNode.decreaseRef();
clearSubstitutionNodes(this);
updateProgressLevel(this);
this.notify( C.RESULT_STATE.REMOVED);
this._listeners = [];
delete this._userData;
delete this._dataflowNode;
Base._flushResultCallbacks();
};
DataNode.prototype.removeParent = function(parent) {
Array.erase(this._parents, parent);
updateProgressLevel(this);
this.notify( C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
};
//noinspection JSUnusedGlobalSymbols
/**
* Detach this DataNode from all connections, including source- and proto-node references
*/
DataNode.prototype.detachFromParents = function(){
for(var i =0; i < this._parents.length; ++i){
var parent = this._parents[i];
if(parent._sourceNode == this)
parent.sourceNode = null;
else if(parent._dataflowNode == this){
parent.dataflowNode = null;
}
else{
parent.removeChild(this);
}
}
this._children = [];
};
/**
* Sets platform of a DataNode. If _platform is defined, it will override the default platform setting of
* an Xflow graph.
*
* @param {String|C.PLATFORM|null} platformSrc
*/
DataNode.prototype.setPlatform = function(platformSrc) {
if (typeof platformSrc === 'string') {
if (platformSrc === "cl") {
this._platform = C.PLATFORM.CL;
}
else if (platformSrc === "gl") {
this._platform = C.PLATFORM.GLSL;
}
else if (platformSrc === "js") {
this._platform = C.PLATFORM.JAVASCRIPT;
}
} else if (!isNaN(parseFloat(platformSrc)) && isFinite(platformSrc)) {
this._platform = platformSrc;
} else {
this._platform = null;
}
this.notify(C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
};
/**
* @const
*/
var filterParser = /^([A-Za-z\s]*)\(([^()]+)\)$/;
/**
* Set filter by string
* @param {string} filterString
*/
DataNode.prototype.setFilter = function(filterString){
filterString = filterString || "";
var newType = C.DATA_FILTER_TYPE.RENAME;
var newMapping = null;
if(filterString){
var result = filterString.trim().match(filterParser);
if(result){
var type = result[1].trim();
switch(type){
case "keep": newType = C.DATA_FILTER_TYPE.KEEP; break;
case "remove": newType = C.DATA_FILTER_TYPE.REMOVE; break;
case "rename": newType = C.DATA_FILTER_TYPE.RENAME; break;
default:
Base.notifyError("Unknown filter type:" + type, this);
}
newMapping = Mapping.Mapping.parse(result[2], this);
}
else{
Base.notifyError("Could not parse filter '" + filterString + "'", this);
}
}
if(!newMapping){
// TODO Remove this? (Mapping can be null from now on)
newMapping = new Mapping.OrderMapping();
}
swapMapping(this, "_filterMapping", newMapping);
this._filterType = newType;
this.notify( C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
};
var computeParser = /^(([^=]+)\=)?([^'(]+('[^']+')?[^'(]+)(\(([^()]*)?\))?$/;
var bracketsParser = /^\(([^()]*)\)$/;
var dataflowParser = /^dataflow\['([^']+)'\]$/;
//noinspection JSUnusedGlobalSymbols
/**
* If the compute string contains a reference to an external dataflow,
* the parser returns its URL. Null, otherwise
* @param computeString
* @returns {string|null}
*/
var getComputeDataflowUrl = function(computeString){
computeString = computeString || "";
var result = computeString.trim().match(computeParser);
if(result){
if(result = result[3].trim().match(dataflowParser)){
return result[1];
}
}
return null;
};
/**
* Set compute by string
* @param {string} computeString
*/
DataNode.prototype.setCompute = function(computeString){
computeString = computeString || "";
var newOperator = "";
var inputMapping = null, outputMapping = null;
var result = computeString.trim().match(computeParser);
if(result){
var output = result[2] ? result[2].trim() : "";
newOperator = result[3].trim();
var input = result[6] ? result[6].trim() : "";
if(result = output.match(bracketsParser)){
output = result[1];
}
if(input)
inputMapping = Mapping.Mapping.parse(input, this);
if(output)
outputMapping = Mapping.Mapping.parse(output, this);
if(result = newOperator.match(dataflowParser)){
this._computeUsesDataflow = true;
newOperator = result[1];
}
else{
this._computeUsesDataflow = false;
}
this._dataflowNode = null;
}
else if(computeString){
Base.notifyError("Error parsing Compute value '" + computeString + "'", this);
}
swapMapping(this, "_computeInputMapping", inputMapping);
swapMapping(this, "_computeOutputMapping", outputMapping);
this._computeOperator = newOperator;
this.notify( C.RESULT_STATE.CHANGED_STRUCTURE);
Base._flushResultCallbacks();
};
/**
* Notifies DataNode about a change. Notification will be forwarded to parents, if necessary
* @param {C.RESULT_STATE} changeType
* @param {GraphNode?} senderNode
*/
DataNode.prototype.notify = function(changeType, senderNode){
//noinspection FallthroughInSwitchStatementJS
switch(changeType) {
case C.RESULT_STATE.REMOVED:
notifyParentsOnChanged(this, C.RESULT_STATE.CHANGED_STRUCTURE);
break;
case C.RESULT_STATE.CHANGED_STRUCTURE:
this._paramNames = null;
this._globalParamNames = null;
this._channelNode.setStructureOutOfSync();
clearSubstitutionNodes(this);
notifyParentsOnChanged(this, changeType);
break;
case C.RESULT_STATE.IMAGE_LOAD_START:
case C.RESULT_STATE.IMAGE_LOAD_END:
updateProgressLevel(this);
if(senderNode){
this._channelNode.notifyDataChange(senderNode, changeType);
}
break;
case C.RESULT_STATE.CHANGED_DATA_VALUE:
case C.RESULT_STATE.CHANGED_DATA_SIZE:
if(senderNode){
this._channelNode.notifyDataChange(senderNode, changeType);
}
break;
}
// Inform listeners (e.g. Requests)
for(var i = 0; i < this._listeners.length; ++i) {
this._listeners[i](changeType);
}
};
DataNode.prototype.addListener = function(listener){
this._listeners.push(listener)
};
DataNode.prototype.removeListener = function(listener) {
Array.erase(this._listeners, listener);
};
DataNode.prototype.addLoadListener = function(listener){
this._loadListeners.push(listener);
};
//noinspection JSUnusedGlobalSymbols
DataNode.prototype.removeLoadListener = function(listener){
Array.erase(this._loadListeners, listener);
};
DataNode.prototype._callLoadListeners = function(newLevel, oldLevel){
var len = this._loadListeners.length;
for(var i = 0; i < len; ++i){
this._loadListeners[i](this, newLevel, oldLevel);
}
};
DataNode.prototype.getOutputNames = function(){
return getForwardNode(this)._channelNode.getOutputNames();
};
DataNode.prototype.getOutputChannelInfo = function(name){
return getForwardNode(this)._channelNode.getOutputChannelInfo(name);
};
//noinspection JSUnusedGlobalSymbols
DataNode.prototype.getParamNames = function(){
return this._getParamNames();
};
/**
* Delegate computation of the result to the channel node
* of the first contributing DataNode.
* @param type
* @param filter
* @returns {Result}
*/
DataNode.prototype._getResult = function(type, filter){
return getForwardNode(this, filter)._channelNode.getResult(type, filter);
};
DataNode.prototype._getForwardNode = function(filter){
return getForwardNode(this, filter);
};
DataNode.prototype._getParamNames = function(){
if(!this._paramNames){
this._paramNames = [];
if(this._sourceNode)
Utils.nameset.add(this._paramNames, this._sourceNode._getParamNames());
else{
for(var i = 0; i < this._children.length; ++i){
Utils.nameset.add(this._paramNames, this._children[i]._getParamNames());
}
}
}
return this._paramNames;
};
DataNode.prototype._getGlobalParamNames = function(){
if(!this._globalParamNames){
this._globalParamNames = [];
if(this._dataflowNode)
Utils.nameset.add(this._globalParamNames, this._dataflowNode._getGlobalParamNames());
if(this._sourceNode)
Utils.nameset.add(this._globalParamNames, this._sourceNode._getGlobalParamNames());
else{
for(var i = 0; i < this._children.length; ++i){
Utils.nameset.add(this._globalParamNames, this._children[i]._getGlobalParamNames());
}
}
}
return this._globalParamNames;
};
/**
* @param {Substitution} substitution
* @returns {ChannelNode}
*/
DataNode.prototype._getOrCreateChannelNode = function(substitution){
if(!substitution)
return this._channelNode;
else{
var key = substitution.getKey(this);
if(!this._substitutionNodes[key]) {
this._substitutionNodes[key] = new ChannelNode(this, substitution);
} else {
this._substitutionNodes[key].increaseRef();
}
return this._substitutionNodes[key];
}
};
/**
* Remove ChannelNode passed as argument from internal substitution nodes
* Decreases reference counter of substitution node and deletes it if not
* used by any other node.
* @param {ChannelNode} substitutionNode
*/
DataNode.prototype._removeSubstitutionNode = function(substitutionNode){
var key = substitutionNode.substitution.getKey(this);
if(this._substitutionNodes[key] && this._substitutionNodes[key].decreaseRef())
delete this._substitutionNodes[key];
};
/**
* Calls clear of all substitutionNodes and clears the map
* @param {DataNode} dataNode
*/
function clearSubstitutionNodes(dataNode){
if (!dataNode._substitutionNodes) {
return;
}
for(var name in dataNode._substitutionNodes) {
dataNode._substitutionNodes[name].clear();
}
dataNode._substitutionNodes = {};
for (var i in dataNode._children) {
clearSubstitutionNodes(dataNode._children[i]);
}
}
//----------------------------------------------------------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------------------------------------------------------
/**
* Skips nodes, if it does not contribute to the result (optimization)
* @param {DataNode} dataNode
* @param {array.<string>?} filter
* @returns {DataNode}
*/
function getForwardNode(dataNode, filter){
var hasFilter = (dataNode._filterMapping && !dataNode._filterMapping.isEmpty());
if(hasFilter)
return dataNode;
if(!dataNode._computeOperator ){
if(dataNode._sourceNode && dataNode._children.length == 0)
return getForwardNode(dataNode._sourceNode);
if(dataNode._children.length == 1 && dataNode._children[0] instanceof DataNode)
return getForwardNode(dataNode._children[0]);
}
var idx = dataNode._channelNode.getChildDataIndex(filter);
if(idx != -1 && idx != undefined){
if(dataNode._sourceNode)
return getForwardNode(dataNode._sourceNode);
else
return getForwardNode(dataNode._children[idx]);
}
return dataNode;
}
/**
* Computes the progress level
* @private
* @param {DataNode} node
*/
function updateProgressLevel(node){
var progressLevel = node._loading ? node._loadLevel : Infinity;
var i;
for(i = 0; progressLevel && i < node._children.length; ++i){
var child = node._children[i];
if(child instanceof DataNode){
progressLevel = Math.min(progressLevel, Math.max(child._loadLevel, child._progressLevel) );
}
else if(child._data && child._data.isLoading && child._data.isLoading()){
progressLevel = Math.min(progressLevel, 1);
}
}
if(progressLevel && node._sourceNode){
progressLevel = Math.min(progressLevel, Math.max(node._sourceNode._loadLevel, node._sourceNode._progressLevel));
}
if(progressLevel && node._dataflowNode){
progressLevel = Math.min(progressLevel, Math.max(node._dataflowNode._loadLevel, node._dataflowNode._progressLevel));
}
var oldLevel = node._progressLevel;
node._progressLevel = progressLevel;
if(oldLevel != node._progressLevel){
node._callLoadListeners(node._progressLevel, oldLevel);
for(i = 0; i < node._parents.length; ++i)
updateProgressLevel(node._parents[i]);
}
}
/**
* @private
* @param {DataNode} parent
* @param {GraphNode} child
*/
function addParent(parent, child){
child._parents.push(parent);
}
/**
* @private
* @param {DataNode} parent
* @param {GraphNode} child
*/
function removeParent(parent, child){
Array.erase(child._parents, parent);
}
/**
* Notify all parent nodes about a change
* @param {GraphNode} node
* @param {exports.C.RESULT_STATE} changeType
* @private
*/
function notifyParentsOnChanged(node, changeType){
for(var i = 0; i < node._parents.length; ++i){
node._parents[i].notify(changeType, node);
}
}
/**
* Update the owners of the mappings
* @param {DataNode} dataNode
* @param {string} key
* @param {Mapping} mapping
*/
function swapMapping(dataNode, key, mapping){
dataNode[key] && dataNode[key]._removeOwner(dataNode);
dataNode[key] = mapping;
dataNode[key] && dataNode[key]._addOwner(dataNode);
}
function replaceNodeInHierarchy(node, field, newChild) {
var oldChild = node[field];
if(oldChild) {
removeParent(node, oldChild);