-
Notifications
You must be signed in to change notification settings - Fork 967
/
workflow-terminals.js
613 lines (592 loc) · 20.6 KB
/
workflow-terminals.js
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
// TODO; tie into Galaxy state?
window.workflow_globals = window.workflow_globals || {};
function CollectionTypeDescription(collectionType) {
this.collectionType = collectionType;
this.isCollection = true;
this.rank = collectionType.split(":").length;
}
var NULL_COLLECTION_TYPE_DESCRIPTION = {
isCollection: false,
canMatch: function(other) {
return false;
},
canMapOver: function(other) {
return false;
},
toString: function() {
return "NullCollectionType[]";
},
append: function(otherCollectionType) {
return otherCollectionType;
},
equal: function(other) {
return other === this;
}
};
var ANY_COLLECTION_TYPE_DESCRIPTION = {
isCollection: true,
canMatch: function(other) {
return NULL_COLLECTION_TYPE_DESCRIPTION !== other;
},
canMapOver: function(other) {
return false;
},
toString: function() {
return "AnyCollectionType[]";
},
append: function(otherCollectionType) {
throw "Cannot append to ANY_COLLECTION_TYPE_DESCRIPTION";
},
equal: function(other) {
return other === this;
}
};
$.extend(CollectionTypeDescription.prototype, {
append: function(otherCollectionTypeDescription) {
if (
otherCollectionTypeDescription === NULL_COLLECTION_TYPE_DESCRIPTION
) {
return this;
}
if (
otherCollectionTypeDescription === ANY_COLLECTION_TYPE_DESCRIPTION
) {
return otherCollectionType;
}
return new CollectionTypeDescription(
this.collectionType +
":" +
otherCollectionTypeDescription.collectionType
);
},
canMatch: function(otherCollectionTypeDescription) {
if (
otherCollectionTypeDescription === NULL_COLLECTION_TYPE_DESCRIPTION
) {
return false;
}
if (
otherCollectionTypeDescription === ANY_COLLECTION_TYPE_DESCRIPTION
) {
return true;
}
return (
otherCollectionTypeDescription.collectionType == this.collectionType
);
},
canMapOver: function(otherCollectionTypeDescription) {
if (
otherCollectionTypeDescription === NULL_COLLECTION_TYPE_DESCRIPTION
) {
return false;
}
if (
otherCollectionTypeDescription === ANY_COLLECTION_TYPE_DESCRIPTION
) {
return false;
}
if (this.rank <= otherCollectionTypeDescription.rank) {
// Cannot map over self...
return false;
}
var requiredSuffix = otherCollectionTypeDescription.collectionType;
return this._endsWith(this.collectionType, requiredSuffix);
},
effectiveMapOver: function(otherCollectionTypeDescription) {
var otherCollectionType = otherCollectionTypeDescription.collectionType;
var effectiveCollectionType = this.collectionType.substring(
0,
this.collectionType.length - otherCollectionType.length - 1
);
return new CollectionTypeDescription(effectiveCollectionType);
},
equal: function(otherCollectionTypeDescription) {
return (
otherCollectionTypeDescription.collectionType == this.collectionType
);
},
toString: function() {
return "CollectionType[" + this.collectionType + "]";
},
_endsWith: function(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
});
var TerminalMapping = Backbone.Model.extend({
initialize: function(attr) {
this.mapOver = attr.mapOver || NULL_COLLECTION_TYPE_DESCRIPTION;
this.terminal = attr.terminal;
this.terminal.terminalMapping = this;
},
disableMapOver: function() {
this.setMapOver(NULL_COLLECTION_TYPE_DESCRIPTION);
},
setMapOver: function(collectionTypeDescription) {
// TODO: Can I use "attributes" or something to auto trigger "change"
// event?
this.mapOver = collectionTypeDescription;
this.trigger("change");
}
});
var Terminal = Backbone.Model.extend({
initialize: function(attr) {
this.element = attr.element;
this.connectors = [];
},
connect: function(connector) {
this.connectors.push(connector);
if (this.node) {
this.node.markChanged();
}
},
disconnect: function(connector) {
this.connectors.splice($.inArray(connector, this.connectors), 1);
if (this.node) {
this.node.markChanged();
this.resetMappingIfNeeded();
}
},
redraw: function() {
$.each(this.connectors, function(_, c) {
c.redraw();
});
},
destroy: function() {
$.each(this.connectors.slice(), function(_, c) {
c.destroy();
});
},
destroyInvalidConnections: function() {
_.each(this.connectors, function(connector) {
connector && connector.destroyIfInvalid();
});
},
setMapOver: function(val) {
if (this.multiple) {
return; // Cannot set this to be multirun...
}
if (!this.mapOver().equal(val)) {
this.terminalMapping.setMapOver(val);
_.each(this.node.output_terminals, function(outputTerminal) {
outputTerminal.setMapOver(val);
});
}
},
mapOver: function() {
if (!this.terminalMapping) {
return NULL_COLLECTION_TYPE_DESCRIPTION;
} else {
return this.terminalMapping.mapOver;
}
},
isMappedOver: function() {
return (
this.terminalMapping && this.terminalMapping.mapOver.isCollection
);
},
resetMapping: function() {
this.terminalMapping.disableMapOver();
},
resetMappingIfNeeded: function() {} // Subclasses should override this...
});
var OutputTerminal = Terminal.extend({
initialize: function(attr) {
Terminal.prototype.initialize.call(this, attr);
this.datatypes = attr.datatypes;
},
resetMappingIfNeeded: function() {
// If inputs were only mapped over to preserve
// an output just disconnected reset these...
if (
!this.node.hasConnectedOutputTerminals() &&
!this.node.hasConnectedMappedInputTerminals()
) {
_.each(this.node.mappedInputTerminals(), function(mappedInput) {
mappedInput.resetMappingIfNeeded();
});
}
var noMappedInputs = !this.node.hasMappedOverInputTerminals();
if (noMappedInputs) {
this.resetMapping();
}
},
resetMapping: function() {
this.terminalMapping.disableMapOver();
_.each(this.connectors, function(connector) {
var connectedInput = connector.handle2;
if (connectedInput) {
// Not exactly right because this is still connected.
// Either rewrite resetMappingIfNeeded or disconnect
// and reconnect if valid.
connectedInput.resetMappingIfNeeded();
connector.destroyIfInvalid();
}
});
}
});
var BaseInputTerminal = Terminal.extend({
initialize: function(attr) {
Terminal.prototype.initialize.call(this, attr);
this.update(attr.input); // subclasses should implement this...
},
canAccept: function(other) {
if (this._inputFilled()) {
return false;
} else {
return this.attachable(other);
}
},
resetMappingIfNeeded: function() {
var mapOver = this.mapOver();
if (!mapOver.isCollection) {
return;
}
// No output terminals are counting on this being mapped
// over if connected inputs are still mapped over or if none
// of the outputs are connected...
var reset =
this.node.hasConnectedMappedInputTerminals() ||
!this.node.hasConnectedOutputTerminals();
if (reset) {
this.resetMapping();
}
},
resetMapping: function() {
this.terminalMapping.disableMapOver();
if (!this.node.hasMappedOverInputTerminals()) {
_.each(this.node.output_terminals, function(terminal) {
// This shouldn't be called if there are mapped over
// outputs.
terminal.resetMapping();
});
}
},
connected: function() {
return this.connectors.length !== 0;
},
_inputFilled: function() {
var inputFilled;
if (!this.connected()) {
inputFilled = false;
} else {
if (this.multiple) {
if (this._collectionAttached()) {
// Can only attach one collection to multiple input
// data parameter.
inputsFilled = true;
} else {
inputFilled = false;
}
} else {
inputFilled = true;
}
}
return inputFilled;
},
_collectionAttached: function() {
if (!this.connected()) {
return false;
} else {
var firstOutput = this.connectors[0].handle1;
if (!firstOutput) {
return false;
} else {
if (
firstOutput.isCollection ||
firstOutput.isMappedOver() ||
firstOutput.datatypes.indexOf("input_collection") > 0
) {
return true;
} else {
return false;
}
}
}
},
_mappingConstraints: function() {
// If this is a connected terminal, return list of collection types
// other terminals connected to node are constraining mapping to.
if (!this.node) {
return []; // No node - completely unconstrained
}
var mapOver = this.mapOver();
if (mapOver.isCollection) {
return [mapOver];
}
var constraints = [];
if (!this.node.hasConnectedOutputTerminals()) {
_.each(this.node.connectedMappedInputTerminals(), function(
inputTerminal
) {
constraints.push(inputTerminal.mapOver());
});
} else {
// All outputs should have same mapOver status - least specific.
constraints.push(
_.first(_.values(this.node.output_terminals)).mapOver()
);
}
return constraints;
},
_producesAcceptableDatatype: function(other) {
// other is a non-collection output...
for (var t in this.datatypes) {
var thisDatatype = this.datatypes[t];
if (thisDatatype == "input") {
return true;
}
var cat_outputs = new Array();
cat_outputs = cat_outputs.concat(other.datatypes);
if (other.node.post_job_actions) {
for (var pja_i in other.node.post_job_actions) {
var pja = other.node.post_job_actions[pja_i];
if (
pja.action_type == "ChangeDatatypeAction" &&
(pja.output_name == "" ||
pja.output_name == other.name) &&
pja.action_arguments
) {
cat_outputs.push(pja.action_arguments["newtype"]);
}
}
}
// FIXME: No idea what to do about case when datatype is 'input'
for (var other_datatype_i in cat_outputs) {
var other_datatype = cat_outputs[other_datatype_i];
if (
other_datatype == "input" ||
other_datatype == "_sniff_" ||
other_datatype == "input_collection" ||
window.workflow_globals.app.isSubType(
cat_outputs[other_datatype_i],
thisDatatype
)
) {
return true;
}
}
}
return false;
},
_otherCollectionType: function(other) {
var otherCollectionType = NULL_COLLECTION_TYPE_DESCRIPTION;
if (other.isCollection) {
otherCollectionType = other.collectionType;
}
var otherMapOver = other.mapOver();
if (otherMapOver.isCollection) {
otherCollectionType = otherMapOver.append(otherCollectionType);
}
return otherCollectionType;
}
});
var InputTerminal = BaseInputTerminal.extend({
update: function(input) {
this.datatypes = input.extensions;
this.multiple = input.multiple;
this.collection = false;
},
connect: function(connector) {
BaseInputTerminal.prototype.connect.call(this, connector);
var other_output = connector.handle1;
if (!other_output) {
return;
}
var otherCollectionType = this._otherCollectionType(other_output);
if (otherCollectionType.isCollection) {
this.setMapOver(otherCollectionType);
}
},
attachable: function(other) {
var otherCollectionType = this._otherCollectionType(other);
var thisMapOver = this.mapOver();
if (otherCollectionType.isCollection) {
if (this.multiple) {
if (this.connected() && !this._collectionAttached()) {
// if single inputs attached, cannot also attach a
// collection (yet...)
return false;
}
if (otherCollectionType.rank == 1) {
return this._producesAcceptableDatatype(other);
} else {
// TODO: Allow subcollection mapping over this as if it were
// a list collection input.
return false;
}
}
if (
thisMapOver.isCollection &&
thisMapOver.canMatch(otherCollectionType)
) {
return this._producesAcceptableDatatype(other);
} else {
// Need to check if this would break constraints...
var mappingConstraints = this._mappingConstraints();
if (
mappingConstraints.every(
_.bind(
otherCollectionType.canMatch,
otherCollectionType
)
)
) {
return this._producesAcceptableDatatype(other);
} else {
return false;
}
}
} else if (thisMapOver.isCollection) {
// Attempting to match a non-collection output to an
// explicitly collection input.
return false;
}
return this._producesAcceptableDatatype(other);
}
});
var InputCollectionTerminal = BaseInputTerminal.extend({
update: function(input) {
this.multiple = false;
this.collection = true;
this.datatypes = input.extensions;
var collectionTypes = [];
if (input.collection_types) {
_.each(input.collection_types, function(collectionType) {
collectionTypes.push(
new CollectionTypeDescription(collectionType)
);
});
} else {
collectionTypes.push(ANY_COLLECTION_TYPE_DESCRIPTION);
}
this.collectionTypes = collectionTypes;
},
connect: function(connector) {
BaseInputTerminal.prototype.connect.call(this, connector);
var other = connector.handle1;
if (!other) {
return;
}
var effectiveMapOver = this._effectiveMapOver(other);
this.setMapOver(effectiveMapOver);
},
_effectiveMapOver: function(other) {
var collectionTypes = this.collectionTypes;
var otherCollectionType = this._otherCollectionType(other);
var canMatch = _.some(collectionTypes, function(collectionType) {
return collectionType.canMatch(otherCollectionType);
});
if (!canMatch) {
for (var collectionTypeIndex in collectionTypes) {
var collectionType = collectionTypes[collectionTypeIndex];
if (otherCollectionType.canMapOver(collectionType)) {
var effectiveMapOver = otherCollectionType.effectiveMapOver(
collectionType
);
if (effectiveMapOver != NULL_COLLECTION_TYPE_DESCRIPTION) {
return effectiveMapOver;
}
}
}
}
return NULL_COLLECTION_TYPE_DESCRIPTION;
},
_effectiveCollectionTypes: function() {
var thisMapOver = this.mapOver();
return _.map(this.collectionTypes, function(t) {
return thisMapOver.append(t);
});
},
attachable: function(other) {
var otherCollectionType = this._otherCollectionType(other);
if (otherCollectionType.isCollection) {
var effectiveCollectionTypes = this._effectiveCollectionTypes();
var thisMapOver = this.mapOver();
var canMatch = _.some(effectiveCollectionTypes, function(
effectiveCollectionType
) {
return effectiveCollectionType.canMatch(otherCollectionType);
});
if (canMatch) {
// Only way a direct match...
return this._producesAcceptableDatatype(other);
// Otherwise we need to mapOver
} else if (thisMapOver.isCollection) {
// In this case, mapOver already set and we didn't match skipping...
return false;
} else if (
_.some(this.collectionTypes, function(collectionType) {
return otherCollectionType.canMapOver(collectionType);
})
) {
var effectiveMapOver = this._effectiveMapOver(other);
if (!effectiveMapOver.isCollection) {
return false;
}
// Need to check if this would break constraints...
var mappingConstraints = this._mappingConstraints();
if (mappingConstraints.every(effectiveMapOver.canMatch)) {
return this._producesAcceptableDatatype(other);
}
}
}
return false;
}
});
var OutputCollectionTerminal = Terminal.extend({
initialize: function(attr) {
Terminal.prototype.initialize.call(this, attr);
this.datatypes = attr.datatypes;
if (attr.collection_type) {
this.collectionType = new CollectionTypeDescription(
attr.collection_type
);
} else {
var collectionTypeSource = attr.collection_type_source;
if (!collectionTypeSource) {
console.log(
"Warning: No collection type or collection type source defined."
);
}
this.collectionType = ANY_COLLECTION_TYPE_DESCRIPTION;
}
this.isCollection = true;
},
update: function(output) {
var newCollectionType;
if (output.collection_type) {
newCollectionType = new CollectionTypeDescription(
output.collection_type
);
} else {
var collectionTypeSource = output.collection_type_source;
if (!collectionTypeSource) {
console.log(
"Warning: No collection type or collection type source defined."
);
}
newCollectionType = ANY_COLLECTION_TYPE_DESCRIPTION;
}
if (
newCollectionType.collectionType !=
this.collectionType.collectionType
) {
_.each(this.connectors, function(connector) {
// TODO: consider checking if connection valid before removing...
connector.destroy();
});
}
this.collectionType = newCollectionType;
}
});
export default {
InputTerminal: InputTerminal,
OutputTerminal: OutputTerminal,
InputCollectionTerminal: InputCollectionTerminal,
OutputCollectionTerminal: OutputCollectionTerminal,
TerminalMapping: TerminalMapping,
// test export
CollectionTypeDescription: CollectionTypeDescription,
NULL_COLLECTION_TYPE_DESCRIPTION: NULL_COLLECTION_TYPE_DESCRIPTION,
ANY_COLLECTION_TYPE_DESCRIPTION: ANY_COLLECTION_TYPE_DESCRIPTION
};