asworker.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
  2. * Copyright 2011 by the AToMPM team and licensed under the LGPL
  3. * See COPYING.lesser and README.md in the root of this project for full details
  4. */
  5. {
  6. /************************** REST REQUEST HANDLING **************************/
  7. /* INTENT :
  8. ask our mtworker to do something (e.g., change transformation execution
  9. mode)
  10. IN PRACTICE:
  11. adjust uri and forward to mtworker
  12. 1. setup sync/async action chaining
  13. a) if this asworker doesn't already have an mtworker
  14. i. create mtworker
  15. ii. subscribe it to this asworker
  16. iii. send it this asworker's current model
  17. b) ask asworker to forward initial request to its mtworker
  18. 2. launch chain... return success code or error */
  19. '__mtwid':undefined,
  20. 'mtwRequest' :
  21. function(resp,method,uri,reqData)
  22. {
  23. var self = this,
  24. actions =
  25. [__successContinuable(),
  26. function()
  27. {
  28. uri = uri.substring('/__mt'.length);
  29. return __httpReq(
  30. method,
  31. uri+'?wid='+self.__mtwid,
  32. reqData,
  33. 8125);
  34. }];
  35. if( this.__mtwid == undefined )
  36. {
  37. if( reqData == undefined ||
  38. reqData['transfs'] == undefined ||
  39. reqData['transfs'].length == 0 )
  40. return __postInternalErrorMsg(resp,'missing transformations');
  41. else if( reqData == undefined ||
  42. reqData['username'] == undefined ||
  43. reqData['username'].length == 0 )
  44. return __postInternalErrorMsg(resp,'missing username');
  45. actions.splice(1,0,
  46. function()
  47. {
  48. return __httpReq(
  49. 'PUT',
  50. '/GET/console?wid='+__wid,
  51. {'text':'please wait while model transformation module'+
  52. ' initializes (this may take a few seconds)'});
  53. },
  54. function()
  55. {
  56. return __httpReq('POST','/mtworker',undefined,8125);
  57. },
  58. function(mtwid)
  59. {
  60. self.__mtwid = mtwid;
  61. return __httpReq(
  62. 'PUT',
  63. '/aswSubscription?wid='+self.__mtwid,
  64. {'aswid':__wid},
  65. 8125);
  66. },
  67. function()
  68. {
  69. return _fs.readFile('./users/'+reqData['username']+
  70. '/prefs','utf8');
  71. },
  72. function(prefs)
  73. {
  74. try {prefs = eval('('+prefs+')');}
  75. catch(err) {
  76. return __errorContinuable(
  77. 'an error occurred while reading your preferences '+
  78. '(you must restart your client before successful '+
  79. 'model transformation module initialization becomes'+
  80. ' possible :: '+err);}
  81. return __httpReq(
  82. 'PUT',
  83. '/envvars?wid='+self.__mtwid,
  84. {'username':reqData['username'],
  85. 'defaultDCL':prefs['default-mt-dcl']['value']},
  86. 8125);
  87. },
  88. function()
  89. {
  90. if( (mms = _mmmk.readMetamodels())['$err'] )
  91. return __errorContinuable(mms['$err']);
  92. else if( (m = _mmmk.read())['$err'] )
  93. return __errorContinuable(m['$err']);
  94. return __httpReq(
  95. 'PUT',
  96. '/current.model?wid='+self.__mtwid,
  97. {'mms':mms,
  98. 'm':m,
  99. 'sequence#':__sequenceNumber(0)},
  100. 8125);
  101. },
  102. function()
  103. {
  104. return __httpReq(
  105. 'PUT',
  106. '/current.transform?wid='+self.__mtwid,
  107. {'transfs':reqData['transfs']},
  108. 8125);
  109. },
  110. function()
  111. {
  112. return __httpReq(
  113. 'PUT',
  114. '/GET/console?wid='+__wid,
  115. {'text':'model tranformation module is ready to go!'});
  116. }
  117. );
  118. }
  119. _do.chain(actions)(
  120. function()
  121. {
  122. __postMessage({'statusCode':200, 'respIndex':resp});
  123. },
  124. function(err) {__postInternalErrorMsg(resp,err);}
  125. );
  126. },
  127. /* load a metamodel
  128. 1. setup sync/async action chaining
  129. a) if no hitchhiker containing CSMM information is provided, or if only
  130. a CSMM name is provided, setup hitchihiker construction/completion
  131. b) read specified mm from disk
  132. 2. launch chain... on success, load requested metamodel into _mmmk and
  133. return success code and 'hitchhiker' (see notes at top of csworker.js
  134. for more about 'hitchhikers')... on error, return error */
  135. 'PUT /current.metamodels' :
  136. function(resp,uri,reqData/*mm,hitchhiker*/)
  137. {
  138. var actions = [__successContinuable()];
  139. if( reqData['hitchhiker'] == undefined ||
  140. 'path' in reqData['hitchhiker'] )
  141. actions.push(
  142. function()
  143. {
  144. var ext = (reqData['mm'].match(/\.pattern\.metamodel$/) ?
  145. '.pattern.metamodel' :
  146. '.metamodel'),
  147. path =
  148. (reqData['hitchhiker'] && 'path' in reqData['hitchhiker'] ?
  149. reqData['hitchhiker']['path'] :
  150. reqData['mm'].match(/(.*)\.metamodel/)[1]+
  151. '.defaultIcons'+ext),
  152. name = path.match(/.+?(\/.*)\.metamodel/)[1];
  153. reqData['hitchhiker'] = {};
  154. reqData['hitchhiker']['name'] = name;
  155. return _fs.readFile('./users'+path,'utf8');
  156. },
  157. function(csmmData)
  158. {
  159. reqData['hitchhiker']['csmm'] = csmmData;
  160. return __successContinuable();
  161. });
  162. actions.push(
  163. function()
  164. {
  165. return _fs.readFile('./users'+reqData['mm'],'utf8');
  166. });
  167. _do.chain(actions)(
  168. function(asmmData)
  169. {
  170. var mm = reqData['mm'].match(/.+?(\/.*)\.metamodel/)[1],
  171. res = _mmmk.loadMetamodel(mm,asmmData);
  172. __postMessage(
  173. {'statusCode':200,
  174. 'changelog':res['changelog'],
  175. 'sequence#':__sequenceNumber(),
  176. 'hitchhiker':reqData['hitchhiker'],
  177. 'respIndex':resp});
  178. },
  179. function(err) {__postInternalErrorMsg(resp,err);}
  180. );
  181. },
  182. /* unload a metamodel (deletes all entities from that metamodel) */
  183. 'DELETE *.metamodel' :
  184. function(resp,uri)
  185. {
  186. var mm = uri.match(/(.*)\.metamodel/)[1],
  187. res = _mmmk.unloadMetamodel(mm);
  188. __postMessage(
  189. {'statusCode':200,
  190. 'changelog':res['changelog'],
  191. 'sequence#':__sequenceNumber(),
  192. 'respIndex':resp});
  193. },
  194. /* load a model */
  195. 'PUT /current.model' :
  196. function(resp,uri,reqData/*m,name,insert,hitchhiker*/)
  197. {
  198. if( (res = _mmmk.loadModel(
  199. reqData['name'],
  200. reqData['m'],
  201. reqData['insert']))['$err'] )
  202. __postInternalErrorMsg(resp,res['$err']);
  203. else
  204. __postMessage(
  205. {'statusCode':200,
  206. 'changelog':res['changelog'],
  207. 'sequence#':__sequenceNumber(),
  208. 'hitchhiker':reqData['hitchhiker'],
  209. 'respIndex':resp});
  210. },
  211. /* create a new instance of type... if reqData has 'src' and 'dest' fields,
  212. type is a connector
  213. 1. if *.type is a node, ask _mmmk to create it
  214. 1. if *.type is a connector, ask _mmmk to create it and connect
  215. its endpoints
  216. 2. return success or error code
  217. NOTE:: since create() is given 'attrs' (i.e., these aren't given to
  218. update() after creation), it is important that post-edit
  219. constraints also be made post-create constraints... otherwise, it
  220. would be possible to create nodes via copy-paste that would
  221. otherwise be blocked by post-edit constraints */
  222. 'POST *.type' :
  223. function(resp,uri,reqData/*[src,dest],hitchhiker,attrs*/)
  224. {
  225. var matches = uri.match(/(.*)\.type/),
  226. fulltype = matches[1],
  227. res = (reqData == undefined || reqData['src'] == undefined ?
  228. _mmmk.create(fulltype,reqData['attrs']) :
  229. _mmmk.connect(
  230. __uri_to_id(reqData['src']),
  231. __uri_to_id(reqData['dest']),
  232. fulltype,
  233. reqData['attrs']));
  234. if( '$err' in res )
  235. __postInternalErrorMsg(resp,res['$err']);
  236. else
  237. __postMessage(
  238. {'statusCode':200,
  239. 'changelog':res['changelog'],
  240. 'data':res['id'],
  241. 'sequence#':__sequenceNumber(),
  242. 'hitchhiker':reqData['hitchhiker'],
  243. 'respIndex':resp});
  244. },
  245. /* return an instance */
  246. 'GET *.instance' :
  247. function(resp,uri)
  248. {
  249. var id = __uri_to_id(uri);
  250. if( (res = _mmmk.read(id))['$err'] )
  251. __postInternalErrorMsg(resp,res['$err']);
  252. else
  253. __postMessage(
  254. {'statusCode':200,
  255. 'data':res,
  256. 'sequence#':__sequenceNumber(0),
  257. 'respIndex':resp});
  258. },
  259. /* updates an instance
  260. NOTE:: if this update produces no changelog *and* to-do-cschanges are
  261. bundled, we return a dummy changelog that ensures the said to-do-
  262. cschanges get handled by csworker.__applyASWChanges().CHATTR */
  263. 'PUT *.instance' :
  264. function(resp,uri,reqData/*changes[,hitchhiker]*/)
  265. {
  266. var id = __uri_to_id(uri);
  267. if( (res = _mmmk.update(id,reqData['changes']))['$err'] )
  268. __postInternalErrorMsg(resp,res['$err']);
  269. else
  270. {
  271. var changelog =
  272. (res['changelog'].length == 0 && reqData['hitchhiker'] ?
  273. [{'op':'CHATTR','id':id}] :
  274. res['changelog']);
  275. __postMessage(
  276. {'statusCode':200,
  277. 'changelog':changelog,
  278. 'sequence#':__sequenceNumber(),
  279. 'hitchhiker':reqData['hitchhiker'],
  280. 'respIndex':resp});
  281. }
  282. },
  283. 'POST *.instance.click' :
  284. function(id,vid)
  285. {
  286. //TODO
  287. },
  288. /* delete an instance
  289. NOTE:: this function does not return asworker errors to the client (see
  290. NOTE for csworker.DELETE *.instance with the difference that in
  291. this context, the client is the mtworker) */
  292. 'DELETE *.instance' :
  293. function(resp,uri)
  294. {
  295. var id = __uri_to_id(uri);
  296. if( (res = _mmmk.read(id))['$err'] )
  297. __postMessage({'statusCode':200, 'respIndex':resp});
  298. else if( (res = _mmmk['delete'](id))['$err'] )
  299. __postInternalErrorMsg(resp,res['$err']);
  300. else
  301. __postMessage(
  302. {'statusCode':200,
  303. 'changelog':res['changelog'],
  304. 'sequence#':__sequenceNumber(),
  305. 'respIndex':resp});
  306. },
  307. /* generate a metamodel from the current model and write it to disk
  308. 1. generate metamodel
  309. 1. setup sync/async action chaining
  310. a) write specified mm to disk
  311. 2. launch chain... return success or error code */
  312. 'PUT *.metamodel' :
  313. function(resp,uri,reqData/*[csm]*/)
  314. {
  315. if( uri.match(/(.*)\..*Icons\.metamodel/) &&
  316. (res = _mmmk.
  317. compileToIconDefinitionMetamodel(reqData['csm'], reqData['asmm']))['$err'] )
  318. __postInternalErrorMsg(resp,res['$err']);
  319. else if( ! uri.match(/(.*)\..*Icons\.metamodel/) &&
  320. (res = _mmmk.compileToMetamodel())['$err'] )
  321. __postInternalErrorMsg(resp,res['$err']);
  322. else
  323. {
  324. var uri = uri.substring('/GET'.length),
  325. actions = [_fs.writeFile('./users'+uri,res)];
  326. _do.chain(actions)(
  327. function()
  328. {
  329. __postMessage(
  330. {'statusCode':200,
  331. 'respIndex':resp});
  332. },
  333. function(err) {__postInternalErrorMsg(resp,err);}
  334. );
  335. }
  336. },
  337. /* validate a model */
  338. 'GET /validatem' :
  339. function(resp)
  340. {
  341. var err = _mmmk.validateModel();
  342. if( err )
  343. __postInternalErrorMsg(resp,err['$err']);
  344. else
  345. __postMessage(
  346. {'statusCode':200,
  347. 'respIndex':resp});
  348. },
  349. /* undo the effects of a csworker's last not yet undone action
  350. OR
  351. undo until the specified user-checkpoint */
  352. 'POST /undo' :
  353. function(resp,uri,reqData/*[undoUntil],hitchhiker*/)
  354. {
  355. __postMessage(
  356. {'statusCode':200,
  357. 'changelog':_mmmk.undo(reqData['undoUntil'])['changelog'],
  358. 'sequence#':__sequenceNumber(),
  359. 'hitchhiker':reqData['hitchhiker'],
  360. 'respIndex':resp});
  361. },
  362. /* redo the effects of a csworker's last undone action
  363. OR
  364. redo until the specified user-checkpoint */
  365. 'POST /redo' :
  366. function(resp,uri,reqData/*[redoUntil],hitchhiker*/)
  367. {
  368. __postMessage(
  369. {'statusCode':200,
  370. 'changelog':_mmmk.redo(reqData['redoUntil'])['changelog'],
  371. 'sequence#':__sequenceNumber(),
  372. 'hitchhiker':reqData['hitchhiker'],
  373. 'respIndex':resp});
  374. },
  375. /* evaluate a set of mapping functions (i.e., those functions that compute
  376. the values of coded VisualObject attributes based on the AS model)... of
  377. interest is that this function 'succeeds' even if one or more of the given
  378. mapping function produces an error: in such cases, the new value of the
  379. associated attribute will be the produced error
  380. NOTE:
  381. this request is actually a GET : it doesn't change anything anywhere...
  382. however, due to the possibly large amount of reqData it requires, we're
  383. forced to make it a POST */
  384. 'POST *.mappings' :
  385. function(resp,uri,reqData/*{...,fullvid:mapper,...}*/)
  386. {
  387. var id = __uri_to_id(uri),
  388. attrVals = {};
  389. for( var fullvid in reqData )
  390. {
  391. var res = _mmmk.runDesignerAccessorCode(
  392. reqData[fullvid],
  393. 'mapper evaluation ('+uri+')',
  394. id);
  395. if( res == undefined )
  396. continue;
  397. else if( !_utils.isObject(res) )
  398. {
  399. attrVals =
  400. {'$err':
  401. 'mapper returned non-dictionary type :: '+(typeof res)};
  402. break;
  403. }
  404. else if( '$err' in res )
  405. {
  406. attrVals = res;
  407. break;
  408. }
  409. else
  410. for( var attr in res )
  411. attrVals[fullvid+attr] = res[attr];
  412. }
  413. __postMessage(
  414. {'statusCode':200,
  415. 'data':attrVals,
  416. 'respIndex':resp});
  417. },
  418. /* little hack that allows pretty much anyone to send some text to the client
  419. for console output... this is currently used by mtworkers to post feedback
  420. about the state of a running transformation... eventually, it could also
  421. be used to implement inter-collaborator chatting */
  422. 'PUT /GET/console' :
  423. function(resp,uri,reqData/*text*/)
  424. {
  425. __postMessage(
  426. {'statusCode':200,
  427. 'changelog':[{'op':'SYSOUT','text':reqData['text']}],
  428. 'sequence#':__sequenceNumber(),
  429. 'respIndex':resp});
  430. },
  431. /* place an easily identifiable user-checkpoint in the journal */
  432. 'POST /batchCheckpoint' :
  433. function(resp,uri,reqData)
  434. {
  435. _mmmk.setUserCheckpoint(reqData['name']);
  436. __postMessage(
  437. {'statusCode':200,
  438. 'changelog':[{'op':'MKBTCCHKPT','name':reqData['name']}],
  439. 'sequence#':__sequenceNumber(),
  440. 'respIndex':resp});
  441. }
  442. }