-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmapping.js
More file actions
356 lines (316 loc) · 10 KB
/
mapping.js
File metadata and controls
356 lines (316 loc) · 10 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
var Base = require("../base.js");
var C = require("./../interface/constants.js");
var orderMappingParser = /^([^:,{}]+)(,[^:{},]+)*$/;
var nameMappingParser = /^\{(([^:,{}]+:[^:{},]+)(,[^:{},]+:[^:},]+)*)}$/;
/**
* A mapping used for a filter or a compute properties of a DataNode
* @abstract
*/
var Mapping = function(){
/**
* @type {Array<DataNode>}
*/
this._owners = [];
};
/**
* Parse a Mapping (both C.OrderMapping or C.ComputeMapping) from a syntax string.
* @param {string} string The syntax string.
* @param {C.DataNode} dataNode DataNode of the Mapping
* @returns {?C.Mapping}
*/
Mapping.parse = function(string, dataNode){
string = string.trim();
var results = string.trim().match(orderMappingParser);
if(results)
return OrderMapping.parse(string, dataNode);
results = string.trim().match(nameMappingParser);
if(results)
return NameMapping.parse(results[1], dataNode);
Base.notifyError("Cannot parse name mapping '" + string + "'", dataNode);
return null;
};
/**
*
* @param {DataNode} owner
* @private
*/
Mapping.prototype._addOwner = function(owner){
var idx = this._owners.indexOf(owner);
if(idx == -1)
this._owners.push(owner);
};
/**
*
* @param {DataNode} owner
* @private
*/
Mapping.prototype._removeOwner = function(owner){
var idx = this._owners.indexOf(owner);
if(idx != -1)
this._owners.splice(idx, -1);
};
//----------------------------------------------------------------------------------------------------------------------
// OrderMapping
//----------------------------------------------------------------------------------------------------------------------
/**
* An OrderMapping used for a filter or compute properties of a DataNode
* It describes a mapping of names referring to the order of arguments / output values.
* OrderMapping syntax examples in compute:
* position = C.morph(position, posAdd, weight)
* @constructor
* @extends {Mapping}
*/
var OrderMapping = function(){
Mapping.call(this);
this._names = [];
};
Base.createClass(OrderMapping, Mapping);
OrderMapping.parse = function(string, dataNode){
var mapping = new OrderMapping(dataNode);
var token = string.split(",");
for(var i = 0; i < token.length; i++){
mapping._names.push(token[i].trim());
}
return mapping;
};
Object.defineProperty(OrderMapping.prototype, "length", {
set: function(){ throw new Error("length is read-only");
},
get: function(){ return this._names.length; }
});
OrderMapping.prototype.getName = function(idx){
return this._names[idx];
};
OrderMapping.prototype.clear = function(){
this._names = [];
mappingNotifyOwner(this);
};
OrderMapping.prototype.setName = function(index, name){
this._names[index] = name;
mappingNotifyOwner(this);
};
//noinspection JSUnusedGlobalSymbols
OrderMapping.prototype.removeName = function(index){
this._names.splice(index);
mappingNotifyOwner(this);
};
OrderMapping.prototype.isEmpty = function(){
return this._names.length == 0;
};
/**
*
* @param {ChannelMap} destMap
* @param {ChannelMap} sourceMap
* @param {exports.C.DATA_FILTER_TYPE} filterType
* @param {function(ChannelMap, string, ChannelMap, string)} callback
*/
OrderMapping.prototype.applyFilterOnChannelMap = function(destMap, sourceMap, filterType, callback){
var i;
if(filterType == C.DATA_FILTER_TYPE.KEEP){
for(i = 0; i < this._names.length; ++i){
var name = this._names[i];
if(sourceMap.map[name]) {
callback(destMap, name, sourceMap, name);
}
}
}
else{
for(i in sourceMap.map){
var idx = this._names.indexOf(i);
if(filterType == C.DATA_FILTER_TYPE.RENAME ||
(filterType == C.DATA_FILTER_TYPE.REMOVE && idx == -1))
callback(destMap, i, sourceMap, i);
}
}
};
/**
* Return the name of the input value assigned to operator argument.
* Returns null, if no mapping is defined.
* @param {number} index Position of the operator argument
* @returns {string|null}
*/
OrderMapping.prototype.getScriptInputName = function(index /*, destName */){
if(this._names[index])
return this._names[index];
else
return null;
};
/**
* Returns the name of the output parameter as it should be used for the
* following dataflow. Returns null, if no mapping is defined.
* @param index
* @returns {string|null}
*/
OrderMapping.prototype.getScriptOutputName = function(index /*, srcName */){
if(this._names[index])
return this._names[index];
else
return null;
};
/**
* Returns the inverse name of the output parameter as it should be used for the
* following dataflow. Returns null, if no mapping is defined.
* @param {string} destName
* @param {array<object>} operatorOutputs
* @returns {string|null}
*/
OrderMapping.prototype.getScriptOutputNameInv = function(destName, operatorOutputs){
var index = this._names.indexOf(destName);
if(index == -1)
return null;
return operatorOutputs[index].name;
};
/**
* Identity function. Used to implement interface. Usually you don't rename with order
* mapping.
* @param name
* @returns {string}
*/
OrderMapping.prototype.getRenameSrcName = function(name){
return name;
};
//----------------------------------------------------------------------------------------------------------------------
// NameMapping
//----------------------------------------------------------------------------------------------------------------------
/**
* An NameMapping used for a filter or compute properties of a DataNode
* It describes a mapping of names referring to the original names of the arguments / output values.
* NameMapping syntax examples in compute:
* {position: result} = C.morph({value: position, valueAdd: posAdd, weight: weight})
* @constructor
* @extends {Mapping}
*/
var NameMapping = function(){
Mapping.call(this);
this._destNames = [];
this._srcNames = [];
};
Base.createClass(NameMapping, Mapping);
NameMapping.parse = function(string, dataNode) {
var mapping = new NameMapping(dataNode);
var token = string.split(",");
for(var i = 0; i < token.length; i++){
var pair = token[i].split(":");
var dest = pair[0].trim(); var src = pair[1].trim();
mapping.setNamePair(dest, src);
}
return mapping;
};
Object.defineProperty(NameMapping.prototype, "length", {
set: function(){ throw new Error("length is read-only");
},
get: function(){ return this._srcNames.length; }
});
NameMapping.prototype.getDestName = function(idx){
return this._destNames[idx];
};
NameMapping.prototype.getSrcName = function(idx){
return this._srcNames[idx];
};
NameMapping.prototype.getSrcNameFromDestName = function(destName){
var idx = this._destNames.indexOf(destName);
return idx == -1 ? null : this._srcNames[idx];
};
NameMapping.prototype.getDestNameFromSrcName = function(srcName){
var idx = this._srcNames.indexOf(srcName);
return idx == -1 ? null : this._destNames[idx];
};
NameMapping.prototype.clear = function(){
this._srcNames = [];
this._destNames = [];
mappingNotifyOwner(this);
};
NameMapping.prototype.setNamePair = function(destName, srcName){
var idx = this._destNames.indexOf(destName);
if(idx != -1){
this._destNames.splice(idx,1);
this._srcNames.splice(idx,1);
}
this._destNames.push(destName);
this._srcNames.push(srcName);
mappingNotifyOwner(this);
};
//noinspection JSUnusedGlobalSymbols
NameMapping.prototype.removeNamePair = function(destName){
var idx = this._destNames.indexOf(destName);
if(idx != -1){
this._destNames.splice(idx,1);
this._srcNames.splice(idx,1);
}
mappingNotifyOwner(this);
};
NameMapping.prototype.isEmpty = function(){
return this._destNames.length == 0;
};
/**
* @see OrderMapping.applyFilterOnChannelMap
* @param {ChannelMap} destMap
* @param {ChannelMap} sourceMap
* @param {C.DATA_FILTER_TYPE} filterType
* @param {function} callback
*/
NameMapping.prototype.applyFilterOnChannelMap = function(destMap, sourceMap, filterType, callback) {
var i;
if(filterType == C.DATA_FILTER_TYPE.REMOVE){
for(i in sourceMap.map)
if(this._srcNames.indexOf(i) == -1)
callback(destMap, i, sourceMap, i);
}
else{
if(filterType == C.DATA_FILTER_TYPE.RENAME){
for(i in sourceMap.map)
if(this._srcNames.indexOf(i) == -1)
callback(destMap, i, sourceMap, i);
}
for(i in this._destNames){
callback(destMap, this._destNames[i], sourceMap, this._srcNames[i]);
}
}
};
/**
* Renames: Look-up the destination name and return the source name
* @param {string} name
* @returns {string}
*/
NameMapping.prototype.getRenameSrcName = function(name){
return this.getSrcNameFromDestName(name) || name;
};
/**
* Return the name of the input value assigned to operator argument
* @param {number} index Position of the operator argument
* @param {string} destinationName Name of the operator argument
* @returns {string|null}
*/
NameMapping.prototype.getScriptInputName= function(index, destinationName){
return this.getSrcNameFromDestName(destinationName);
};
/**
* @see OrderMapping.getScriptOutputName
*/
NameMapping.prototype.getScriptOutputName = function(index, srcName){
return this.getDestNameFromSrcName(srcName);
};
/**
* @see OrderMapping.getScriptOutputNameInv
*/
NameMapping.prototype.getScriptOutputNameInv = function(destName /*, operatorOutputs */){
var index = this._destNames.indexOf(destName);
if(index == -1)
return null;
return this._srcNames[index];
};
/**
* Notify all DataNodes that use the mapping passed to the function
* @param {Mapping} mapping
*/
function mappingNotifyOwner(mapping){
for(var i = 0; i < mapping._owners.length; ++i) {
mapping._owners[i].notify(C.RESULT_STATE.CHANGED_STRUCTURE);
}
Base._flushResultCallbacks();
}
module.exports = {
NameMapping: NameMapping,
OrderMapping: OrderMapping,
Mapping: Mapping
};