source: gs3-extensions/web-audio/trunk/js-dsp/component/workflow-core.js@ 28388

Last change on this file since 28388 was 28388, checked in by davidb, 11 years ago

Set of JS, CSS, PNG etc web resources to support a mixture of audio player/document display capabilities

File size: 5.8 KB
Line 
1
2
3// ******
4// 'Component.java' helper enums
5// ******
6
7
8var FiringPolicy = {
9 all: 0,
10 any: 1
11};
12
13var Licenses = {
14 UofINCSA: 0,
15 ASL_2: 1,
16 Other: 2
17};
18
19var Runnable = {
20 java: 0,
21 python: 1,
22 lisp: 2,
23 javascript: 3 // **** extension for OeRC work
24};
25
26
27// ******
28// StringSerminator
29// ******
30
31var StringTerminator = function() {
32 this.type = "terminator";
33}
34
35StringTerminator.isStreamTerminator = function (obj) {
36
37 return ((obj.type!=null) && (obj.type == "terminator"));
38}
39
40
41// ******
42// ComponentContext
43// ******
44
45
46var Component = function() {
47 this.outputConnector = {};
48 this.outputConnectorInputName = {};
49}
50
51Component.method('connectOutput', function(outputName, inputCpt, inputCptName) {
52
53 this.outputConnector[outputName] = inputCpt;
54 this.outputConnectorInputName[outputName] = inputCptName;
55
56});
57
58
59Component.method('setProperty', function(p,v) {
60
61 this.getset[p] = v;
62});
63
64
65Component.method('getProperty', function(p) {
66
67 return getset[p];
68});
69
70
71
72
73// ******
74// ComponentContext
75// ******
76
77
78var _wfcPushDataCount = 0;
79
80var ComponentContext = function(component) {
81 this.component = component;
82 this.outputConnectorContext = {};
83
84
85 var cia = this.component.ComponentInputArray;
86 var cia_len = cia.length;
87
88 // Create an input set with all ComponentInputs set to null
89 var input_set = {};
90
91 for (var i=0; i<cia_len; i++) {
92
93 input_set[name] = null;
94 }
95
96 this.inputSet = input_set;
97
98 // now work through all the components linked through 'outputConnectors'
99 // and recursively set up ComponentContexts for these as well
100
101 for (var output_name in component.outputConnector) {
102
103 var next_cpt = component.outputConnector[output_name];
104 this.outputConnectorContext[output_name] = new ComponentContext(next_cpt)
105 }
106}
107
108
109ComponentContext.prototype.findComponent = function(ca,name) {
110
111 var ca_len = ca.length;
112
113 var c = null;
114
115 for (var i=0; i<ca_len; i++) {
116 if (ca[i].name == name) {
117 c = ca[i];
118 }
119 }
120
121 return c;
122}
123
124
125
126ComponentContext.prototype.getDataComponentFromInput = function(name) {
127
128 var ci = this.findComponent(this.component.ComponentInputArray,name);
129
130 //console.log("*** getData input: name = " + name + " cpt type = " + Object.prototype.toString.call(this.component));
131
132 if (ci==null) {
133 // requested name does not exist
134 throw "getDataComponentFromInput(): Input name '" + name + " 'is not defined for this '"
135 + Object.prototype.toString.call(this.component) + "' component";
136 }
137
138 var ci_data = this.inputSet[name]; // note: might be null if input 'name' has not yet arrived
139
140 return ci_data;
141}
142
143
144
145ComponentContext.prototype.pushDataComponentToOutput = function(name, obj) {
146
147 if ((_wfcPushDataCount%500)==0) {
148 console.log("workflow-core::pushDataComponentToOutput() count: " + _wfcPushDataCount);
149 }
150 _wfcPushDataCount++;
151
152 var co = this.findComponent(this.component.ComponentOutputArray,name);
153
154 if (co==null) {
155 // requested name does not exist
156 throw "pushDataComponentToOutput(): Output name '" + name + " 'is not defined for '"
157 + toType(this.component) + "' component";
158 }
159
160
161 // retrieve the destination Cpt specified by 'name' and its CC
162
163 var next_cpt = this.component.outputConnector[name];
164 var next_input_name = this.component.outputConnectorInputName[name];
165
166 var next_cc = this.outputConnectorContext[name];
167
168 var next_input_set = next_cc.inputSet;
169
170 if (next_input_set[name] != null) {
171 throw "pushDataComponentToOutput(): Input for '" + next_input_name + "' already exists";
172 }
173
174 next_input_set[next_input_name] = obj;
175
176 // Now see if the component's executeCallBack method is due to be fired
177
178 var fire_exec = true;
179
180 var firing_policy = next_cpt.firingPolicy;
181
182 //console.log("*** firing policy = " + firing_policy);
183
184 if ((firing_policy!=null) && (firing_policy == FiringPolicy.all)) {
185
186 for (var k in next_input_set) {
187 if (next_input_set[k] == null) {
188 // evidence that all inputs are not there yet
189 fire_exec = false;
190 break;
191 }
192 }
193 }
194
195 if (fire_exec) {
196 // console.log("**** Away to call executCallback");
197
198 next_cpt.executeCallBack(next_cc);
199 }
200}
201
202
203// ******
204// Workflow
205// ******
206
207var Workflow = function(startingComponent) {
208 this.startingComponent = startingComponent;
209
210 var unique_list = [ startingComponent ];
211 var working_queue = [ startingComponent ];
212
213 var visited_cpt = {};
214 visited_cpt[startingComponent.getHashCode] = true;
215
216 while (working_queue.length>0) {
217 var curr_cpt = working_queue.shift();
218
219 curr_cpt.initializeCallBack();
220
221 for (var outputName in this.outputConnector) {
222
223 var next_cpt = this.outputConnector[outputName];
224 var next_cpt_hashCode = next_cpt.getHashCode;
225
226 if (!visited_cpt[next_cpt_hashCode]) {
227 // haven't seen it before
228 // => record as seen, and push on queue
229
230
231 visited_cpt[next_cpt_hashCode] = true;
232 working_queue.push(next_cpt);
233 }
234 }
235 }
236
237
238 this.uniqueCptList = unique_list;
239
240}
241
242Workflow.prototype.initialize = function() {
243
244 for (var i=0; i<this.uniqueCptList.length; i++) {
245 var curr_cpt = this.uniqueCptList[i];
246 curr_cpt.initializeCallBack();
247 }
248};
249
250
251Workflow.prototype.pumpDataHead = function(name,data) {
252
253 var cc = new ComponentContext(this.startingComponent);
254 cc.inputSet[name] = data;
255
256 this.startingComponent.executeCallBack(cc);
257};
258
259
260Workflow.prototype.dispose = function() {
261
262 for (var i=0; i<this.uniqueCptList.length; i++) {
263 var curr_cpt = this.uniqueCptList[i];
264 curr_cpt.disposeCallBack();
265 }
266};
267
Note: See TracBrowser for help on using the repository browser.