forked from xml3d/xml3d.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptinstance.js
More file actions
129 lines (115 loc) · 4.62 KB
/
scriptinstance.js
File metadata and controls
129 lines (115 loc) · 4.62 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
// data/script.js
(function() {
"use strict";
var ScriptInstance = function(data, script) {
this.data = data;
this.script = script;
this.result = {};
this.needsEvaluation = true;
this.outputListener = new Array();
this.tables = new Array();
this.getValue = function(name,cb) {
if(this.needsEvaluation)
this.evaluate();
if(cb instanceof Function)
cb();
return this.result[name];
};
this.getTupleSize = function(name) {
for(var i = 0; i < this.script.outputs.length; i++) {
if(this.script.outputs[i].name == name)
return this.script.outputs[i].tupleSize;
};
};
this.dataChanged = function(dataTable, entries) {
this.needsEvaluation = true;
this.markOutputs(true);
this.notifyTables();
};
this.evaluate = function() {
var args = [];
var that = this;
this.script.params.forEach(function(param){
var arg = that.table.providers[param];
if (!arg) {
XML3D.debug.logInfo("Missing input in xflow script: " + param);
args.push(undefined);
}
else {
//console.log("Add argument " + param + ": " + arg);
args.push(arg.getValue(XML3D._parallel));
}
});
this.args = args;
this.result = null;
this.result = {};
try {
var ok = false;
if (XML3D._parallel && this.script.evaluate_parallel) {
XML3D.debug.logDebug("Evaluate " + this.script.name + " on " + this.data.node.id + " using RiverTrail");
//(this.script.name == "skinning") && window.museum && window.museum.stats.begin();
ok = this.script.evaluate_parallel.apply(this,this.args);
//(this.script.name == "skinning") && window.museum && window.museum.stats.end();
} else {
XML3D.debug.logDebug("Evaluate " + this.script.name + " on " + this.data.node.id);
//(this.script.name == "skinning") && window.museum && window.museum.stats.begin();
ok = this.script.evaluate.apply(this,this.args);
//(this.script.name == "skinning") && window.museum && window.museum.stats.end();
}
//console.dir(this.result);
} catch (e) {
XML3D.debug.logExecption(e, "Failed to evaluate xflow script: ");
}
this.needsEvaluation = false;
};
this.registerOutput = function(output) {
var length = this.outputListener.length;
for(var i = 0; i < length; i++)
{
if(this.outputListener[i] == output)
{
XML3D.debug.logWarning("Observer " + output + " is already registered");
return;
}
}
this.outputListener.push(output);
this.registerTable(output.table);
};
this.markOutputs = function(flag) {
var length = this.outputListener.length;
for(var i = 0; i < length; i++)
{
this.outputListener[i].dirty = flag;
}
};
this.registerTable = function(table) {
var length = this.tables.length;
for(var i = 0; i < length; i++)
{
if(this.tables[i] == table)
{
return;
}
}
this.tables.push(table);
};
this.notifyTables = function() {
var length = this.tables.length;
for(var i = 0; i < length; i++)
{
this.tables[i].notifyDataChanged(this);
}
};
// This script instance is a consumer itself
//console.log("Creating Table for "+ this.script.name + " ScriptInstance of " + this.data.node.id);
this.table = new XML3D.data.ProcessTable(this, this.script.params, this.dataChanged);
this.data.requestInputData(this.table);
//console.log("Table for "+ this.script.name + " ScriptInstance of " + this.data.node.id + ": ");
//console.dir(this.table);
this.table.close();
};
ScriptInstance.prototype.toString = function() {
return "ScriptInstance("+this.data.node.id+"/"+this.script.name+")";
};
XML3D.data.ScriptInstance = ScriptInstance;
}());