| 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 |
1
1
4
4
4
4
13
13
93
13
80
13
13
9
4
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
4
4
4
4
4
4
13
2
2
2
13
4
4
1
4
15
15
4
4
9
9
9
4
4
4
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
3
3
3
3
21
21
21
21
21
3
3
2
2
| /**
* CollaborationService.js
*
* @description :: Service that takes care of managing a graph.
* @docs :: http://sailsjs.org/#!documentation/services
*/
var parser = require('../../lib/parser/parser.js');
function metaGraphfromCommand(command){
Iif(!command){
return {
components:[],
connections:[]
}
}
var parsedGraph = parser.parseCommand(command);
//parsedGraph.connections = parsedGraph.connections.map(function(connection){return connection.toJSON()});
var componentMap = {}
parsedGraph.components = parsedGraph.components.map(function(component){
var obj = {}
for(var key in component){
switch(key){
case 'id': break;
default: obj[key] = component[key];
}
}
componentMap[component.id] = obj
return obj;
});
parsedGraph.connections = parsedGraph.connections.map(function(connection){return {
startNode: componentMap[connection.startComponent.id],
startPort: connection.startPort,
endNode: componentMap[connection.endComponent.id],
endPort: connection.endPort
}});
return parsedGraph.toJSON();
}
/* istanbul ignore next */
function createComponent(projectId, graphId, command, position, cb){
var indexOfSpace = command.indexOf(" ");
var firstWord = (indexOfSpace > -1 ) ? command.slice(0, command.indexOf(" ")) : command;
var componentData;
var input = "input"
//sails.log('command ', command, "1st word:", firstWord);
Iif(firstWord.indexOf('.') >= 0){
componentData = {
graph: graphId,
type:"file",
data: { type:"file", filename: firstWord }
};
} else Iif(parser.isImplemented(firstWord)){
var metagraph = metaGraphfromCommand(command);
componentData = {
graph: graphId,
type:"command",
data: metagraph.components[0]
};
} else {
Graph.find({project:projectId}).exec(function(err, res){
res
.filter(function(graph){return graph.type !== 'root'})
.every(function(graph){
Eif(graph.data.name === command){
componentData = {
graph: graphId,
type:"macro",
data: { type:"macro", graph: graph.id }
};
input = "macroIn0";
return true
} else return false
})
})
}
Iif(!componentData){
return cb("unable to create component \""+firstWord+"\"");
}
Iif(position){
componentData.data.position = position;
}
Component.create(componentData).exec(function(err,res){
cb(err,res,input);
});
}
/* istanbul ignore next */
function createAndConnectComponent(projectId, graphId, command, componentId, port, position, fromInput, cb){
Eif(typeof fromInput == "function"){
cb = fromInput;
fromInput = false;
}
createComponent(projectId, graphId, command,position, function(err,created, endPort){
Iif(err) return cb(err);
var connectionData = (fromInput) ? {
graph : graphId,
startNode : created.id,
startPort : (endPort=="input")?"output": "macroOut0",
endNode : componentId,
endPort : port
}: {
graph : graphId,
startNode : componentId,
startPort : port,
endNode : created.id,
endPort : endPort
}
//sails.log('connectionData:', connectionData)
Connection.create(connectionData).exec(function(err,createdConn){
cb(err, {component: created, connection:createdConn})
});
});
}
module.exports = {
metaGraphfromCommand:metaGraphfromCommand,
parser:parser,
addToGraph: function addToGraph(graphId, command, cb, addInOut){
cb = cb || function(){};
var metagraph = GraphGeneratorService.metaGraphfromCommand(command);
var components = metagraph.components;
var maxPos = 0
var midPos = 0
var componentsToCreate = components.map(function(component){
if(addInOut){
component.position.x += 400;
maxPos = Math.max(maxPos, component.position.x);
midPos = Math.max(midPos, component.position.y);
}
return {
graph: graphId,
data: JSON.stringify(component)
}
})
midPos /= 2;
if(addInOut){
componentsToCreate = componentsToCreate.concat({
graph: graphId,
data: JSON.stringify({
type:"input",
position: {x:0, y:midPos},
ports:["input"]
})
},{
graph: graphId,
data: JSON.stringify({
type:"output",
position: {x:maxPos + 400, y:midPos},
ports:["output","error"]
})
})
}
async.parallel(
componentsToCreate.map(function(component){
return function(done){
Component.create(component).exec(done);
};
}), function(err, createdComponents){
/* istanbul ignore if */ Iif(err || !createdComponents) return cb(err)
metagraph.connections.forEach(function(connection){
connection.graph = graphId
connection.startNode = createdComponents[components.indexOf(connection.startNode)].id
connection.endNode = createdComponents[components.indexOf(connection.endNode)].id
});
Connection.create(metagraph.connections).exec(function(err,createdConnections){
/* istanbul ignore if */ Iif(err || !createdConnections) return cb(err)
else return cb(null, {components: createdComponents, connections: createdConnections})
});
});
},
createComponent: createComponent,
createAndConnectComponent:createAndConnectComponent,
removeComponent: /* istanbul ignore next */ function(id, callback){
async.auto({
components: function (cb) {Component.destroy({id:id}).exec(cb)},
connections: function (cb) {Connection.destroy().where({or:[{startNode:id},{endNode:id}]}).exec(cb)}
}, callback);
},
removeComponentFilePort: /* istanbul ignore next */ function(id, port, callback){
var portToremove = "file"+port
async.series([
function (cb) {Connection.destroy().where({endNode:id, endPort: portToremove}).exec(cb)},
function (cb) {Connection.find({endNode:id, endPort: {contains:"file"}}).exec(function(err,results){
/* istanbul ignore if */ if(err || !results) return cb(err);
var updatedRecords = []
results.forEach(function(res){
var resPort = parseInt(res.endPort.slice(4));
if(resPort > port){
res.endPort = "file" + ( resPort + 1 )
res.save(function(){});
updatedRecords.push(res);
}
})
cb(null, updatedRecords);
})}],callback);
},
createGraphfromCommand: /* istanbul ignore next */ function(params, command){
Graph.create(params).exec(function(err,created){
//if(err) return next(err);
//if(!created) return next();
addToGraph(created.id, command)
sails.log('Created project with name '+created.name);
});
},
compileProject: function(id, cb){
Graph.find({project: id}).populate('components').populate('connections').exec(function (err, graphs){
/* istanbul ignore if */ Iif(err) return cb(err, null);
graphs.forEach(function(graph){
graph.components = graph.components.map(function(comp){
var data = comp.data;
Iif(data.type == "macro"){
var macro = _.find(graphs, function(graph){return graph.id == data.graph});
data.macro = macro
data.inputs = macro.data.inputs
data.outputs = macro.data.outputs
}
Iif(data.type == "input"){
data.ports = graph.data.inputs
}
Iif(data.type == "output"){
data.ports = graph.data.outputs
}
data.id = comp.id; return data
});
});
var rootGraph = _.find(graphs, function(graph){return graph.type == "root"});
return cb(null, parser.parseVisualDataExperimental(parser.graphFromJsonObject(rootGraph)));
});
},
compileGraph: function(id, cb){
Graph.findOne(id).exec(function (err, graph){
module.exports.compileProject(graph.project,cb)
});
}
}; |