csworker.js 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301
  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. /* NOTES:
  6. it is assumed that csworker _mmmk cud operations NEVER FAIL...
  7. 1. IMPLICATIONS
  8. a) we don't check for failure
  9. b) if failure were to occur, we wouldn't rollback the associated
  10. asworker perations
  11. c) *Icons.metamodels should NOT support actions, constraints or
  12. multiplicities to ensure neither of these block cud operations...
  13. they do however support parsing functions (designer code that
  14. translates CS updates into AS operations) which are somewhat akin to
  15. pre-edit actions: if the parsing function or the AS operations fail,
  16. the CS update fails
  17. 2. REASONING
  18. a) if we allowed csworker to rollback asworker operations, it would be
  19. a natural extension for csworkers to be allowed to fail and "respond
  20. negatively" to pushed changelogs and request asworker rollbacks...
  21. this might cause severe user experience problems in collaboration
  22. scenarios (e.g., A's changes could be repeatedly undone by failures
  23. produced by B's csworker)
  24. b) it doesn't make sense for logic that could overturn AS cud
  25. operations to live anywhere else than in the AS spec
  26. csworker cud operations are CHANGELOG-DRIVEN... in other words, when a client
  27. makes PUT/POST/DELETE requests to a csworker, these are translated into
  28. appropriate requests for an asworker...
  29. 1. on success, the asworker
  30. a) returns a 20x status code to the csworker who made the request, in
  31. turn, it forwards it to the client who made the request
  32. b) returns a changelog that describes the AS impacts of the request...
  33. this changelog is pushed to all of its csworker subscribers
  34. (including the one who made the request) and which point their
  35. internal CS models are adjusted to reflect AS changes... finally,
  36. the result of these CS model modifications are themselves bundled
  37. into changelogs that are forwarded to all subscribed clients
  38. (subscription managment is performed in httpwsd.js)
  39. 2. on failure, the asworker
  40. a) returns a 40x|50x status code to the csworker...
  41. so basically, when a client asks a csworker to change something, all that
  42. csworker does is forward the request to its asworker... if and when the
  43. csworker does comply, it will be in response to an asworker changelog
  44. received sometime after it has responded a 20x status code to the client
  45. 'HITCHHIKERS' allow subscriber information exchange... for instance, if A
  46. loads SC.purpleIcons.metamodel, we'd like for all csworkers subscribed to A's
  47. associated asworker to be told (1) that their asworker has loaded
  48. SC.metamodel *and* (2) that they should load SC.purpleIcons.metamodel... with
  49. hitchhikers, 'subscriber-relevant' but 'worker-irrelevant' data is sent to
  50. workers so that they may push it back to all of their subscribers upon
  51. returning
  52. technically, asworkers and csworkers could be run on DISTINCT MACHINES...
  53. however, for the moment, this is neither supported nor recommended:
  54. 1. csworkers as they are now are not robust to asworker failures caused by
  55. timeouts and/or other network problems
  56. 2. requests to asworkers would need to be prepended with the actual url of
  57. wherever the asworker is being served from
  58. there are at least 3 alternatives for EVALUATING A MAPPING FUNCTION
  59. 1. retrieve the full AS model and run the mapping function within this
  60. csworker within some scope where the said model is accessible via the
  61. designer API (ala. _mmmk.__runDesignerCode())
  62. + RESTful requests to asworker
  63. - possibly very inefficient to transmit AS model
  64. 2. run the mapping function within this csworker within some scope where
  65. calls to getAttr() are translated to REST that retrieve desired
  66. information from the asworker
  67. + RESTful requests to asworker
  68. - possibly numerous queries
  69. 3. send mapping function to asworker and run it there
  70. - non-RESTful request
  71. + most efficient in terms of data traffic
  72. we chose the 3rd approach for its efficiency... furthermore, when
  73. regenerating icons (who may contain numerous VisualObjects with numerous
  74. coded attributes), to avoid having to send many queries to the asworker
  75. (i.e., one for each mappingf), we bundle together all of the icon's mappingfs
  76. and send them to the asworker for evaluation in a single query
  77. CREATING AN ICON AT A SPECIFIC POSITION brings up a few issues in the context
  78. where icon position plays a role in AS attribute values... consider this
  79. scenario:
  80. 1. user creates a BuildingIcon at (500,600)
  81. 2. request is received by csworker and forwarded to asworker
  82. 3. asworker creates a Building and sets abstract attribute 'address' to
  83. metamodel-specified default (0,0)
  84. 4. later, __applyASWChanges() receives MKNODE
  85. a) it creates a BuildingIcon
  86. b) sets its 'position' to (500,600)
  87. c) calls __regenIcon() who determines that 'position' should be (0,0)
  88. after mapping AS attribute 'address'
  89. d) emits changelog instructing client to create a BuildingIcon at (0,0)
  90. the core issue here is that step 4b) is a hack... instead of 'position' being
  91. set via 'PUT *.cs' which would have parsed 'position', appropriately updated
  92. 'address', and only later -- via AS changelog -- mapped 'address' and updated
  93. 'position', step 4b) bypasses this whole pipeline... this causes CS and AS to
  94. be out-of-sync, which in turn causes the behaviour described for step 4c)...
  95. to address this issue, during step 2 we perform a task similar to that of
  96. 'PUT *.cs'... step 2 from the above scenario thus becomes:
  97. 0. request is received by csworker
  98. A. retrieve the BuildingIcon's parser
  99. B. create a dummy context where a *Icon's parser can be run... within this
  100. context, 'orientation' and 'scale' are set to their defaults, but
  101. 'position' is set to (500,600)
  102. C. run the BuildingIcon's parser within this dummy context, this could
  103. yield {'address':(5,6)}
  104. D. forwarded creation request to asworker *and* bundle the result from
  105. step C.
  106. step 3 is also slightly changed: the asworker creates a new Building *and*
  107. updates any specified attributes, in our example, 'address' would be set to
  108. (5,6)... step 4 is left unchanged but step 4c) no longer causes any problems
  109. because __regenIcon's mapping of 'address' will return (500,600)
  110. supporting undo/redo requires REMEMBERING HITCHHIKERS... __applyASWChanges()
  111. sometimes expects asworker changelogs to be bundled with hitchhikers... this
  112. is the case when changelogs are the result of 'normal' requests getting
  113. forwarded to the asworker by the csworker... however, for undos/redos, no
  114. hitchhikers are bundled and as such, when __applyASWChanges() is called as a
  115. result of undos and redos on the asworker, required information that would
  116. normally be in hitchhikers is missing... to address this, we 'remember'
  117. hitchhikers as we encounter them like so:
  118. 1. MKNODE hitchhikers are remembered by asid
  119. 2. LOADMM hitchhikers are remembered by asmm
  120. 3. RESETM hitchhikers are remembered by name *
  121. * for this case, we also create and remember a hitchhiker to enable
  122. undoing loading a model over an unsaved non-empty model
  123. long story short, when handling asworker changelogs, missing hitchhikers, if
  124. any, are retrieved from the __hitchhikerJournal
  125. supporting FULL UNDO/REDO in our distributed environment presents a few
  126. challenges
  127. 1. operations with no AS implications should be undone/redone by csworkers
  128. 2. operations with AS implications should be undone/redone by asworkers
  129. *but* resulting changelogs should be handled specially to ensure that
  130. csworkers undo/redo in response to asworer undo/redos
  131. challenge 1 requires means to determine what kind of operation the client
  132. wishes to undo/redo... we addressed this by logging handled sequence#s (via
  133. __checkpointUserOperation()... when an undo/redo request is received, the
  134. current sequence# to undo/redo dictates whether we're in case 1 or 2.
  135. challenge 2 requires means to determine whether or not an asworker changelog
  136. pertains to an undo/redo operation *and*, which csworker operations to undo/
  137. redo in response to an asworker undo/redo... we addressed this in 3 parts
  138. 1. when DOing something in response to asworker changelogs, the csworker
  139. sets a user-checkpoint (named after the asworker sequence#) in its
  140. journal
  141. 2. when forwarding undo/redo requests to asworkers, the asworker sequence#
  142. to undo/redo is bundled in a hitchhiker
  143. 3. when an asworker changelog has a bundled undo/redo hitchhiker, the
  144. csworker handles it by undoing/redoing all of the changes the bundled
  145. asworker sequence# had originally induced
  146. step 3 is paramount... if the csworker responded to undo/redos like it would
  147. any other request (e.g., respond to RMNODE by RMNODE), it would become out of
  148. sync with the asworker... see example below:
  149. 1. client creates A/0, AIcon/0
  150. 2. client moves AIcon/0
  151. 3. client undoes move (csworker only, OK)
  152. 4. client undoes create (would trigger RMNODE A/0, AIcon/0)
  153. 5. client redoes create (would trigger MKNODE A/0, AIcon/1)
  154. 6. client redoes move (will fail because AIcon/0 doesn't exist)
  155. in short, proper undo/redo requires that operations resultings from undo/redo
  156. be distinguishable from normal ones
  157. TBI:: undoing/redoing SYSOUT-only changelogs has no perceptible effect from
  158. client... one inconvenient side-effect of this is that rules require 2
  159. undos/redos to undo/redo: 1 to undo/redo the rule, 1 to undo/redo the
  160. SYSOUT message announcing the launching of the rule... a sensible and
  161. nice solution would be not to remember such changelogs in
  162. __handledSeqNums */
  163. let {
  164. __id_to_uri,
  165. __ids2uris, __nextSequenceNumber,
  166. __postBadReqErrorMsg,
  167. __postForbiddenErrorMsg,
  168. __wtype,
  169. GET__current_state
  170. } = require("./__worker");
  171. const {
  172. __batchCheckpoint,
  173. __errorContinuable,
  174. __httpReq,
  175. __wHttpReq,
  176. __postInternalErrorMsg, __postMessage,
  177. __sequenceNumber,
  178. __successContinuable,
  179. __uri_to_id
  180. } = require("./__worker");
  181. const _do = require("./___do");
  182. const _utils = require('./utils');
  183. const _mmmk = require("./mmmk");
  184. const _fs = _do.convert(require('fs'), ['readFile', 'writeFile', 'readdir']);
  185. const _path = require('path');
  186. const _fspp = _do.convert(require('./___fs++'), ['mkdirs']);
  187. const _svg = require('./libsvg').SVG;
  188. const _mt = require('./libmt');
  189. const _siocl = require('socket.io-client');
  190. module.exports = {
  191. '__REGEN_ICON_RETRY_DELAY_MS':200,
  192. '__asmm2csmm':{},
  193. '__asid2csid':{},
  194. '__aswid':undefined,
  195. '__handledSeqNums':{'i':undefined,'#s':[]},
  196. /*************************** ASWORKER INTERACTION **************************/
  197. /* apply asworker changes
  198. 0. check the changelog's sequence number to know if we should handle it
  199. now or later
  200. 1. iterate through the AS changelog setting up sync/async actions that
  201. appropriately modify the CS while accumulating CS changelogs (for
  202. pushing to subscribed clients)
  203. 2. launch sync/async action chain... on error, post error... on success,
  204. a) flatten the CS changelogs into a single changelog
  205. b) post message to server with flattened CS changelog, the server will
  206. then push it to subscribed clients
  207. c) apply next pending asworker changelog, if any and if applicable
  208. __nextASWSequenceNumber
  209. used to determine if a changelog is received out of order, and if a
  210. pending changelog is now ready to be handled
  211. __pendingChangelogs
  212. stores out or order changelogs until we're ready to handle them
  213. __hitchhikerJournal
  214. stores encountered hithchikers for future use (see NOTES above) */
  215. '__nextASWSequenceNumber':'/asworker#1',
  216. '__pendingChangelogs':[],
  217. '__hitchhikerJournal':{},
  218. '__applyASWChanges' :
  219. function(changelog,aswSequenceNumber,hitchhiker)
  220. {
  221. //console.error('w#'+__wid+' ++ ('+aswSequenceNumber+') '+
  222. // _utils.jsons(changelog));
  223. if( _utils.sn2int(aswSequenceNumber) >
  224. _utils.sn2int(this.__nextASWSequenceNumber) )
  225. {
  226. this.__pendingChangelogs.push(
  227. {'changelog':changelog,
  228. 'sequence#':aswSequenceNumber,
  229. 'hitchhiker':hitchhiker});
  230. var self = this;
  231. this.__pendingChangelogs.sort(
  232. function(a,b)
  233. {
  234. return self.__sn2int(a['sequence#']) -
  235. self.__sn2int(b['sequence#']);
  236. });
  237. return;
  238. }
  239. else if( _utils.sn2int(aswSequenceNumber) <
  240. _utils.sn2int(this.__nextASWSequenceNumber) )
  241. throw 'invalid changelog sequence#';
  242. var cschangelogs = [],
  243. cshitchhiker,
  244. actions = [__successContinuable()],
  245. self = this;
  246. /* special handling of undo/redo changelogs (see NOTES above) */
  247. if( hitchhiker && 'undo' in hitchhiker )
  248. cschangelogs.push(_mmmk.undo(hitchhiker['undo'])['changelog']);
  249. else if( hitchhiker && 'redo' in hitchhiker )
  250. cschangelogs.push(_mmmk.redo(hitchhiker['redo'])['changelog']);
  251. /* special handling of batchCheckpoint changelogs (see NOTES above) */
  252. else if( changelog.length == 1 && changelog[0]['op'] == 'MKBTCCHKPT' )
  253. this.__checkpointUserOperation(changelog[0]['name']);
  254. /* handle any other changelog */
  255. else
  256. {
  257. var manageHitchhiker =
  258. function(hhid,hh)
  259. {
  260. /* remember/restore a hitchhiker given specified id */
  261. if( hh )
  262. self.__hitchhikerJournal[hhid] = hh;
  263. else if( hitchhiker )
  264. self.__hitchhikerJournal[hhid] = hitchhiker;
  265. else
  266. hitchhiker = self.__hitchhikerJournal[hhid];
  267. };
  268. this.__checkpointUserOperation(aswSequenceNumber);
  269. changelog.forEach(
  270. function(step)
  271. {
  272. /* no legal connections exist between *Icon types, so we
  273. simply simulate the CS change such a [dis]connection would
  274. incur */
  275. if( step['op'] == 'MKEDGE' || step['op'] == 'RMEDGE' )
  276. actions.push(
  277. function()
  278. {
  279. var asid1 = step['id1'],
  280. asid2 = step['id2'],
  281. csid1 = self.__asid_to_csid(asid1),
  282. csid2 = self.__asid_to_csid(asid2);
  283. cschangelogs.push(
  284. [{'op':step['op'],'id1':csid1,'id2':csid2}]);
  285. return __successContinuable();
  286. });
  287. /* create appropriate CS instance and associate it with new AS
  288. instance (remember the association in __asid2csid to
  289. optimize future operations) */
  290. else if (step['op'] == 'MKNODE') {
  291. actions.push(
  292. function () {
  293. manageHitchhiker(step['id']);
  294. let asid = step['id'],
  295. node = _utils.jsonp(step['node']),
  296. isLink = ('segments' in hitchhiker),
  297. fullastype = node['$type'],
  298. fullcstype = self.__astype_to_cstype(
  299. fullastype,
  300. isLink),
  301. asuri = fullastype + '/' + asid + '.instance',
  302. attrs = {'$asuri': asuri};
  303. if ('pos' in hitchhiker)
  304. attrs['position'] = hitchhiker['pos'];
  305. else if ('neighborhood' in hitchhiker) {
  306. let nc = self.__nodesCenter(
  307. hitchhiker['neighborhood']);
  308. attrs['position'] =
  309. [(nc[0] || 200), (nc[1] || 200)];
  310. }
  311. else if ('clone' in hitchhiker)
  312. attrs = _utils.mergeDicts(
  313. [attrs, hitchhiker['clone']]);
  314. else
  315. attrs['position'] = [200, 200];
  316. let res = _mmmk.create(fullcstype, attrs),
  317. csid = res['id'];
  318. self.__asid2csid[asid] = csid;
  319. cschangelogs.push(res['changelog']);
  320. if (isLink) {
  321. let s = {},
  322. src =
  323. hitchhiker['src'] ||
  324. self.__asuri_to_csuri(hitchhiker['asSrc']),
  325. dest =
  326. hitchhiker['dest'] ||
  327. self.__asuri_to_csuri(hitchhiker['asDest']),
  328. segments =
  329. hitchhiker['segments'] ||
  330. self.__defaultSegments(src, dest);
  331. s[src + '--' + __id_to_uri(csid)] = segments[0];
  332. s[__id_to_uri(csid) + '--' + dest] = segments[1];
  333. cschangelogs.push(
  334. _mmmk.update(
  335. csid,
  336. {'$segments': s})['changelog'],
  337. self.__positionLinkDecorators(csid));
  338. }
  339. return self.__regenIcon(csid);
  340. },
  341. function (riChangelog) {
  342. cschangelogs.push(riChangelog);
  343. return __successContinuable();
  344. });
  345. }
  346. /* remove appropriate CS instance... update __asid2csid for it
  347. to remain consistent */
  348. else if( step['op'] == 'RMNODE' )
  349. actions.push(
  350. function()
  351. {
  352. var asid = step['id'],
  353. csid = self.__asid_to_csid(asid);
  354. cschangelogs.push(_mmmk['delete'](csid)['changelog']);
  355. delete self.__asid2csid[asid];
  356. return __successContinuable();
  357. });
  358. /* regenerate the icon to re-evaluate any coded attributes
  359. NOTE:: CS changes may be bundled (i.e., if an AS update was
  360. simulated by a CS update)... if so, perform them
  361. before regenerating the icon */
  362. else if( step['op'] == 'CHATTR' )
  363. actions.push(
  364. function()
  365. {
  366. var asid = step['id'],
  367. csid = self.__asid_to_csid(asid);
  368. if( hitchhiker && 'cschanges' in hitchhiker )
  369. {
  370. var cschanges = hitchhiker['cschanges'];
  371. cschangelogs.push(
  372. _mmmk.update(csid,cschanges)['changelog'],
  373. ('$segments' in cschanges ?
  374. self.__positionLinkDecorators(csid) :
  375. []));
  376. }
  377. return self.__regenIcon(csid);
  378. },
  379. function(riChangelog)
  380. {
  381. cschangelogs.push(riChangelog);
  382. return __successContinuable();
  383. });
  384. /* load appropriate CS metamodel (stored in hitchhiker)...
  385. remember AS-to-CS metamodel mapping in __asmm2csmm to
  386. optimize future operations */
  387. else if( step['op'] == 'LOADMM' )
  388. actions.push(
  389. function()
  390. {
  391. manageHitchhiker(step['name']);
  392. var asmm = step['name'],
  393. csmm = hitchhiker['name'],
  394. data = hitchhiker['csmm'];
  395. cschangelogs.push(
  396. _mmmk.loadMetamodel(csmm,data)['changelog'],
  397. {'op':'LOADASMM',
  398. 'name':asmm,
  399. 'mm':step['mm']});
  400. self.__asmm2csmm[asmm] = csmm;
  401. return __successContinuable();
  402. });
  403. /* unload appropriate CS metamodel... update __asmm2csmm for
  404. it to remain consistent */
  405. else if( step['op'] == 'DUMPMM' )
  406. actions.push(
  407. function()
  408. {
  409. var asmm = step['name'],
  410. csmm = self.__asmm2csmm[asmm];
  411. cschangelogs.push(_mmmk.unloadMetamodel(csmm)['changelog']);
  412. delete self.__asmm2csmm[asmm];
  413. return __successContinuable();
  414. });
  415. /* load appropriate CS model (stored in hitchhiker) and
  416. overwrite past hitchhiker associated to initial load of
  417. current model, if any... when step['insert'] is specified,
  418. adjust $asuris to compensate for offsetting of inserted asm
  419. and $segments to compensate for upcoming offsetting of to-
  420. be-inserted csm */
  421. else if (step['op'] == 'RESETM')
  422. actions.push(
  423. function () {
  424. manageHitchhiker(
  425. step['old_name'],
  426. {'csm': _mmmk.read()});
  427. manageHitchhiker(step['new_name']);
  428. var csm = hitchhiker['csm'];
  429. var _csm = eval('(' + csm + ')');
  430. if (step['insert']) {
  431. var asoffset = parseInt(step['insert']),
  432. csoffset = _mmmk.next_id,
  433. incUri =
  434. function (oldUri, offset) {
  435. var matches = oldUri.match(/(.+\/)(.+)(\.instance)/);
  436. return matches[1] +
  437. (parseInt(matches[2]) + offset) +
  438. matches[3];
  439. };
  440. for (var id in _csm.nodes) {
  441. _csm.nodes[id]['$asuri']['value'] =
  442. incUri(_csm.nodes[id]['$asuri']['value'],
  443. asoffset);
  444. if (!('$segments' in _csm.nodes[id]))
  445. continue;
  446. var segments =
  447. _csm.nodes[id]['$segments']['value'],
  448. _segments = {};
  449. for (var edgeId in segments) {
  450. var uris = edgeId.match(
  451. /^(.*\.instance)--(.*\.instance)$/);
  452. _segments[incUri(uris[1], csoffset) + '--' +
  453. incUri(uris[2], csoffset)] =
  454. segments[edgeId];
  455. }
  456. _csm.nodes[id]['$segments']['value'] = _segments;
  457. }
  458. csm = _utils.jsons(_csm, null, '\t');
  459. }
  460. //see if any cs metamodels are missing
  461. //this fixes issue #28
  462. //this loading should be done elsewhere in the model loading chain
  463. for (var i in _csm.metamodels) {
  464. var mm = _csm.metamodels[i];
  465. if (!(_mmmk.model.metamodels.includes(mm))) {
  466. console.error("Last-minute loading for CS metamodel: " + mm);
  467. var csmm = _fs.readFile('./users/' + mm, 'utf8');
  468. _mmmk.loadMetamodel(mm, csmm);
  469. }
  470. }
  471. var res = _mmmk.loadModel(
  472. step['new_name'],
  473. csm,
  474. step['insert']);
  475. if (res["$err"] == undefined) {
  476. cschangelogs.push(res['changelog']);
  477. return __successContinuable();
  478. } else {
  479. return __errorContinuable();
  480. }
  481. });
  482. /* forward this SYSOUT command */
  483. else if( step['op'] == 'SYSOUT' )
  484. actions.push(
  485. function()
  486. {
  487. cschangelogs.push([step]);
  488. return __successContinuable();
  489. });
  490. });
  491. actions.push(
  492. function()
  493. {
  494. cschangelogs.push( self.__solveLayoutContraints(changelog) );
  495. return __successContinuable();
  496. });
  497. }
  498. _do.chain(actions)(
  499. function()
  500. {
  501. var cschangelog = _utils.flatten(cschangelogs);
  502. //console.error('w#'+__wid+' -- ('+aswSequenceNumber+') '+
  503. // _utils.jsons(cschangelog));
  504. __postMessage(
  505. {'statusCode':200,
  506. 'changelog':cschangelog,
  507. 'sequence#':aswSequenceNumber,
  508. 'hitchhiker':cshitchhiker});
  509. self.__nextASWSequenceNumber =
  510. _utils.incrementSequenceNumber(self.__nextASWSequenceNumber);
  511. self.__applyPendingASWChanges();
  512. },
  513. function(err)
  514. {
  515. throw 'unexpected error while applying changelogs :: '+err;
  516. }
  517. );
  518. },
  519. /* apply pending asworker changelogs, if any and if applicable */
  520. '__applyPendingASWChanges' :
  521. function()
  522. {
  523. if( this.__pendingChangelogs.length > 0 &&
  524. this.__nextASWSequenceNumber ==
  525. this.__pendingChangelogs[0]['sequence#'] )
  526. {
  527. var pc = this.__pendingChangelogs.shift();
  528. this.__applyASWChanges(
  529. pc['changelog'],
  530. pc['sequence#'],
  531. pc['hitchhiker']);
  532. }
  533. },
  534. /* initialize a socket that will listen for and handle changelogs returned by
  535. this csworker's associated asworker
  536. 1. onconnect() is triggered when 2 way communication is established, at
  537. which point we attempt to subscribe for specified asworker
  538. 2. onmessage() is triggered once in response to our subscription attempt:
  539. a) we set this.__aswid to specified aswid
  540. b) if a cswid was provided (i.e., a shared model session is being
  541. set up)
  542. i. retrieve the specified csworker's internal state
  543. ii. setup this csworker's state and _mmmk based on the
  544. results from step ii.
  545. iii. return the new state to the client (via callback())
  546. iv. remove any obsolete changelogs received since step i.
  547. and set __nextASWSequenceNumber to the same value as
  548. that of the csworker whose state we're cloning
  549. v. apply pending changelogs, if any
  550. all future triggers of onmessage() are due to the asworker pushing
  551. changelogs, these are handled by __applyASWChanges
  552. NOTE : actually, there is a 3rd case where onmessage() is triggered...
  553. between the moment where the socket is created and the moment
  554. where we receive the response to our subscription attempt, we
  555. will receive *all* messages broadcasted by the websocket server
  556. in httpwsd.js... these are detected and discarded */
  557. '__aswSubscribe' :
  558. function(aswid,cswid)
  559. {
  560. var self = this;
  561. return function(callback,errback)
  562. {
  563. var socket = _siocl.connect('127.0.0.1',{port:8124});
  564. socket.on('connect',
  565. function()
  566. {
  567. socket.emit('message',
  568. {'method':'POST','url':'/changeListener?wid='+aswid});
  569. });
  570. socket.on('disconnect',
  571. function() {self.__aswid = undefined;});
  572. socket.on('message',
  573. function(msg)
  574. {
  575. /* on POST /changeListener response */
  576. if( msg.statusCode != undefined )
  577. {
  578. if( ! _utils.isHttpSuccessCode(msg.statusCode) )
  579. return errback(msg.statusCode+':'+msg.reason);
  580. self.__aswid = aswid;
  581. if( cswid != undefined )
  582. {
  583. var actions =
  584. [__wHttpReq('GET','/internal.state?wid='+cswid)];
  585. _do.chain(actions)(
  586. function(respData)
  587. {
  588. var state = respData['data'];
  589. _mmmk.clone(state['_mmmk']);
  590. self.__clone(state['_wlib']);
  591. __ids2uris = state['__ids2uris'];
  592. __nextSequenceNumber =
  593. state['__nextSequenceNumber'];
  594. self.__pendingChangelogs =
  595. self.__pendingChangelogs.filter(
  596. function(pc)
  597. {
  598. return self.__sn2int(pc['sequence#']) >
  599. self.__sn2int(
  600. self.__nextASWSequenceNumber);
  601. });
  602. callback();
  603. self.__applyPendingASWChanges();
  604. },
  605. function(err) {errback(err);}
  606. );
  607. }
  608. else
  609. callback();
  610. }
  611. /* on changelog reception (ignore changelogs while not
  612. subscribed to an asworker... see NOTE) */
  613. else if( self.__aswid != undefined )
  614. self.__applyASWChanges(
  615. msg.data.changelog,
  616. msg.data['sequence#'],
  617. msg.data.hitchhiker);
  618. });
  619. };
  620. },
  621. /***************************** ICON GENERATION *****************************/
  622. /* determine the correct positions and orientations of the given link's
  623. decorators, adjust them and return changelogs
  624. 0. concatenate segments into a single path
  625. 1. for each link decorator (i.e., Link $contents)
  626. *. do nothing if link decoration information is missing... this ensures
  627. backward compatibility with pre-link decorator models
  628. a. extract link decoration information, i.e., xratio and yoffset
  629. b. determine point on path at xratio and its orientation
  630. c. adjust yoffset given orientation (yoffset was specified for 0deg)
  631. d. adjust endAt given orientation (endAt is specified for 0deg)... note
  632. that endAt is only relevant for arrowtails
  633. e. relativize point from step b. w.r.t. to Link center and adjust
  634. position by adjusted yoffset
  635. f. set new position and orientation in mmmk and remember changelogs
  636. 2. return flattened changelogs
  637. NOTE:: the initial values of vobject geometric attribute values must
  638. always be remembered... they are needed on the client to properly
  639. support the drawing and transformation of vobjects... this is
  640. captured by buildVobjGeomAttrVal(), which we use in step 1f */
  641. '__positionLinkDecorators' :
  642. function(id)
  643. {
  644. var link = _utils.jsonp(_mmmk.read(id)),
  645. vobjs = link['$contents']['value'].nodes,
  646. segments = _utils.values(link['$segments']['value']),
  647. path = segments[0]+
  648. segments[1].substring(segments[1].indexOf('L')),
  649. changelogs = [];
  650. for( var vid in vobjs )
  651. {
  652. if( !('$linkDecoratorInfo' in vobjs[vid]) )
  653. continue;
  654. var ldi = vobjs[vid]['$linkDecoratorInfo']['value'],
  655. pp = _svg.fns.getPointOnPathAtRatio(path,ldi.xratio);
  656. if (pp == undefined)
  657. continue;
  658. var yoffset = new _svg.types.Point(0,ldi.yoffset).rotate(pp.O),
  659. endAt = (ldi.xratio >= 1 ?
  660. new _svg.types.Point(100,0).rotate(pp.O) :
  661. new _svg.types.Point(0,0)),
  662. changes = {};
  663. pp.x += yoffset.x - link['position']['value'][0];
  664. pp.y += yoffset.y - link['position']['value'][1];
  665. changes['$contents/value/nodes/'+vid+'/position'] =
  666. [_utils.buildVobjGeomAttrVal(
  667. vobjs[vid]['position']['value'][0], pp.x+','+endAt.x+'%'),
  668. _utils.buildVobjGeomAttrVal(
  669. vobjs[vid]['position']['value'][1], pp.y+','+endAt.y+'%')];
  670. changes['$contents/value/nodes/'+vid+'/orientation'] =
  671. _utils.buildVobjGeomAttrVal(
  672. vobjs[vid]['orientation']['value'], pp.O);
  673. changelogs.push( _mmmk.update(id,changes)['changelog'] );
  674. }
  675. return _utils.flatten(changelogs);
  676. },
  677. /* regenerate specified icon... if newCsmm is specified, the regeneration
  678. process essentially transforms the icon for it to conform to whatever is
  679. specified by newCsmm... otherwise, it merely involves re-evaluating
  680. VisualObject mappers
  681. 1. if newCSmm is defined
  682. a) create a new instance I of node 'id''s icontype given newCsmm
  683. b) copy node 'id''s '$asuri', 'position', etc. attributes to I
  684. c) delete node 'id'
  685. d) update__asid2csid and 'id' variable to be I's id
  686. e) save the changelogs of steps a-c)
  687. 2. in either case, [re-]eval VisualObject mappers
  688. a) retrieve Icon and VisualObject mappers...
  689. i. fetch specified node from mmmk
  690. ii. retrieve its '$contents' attribute
  691. iii. retrieve the 'mapper' attribute for the node itself
  692. iii. retrieve the 'mapper' attribute of VisualObjects within
  693. '$contents'
  694. b) return empty changelog if all mappers are empty
  695. c) setup sync/async action chaining
  696. i. ask asworker to evaluate a bunch of mappers
  697. ii. save results for later access
  698. d) launch chain...
  699. on success, populate attributes with results from step 2ci and
  700. 'return' changelog OR 'return' SYSOUT error if evaluating mappers
  701. raised exceptions
  702. on failure (this only occurs if we were unable to obtain an
  703. asworker read-lock), relaunch chain after short delay
  704. TBI: step 2d) could potentially lead to an infinite loop if failure is due
  705. to unforeseen error or to very long delays if lock holder takes a
  706. long time to finish... we could address this by *not* relaunching the
  707. chain if some number of tries have failed, and instead setting coded
  708. attribute values to '<out-of-date>' */
  709. '__regenIcon' :
  710. function(id,newCsmm)
  711. {
  712. var changelogs = [],
  713. self = this;
  714. return function(callback,errback)
  715. {
  716. if( newCsmm != undefined )
  717. {
  718. var node = _utils.jsonp(_mmmk.read(id)),
  719. asuri = _mmmk.read(id,'$asuri'),
  720. asid = __uri_to_id(asuri),
  721. attrs = _utils.mergeDicts([
  722. {'$asuri':asuri,
  723. 'position':node['position']['value'],
  724. 'orientation':node['orientation']['value'],
  725. 'scale':node['scale']['value']},
  726. ((s=_mmmk.read(id,'$segments'))['$err'] ?
  727. {} : {'$segments':s}) ]),
  728. cres = _mmmk.create(
  729. newCsmm+'/'+node['$type'].match(/.*\/(.*)/)[1],
  730. attrs),
  731. csid = cres['id'],
  732. dres = _mmmk['delete'](id);
  733. self.__asid2csid[asid] = id = csid;
  734. changelogs.push(
  735. cres['changelog'],
  736. dres['changelog']);
  737. }
  738. var csuri = __id_to_uri(id),
  739. asuri = self.__csuri_to_asuri(csuri),
  740. icon = _utils.jsonp(_mmmk.read(id)),
  741. vobjects = icon['$contents']['value'],
  742. mappers = {};
  743. if( icon['mapper']['value'] != '' )
  744. mappers[''] = icon['mapper']['value'];
  745. for( var vid in vobjects['nodes'] )
  746. if( 'mapper' in vobjects['nodes'][vid] &&
  747. vobjects['nodes'][vid]['mapper']['value'] != '' )
  748. mappers['$contents/value/nodes/'+vid+'/'] =
  749. vobjects['nodes'][vid]['mapper']['value'];
  750. if( _utils.keys(mappers).length > 0 )
  751. {
  752. var actions =
  753. [__wHttpReq(
  754. 'POST',
  755. '/GET/'+asuri+'.mappings?wid='+self.__aswid,
  756. mappers)],
  757. successf =
  758. function(attrVals)
  759. {
  760. if( '$err' in attrVals )
  761. callback(
  762. [{'op':'SYSOUT',
  763. 'text':'ERROR :: '+attrVals['$err']}]);
  764. else
  765. {
  766. var changes = {};
  767. for( var fullattr in attrVals )
  768. changes[fullattr] = attrVals[fullattr];
  769. var result = _mmmk.update(id,changes);
  770. if ( '$err' in result )
  771. callback(
  772. [{'op':'SYSOUT',
  773. 'text':'ERROR :: '+result['$err']}]);
  774. else {
  775. changelogs.push(
  776. result['changelog'] );
  777. callback( _utils.flatten(changelogs) );
  778. }
  779. }
  780. },
  781. failuref =
  782. function(err)
  783. {
  784. console.error('"POST *.mappings" failed on :: '+err+
  785. '\n(will try again soon)');
  786. setTimeout(
  787. _do.chain(actions),
  788. self.__REGEN_ICON_RETRY_DELAY_MS,
  789. successf,
  790. failuref);
  791. };
  792. _do.chain(actions)(successf,failuref);
  793. }
  794. else
  795. callback( _utils.flatten(changelogs) );
  796. };
  797. },
  798. '__solveLayoutContraints':
  799. function(changelog)
  800. {
  801. // TBC actually implement this function
  802. // use ids in changelog to determine what changed
  803. // add 2 lines below to mmmk.__create() if necessary
  804. // if( type in this.metamodels[metamodel]['connectorTypes'] )
  805. // new_node['$linktype'] = this.metamodels[metamodel]['connectorTypes'][type];
  806. return [];
  807. // return [{'op':'SYSOUT',
  808. // 'text':'WARNING :: '+
  809. // 'a proper layout constraint solver has yet to be '+
  810. // 'implemented... inter-VisualObject relationships are '+
  811. // 'ignored and containers do not resize to fit their '+
  812. // 'contents'}];
  813. },
  814. /* transform all icons from tgtCsmm into appropriate icons of newCsmm
  815. 1. read model and newCsmm from _mmmk
  816. 2. for each icon of tgtCsmm, if newCsmm defines a replacement icon, save
  817. the icon's id in 'tgtIds'... otherwise, return error
  818. 3. init sync/async action chaining...
  819. a) for each id in 'tgtIds', add 2 entries to chain
  820. i. call __regenIcon on specified icon
  821. ii. save resulting changelog and continue
  822. 4. launch chain...
  823. on success,
  824. a) adjust $segments attributes from all Links to account for edge
  825. end change of id (and uri)
  826. b) 'return' flattened changelogs
  827. on failure, 'return' error */
  828. '__transformIcons' :
  829. function(tgtCsmm,newCsmm)
  830. {
  831. var self = this;
  832. return function(callback,errback)
  833. {
  834. var m = _utils.jsonp(_mmmk.read()),
  835. newCsmmData = _utils.jsonp(_mmmk.readMetamodels(newCsmm)),
  836. tgtIds = [],
  837. newIds = {},
  838. changelogs = [],
  839. actions = [__successContinuable()];
  840. for( var id in m.nodes )
  841. if( (matches = m.nodes[id]['$type'].match('^'+tgtCsmm+'/(.*)')) )
  842. {
  843. var type = matches[1];
  844. if( newCsmmData.types[type] == undefined )
  845. {
  846. errback('Icons mm should define type :: '+type);
  847. return;
  848. }
  849. tgtIds.push(id);
  850. }
  851. tgtIds.forEach(
  852. function(id)
  853. {
  854. actions.push(
  855. function() {return self.__regenIcon(id,newCsmm);},
  856. function(changelog)
  857. {
  858. var newId = changelog[0]['id'];
  859. newIds[id] = newId;
  860. changelogs.push(
  861. changelog.concat(
  862. '$err' in _mmmk.read(newId,'$segments') ?
  863. [] : self.__positionLinkDecorators(newId)) );
  864. return __successContinuable();
  865. });
  866. });
  867. _do.chain(actions)(
  868. function()
  869. {
  870. var m = _utils.jsonp(_mmmk.read());
  871. for( var id in m.nodes )
  872. if( (matches = m.nodes[id]['$type'].match(/Link$/)) )
  873. {
  874. var s = _mmmk.read(id,'$segments'),
  875. changed = false;
  876. for( var edgeId in s )
  877. {
  878. var ends =
  879. edgeId.match(/(.*\.instance)--(.*\.instance)/),
  880. id1 = __uri_to_id(ends[1]),
  881. id2 = __uri_to_id(ends[2]);
  882. if( id1 in newIds )
  883. {
  884. ends[1] = __id_to_uri( newIds[id1] );
  885. changed = true;
  886. }
  887. if( id2 in newIds )
  888. {
  889. ends[2] = __id_to_uri( newIds[id2] );
  890. changed = true;
  891. }
  892. if( changed )
  893. {
  894. s[ends[1]+'--'+ends[2]] = s[edgeId];
  895. delete s[edgeId];
  896. }
  897. }
  898. if( changed )
  899. changelogs.push(
  900. _mmmk.update(id,{'$segments':s})['changelog'] );
  901. }
  902. callback(_utils.flatten(changelogs));
  903. },
  904. function(err)
  905. {
  906. errback('__transformIcons() should never fail... '+
  907. 'failed on :: '+err);
  908. }
  909. );
  910. };
  911. },
  912. /************************** REST REQUEST HANDLING **************************/
  913. /* INTENT :
  914. ask our asworker's mtworker to do something (e.g., change
  915. transformation execution mode)
  916. IN PRACTICE:
  917. adjust uri and forward to asworker
  918. 1. setup sync/async action chaining
  919. a) ask asworker to forward request to its mtworker
  920. 2. launch chain... return success code or error */
  921. 'mtwRequest' :
  922. function(resp,method,uri,reqData)
  923. {
  924. var actions = [__wHttpReq(
  925. method,
  926. uri+'?wid='+this.__aswid,
  927. reqData)];
  928. _do.chain(actions)(
  929. function()
  930. {
  931. __postMessage({'statusCode':200, 'respIndex':resp});
  932. },
  933. function(err) {__postInternalErrorMsg(resp,err);}
  934. );
  935. },
  936. /* return sufficient information to clone the current csworker to the tinyest
  937. detail... this is a pimped out version of GET current.state for internal
  938. use only
  939. 1. bundle info about _mmmk and _wlib
  940. 2. return to querier */
  941. 'GET /internal.state' :
  942. function(resp)
  943. {
  944. __postMessage(
  945. {'statusCode':200,
  946. 'data':{'_mmmk':_mmmk.clone(),
  947. '_wlib':this.__clone(),
  948. '__ids2uris':_utils.clone(__ids2uris),
  949. '__nextSequenceNumber':__nextSequenceNumber},
  950. 'sequence#':__sequenceNumber(0),
  951. 'respIndex':resp});
  952. },
  953. /* subscribe to an existing or to-be-created asworker
  954. 1. validate parameters
  955. 2. if 'aswid' and 'cswid' are given,
  956. a) setup sync/async action chaining
  957. i) attempt to subsribe to specified asworker using specified
  958. csworker's current model as this csworker's initial model
  959. b) launch chain... on success, 'return' current state (via
  960. GET__current_state())... on error, return error
  961. 2. otherwise,
  962. a) setup sync/async action chaining
  963. i) spawn new asworker
  964. ii) subscribe to it
  965. b) launch chain... return success code or error */
  966. 'PUT /aswSubscription' :
  967. function(resp,uri,reqData/*wid*/)
  968. {
  969. if( this.__aswid > -1 )
  970. return __postForbiddenErrorMsg(
  971. resp,
  972. 'already subscribed to an asworker');
  973. if( reqData != undefined )
  974. {
  975. if( reqData['aswid'] == undefined ||
  976. reqData['cswid'] == undefined )
  977. return __postInternalErrorMsg(resp, 'missing AS and/or CS wid');
  978. var self = this,
  979. actions =
  980. [this.__aswSubscribe(reqData['aswid'],reqData['cswid'])];
  981. _do.chain(actions)(
  982. function()
  983. {
  984. GET__current_state(resp);
  985. },
  986. function(err) {__postInternalErrorMsg(resp,err);}
  987. );
  988. }
  989. else
  990. {
  991. var self = this,
  992. actions =
  993. [__httpReq('POST','/asworker'),
  994. function(aswid) {return self.__aswSubscribe(aswid);}];
  995. _do.chain(actions)(
  996. function()
  997. {
  998. __postMessage(
  999. {'statusCode':200,
  1000. 'data':self.__aswid,
  1001. 'respIndex':resp});
  1002. },
  1003. function(err) {__postInternalErrorMsg(resp,err);}
  1004. );
  1005. }
  1006. },
  1007. /* INTENT :
  1008. load a CS and an AS metamodel from disk
  1009. *OR*
  1010. switch between different CS metamodels
  1011. IN PRACTICE:
  1012. adjust uri and reqData and forward to asworker
  1013. *OR*
  1014. fulfill intent
  1015. 1. parse + validate parameters
  1016. 2. if no asmm is specified (CS switch),
  1017. a) setup sync/async action chaining
  1018. i. read specified csmm from disk
  1019. ii. load read data into _mmmk
  1020. iii. try to regen all icons from overwritten csmm
  1021. b) launch chain...
  1022. i. on failure, undo step 2.a)ii. if it ran, and return error
  1023. ii. on success,
  1024. j. return success code
  1025. jj. unload previous csmm
  1026. jjj. post bundled and flattened changelogs
  1027. 2. if an asmm is specified (MM load)
  1028. a) setup sync/async action chaining
  1029. i. read specified csmm from disk
  1030. ii. ask asworker to load AS metamodel + pass CS metamodel name and
  1031. data as 'hitchhiker'
  1032. b) launch chain... return success code or error */
  1033. 'PUT /current.metamodels' :
  1034. function(resp,uri,reqData/*[asmm,]csmm*/)
  1035. {
  1036. if( reqData == undefined )
  1037. return __postBadReqErrorMsg(resp, 'missing request data');
  1038. else if( reqData['csmm'] == undefined )
  1039. return __postBadReqErrorMsg(resp, 'missing CS mm');
  1040. else if( ! (matches = reqData['csmm'].
  1041. match(/.+?((\/.*)\..*Icons(\.pattern){0,1})\.metamodel/)) )
  1042. return __postBadReqErrorMsg(
  1043. resp,
  1044. 'bad uri for Icons mm :: '+reqData['csmm']);
  1045. var asmm = matches[2]+(matches[3] || ''),
  1046. csmm = matches[1];
  1047. if( reqData['asmm'] == undefined )
  1048. {
  1049. if( this.__asmm2csmm[asmm] == undefined )
  1050. return __postBadReqErrorMsg(resp, 'missing AS mm');
  1051. var lres = undefined,
  1052. sn = undefined,
  1053. self = this,
  1054. actions =
  1055. [_fs.readFile('./users'+reqData['csmm'],'utf8'),
  1056. function(csmmData)
  1057. {
  1058. sn = __sequenceNumber();
  1059. self.__checkpointUserOperation(sn);
  1060. lres = _mmmk.loadMetamodel(csmm,csmmData);
  1061. return __successContinuable();
  1062. },
  1063. function()
  1064. {
  1065. return self.__transformIcons(
  1066. self.__asmm2csmm[asmm],
  1067. csmm);
  1068. }];
  1069. _do.chain(actions)(
  1070. function(changelog)
  1071. {
  1072. __postMessage({'statusCode':202, 'respIndex':resp});
  1073. var ures = _mmmk.unloadMetamodel(self.__asmm2csmm[asmm]),
  1074. changelogs =
  1075. [lres['changelog'],changelog,ures['changelog']];
  1076. self.__asmm2csmm[asmm] = csmm;
  1077. __postMessage(
  1078. {'statusCode':200,
  1079. 'changelog':_utils.flatten(changelogs),
  1080. 'sequence#':sn});
  1081. },
  1082. function(err)
  1083. {
  1084. if( sn != undefined )
  1085. __postInternalErrorMsg(resp,
  1086. 'CS switch should never fail for non-I/O reason'+
  1087. '... backend may now be in unstable state... '+
  1088. 'failed on :: '+err);
  1089. else
  1090. __postInternalErrorMsg(resp,err);
  1091. }
  1092. );
  1093. }
  1094. else
  1095. {
  1096. var self = this,
  1097. actions =
  1098. [_fs.readFile('./users'+reqData['csmm'],'utf8'),
  1099. function(csmmData)
  1100. {
  1101. return __wHttpReq(
  1102. 'PUT',
  1103. uri+'?wid='+self.__aswid,
  1104. {'mm':reqData['asmm'],
  1105. 'hitchhiker':{'csmm':csmmData,'name':csmm}});
  1106. }];
  1107. _do.chain(actions)(
  1108. function()
  1109. {
  1110. __postMessage({'statusCode':202, 'respIndex':resp});
  1111. },
  1112. function(err) {__postInternalErrorMsg(resp,err);}
  1113. );
  1114. }
  1115. },
  1116. /* INTENT :
  1117. unload a metamodel (deletes all entities from that metamodel)
  1118. IN PRACTICE:
  1119. adjust uri and forward to asworker
  1120. 1. parse + validate parameters
  1121. 2. setup sync/async action chaining
  1122. a) ask asworker to unload corresponding AS mm
  1123. 3. launch chain... on success, unload specified metamodel */
  1124. 'DELETE *.metamodel' :
  1125. function(resp,uri)
  1126. {
  1127. var matches = uri.match(/(.*)\..*Icons(\.pattern){0,1}\.metamodel/);
  1128. if( ! matches )
  1129. return __postBadReqErrorMsg(resp,'bad uri for Icons mm :: '+uri);
  1130. var asuri = matches[1]+(matches[2] || '')+'.metamodel',
  1131. actions =
  1132. [__wHttpReq('DELETE',asuri+'?wid='+this.__aswid)];
  1133. _do.chain(actions)(
  1134. function()
  1135. {
  1136. __postMessage({'statusCode':202, 'respIndex':resp});
  1137. },
  1138. function(err) {__postInternalErrorMsg(resp,err);}
  1139. );
  1140. },
  1141. /* INTENT :
  1142. load a model from disk
  1143. OR
  1144. clear current model (and metamodels)
  1145. IN PRACTICE:
  1146. adjust reqData and forward to asworker
  1147. 1. parse + validate parameters
  1148. 2. read specified model from disk
  1149. OR
  1150. fabricate empty model
  1151. 3. setup sync/async action chaining
  1152. a) ask asworker to load associated AS model + pass CS model as
  1153. 'hitchhiker'
  1154. 4. launch chain... return success code or error */
  1155. 'PUT /current.model' :
  1156. function(resp,uri,reqData/*m[,insert]*/)
  1157. {
  1158. if( reqData == undefined )
  1159. return __postBadReqErrorMsg(resp, 'missing model');
  1160. var self = this,
  1161. mData = undefined,
  1162. actions = [];
  1163. if( reqData['m'] == undefined )
  1164. actions =
  1165. [__successContinuable(),
  1166. function()
  1167. {
  1168. mData = {"csm": {"nodes": {},
  1169. "edges": [],
  1170. "metamodels": []},
  1171. "asm": {"nodes": {},
  1172. "edges": [],
  1173. "metamodels": []}};
  1174. reqData['m'] = 'new';
  1175. return __successContinuable(mData);
  1176. }];
  1177. else {
  1178. try {
  1179. _fs.accessSync('./users/'+reqData['m']);
  1180. } catch (e) {
  1181. return __postBadReqErrorMsg(resp, 'cannot read ' + reqData['m']);
  1182. }
  1183. actions = [ _fs.readFile('./users/'+reqData['m'],'utf8'),
  1184. function(_mData)
  1185. {
  1186. mData = eval('('+_mData+')');
  1187. return __successContinuable(mData);
  1188. }];
  1189. actions.push(
  1190. function(m)
  1191. {
  1192. var asmData = _utils.jsons(m['asm']),
  1193. csmData = _utils.jsons(m['csm']);
  1194. return __wHttpReq(
  1195. 'PUT',
  1196. uri+'?wid='+self.__aswid,
  1197. {'m':asmData,
  1198. 'name':reqData['m']+(new Date().getTime()),
  1199. 'insert':reqData['insert'],
  1200. 'hitchhiker':{'csm':csmData}});
  1201. });
  1202. _do.chain(actions)(
  1203. function()
  1204. {
  1205. __postMessage({'statusCode':202,'respIndex':resp});
  1206. },
  1207. function(err)
  1208. {
  1209. var MISSING_MM_ERROR = 'metamodel not loaded :: ';
  1210. if( (matches = err.match('^500:'+MISSING_MM_ERROR+
  1211. '(.*?)(\\.pattern){0,1}$')) )
  1212. {
  1213. var asmm = matches[1],
  1214. pmm = (matches[2] != undefined),
  1215. csmm;
  1216. mData['csm'].metamodels.some(
  1217. function(mm)
  1218. {
  1219. if( (pmm && mm.match(
  1220. '^'+asmm+'\\.[a-zA-Z0-9]*Icons\\.pattern$')) ||
  1221. (!pmm && mm.match(
  1222. '^'+asmm+'\\.[a-zA-Z0-9]*Icons$')) )
  1223. csmm = mm;
  1224. return csmm;
  1225. });
  1226. __postInternalErrorMsg(resp,MISSING_MM_ERROR+csmm);
  1227. }
  1228. else
  1229. __postInternalErrorMsg(resp,err);
  1230. }
  1231. );
  1232. }
  1233. },
  1234. /* INTENT :
  1235. create a new instance of specified type (if reqData has
  1236. 'src' and 'dest' fields, type is a connector)
  1237. IN PRACTICE:
  1238. adjust uri (and reqData) and forward to asworker
  1239. 1. parse + validate parameters
  1240. 2. setup sync/async action chaining
  1241. a) construct reqData for asworker.POST *.type
  1242. i. handle connector ends if applicable
  1243. ii. handle 'pos'... pass as hitchhiker and evaluate the to-be
  1244. *Icon's parser within a dummy context where 'position' is set
  1245. to 'pos'... see NOTES above for more details on this
  1246. b) ask asworker to create an instance of appropriate AS type
  1247. 3. launch chain... return success code or error */
  1248. 'POST *.type':
  1249. function (resp, uri, reqData/*pos|clone,[segments,src,dest]*/) {
  1250. let matches =
  1251. uri.match(/((.*)\..*Icons)(\.pattern){0,1}\/((.*)Icon)\.type/) ||
  1252. uri.match(/((.*)\..*Icons)(\.pattern){0,1}\/((.*)Link)\.type/);
  1253. if (!matches)
  1254. return __postBadReqErrorMsg(
  1255. resp,
  1256. 'bad uri for Icon/Link type :: ' + uri);
  1257. let asuri = matches[2] + (matches[3] || '') + '/' + matches[5] + '.type',
  1258. csmm = matches[1] + (matches[3] || ''),
  1259. cstype = matches[4],
  1260. types = _utils.jsonp(_mmmk.readMetamodels(csmm))['types'];
  1261. if (!(cstype in types)) {
  1262. return __postBadReqErrorMsg(
  1263. resp, 'no concrete syntax definition found for ' + cstype);
  1264. }
  1265. let parser =
  1266. types[cstype].filter(
  1267. function (attr) {
  1268. return attr['name'] == 'parser';
  1269. })[0]['default'],
  1270. self = this,
  1271. actions =
  1272. [__successContinuable(),
  1273. function () {
  1274. if (reqData == undefined)
  1275. return __errorContinuable('missing creation parameters');
  1276. let hitchhiker = {},
  1277. reqParams = {},
  1278. segments = reqData['segments'],
  1279. src = reqData['src'],
  1280. dest = reqData['dest'],
  1281. pos = reqData['pos'];
  1282. if (src != undefined &&
  1283. dest != undefined) {
  1284. let src_asuri = self.__csuri_to_asuri(src);
  1285. if (src_asuri['$err'])
  1286. return __errorContinuable(src_asuri['$err']);
  1287. let dest_asuri = self.__csuri_to_asuri(dest);
  1288. if (dest_asuri['$err'])
  1289. return __errorContinuable(dest_asuri['$err']);
  1290. if (segments == undefined) {
  1291. segments = self.__defaultSegments(src, dest);
  1292. }
  1293. if (pos == undefined) {
  1294. pos = self.__nodesCenter([src_asuri, dest_asuri]);
  1295. }
  1296. hitchhiker = {
  1297. 'segments': segments,
  1298. 'src': src,
  1299. 'dest': dest
  1300. };
  1301. reqParams = {
  1302. 'src': src_asuri,
  1303. 'dest': dest_asuri
  1304. };
  1305. }
  1306. if (pos == undefined)
  1307. return __errorContinuable('missing position');
  1308. hitchhiker['pos'] = pos;
  1309. reqParams['attrs'] =
  1310. self.__runParser(
  1311. parser,
  1312. {
  1313. 'position': pos,
  1314. 'orientation': 0,
  1315. 'scale': [1, 1]
  1316. },
  1317. {});
  1318. return __successContinuable(
  1319. _utils.mergeDicts(
  1320. [{'hitchhiker': hitchhiker}, reqParams]));
  1321. },
  1322. function (asreqData) {
  1323. return __wHttpReq(
  1324. 'POST',
  1325. asuri + '?wid=' + self.__aswid,
  1326. asreqData);
  1327. }];
  1328. _do.chain(actions)(
  1329. function (res) {
  1330. __postMessage({'statusCode': 202, 'respIndex': resp, 'reason': res});
  1331. },
  1332. function (err) {
  1333. __postInternalErrorMsg(resp, err);
  1334. }
  1335. );
  1336. },
  1337. /* return an AS instance, and optionally also the associated CS instance
  1338. 1. setup sync/async action chaining
  1339. a) get AS instance uri for specified CS instance
  1340. b) ask asworker for AS instance
  1341. 2. launch chain
  1342. ... on success, 'return' instance possibly bundling CS instance
  1343. ... on error, 'return' error */
  1344. 'GET *.instance' :
  1345. function(resp,uri,reqData/*[full]*/)
  1346. {
  1347. var self = this,
  1348. actions =
  1349. [__successContinuable(),
  1350. function()
  1351. {
  1352. if( (asuri = self.__csuri_to_asuri(uri))['$err'] )
  1353. return __errorContinuable(asuri['$err']);
  1354. return __successContinuable(asuri);
  1355. },
  1356. function(asuri)
  1357. {
  1358. return __wHttpReq('GET',asuri+'?wid='+self.__aswid);
  1359. }];
  1360. _do.chain(actions)(
  1361. function(respData)
  1362. {
  1363. if( reqData && 'full' in reqData )
  1364. var data = {'cs':_mmmk.read(__uri_to_id(uri)),
  1365. 'as':respData['data']};
  1366. else
  1367. var data = respData['data'];
  1368. __postMessage(
  1369. {'statusCode':200,
  1370. 'data':data,
  1371. 'sequence#':respData['sequence#'],
  1372. 'respIndex':resp});
  1373. },
  1374. function(err) {__postInternalErrorMsg(resp,err);}
  1375. );
  1376. },
  1377. /* INTENT :
  1378. update an AS instance's attributes
  1379. IN PRACTICE:
  1380. adjust uri and forward to asworker
  1381. 1. setup sync/async action chaining
  1382. a) determine associated AS instance uri
  1383. b) ask asworker to update it
  1384. 2. launch chain... return success code or error */
  1385. 'PUT *.instance' :
  1386. function(resp,uri,reqData)
  1387. {
  1388. var self = this,
  1389. actions =
  1390. [__successContinuable(),
  1391. function()
  1392. {
  1393. if( (asuri = self.__csuri_to_asuri(uri))['$err'] )
  1394. return __errorContinuable(asuri['$err']);
  1395. return __successContinuable(asuri);
  1396. },
  1397. function(asuri)
  1398. {
  1399. return __wHttpReq(
  1400. 'PUT',
  1401. asuri+'?wid='+self.__aswid,
  1402. reqData);
  1403. }];
  1404. _do.chain(actions)(
  1405. function(asnode)
  1406. {
  1407. __postMessage(
  1408. {'statusCode':202,
  1409. 'respIndex':resp});
  1410. },
  1411. function(err) {__postInternalErrorMsg(resp,err);}
  1412. );
  1413. },
  1414. 'POST *.instance.click' :
  1415. function(resp,uri,reqData)
  1416. {
  1417. /* TBA
  1418. REST requests vs. code run on ASw
  1419. + checkout bak/ for _mmmk.handleVisualObjectClick */
  1420. },
  1421. /* INTENT :
  1422. delete an instance
  1423. IN PRACTICE:
  1424. adjust uri and forward to asworker
  1425. 1. setup sync/async action chaining
  1426. a) determine associated AS instance uri
  1427. b) ask asworker to delete it
  1428. 2. launch chain... return success code
  1429. NOTE:: this function does not return csworker errors to the client (i.e.,
  1430. errors triggered by __csuri_to_asuri() failures)... the reason for
  1431. this is that mmmk sometimes cascades deletes (e.g., deleting a
  1432. node alsos deletes any connected links) which often causes errors
  1433. during 'mass' deletions, e.g.,
  1434. a) client requests
  1435. delete A
  1436. delete A->B
  1437. delete B
  1438. b) csworker handles delete A (deletes A and A->B)
  1439. c) csworker handles delete A->B (error, A->B already deleted)
  1440. ... */
  1441. 'DELETE *.instance' :
  1442. function(resp,uri)
  1443. {
  1444. var self = this,
  1445. asuri = this.__csuri_to_asuri(uri),
  1446. actions =
  1447. [__wHttpReq('DELETE',asuri+'?wid='+self.__aswid)];
  1448. if( asuri['$err'] )
  1449. __postMessage({'statusCode':200, 'respIndex':resp});
  1450. else
  1451. _do.chain(actions)(
  1452. function()
  1453. {
  1454. __postMessage({'statusCode':202, 'respIndex':resp});
  1455. },
  1456. function(err)
  1457. {
  1458. __postInternalErrorMsg(resp,err);
  1459. }
  1460. );
  1461. },
  1462. /* INTENT :
  1463. update a CS instance's attributes (position,...)
  1464. IN PRACTICE :
  1465. perform intent *or* possibly adjust uri and reqData and forward to
  1466. asworker
  1467. 1. retrieve the icon's parser
  1468. 2. execute parser to determine AS impacts
  1469. 3. if parsing produced an error, return it
  1470. 3. if there are AS impacts, simulate an edit-via-dialog update by
  1471. 'forwarding' the request (with modified reqData) to 'PUT *.instance'
  1472. *and* bundle requested CS update (for later handling)
  1473. 3. if there are no AS impacts, perform requested CS update and post
  1474. changelog */
  1475. 'PUT *.cs' :
  1476. function(resp,uri,reqData)
  1477. {
  1478. var id = __uri_to_id(uri),
  1479. icon = _utils.jsonp( _mmmk.read(id) ),
  1480. updd = this.__runParser(
  1481. icon['parser']['value'],
  1482. reqData['changes'],
  1483. icon );
  1484. if( updd && updd['$err'] )
  1485. return __postInternalErrorMsg(resp,updd['$err']);
  1486. if( updd )
  1487. this['PUT *.instance'](
  1488. resp,
  1489. uri,
  1490. {'changes':updd,
  1491. 'hitchhiker':{'cschanges':reqData['changes']}} );
  1492. else
  1493. {
  1494. var sn = __sequenceNumber();
  1495. this.__checkpointUserOperation(sn);
  1496. __postMessage(
  1497. {'statusCode':200,
  1498. 'changelog':
  1499. _mmmk.update(id,reqData['changes'])['changelog'].
  1500. concat('$segments' in reqData['changes'] ?
  1501. this.__positionLinkDecorators(id) :
  1502. []),
  1503. 'sequence#':sn,
  1504. 'respIndex':resp});
  1505. }
  1506. },
  1507. /* INTENT :
  1508. update a VisualObject's attributes
  1509. IN PRACTICE:
  1510. perform intent *or* possibly adjust uri and reqData and forward to
  1511. asworker
  1512. 1. parse + validate parameters
  1513. 2. retrieve VisualObject and its parsing function
  1514. 3. execute parsing function to determine AS impacts
  1515. 4. if parsing produced an error, return it
  1516. 4. if there are AS impacts, simulate an edit-via-dialog update by
  1517. 'forwarding' the request (with modified reqData) to 'PUT *.instance'
  1518. *and* bundle requested CS update (for later handling)
  1519. 4. if there are no AS impacts, perform the requested VisualObject update
  1520. and post changelog */
  1521. 'PUT *.vobject' :
  1522. function(resp,uri,reqData)
  1523. {
  1524. var matches = uri.match(/.*\/(.*)\.instance\/(.*)\.vobject/);
  1525. if( ! matches )
  1526. return __postBadReqErrorMsg(
  1527. resp,
  1528. 'bad uri for VisualObject :: '+uri);
  1529. var id = matches[1],
  1530. vid = matches[2],
  1531. vobjAttr = '$contents/value/nodes/'+vid;
  1532. if( (res = _mmmk.read(id,'$contents'))['$err'] )
  1533. return __postBadReqErrorMsg(resp,res['$err']);
  1534. var __vo__ = res['nodes'][vid],
  1535. parsingf = __vo__['parser']['value'],
  1536. updd = this.__runParser(
  1537. parsingf,
  1538. reqData['changes'],
  1539. __vo__ );
  1540. if( updd && updd['$err'] )
  1541. return __postInternalErrorMsg(resp,updd['$err']);
  1542. var _reqData = {};
  1543. for( var attr in reqData['changes'] )
  1544. _reqData[vobjAttr+'/'+attr] = reqData['changes'][attr];
  1545. if( updd )
  1546. this['PUT *.instance'](
  1547. resp,
  1548. uri,
  1549. {'changes':updd,
  1550. 'hitchhiker':{'cschanges':_reqData}} );
  1551. else
  1552. {
  1553. var sn = __sequenceNumber();
  1554. this.__checkpointUserOperation(sn);
  1555. __postMessage(
  1556. {'statusCode':200,
  1557. 'changelog':_mmmk.update(id,_reqData)['changelog'],
  1558. 'sequence#':sn,
  1559. 'respIndex':resp});
  1560. }
  1561. },
  1562. /* INTENT :
  1563. A) generate a metamodel from the current AS model and write it to disk
  1564. OR
  1565. B) generate a CS metamodel (i.e., an icon definition metamodel) from
  1566. the current AS and CS models and write it to disk
  1567. OR
  1568. C) generate pattern metamodels from AS and CS metamodels
  1569. IN PRACTICE:
  1570. A) adjust uri and forward to asworker
  1571. OR
  1572. B) adjust uri and forward to asworker + bundle CS model
  1573. OR
  1574. C) do the deed
  1575. C) if the uri is a pattern metamodel uri,
  1576. 1. setup sync/async action chaining
  1577. a) read AS metamodel + store contents
  1578. b) read contents of parent dir
  1579. 2. launch chain... on error, return error... on success,
  1580. a) setup another sync/async action chaining
  1581. i. read all CS metamodels + store contents
  1582. ii. call _mt.ramify() with AS and CS metamodel data
  1583. iii. write results to disk
  1584. b) launch chain... return success or error code
  1585. B) if the uri is an icon definition metamodel uri,
  1586. 1. setup sync/async action chaining
  1587. a) ask asworker to generate metamodel and write it to disk... asworker
  1588. request reqData includes the current CS model
  1589. 2. launch chain... return success code or error
  1590. A) otherwise,
  1591. 1. setup sync/async action chaining
  1592. a) ask asworker to generate metamodel and write it to disk
  1593. 2. launch chain... return success code or error */
  1594. 'PUT *.metamodel' :
  1595. function(resp,uri)
  1596. {
  1597. if( (matches = uri.match(/\.pattern\.metamodel/)) )
  1598. {
  1599. var matches = uri.match(/\/GET(((.*\/).*).pattern.metamodel)/),
  1600. RAMasmmPath = './users'+matches[1],
  1601. asmmPath = './users'+matches[2]+'.metamodel',
  1602. parentDir = './users'+matches[3],
  1603. asmm = undefined,
  1604. csmms = {},
  1605. actions =
  1606. [_fs.readFile(asmmPath,'utf8'),
  1607. function(data)
  1608. {
  1609. asmm = data;
  1610. return _fs.readdir(parentDir);
  1611. }];
  1612. _do.chain(actions)(
  1613. function(files)
  1614. {
  1615. files =
  1616. files.filter(
  1617. function(f)
  1618. {return f.match(/\..*Icons\.metamodel/);});
  1619. var ramActions = [__successContinuable()];
  1620. files.forEach(
  1621. function(f)
  1622. {
  1623. ramActions.push(
  1624. function()
  1625. {
  1626. return _fs.readFile(parentDir+f,'utf8');
  1627. },
  1628. function(data)
  1629. {
  1630. csmms[f] = data;
  1631. return __successContinuable();
  1632. });
  1633. });
  1634. ramActions.push(
  1635. function()
  1636. {
  1637. var res = _mt.ramify(_utils.jsonp(asmm),csmms);
  1638. asmm = res['asmm'];
  1639. csmms = res['csmms'];
  1640. return _fs.writeFile(
  1641. RAMasmmPath,
  1642. _utils.jsons(asmm,null,'\t'));
  1643. });
  1644. files.forEach(
  1645. function(f)
  1646. {
  1647. ramActions.push(
  1648. function()
  1649. {
  1650. var RAMf = parentDir +
  1651. f.match(/(.*)\.metamodel/)[1] +
  1652. '.pattern.metamodel';
  1653. return _fs.writeFile(
  1654. RAMf,
  1655. _utils.jsons(csmms[f],null,'\t'));
  1656. });
  1657. });
  1658. _do.chain(ramActions)(
  1659. function()
  1660. {
  1661. __postMessage({'statusCode':200,'respIndex':resp});
  1662. },
  1663. function(err) {__postInternalErrorMsg(resp,err);}
  1664. );
  1665. },
  1666. function(err) {__postInternalErrorMsg(resp,err);}
  1667. );
  1668. }
  1669. else
  1670. {
  1671. var matches = uri.match(/\/GET(((.*\/).*)\..*Icons.metamodel)/),
  1672. asmmPath = (matches ? ('./users'+matches[2]+'.metamodel') : undefined),
  1673. asmm = undefined;
  1674. aswid = this.__aswid;
  1675. actions = [];
  1676. if (asmmPath) {
  1677. actions = [
  1678. _fs.readFile(asmmPath,'utf8'),
  1679. function(data) {
  1680. asmm = _utils.jsonp(data);
  1681. return __successContinuable();
  1682. },
  1683. function(result) {
  1684. return __wHttpReq('PUT',
  1685. uri+'?wid='+aswid,
  1686. ({'csm':_mmmk.read(), 'asmm': asmm}));
  1687. }];
  1688. } else {
  1689. actions = [__wHttpReq('PUT',
  1690. uri+'?wid='+aswid,
  1691. undefined)];
  1692. }
  1693. _do.chain(actions)(
  1694. function() { __postMessage({'statusCode':200,'respIndex':resp}); },
  1695. function(err) {__postInternalErrorMsg(resp,err);}
  1696. );
  1697. }
  1698. },
  1699. /* write a bundle containing this CS model and its associated AS model to
  1700. disk
  1701. 1. setup sync/async action chaining
  1702. a) ask asworker for current model (AS)
  1703. 2. launch chain... on error, return error... on success, compare sequence#
  1704. of returned AS model with next expected sequence# from asworker
  1705. a) if AS model is too old, restart
  1706. b) if AS model is too recent, wait 200 ms and restart
  1707. c) otherwise,
  1708. i. extract path info
  1709. ii. return error on invalid path
  1710. iii. ask _mmmk for current model (CS)
  1711. iv. setup another sync/async action chaining
  1712. a) write bundled AS and CS models to disk
  1713. v. launch chain... return success or error code */
  1714. 'PUT *.model' :
  1715. function(resp,uri)
  1716. {
  1717. var self = this,
  1718. actions = [__wHttpReq('GET','/current.model?wid='+this.__aswid)];
  1719. _do.chain(actions)(
  1720. function(asdata)
  1721. {
  1722. var sn = asdata['sequence#'];
  1723. if( self.__nextASWSequenceNumber - 1 > sn )
  1724. self['PUT *.model'](resp,uri);
  1725. else if( self.__nextASWSequenceNumber - 1 < sn )
  1726. setTimeout(self['PUT *.model'], 200, resp, uri);
  1727. else
  1728. {
  1729. if( (res = _mmmk.read())['$err'] )
  1730. __postInternalErrorMsg(resp,res['$err']);
  1731. else
  1732. {
  1733. var path = './users'+uri.substring('/GET'.length),
  1734. dir = _path.dirname(path);
  1735. if( dir.match(/"/) )
  1736. throw 'illegal filename... these characters are not'+
  1737. ' allowed in filenames :: "';
  1738. var mData = {
  1739. 'csm':_utils.jsonp(res),
  1740. 'asm':_utils.jsonp(asdata['data'])},
  1741. writeActions =
  1742. [_fspp.mkdirs(dir),
  1743. function()
  1744. {
  1745. return _fs.writeFile(
  1746. path,
  1747. _utils.jsons(mData,null,'\t'));
  1748. }];
  1749. _do.chain(writeActions)(
  1750. function()
  1751. {
  1752. __postMessage(
  1753. {'statusCode':200,
  1754. 'respIndex':resp});
  1755. },
  1756. function(writeErr)
  1757. {
  1758. __postInternalErrorMsg(resp,writeErr);
  1759. }
  1760. );
  1761. }
  1762. }
  1763. },
  1764. function(err) {__postInternalErrorMsg(resp,err);}
  1765. );
  1766. },
  1767. /* INTENT :
  1768. validate the associated AS model
  1769. IN PRACTICE:
  1770. adjust uri and forward to asworker
  1771. 1. setup sync/async action chaining
  1772. a) ask asworker to validate its model
  1773. 2. launch chain... return success code or error */
  1774. 'GET /validatem' :
  1775. function(resp,uri)
  1776. {
  1777. var actions = [__wHttpReq('GET',uri+'?wid='+this.__aswid)];
  1778. _do.chain(actions)(
  1779. function()
  1780. {
  1781. __postMessage({'statusCode':200, 'respIndex':resp});
  1782. },
  1783. function(err) {__postInternalErrorMsg(resp,err);}
  1784. );
  1785. },
  1786. /* INTENT :
  1787. undo/redo the effects of a client's last not yet undone/redone action
  1788. IN PRACTICE:
  1789. adjust uri and forward asworker undo/redos to asworker, and handle
  1790. csworker undo/redos locally
  1791. 1. if next __handledSeqNums is a non-csworker sequence#,
  1792. a) if it's a batchEdit marker:
  1793. i. adjust __handledSeqNums['i'] to account for multiple undos/redos
  1794. ii. populate reqData['redo/undoUntil'] with batchEdit marker s.t.
  1795. asworker knows to undo/redo everything til specified marker
  1796. iii. populate hitchhiker['redo/undo'] with batchEdit marker s.t. when
  1797. asworker undo/redo changelog comes around, csworkers know until
  1798. where they should undo/redo
  1799. iv. forward request to asworker and return success code or error
  1800. a) if it's an asworker marker:
  1801. i. populate hitchhiker['redo/undo'] with asworker marker s.t. when
  1802. asworker undo/redo changelog comes around, csworkers know until
  1803. where they should undo/redo
  1804. ii. forward request to asworker and return success code or error */
  1805. 'POST /undo' :
  1806. function(resp,uri,reqData/*[undoUntil]*/)
  1807. {
  1808. if( this.__handledSeqNums['i'] == undefined )
  1809. this.__handledSeqNums['i'] = this.__handledSeqNums['#s'].length-1;
  1810. if( this.__handledSeqNums['#s'][this.__handledSeqNums['i']] )
  1811. this.__undoredo(
  1812. resp,
  1813. uri,
  1814. (reqData != undefined && 'undoUntil' in reqData ?
  1815. reqData['undoUntil'] :
  1816. this.__handledSeqNums['#s'][this.__handledSeqNums['i']--]),
  1817. 'undo');
  1818. else
  1819. __postMessage({'statusCode':200, 'respIndex':resp});
  1820. },
  1821. 'POST /redo' :
  1822. function(resp,uri)
  1823. {
  1824. if( this.__handledSeqNums['i'] == undefined )
  1825. this.__handledSeqNums['i'] = this.__handledSeqNums['#s'].length-1;
  1826. if( this.__handledSeqNums['#s'][this.__handledSeqNums['i']+1] )
  1827. this.__undoredo(
  1828. resp,
  1829. uri,
  1830. this.__handledSeqNums['#s'][++this.__handledSeqNums['i']],
  1831. 'redo');
  1832. else
  1833. __postMessage({'statusCode':200, 'respIndex':resp});
  1834. },
  1835. '__undoredo' :
  1836. function(resp,uri,sn,func)
  1837. {
  1838. if( ! sn.match(__wtype) )
  1839. {
  1840. var hitchhiker = {},
  1841. reqData = {'hitchhiker':hitchhiker},
  1842. actions = [__wHttpReq(
  1843. 'POST',
  1844. uri+'?wid='+this.__aswid,
  1845. reqData) ];
  1846. if( (matches = sn.match(/^bchkpt@([0-9]+)/)) )
  1847. {
  1848. if( func == 'undo' )
  1849. {
  1850. for( ;
  1851. ! this.__handledSeqNums['#s'][this.__handledSeqNums['i']].
  1852. match('^bchkpt@'+matches[1]);
  1853. this.__handledSeqNums['i']-- )
  1854. ;
  1855. this.__handledSeqNums['i']--;
  1856. reqData[func+'Until'] = hitchhiker[func] =
  1857. __batchCheckpoint(matches[1],true);
  1858. }
  1859. else
  1860. {
  1861. this.__handledSeqNums['i']++;
  1862. for( ;
  1863. ! this.__handledSeqNums['#s'][this.__handledSeqNums['i']].
  1864. match('^bchkpt@'+matches[1]);
  1865. this.__handledSeqNums['i']++ )
  1866. ;
  1867. reqData[func+'Until'] = hitchhiker[func] =
  1868. __batchCheckpoint(matches[1]);
  1869. }
  1870. }
  1871. else
  1872. hitchhiker[func] = sn;
  1873. _do.chain( actions )(
  1874. function()
  1875. {
  1876. __postMessage({'statusCode':202, 'respIndex':resp});
  1877. },
  1878. function(err) {__postInternalErrorMsg(resp,err);}
  1879. );
  1880. }
  1881. else
  1882. __postMessage(
  1883. {'statusCode':200,
  1884. 'changelog':_mmmk[func](sn)['changelog'],
  1885. 'sequence#':__sequenceNumber(),
  1886. 'respIndex':resp});
  1887. },
  1888. /* INTENT :
  1889. place an easily identifiable user-checkpoint in the journal
  1890. IN PRACTICE:
  1891. adjust uri and forward to asworker
  1892. 1. setup sync/async action chaining
  1893. a) forward request to asworker
  1894. 2. launch chain... return success code or error */
  1895. 'POST /batchCheckpoint' :
  1896. function(resp,uri,reqData)
  1897. {
  1898. var actions = [
  1899. __wHttpReq('POST',uri+'?wid='+this.__aswid,reqData)];
  1900. _do.chain(actions)(
  1901. function()
  1902. {
  1903. __postMessage({'statusCode':202, 'respIndex':resp});
  1904. },
  1905. function(err) {__postInternalErrorMsg(resp,err);}
  1906. );
  1907. },
  1908. /********************************** UTILS **********************************/
  1909. /* wrapper around reads to '__asid2csid'... needed because loading a model
  1910. (as opposed to creating it from scratch) doesn't populate this data
  1911. structure... this wrapper enables its lazy and transparent population as
  1912. read queries are made */
  1913. '__asid_to_csid' :
  1914. function(asid)
  1915. {
  1916. if( this.__asid2csid[asid] != undefined )
  1917. return this.__asid2csid[asid];
  1918. var csm = _utils.jsonp(_mmmk.read());
  1919. for( var csid in csm.nodes )
  1920. if( __uri_to_id(_mmmk.read(csid,'$asuri')) == asid )
  1921. return (this.__asid2csid[asid] = csid);
  1922. },
  1923. /* return a fullcstype from a fullastype */
  1924. '__astype_to_cstype' :
  1925. function(fullastype,isLink)
  1926. {
  1927. var matches = fullastype.match(/(.*)\/(.*)/),
  1928. asmm = matches[1],
  1929. astype = matches[2];
  1930. return this.__asmm2csmm[asmm]+'/'+astype+(isLink ? 'Link' : 'Icon');
  1931. },
  1932. /* return the CS instance uri associated to the AS instance described by the
  1933. given uri */
  1934. '__asuri_to_csuri' :
  1935. function(uri)
  1936. {
  1937. if( (asid = __uri_to_id(uri))['$err'] )
  1938. return asid;
  1939. return __id_to_uri(this.__asid_to_csid(asid));
  1940. },
  1941. /* add a checkpointing marker in mmmk and log the said marker as a
  1942. non-undo/redo operation (remove any undone operations from log first) */
  1943. '__checkpointUserOperation' :
  1944. function(sn)
  1945. {
  1946. _mmmk.setUserCheckpoint(sn);
  1947. if( this.__handledSeqNums['i'] != undefined )
  1948. {
  1949. this.__handledSeqNums['#s'].splice( this.__handledSeqNums['i']+1 );
  1950. this.__handledSeqNums['i'] = undefined;
  1951. }
  1952. this.__handledSeqNums['#s'].push(sn);
  1953. },
  1954. /* produce a bundle of internal state variables sufficient to fully clone
  1955. this instance
  1956. OR
  1957. use a provided bundle to overwrite this instance's internal state */
  1958. '__clone' :
  1959. function(clone)
  1960. {
  1961. if( clone )
  1962. {
  1963. this.__asmm2csmm = clone.__asmm2csmm;
  1964. this.__asid2csid = clone.__asid2csid;
  1965. this.__aswid = clone.__aswid;
  1966. this.__handledSeqNums = clone.__handledSeqNums;
  1967. this.__nextASWSequenceNumber = clone.__nextASWSequenceNumber;
  1968. this.__pendingChangelogs = clone.__pendingChangelogs;
  1969. this.__hitchhikerJournal = clone.__hitchhikerJournal;
  1970. }
  1971. else
  1972. return _utils.clone(
  1973. {'__asmm2csmm': this.__asmm2csmm,
  1974. '__asid2csid': this.__asid2csid,
  1975. '__aswid': this.__aswid,
  1976. '__handledSeqNums': this.__handledSeqNums,
  1977. '__nextASWSequenceNumber': this.__nextASWSequenceNumber,
  1978. '__pendingChangelogs': this.__pendingChangelogs,
  1979. '__hitchhikerJournal': this.__hitchhikerJournal});
  1980. },
  1981. /* return the AS instance uri associated to the CS instance described by the
  1982. given uri */
  1983. '__csuri_to_asuri' :
  1984. function(uri)
  1985. {
  1986. if( (csid = __uri_to_id(uri))['$err'] )
  1987. return csid;
  1988. else if( (asuri = _mmmk.read(csid,'$asuri'))['$err'] )
  1989. return asuri;
  1990. return asuri;
  1991. },
  1992. /* compute 'default' segments between the given icons (specified via uri) */
  1993. '__defaultSegments' :
  1994. function(src,dest)
  1995. {
  1996. var pos1 = _mmmk.read(__uri_to_id(src),'position'),
  1997. pos2 = _mmmk.read(__uri_to_id(dest),'position'),
  1998. middle = [pos2[0]-(pos2[0]-pos1[0])/2.0,
  1999. pos2[1]-(pos2[1]-pos1[1])/2.0];
  2000. return ['M'+pos1+'L'+middle, 'M'+middle+'L'+pos2];
  2001. },
  2002. /* compute the x,y center of the icons given by the specified AS uris */
  2003. '__nodesCenter' :
  2004. function(asuris)
  2005. {
  2006. var sumx = 0,
  2007. sumy = 0,
  2008. self = this;
  2009. asuris.forEach(
  2010. function(asuri)
  2011. {
  2012. var asid = __uri_to_id(asuri),
  2013. csid = self.__asid_to_csid(asid),
  2014. pos = _mmmk.read(csid,'position');
  2015. sumx += parseFloat(pos[0]);
  2016. sumy += parseFloat(pos[1]);
  2017. });
  2018. return [sumx/asuris.length, sumy/asuris.length];
  2019. },
  2020. /* run given code within given contexts
  2021. 1. setup a very stripped down version of _mmmk.__runDesignerCode() with
  2022. getAttr() and safe_eval() (see _mmmk.__runDesignerCode() for more
  2023. elaborate comments)
  2024. 2. safely evaluate code and return result
  2025. NOTE: getAttr() first checks the 'local' context and then the 'global'
  2026. context to find requested attributes... this enables such behaviours
  2027. as accessing 'new' attribute values as opposed to those stored in
  2028. _mmmk */
  2029. '__runParser' :
  2030. function(parser,local,global)
  2031. {
  2032. function getAttr(_attr)
  2033. {
  2034. if( _attr in local )
  2035. return local[_attr];
  2036. else if( !(_attr in global) || _attr.charAt(0) == '$')
  2037. throw 'invalid getAttr() attribute :: '+_attr;
  2038. return _utils.clone(global[_attr]['value']);
  2039. }
  2040. function safe_eval(code)
  2041. {
  2042. try {return eval(code);}
  2043. catch(err) {return {'$err':err};}
  2044. }
  2045. return safe_eval(parser);
  2046. },
  2047. /* returns the numeric part of sequence# of the form 'src#number' */
  2048. '__sn2int' :
  2049. function(sn)
  2050. {
  2051. return parseInt(sn.match(/.*#(\d*)/)[1]);
  2052. }
  2053. };