forked from AToMPM/atompm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asworker.js
546 lines (486 loc) · 14.8 KB
/
asworker.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
/* This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
* Copyright 2011 by the AToMPM team and licensed under the LGPL
* See COPYING.lesser and README.md in the root of this project for full details
*/
const {
__errorContinuable,
__httpReq,
__postInternalErrorMsg, __postMessage,
__sequenceNumber,
__successContinuable,
__uri_to_id
} = require("./__worker");
const _do = require("./___do");
const _utils = require('./utils');
const _mmmk = require("./mmmk");
const _libcompile = require("./libcompile");
const _libeventhandler = require("./libeventhandler")
const _fs = _do.convert(require('fs'), ['readFile', 'writeFile', 'readdir']);
module.exports = {
/************************** REST REQUEST HANDLING **************************/
/* INTENT :
ask our mtworker to do something (e.g., change transformation execution
mode)
IN PRACTICE:
adjust uri and forward to mtworker
1. setup sync/async action chaining
a) if this asworker doesn't already have an mtworker
i. create mtworker
ii. subscribe it to this asworker
iii. send it this asworker's current model
b) ask asworker to forward initial request to its mtworker
2. launch chain... return success code or error */
'__mtwid':undefined,
'mtwRequest' :
function(resp,method,uri,reqData)
{
let self = this;
let actions =
[__successContinuable(),
function () {
uri = uri.substring('/__mt'.length);
return __httpReq(
method,
uri + '?wid=' + self.__mtwid,
reqData,
8125);
}];
if( this.__mtwid == undefined )
{
if( reqData == undefined ||
reqData['transfs'] == undefined ||
reqData['transfs'].length == 0 )
return __postInternalErrorMsg(resp,'missing transformations');
else if( reqData['username'] == undefined ||
reqData['username'].length == 0 )
return __postInternalErrorMsg(resp,'missing username');
actions.splice(1,0,
function()
{
return __httpReq(
'PUT',
'/GET/console?wid='+__wid,
{'text':'please wait while model transformation module'+
' initializes (this may take a few seconds)'});
},
function()
{
return __httpReq('POST','/mtworker',undefined,8125);
},
function(mtwid)
{
self.__mtwid = mtwid;
return __httpReq(
'PUT',
'/aswSubscription?wid='+self.__mtwid,
{'aswid':__wid},
8125);
},
function()
{
return _fs.readFile('./users/'+reqData['username']+
'/prefs','utf8');
},
function(prefs)
{
try {prefs = eval('('+prefs+')');}
catch(err) {
return __errorContinuable(
'an error occurred while reading your preferences '+
'(you must restart your client before successful '+
'model transformation module initialization becomes'+
' possible :: '+err);}
return __httpReq(
'PUT',
'/envvars?wid='+self.__mtwid,
{'username':reqData['username'],
'defaultDCL':prefs['default-mt-dcl']['value']},
8125);
},
function()
{
let mms = _mmmk.readMetamodels();
if (mms['$err'])
return __errorContinuable(mms['$err']);
let m = _mmmk.read();
if (m['$err'])
return __errorContinuable(m['$err']);
return __httpReq(
'PUT',
'/current.model?wid=' + self.__mtwid,
{
'mms': mms,
'm': m,
'sequence#': __sequenceNumber(0)
},
8125);
},
function()
{
return __httpReq(
'PUT',
'/current.transform?wid='+self.__mtwid,
{'transfs':reqData['transfs']},
8125);
},
function()
{
return __httpReq(
'PUT',
'/GET/console?wid='+__wid,
{'text':'model tranformation module is ready to go!'});
}
);
}
_do.chain(actions)(
function () {
__postMessage({'statusCode': 200, 'respIndex': resp});
},
function (err) {
if (err.includes("ECONNREFUSED")) {
let msg = "could not connect to model transformation worker!\n" +
"please ensure the worker is running!";
__postInternalErrorMsg(resp, msg);
} else {
__postInternalErrorMsg(resp, err);
}
}
);
},
/* load a metamodel
1. setup sync/async action chaining
a) if no hitchhiker containing CSMM information is provided, or if only
a CSMM name is provided, setup hitchihiker construction/completion
b) read specified mm from disk
2. launch chain... on success, load requested metamodel into _mmmk and
return success code and 'hitchhiker' (see notes at top of csworker.js
for more about 'hitchhikers')... on error, return error */
'PUT /current.metamodels' :
function(resp,uri,reqData/*mm,hitchhiker*/)
{
let actions = [__successContinuable()];
if( reqData['hitchhiker'] == undefined ||
'path' in reqData['hitchhiker'] )
actions.push(
function()
{
let ext;
if (reqData['mm'].match(/\.pattern\.metamodel$/))
ext = '.pattern.metamodel';
else
ext = '.metamodel';
let path;
if (reqData['hitchhiker'] && 'path' in reqData['hitchhiker'])
path = reqData['hitchhiker']['path'];
else
path = reqData['mm'].match(/(.*)\.metamodel/)[1] + '.defaultIcons' + ext;
let name = path.match(/.+?(\/.*)\.metamodel/)[1];
reqData['hitchhiker'] = {};
reqData['hitchhiker']['name'] = name;
return _fs.readFile('./users'+path,'utf8');
},
function(csmmData)
{
reqData['hitchhiker']['csmm'] = csmmData;
return __successContinuable();
});
actions.push(
function()
{
return _fs.readFile('./users'+reqData['mm'],'utf8');
});
_do.chain(actions)(
function(asmmData)
{
let mm = reqData['mm'].match(/.+?(\/.*)\.metamodel/)[1];
let res = _mmmk.loadMetamodel(mm, asmmData);
__postMessage(
{'statusCode':200,
'changelog':res['changelog'],
'sequence#':__sequenceNumber(),
'hitchhiker':reqData['hitchhiker'],
'respIndex':resp});
},
function (err) {
if (err['code'].includes("ENOENT")) {
err = "Error! File not found: " + err['path'];
}
__postInternalErrorMsg(resp, err);
}
);
},
/* unload a metamodel (deletes all entities from that metamodel) */
'DELETE *.metamodel' :
function(resp,uri)
{
let mm = uri.match(/(.*)\.metamodel/)[1];
let res = _mmmk.unloadMetamodel(mm);
__postMessage(
{'statusCode':200,
'changelog':res['changelog'],
'sequence#':__sequenceNumber(),
'respIndex':resp});
},
/* load a model */
'PUT /current.model' :
function (resp, uri, reqData/*m,name,insert,hitchhiker*/) {
let res = _mmmk.loadModel(reqData['name'], reqData['m'], reqData['insert']);
if (res['$err'])
__postInternalErrorMsg(resp, res['$err']);
else
__postMessage(
{
'statusCode': 200,
'changelog': res['changelog'],
'sequence#': __sequenceNumber(),
'hitchhiker': reqData['hitchhiker'],
'respIndex': resp
});
},
/* create a new instance of type... if reqData has 'src' and 'dest' fields,
type is a connector
1. if *.type is a node, ask _mmmk to create it
1. if *.type is a connector, ask _mmmk to create it and connect
its endpoints
2. return success or error code
NOTE:: since create() is given 'attrs' (i.e., these aren't given to
update() after creation), it is important that post-edit
constraints also be made post-create constraints... otherwise, it
would be possible to create nodes via copy-paste that would
otherwise be blocked by post-edit constraints */
'POST *.type' :
function(resp,uri,reqData/*[src,dest],hitchhiker,attrs*/)
{
let matches = uri.match(/(.*)\.type/);
let fulltype = matches[1];
let res;
if (reqData == undefined || reqData['src'] == undefined) {
res = _mmmk.create(fulltype, reqData['attrs']);
} else {
res = _mmmk.connect(
__uri_to_id(reqData['src']),
__uri_to_id(reqData['dest']),
fulltype,
reqData['attrs']);
}
if( '$err' in res )
__postInternalErrorMsg(resp,res['$err']);
else
__postMessage(
{'statusCode':200,
'changelog':res['changelog'],
'data':res['id'],
'sequence#':__sequenceNumber(),
'hitchhiker':reqData['hitchhiker'],
'respIndex':resp});
},
/* return an instance */
'GET *.instance' :
function(resp,uri)
{
let id = __uri_to_id(uri);
let res = _mmmk.read(id)
if( res['$err'] )
__postInternalErrorMsg(resp,res['$err']);
else
__postMessage(
{'statusCode':200,
'data':res,
'sequence#':__sequenceNumber(0),
'respIndex':resp});
},
/* updates an instance
NOTE:: if this update produces no changelog *and* to-do-cschanges are
bundled, we return a dummy changelog that ensures the said to-do-
cschanges get handled by csworker.__applyASWChanges().CHATTR */
'PUT *.instance' :
function (resp, uri, reqData/*changes[,hitchhiker]*/) {
let id = __uri_to_id(uri);
let res = _mmmk.update(id, reqData['changes']);
if (res['$err'])
__postInternalErrorMsg(resp, res['$err']);
else {
let changelog = res['changelog'];
if (res['changelog'].length == 0 && reqData['hitchhiker']) {
changelog = [{'op': 'CHATTR', 'id': id}];
}
__postMessage(
{
'statusCode': 200,
'changelog': changelog,
'sequence#': __sequenceNumber(),
'hitchhiker': reqData['hitchhiker'],
'respIndex': resp
});
}
},
'POST *.instance.click' :
function(id,vid)
{
//TODO
},
/* delete an instance
NOTE:: this function does not return asworker errors to the client (see
NOTE for csworker.DELETE *.instance with the difference that in
this context, the client is the mtworker) */
'DELETE *.instance' :
function (resp, uri) {
let id = __uri_to_id(uri);
let res = _mmmk.read(id)
if (res['$err']) {
__postMessage({'statusCode': 200, 'respIndex': resp});
return;
}
res = _mmmk['delete'](id);
if (res['$err']) {
__postInternalErrorMsg(resp, res['$err']);
return;
}
__postMessage(
{
'statusCode': 200,
'changelog': res['changelog'],
'sequence#': __sequenceNumber(),
'respIndex': resp
});
},
/* generate a metamodel from the current model and write it to disk
1. generate metamodel
1. setup sync/async action chaining
a) write specified mm to disk
2. launch chain... return success or error code */
'PUT *.metamodel' :
function(resp,uri,reqData/*[csm]*/)
{
let res;
let model = _utils.jsonp(_mmmk.read());
if (uri.match(/(.*)\..*Icons\.metamodel/)) {
let metamodels = _utils.jsonp(_mmmk.readMetamodels());
res = _libcompile.compileToIconDefinitionMetamodel(model, metamodels, reqData['csm'], reqData['asmm'])
} else {
res = _libcompile.compileToMetamodel(model);
}
if (res['$err']) {
__postInternalErrorMsg(resp, res['$err']);
return;
}
let uri2 = uri.substring('/GET'.length);
let actions = [_fs.writeFile('./users' + uri2, res)];
_do.chain(actions)(
function () {
__postMessage(
{
'statusCode': 200,
'respIndex': resp
});
},
function (err) {
__postInternalErrorMsg(resp, err);
}
);
},
/* validate a model */
'GET /validatem' :
function(resp)
{
let err = _libeventhandler.validateModel(_mmmk);
if( err )
__postInternalErrorMsg(resp,err['$err']);
else
__postMessage(
{'statusCode':200,
'respIndex':resp});
},
/* undo the effects of a csworker's last not yet undone action
OR
undo until the specified user-checkpoint */
'POST /undo' :
function(resp,uri,reqData/*[undoUntil],hitchhiker*/)
{
__postMessage(
{'statusCode':200,
'changelog':_mmmk.undo(reqData['undoUntil'])['changelog'],
'sequence#':__sequenceNumber(),
'hitchhiker':reqData['hitchhiker'],
'respIndex':resp});
},
/* redo the effects of a csworker's last undone action
OR
redo until the specified user-checkpoint */
'POST /redo' :
function(resp,uri,reqData/*[redoUntil],hitchhiker*/)
{
__postMessage(
{'statusCode':200,
'changelog':_mmmk.redo(reqData['redoUntil'])['changelog'],
'sequence#':__sequenceNumber(),
'hitchhiker':reqData['hitchhiker'],
'respIndex':resp});
},
/* evaluate a set of mapping functions (i.e., those functions that compute
the values of coded VisualObject attributes based on the AS model)... of
interest is that this function 'succeeds' even if one or more of the given
mapping function produces an error: in such cases, the new value of the
associated attribute will be the produced error
NOTE:
this request is actually a GET : it doesn't change anything anywhere...
however, due to the possibly large amount of reqData it requires, we're
forced to make it a POST */
'POST *.mappings' :
function (resp, uri, reqData/*{...,fullvid:mapper,...}*/) {
let id = __uri_to_id(uri);
let attrVals = {};
for (let fullvid in reqData) {
let res = _libeventhandler.runDesignerAccessorCode(
_mmmk,
reqData[fullvid],
'mapper evaluation (' + uri + ')',
id);
if (res == undefined)
continue;
if (!_utils.isObject(res)) {
attrVals =
{
'$err':
'mapper returned non-dictionary type :: ' + (typeof res)
};
break;
} else if ('$err' in res) {
attrVals = res;
break;
} else
for (let attr in res)
attrVals[fullvid + attr] = res[attr];
}
__postMessage(
{
'statusCode': 200,
'data': attrVals,
'respIndex': resp
});
},
/* little hack that allows pretty much anyone to send some text to the client
for console output... this is currently used by mtworkers to post feedback
about the state of a running transformation... eventually, it could also
be used to implement inter-collaborator chatting */
'PUT /GET/console' :
function(resp,uri,reqData/*text*/)
{
__postMessage(
{'statusCode':200,
'changelog':[{'op':'SYSOUT','text':reqData['text']}],
'sequence#':__sequenceNumber(),
'respIndex':resp});
},
/* place an easily identifiable user-checkpoint in the journal */
'POST /batchCheckpoint' :
function(resp,uri,reqData)
{
_mmmk.setUserCheckpoint(reqData['name']);
__postMessage(
{'statusCode':200,
'changelog':[{'op':'MKBTCCHKPT','name':reqData['name']}],
'sequence#':__sequenceNumber(),
'respIndex':resp});
}
};