modelverse.py 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. import urllib
  2. import json
  3. import random
  4. import sys
  5. import time
  6. import threading
  7. import uuid
  8. from sccd.runtime.statecharts_core import Event
  9. COMPILER_PATH = "interface/HUTN"
  10. MODE_UNCONNECTED = 0
  11. MODE_UNAUTHORIZED = 1
  12. MODE_MODELLING = 2
  13. MODE_MODIFY = 3
  14. MODE_DIALOG = 4
  15. MODE_MANUAL = 5
  16. MODE_SERVICE = 6
  17. # Bind to the compiler (might have to update path manually!)
  18. sys.path.append(COMPILER_PATH)
  19. from hutn_compiler.compiler import main as do_compile
  20. import socket2event
  21. # Exceptions
  22. class ModelverseException(Exception):
  23. pass
  24. class UnknownError(ModelverseException):
  25. pass
  26. class UnknownIdentifier(ModelverseException):
  27. pass
  28. class CompilationError(ModelverseException):
  29. pass
  30. class NoSuchAttribute(ModelverseException):
  31. pass
  32. class UnknownModel(ModelverseException):
  33. pass
  34. class ConnectionError(ModelverseException):
  35. pass
  36. class ModelExists(ModelverseException):
  37. pass
  38. class PermissionDenied(ModelverseException):
  39. pass
  40. class InvalidMode(ModelverseException):
  41. pass
  42. class InterfaceMismatch(ModelverseException):
  43. pass
  44. class UnknownMetamodellingHierarchy(ModelverseException):
  45. pass
  46. # Helper functions and configuration: do not use yourself!
  47. taskname = None
  48. mode = MODE_UNCONNECTED
  49. prev_mode = None
  50. current_model = None
  51. registered_metamodels = {}
  52. outputs = {}
  53. ctrl_input = None
  54. ctrl_output = None
  55. def _output_thread(controller, outputs, taskname):
  56. req_out = controller.addOutputListener("request_out")
  57. my_id = str(uuid.uuid4())
  58. try:
  59. while 1:
  60. controller.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "get_output", "taskname": taskname}), my_id]))
  61. event = req_out.fetch(-1)
  62. if event.parameters[1] == my_id:
  63. outputs[taskname].append(json.loads(event.parameters[0]))
  64. except:
  65. pass
  66. def _exec_on_statechart(statechart):
  67. def _exec_sc(controller, inport, outport):
  68. global taskname
  69. op = controller.addOutputListener(outport)
  70. while 1:
  71. if len(outputs[taskname]) > 1:
  72. # Is an output message of the Mv, so put it in the SC
  73. del outputs[taskname][0]
  74. output_event = outputs[taskname][0]
  75. if output_event == "Success" or output_event == "Failure":
  76. # Is a stop event!
  77. controller.addInput(Event("terminate", inport, []))
  78. break
  79. else:
  80. # Is just a normal event!
  81. controller.addInput(Event("input", inport, [output_event]))
  82. input_event = op.fetch(0)
  83. if input_event is not None:
  84. # Expand the event and make it HTTP input
  85. _input(input_event.parameters)
  86. time.sleep(0.02)
  87. thrd = threading.Thread(target=_exec_sc, args=statechart)
  88. thrd.daemon = True
  89. thrd.start()
  90. return None
  91. def _get_metamodel(model):
  92. global registered_metamodels
  93. try:
  94. return registered_metamodels[model]
  95. except KeyError:
  96. raise UnknownMetamodellingHierarchy(model)
  97. def _check_type(value):
  98. if not isinstance(value, (int, long, float, str, unicode, bool)):
  99. raise UnsupportedValue("%s : %s" % (value, str(type(value))))
  100. def _check_type_list(value):
  101. if isinstance(value, list):
  102. [_check_type(i) for i in value]
  103. else:
  104. _check_type(value)
  105. def _dict_to_list(python_dict):
  106. lst = []
  107. for k, v in python_dict.items():
  108. lst += [k, v]
  109. return lst
  110. def _goto_mode(new_mode, model_name=None):
  111. global mode
  112. if mode == MODE_MANUAL and new_mode == MODE_MODIFY:
  113. if model_name != None and current_model != model_name:
  114. raise InvalidMode("Mode error: cannot modify other models!")
  115. else:
  116. return
  117. elif mode == MODE_MODELLING and new_mode == MODE_MODIFY:
  118. # Are in root view, but want to modify a model
  119. _model_modify(model_name, _get_metamodel(model_name))
  120. elif mode == MODE_MODIFY and new_mode == MODE_MODIFY and model_name != None and current_model != model_name:
  121. # Are in modify mode, but want to modify a different model
  122. mm = _get_metamodel(model_name)
  123. _model_exit()
  124. _model_modify(model_name, mm)
  125. elif mode == MODE_MODIFY and new_mode == MODE_MODELLING:
  126. _model_exit()
  127. elif mode == new_mode:
  128. return
  129. else:
  130. # Go to a mode that we have no automatic transfer to: raise exception
  131. raise InvalidMode("Required mode: %s, current mode: %s" % (new_mode, mode))
  132. def _input(value, port=None):
  133. # Ugly json encoding of primitives
  134. #print("[IN] %s" % value)
  135. if port is None:
  136. port = taskname
  137. if isinstance(value, type([])):
  138. value = json.dumps(value)
  139. ctrl_input.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "set_input", "taskname": port, "data": value}), None]))
  140. else:
  141. value = json.dumps(value)
  142. ctrl_input.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "set_input", "taskname": port, "value": value}), None]))
  143. def _input_raw(value, taskname):
  144. # Ugly json encoding of primitives
  145. ctrl_input.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "set_input", "taskname": taskname, "value": value}), None]))
  146. def _compile_AL(code):
  147. # Compile an action language file and send the compiled code
  148. code_fragments = code.split("\n")
  149. code_fragments = [i for i in code_fragments if i.strip() != ""]
  150. code_fragments = [i.replace(" ", "\t") for i in code_fragments]
  151. initial_tabs = min([len(i) - len(i.lstrip("\t")) for i in code_fragments])
  152. code_fragments = [i[initial_tabs:] for i in code_fragments]
  153. code_fragments.append("")
  154. code = "\n".join(code_fragments)
  155. with open(".code.alc", "w") as f:
  156. f.write(code)
  157. f.flush()
  158. compiled = do_compile(".code.alc", COMPILER_PATH + "/grammars/actionlanguage.g", "CS")
  159. return compiled
  160. def _compile_model(code):
  161. # Compile a model and send the compiled graph
  162. # First change multiple spaces to a tab
  163. code_fragments = code.split("\n")
  164. code_fragments = [i for i in code_fragments if i.strip() != ""]
  165. code_fragments = [i.replace(" ", "\t") for i in code_fragments]
  166. initial_tabs = min([len(i) - len(i.lstrip("\t")) for i in code_fragments])
  167. code_fragments = [i[initial_tabs:] for i in code_fragments]
  168. code_fragments.append("")
  169. code = "\n".join(code_fragments)
  170. with open(".model.mvc", "w") as f:
  171. f.write(code)
  172. f.flush()
  173. return do_compile(".model.mvc", COMPILER_PATH + "/grammars/modelling.g", "M")
  174. def _output(expected=None, port=None):
  175. if port is None:
  176. port = taskname
  177. try:
  178. while len(outputs[port]) < 2:
  179. time.sleep(0.02)
  180. del outputs[port][0]
  181. #print("[OUT] %s" % outputs[0])
  182. except:
  183. raise UnknownError()
  184. if expected is not None and _last_output(port) != expected:
  185. raise InterfaceMismatch(_last_output(port), expected)
  186. return _last_output(port)
  187. def _last_output(port=None):
  188. if port is None:
  189. port = taskname
  190. return outputs[port][0]
  191. # Raise common exceptions
  192. def _handle_output(requested=None, split=False):
  193. value = _output()
  194. if value.startswith("Model exists: "):
  195. raise ModelExists(value.split(": ", 1)[1])
  196. elif value.startswith("Permission denied"):
  197. raise PermissionDenied(value.split(": ", 1)[1])
  198. elif value.startswith("Model not found: "):
  199. raise UnknownModel(value.split(": ", 1)[1])
  200. elif value.startswith("Element not found: "):
  201. raise UnknownIdentifier(value.split(": ", 1)[1])
  202. elif value.startswith("Element exists: "):
  203. raise ElementExists(value.split(": ", 1)[1])
  204. elif value.startswith("Attribute not found: "):
  205. raise NoSuchAttribute(value.split(": ", 1)[1])
  206. elif requested is not None and value.startswith(requested):
  207. if not split:
  208. return value
  209. else:
  210. splitted = value.strip().split(" ", 1)
  211. if len(splitted) > 1:
  212. return splitted[1].split("\n")
  213. else:
  214. return []
  215. else:
  216. raise InterfaceMismatch(value)
  217. def _model_modify(model_name, metamodel_name):
  218. """Modify an existing model."""
  219. global mode
  220. global prev_mode
  221. if mode == MODE_MANUAL:
  222. prev_mode = MODE_MANUAL
  223. mode = MODE_MODIFY
  224. return None
  225. _goto_mode(MODE_MODELLING)
  226. prev_mode = MODE_MODELLING
  227. _input(["model_modify", model_name, metamodel_name])
  228. _handle_output("Success")
  229. global current_model
  230. current_model = model_name
  231. # Mode has changed
  232. mode = MODE_MODIFY
  233. _output("Model loaded, ready for commands!")
  234. def _model_exit():
  235. """Leave model modify mode."""
  236. global mode
  237. global prev_mode
  238. if prev_mode == MODE_MANUAL:
  239. mode = MODE_MANUAL
  240. return
  241. if mode != MODE_MODIFY:
  242. raise InvalidMode()
  243. _input("exit")
  244. _output("Success")
  245. mode = MODE_MODELLING
  246. def alter_context(model_name, metamodel_name):
  247. global registered_metamodels
  248. registered_metamodels[model_name] = metamodel_name
  249. def _start_http_client(addr, port, timeout):
  250. import http_client
  251. ctrl = http_client.Controller()
  252. listener = ctrl.addOutputListener("request_out")
  253. socket2event.boot_translation_service(ctrl)
  254. thrd = threading.Thread(target=ctrl.start)
  255. thrd.daemon = True
  256. thrd.start()
  257. evt = listener.fetch(-1)
  258. if evt.name != "http_client_initialized":
  259. raise Exception("HTTP client did not behave as expected during init: " + str(evt.name))
  260. ctrl.addInput(Event("connect", "request_in", [(addr, port), timeout]))
  261. evt = listener.fetch(-1)
  262. if evt.name == "http_client_timeout":
  263. raise Exception("HTTP client timeout")
  264. if evt.name != "http_client_ready":
  265. raise Exception("HTTP client did not behave as expected during connect: " + str(evt.name))
  266. return ctrl
  267. # Main MvC operations
  268. def init(address_param="127.0.0.1:8001", timeout=20.0):
  269. """Starts up the connection to the Modelverse."""
  270. # Start up the HTTP Client SC
  271. global ctrl_input
  272. global ctrl_output
  273. if ctrl_input is not None:
  274. ctrl_input.stop()
  275. if ctrl_output is not None:
  276. ctrl_output.stop()
  277. global address
  278. global port
  279. address, port = address_param.split(":", 1)
  280. port = int(port)
  281. ctrl_input = _start_http_client(address, port, timeout)
  282. ctrl_output = _start_http_client(address, port, timeout)
  283. controllers = [ctrl_input, ctrl_output]
  284. global mode
  285. start_time = time.time()
  286. task = random.random()
  287. _input_raw('"%s"' % task, "task_manager")
  288. mode = MODE_UNAUTHORIZED
  289. global outputs
  290. global taskname
  291. taskname = task
  292. outputs = {}
  293. _listen_to_output(ctrl_output, task)
  294. def _listen_to_output(controller, task):
  295. global outputs
  296. outputs[task] = [None]
  297. # This re-assign also diconnects the previous get_output connections to the outputs variable
  298. thrd = threading.Thread(target=_output_thread, args=[controller, outputs, task])
  299. thrd.daemon = True
  300. thrd.start()
  301. def login(username, password):
  302. """Log in a user, if user doesn't exist, it is created."""
  303. global mode
  304. _goto_mode(MODE_UNAUTHORIZED)
  305. _output("Log on as which user?")
  306. _input(username)
  307. if _output() == "Password for existing user?":
  308. _input(password)
  309. if _output() == "Welcome to the Model Management Interface v2.0!":
  310. _output("Use the 'help' command for a list of possible commands")
  311. _input("quiet")
  312. mode = MODE_MODELLING
  313. elif _last_output() == "Wrong password!":
  314. raise PermissionDenied()
  315. else:
  316. raise InterfaceMismatch(_last_output())
  317. elif _last_output() == "This is a new user: please give password!":
  318. _input(password)
  319. _output("Please repeat the password")
  320. _input(password)
  321. if _output() == "Passwords match!":
  322. _output("Welcome to the Model Management Interface v2.0!")
  323. _output("Use the 'help' command for a list of possible commands")
  324. _input("quiet")
  325. mode = MODE_MODELLING
  326. elif _last_output() == "Not the same password!":
  327. # We just sent the same password, so it should be identical, unless the interface changed
  328. raise InterfaceMismatch(_last_output())
  329. else:
  330. raise InterfaceMismatch(_last_output())
  331. else:
  332. raise InterfaceMismatch(_last_output())
  333. def model_add(model_name, metamodel_name, model_code=None):
  334. """Instantiate a new model."""
  335. _goto_mode(MODE_MODELLING)
  336. # Do this before creating the model, as otherwise compilation errors would make us inconsistent
  337. if model_code is not None:
  338. try:
  339. compiled = _compile_model(model_code)
  340. except Exception as e:
  341. raise CompilationError(e)
  342. else:
  343. compiled = [0]
  344. _input(["model_add", metamodel_name, model_name])
  345. _handle_output("Waiting for model constructors...")
  346. _input(compiled)
  347. _output("Success")
  348. global registered_metamodels
  349. registered_metamodels[model_name] = metamodel_name
  350. def upload_code(code):
  351. try:
  352. compiled = _compile_AL(code)
  353. except Exception as e:
  354. raise CompilationError(e)
  355. _input(compiled)
  356. def model_delete(model_name):
  357. """Delete an existing model."""
  358. _goto_mode(MODE_MODELLING)
  359. _input(["model_delete", model_name])
  360. _handle_output("Success")
  361. def model_list(location):
  362. """List all models."""
  363. _goto_mode(MODE_MODELLING)
  364. _input(["model_list", location])
  365. return set(_handle_output("Success: ", split=True))
  366. def model_list_full(location):
  367. """List full information on all models."""
  368. _goto_mode(MODE_MODELLING)
  369. _input(["model_list_full", location])
  370. output = _handle_output("Success: ", split=True)
  371. lst = set([])
  372. for v in output:
  373. m = v.strip()
  374. perm, own, grp, m = m.split(" ", 3)
  375. lst.add((m, own, grp, perm))
  376. return lst
  377. def verify(model_name, metamodel_name=None):
  378. """Verify if a model conforms to its metamodel."""
  379. _goto_mode(MODE_MODELLING)
  380. if metamodel_name is None:
  381. metamodel_name = _get_metamodel(model_name)
  382. _input(["verify", model_name, metamodel_name])
  383. return _handle_output("Success: ", split=True)[0]
  384. def model_overwrite(model_name, new_model=None, metamodel_name=None):
  385. """Upload a new model and overwrite an existing model."""
  386. _goto_mode(MODE_MODIFY, model_name)
  387. if new_model is not None:
  388. try:
  389. compiled = _compile_model(new_model)
  390. except Exception as e:
  391. raise CompilationError(e)
  392. else:
  393. compiled = [0]
  394. _input("upload")
  395. _handle_output("Waiting for model constructors...")
  396. _input(compiled)
  397. _output("Success")
  398. if metamodel_name is not None:
  399. global registered_metamodels
  400. registered_metamodels[model_name] = metamodel_name
  401. def user_logout():
  402. """Log out the current user and break the connection."""
  403. global mode
  404. _goto_mode(MODE_MODELLING)
  405. _input("exit")
  406. mode = MODE_UNCONNECTED
  407. def user_delete():
  408. """Removes the current user and break the connection."""
  409. global mode
  410. _goto_mode(MODE_MODELLING)
  411. _input("self-destruct")
  412. mode = MODE_UNCONNECTED
  413. def model_render(model_name, mapper_name):
  414. """Fetch a rendered verion of a model."""
  415. _goto_mode(MODE_MODELLING)
  416. _input(["model_render", model_name, mapper_name])
  417. return json.loads(_handle_output("Success: ", split=True)[0])
  418. def transformation_between(source, target):
  419. _goto_mode(MODE_MODELLING)
  420. _input(["transformation_between", source, target])
  421. output = _handle_output("Success: ", split=True)
  422. return set(output)
  423. def transformation_add_MT(source_metamodels, target_metamodels, operation_name, code, callback=lambda: None):
  424. """Create a new model transformation."""
  425. global mode
  426. _goto_mode(MODE_MODELLING)
  427. import time
  428. start = time.time()
  429. try:
  430. compiled = _compile_model(code)
  431. except Exception as e:
  432. raise CompilationError(e)
  433. #print("Compilation took: %ss" % (time.time() - start))
  434. start = time.time()
  435. mv_dict_rep = _dict_to_list(source_metamodels) + [""] + _dict_to_list(target_metamodels) + [""]
  436. _input(["transformation_add_MT"] + mv_dict_rep + [operation_name])
  437. # Possibly modify the merged metamodel first (add tracability links)
  438. if len(source_metamodels) + len(target_metamodels) > 0:
  439. mode = MODE_MANUAL
  440. _output("Model loaded, ready for commands!")
  441. callback()
  442. _input("exit")
  443. mode = MODE_MODELLING
  444. #print("Callbacks took: %ss" % (time.time() - start))
  445. start = time.time()
  446. # Done, so RAMify and upload the model
  447. _handle_output("Waiting for model constructors...")
  448. _input(compiled)
  449. _handle_output("Success")
  450. #print("Upload and RAMify took: %ss" % (time.time() - start))
  451. def transformation_add_AL(source_metamodels, target_metamodels, operation_name, code, callback=lambda: None):
  452. """Create a new action language model, which can be executed."""
  453. global mode
  454. _goto_mode(MODE_MODELLING)
  455. try:
  456. compiled = _compile_AL(code)
  457. except Exception as e:
  458. raise CompilationError(e)
  459. mv_dict_rep = _dict_to_list(source_metamodels) + [""] + _dict_to_list(target_metamodels) + [""]
  460. _input(["transformation_add_AL"] + mv_dict_rep + [operation_name])
  461. # Possibly modify the merged metamodel first (add tracability links)
  462. if len(source_metamodels) + len(target_metamodels) > 0:
  463. mode = MODE_MANUAL
  464. _output("Model loaded, ready for commands!")
  465. callback()
  466. _input("exit")
  467. mode = MODE_MODELLING
  468. _handle_output("Waiting for code constructors...")
  469. _input(compiled)
  470. _output("Success")
  471. def transformation_add_MANUAL(source_metamodels, target_metamodels, operation_name, callback=lambda: None):
  472. """Create a new manual model operation."""
  473. global mode
  474. _goto_mode(MODE_MODELLING)
  475. mv_dict_rep = _dict_to_list(source_metamodels) + [""] + _dict_to_list(target_metamodels) + [""]
  476. _input(["transformation_add_MANUAL"] + mv_dict_rep + [operation_name])
  477. # Possibly modify the merged metamodel first (add tracability links)
  478. if len(source_metamodels) + len(target_metamodels) > 0:
  479. mode = MODE_MANUAL
  480. _output("Model loaded, ready for commands!")
  481. callback()
  482. _input("exit")
  483. mode = MODE_MODELLING
  484. _handle_output("Success")
  485. def transformation_execute_AL(operation_name, input_models_dict, output_models_dict, statechart=None):
  486. """Execute an existing model operation."""
  487. global mode
  488. _goto_mode(MODE_MODELLING)
  489. mv_dict_rep = _dict_to_list(input_models_dict) + [""] + _dict_to_list(output_models_dict) + [""]
  490. _input(["transformation_execute", operation_name] + mv_dict_rep)
  491. _handle_output("Success: ready for AL execution")
  492. if statechart is not None:
  493. # We are to delegate this information to another statechart, which wants interaction
  494. # As such, we continue on a different thread, where we pipe the information, and let this call return immediately
  495. mode = MODE_DIALOG
  496. _exec_on_statechart(statechart)
  497. mode = MODE_MODELLING
  498. return None
  499. else:
  500. # No statechart associated, so just wait until we are finished
  501. while _output() not in ["Success", "Failure"]:
  502. pass
  503. if _last_output() == "Success":
  504. return True
  505. else:
  506. return False
  507. def transformation_execute_MANUAL(operation_name, input_models_dict, output_models_dict, callback=lambda i: None):
  508. """Execute an existing model operation."""
  509. global mode
  510. _goto_mode(MODE_MODELLING)
  511. mv_dict_rep = _dict_to_list(input_models_dict) + [""] + _dict_to_list(output_models_dict) + [""]
  512. _input(["transformation_execute", operation_name] + mv_dict_rep)
  513. _handle_output("Success: ready for MANUAL execution")
  514. # Skip over the begin of mini_modify
  515. _handle_output("Please perform manual operation ")
  516. _output("Model loaded, ready for commands!")
  517. # We are now executing, so everything we get is part of the dialog, except if it is the string for transformation termination
  518. mode = MODE_MANUAL
  519. callback()
  520. # Finished, so leave
  521. _input("exit")
  522. mode = MODE_MODELLING
  523. # Got termination message, so we are done!
  524. if _output() == "Success":
  525. return True
  526. else:
  527. return False
  528. def transformation_execute_MT(operation_name, input_models_dict, output_models_dict, statechart=None):
  529. """Execute an existing model operation."""
  530. global mode
  531. _goto_mode(MODE_MODELLING)
  532. mv_dict_rep = _dict_to_list(input_models_dict) + [""] + _dict_to_list(output_models_dict) + [""]
  533. _input(["transformation_execute", operation_name] + mv_dict_rep)
  534. _handle_output("Success: ready for MT execution")
  535. if statechart is not None:
  536. # We are to delegate this information to another statechart, which wants interaction
  537. # As such, we continue on a different thread, where we pipe the information, and let this call return immediately
  538. mode = MODE_DIALOG
  539. _exec_on_statechart(statechart)
  540. mode = MODE_MODELLING
  541. return None
  542. else:
  543. # No statechart associated, so just wait until we are finished
  544. while _output() not in ["Success", "Failure"]:
  545. pass
  546. if _last_output() == "Success":
  547. return True
  548. else:
  549. return False
  550. def transformation_list():
  551. """List existing model operations."""
  552. _goto_mode(MODE_MODELLING)
  553. _input("transformation_list")
  554. output = _handle_output("Success: ", split=True)
  555. lst = set([])
  556. for v in output:
  557. t, m = v.strip().split(" ", 1)
  558. t = t[1:-1].strip()
  559. m = m.strip().split(":")[0].strip()
  560. lst.add((t, m))
  561. return lst
  562. def process_execute(process_name, prefix, callbacks):
  563. """Execute a process model."""
  564. global mode
  565. _goto_mode(MODE_MODELLING)
  566. _input(["process_execute", process_name, prefix])
  567. _handle_output("Success")
  568. reuse = False
  569. while reuse or _output() != "Success":
  570. reuse = False
  571. output = _last_output()
  572. if output.startswith("Enacting "):
  573. # Next activity!
  574. t = output.split(" ", 1)[1].split(":", 1)[0]
  575. name = output.split(": ", 1)[1]
  576. if name in callbacks:
  577. callback = callbacks[name]
  578. if t == "ModelTransformation" or t == "ActionLanguage":
  579. while not (_output().startswith("Enacting ") or _last_output() == "Success"):
  580. mode = MODE_DIALOG
  581. reply = callback(_last_output())
  582. mode = MODE_MODELLING
  583. if reply is not None:
  584. _input(reply)
  585. if _last_output().startswith("Enacting "):
  586. reuse = True
  587. elif t == "ManualOperation":
  588. _handle_output("Please perform manual operation ")
  589. _output("Model loaded, ready for commands!")
  590. mode = MODE_MANUAL
  591. callback()
  592. _input("exit")
  593. mode = MODE_MODELLING
  594. def permission_modify(model_name, permissions):
  595. """Modify permissions of a model."""
  596. _goto_mode(MODE_MODELLING)
  597. _input(["permission_modify", model_name, permissions])
  598. _handle_output("Success")
  599. def permission_owner(model_name, owner):
  600. """Modify the owning user of a model."""
  601. _goto_mode(MODE_MODELLING)
  602. _input(["permission_owner", model_name, owner])
  603. _handle_output("Success")
  604. def permission_group(model_name, group):
  605. """Modify the owning group of a model."""
  606. _goto_mode(MODE_MODELLING)
  607. _input(["permission_group", model_name, group])
  608. _handle_output("Success")
  609. def group_create(group_name):
  610. """Create a new group."""
  611. _goto_mode(MODE_MODELLING)
  612. _input(["group_create", group_name])
  613. _handle_output("Success")
  614. def group_delete(group_name):
  615. """Delete a group of which you are an owner."""
  616. _goto_mode(MODE_MODELLING)
  617. _input(["group_delete", group_name])
  618. _handle_output("Success")
  619. def group_owner_add(group_name, user_name):
  620. """Add a new owning user to a group you own."""
  621. _goto_mode(MODE_MODELLING)
  622. _input(["owner_add", group_name, user_name])
  623. _handle_output("Success")
  624. def group_owner_delete(group_name, user_name):
  625. """Delete an owning user to a group you own."""
  626. _goto_mode(MODE_MODELLING)
  627. _input(["owner_delete", group_name, user_name])
  628. _handle_output("Success")
  629. def group_join(group_name, user_name):
  630. """Add a new user to a group you own."""
  631. _goto_mode(MODE_MODELLING)
  632. _input(["group_join", group_name, user_name])
  633. _handle_output("Success")
  634. def group_kick(group_name, user_name):
  635. """Delete a user from a group you own."""
  636. _goto_mode(MODE_MODELLING)
  637. _input(["group_kick", group_name, user_name])
  638. _handle_output("Success")
  639. def group_list():
  640. """List existing groups."""
  641. _goto_mode(MODE_MODELLING)
  642. _input(["group_list"])
  643. _handle_output("Success")
  644. def admin_promote(user_name):
  645. """Promote a user to admin status."""
  646. _goto_mode(MODE_MODELLING)
  647. _input(["admin_promote", user_name])
  648. _handle_output("Success")
  649. def admin_demote():
  650. """Demote a user from admin status."""
  651. _goto_mode(MODE_MODELLING)
  652. _input(["admin_demote", user_name])
  653. _handle_output("Success")
  654. # Actual operations on the model
  655. def element_list(model_name):
  656. """Return a list of all IDs and the type of the element"""
  657. _goto_mode(MODE_MODIFY, model_name)
  658. _input("list_full")
  659. lst = set([])
  660. output = _handle_output("Success: ", split=True)
  661. for v in output:
  662. m, mm = v.split(":")
  663. m = m.strip()
  664. mm = mm.strip()
  665. lst.add((m, mm))
  666. return lst
  667. def types(model_name):
  668. """Return a list of all types usable in the model"""
  669. _goto_mode(MODE_MODIFY, model_name)
  670. _input("types")
  671. lst = set([])
  672. output = _handle_output("Success: ", split=True)
  673. for v in output:
  674. m, mm = v.split(":")
  675. m = m.strip()
  676. lst.add(m)
  677. return lst
  678. def types_full(model_name):
  679. """Return a list of full types usable in the model"""
  680. _goto_mode(MODE_MODIFY, model_name)
  681. _input("types")
  682. lst = set([])
  683. output = _handle_output("Success: ", split=True)
  684. for v in output:
  685. m, mm = v.split(":")
  686. m = m.strip()
  687. mm = mm.strip()
  688. lst.add((m, mm))
  689. return lst
  690. def read(model_name, ID):
  691. """Return a tuple of information on the element: its type and source/target (None if not an edge)"""
  692. _goto_mode(MODE_MODIFY, model_name)
  693. _input(["read", ID])
  694. v = _handle_output("Success: ", split=True)
  695. t = v[1].split(":")[1].strip()
  696. if (not v[2].startswith("Source:")):
  697. rval = (t, None)
  698. else:
  699. src = v[2].split(":")[1].strip()
  700. trg = v[3].split(":")[1].strip()
  701. rval = (t, (src, trg))
  702. return rval
  703. def read_attrs(model_name, ID):
  704. """Return a dictionary of attribute value pairs"""
  705. _goto_mode(MODE_MODIFY, model_name)
  706. _input(["read", ID])
  707. output = _handle_output("Success: ", split=True)
  708. searching = True
  709. rval = {}
  710. for r in output:
  711. if searching:
  712. if r == "Attributes:":
  713. # Start working on attributes
  714. searching = False
  715. else:
  716. key, value = r.split(":", 1)
  717. _, value = value.split("=", 1)
  718. key = json.loads(key.strip())
  719. value = value.strip()
  720. if value == "None":
  721. value = None
  722. elif value == "True":
  723. value = True
  724. elif value == "False":
  725. value = False
  726. else:
  727. value = json.loads(value)
  728. rval[key] = value
  729. return rval
  730. def instantiate(model_name, typename, edge=None, ID=""):
  731. """Create a new instance of the specified typename, between the selected elements (if not None), and with the provided ID (if any)"""
  732. _goto_mode(MODE_MODIFY, model_name)
  733. if edge is None:
  734. _input(["instantiate_node", typename, ID])
  735. else:
  736. _input(["instantiate_edge", typename, ID, edge[0], edge[1]])
  737. return _handle_output("Success: ", split=True)[0]
  738. def delete_element(model_name, ID):
  739. """Delete the element with the given ID"""
  740. _goto_mode(MODE_MODIFY, model_name)
  741. _input(["delete", ID])
  742. _handle_output("Success")
  743. def attr_assign(model_name, ID, attr, value):
  744. """Assign a value to an attribute"""
  745. _check_type(value)
  746. _goto_mode(MODE_MODIFY, model_name)
  747. _input(["attr_add", ID, attr, value])
  748. _handle_output("Success")
  749. def attr_assign_code(model_name, ID, attr, code):
  750. """Assign a piece of Action Language code to the attribute"""
  751. _check_type(code)
  752. try:
  753. compiled = _compile_AL(code)
  754. except Exception as e:
  755. raise CompilationError(e)
  756. _goto_mode(MODE_MODIFY, model_name)
  757. _input(["attr_add_code", ID, attr])
  758. _handle_output("Waiting for code constructors...")
  759. _input(compiled)
  760. _output("Success")
  761. def attr_delete(model_name, ID, attr):
  762. """Remove an attribute."""
  763. _goto_mode(MODE_MODIFY, model_name)
  764. _input(["attr_del", ID, attr])
  765. _handle_output("Success")
  766. def read_outgoing(model_name, ID, typename):
  767. """Returns a list of all outgoing associations of a specific type ("" = all)"""
  768. _goto_mode(MODE_MODIFY, model_name)
  769. _input(["read_outgoing", ID, typename])
  770. output = _handle_output("Success: ", split=True)
  771. return set(output)
  772. def read_incoming(model_name, ID, typename):
  773. """Returns a list of all incoming associations of a specific type ("" = all)"""
  774. _goto_mode(MODE_MODIFY, model_name)
  775. _input(["read_incoming", ID, typename])
  776. output = _handle_output("Success: ", split=True)
  777. return set(output)
  778. def read_association_source(model_name, ID):
  779. """Returns the source of an association."""
  780. _goto_mode(MODE_MODIFY, model_name)
  781. _input(["read_association_source", ID])
  782. return _handle_output("Success: ", split=True)[0]
  783. def read_association_destination(model_name, ID):
  784. """Returns the destination of an association."""
  785. _goto_mode(MODE_MODIFY, model_name)
  786. _input(["read_association_destination", ID])
  787. return _handle_output("Success: ", split=True)[0]
  788. ##### To document:
  789. def service_register(name, function):
  790. """Register a function as a service with a specific name."""
  791. def service_process(service_task):
  792. global address
  793. global port
  794. ctrl = _start_http_client(address, port, 10.0)
  795. _listen_to_output(ctrl, service_task)
  796. while 1:
  797. client_task = service_get(service_task)
  798. ctrl = _start_http_client(address, port, 10.0)
  799. _listen_to_output(ctrl, client_task)
  800. thrd = threading.Thread(target=function, args=[client_task])
  801. thrd.daemon = True
  802. thrd.start()
  803. global mode
  804. _goto_mode(MODE_MODELLING)
  805. _input(["service_register", name])
  806. # Now we are in service-mode
  807. mode = MODE_SERVICE
  808. port = _handle_output("Success: ", split=True)[0]
  809. # Process events in the background!
  810. threading.Thread(target=service_process, args=[port]).start()
  811. def service_stop():
  812. """Stop the currently executing process."""
  813. _goto_mode(MODE_SERVICE)
  814. _input("service_stop")
  815. _handle_output("Success")
  816. global mode
  817. mode = MODE_MODELLING
  818. def service_get(port):
  819. """Get the values on the specified port."""
  820. _goto_mode(MODE_SERVICE)
  821. return _output(port=port)
  822. def service_set(port, value):
  823. """Set a value on a specified port."""
  824. _check_type_list(value)
  825. _goto_mode(MODE_SERVICE)
  826. _input(value, port=port)
  827. def user_password(user, password):
  828. """Change a user's password."""
  829. raise NotImplementedError()
  830. def transformation_read_signature(transformation):
  831. """Reads an operation's signature, specifying the names and their required types."""
  832. raise NotImplementedError()
  833. def element_list_nice(model_name):
  834. """Fetches a nice representation of models."""
  835. _goto_mode(MODE_MODELLING)
  836. _input(["element_list_nice", model_name, _get_metamodel(model_name)])
  837. data = _handle_output("Success: ", split=True)[0]
  838. try:
  839. return json.loads(data)
  840. except:
  841. print(data)
  842. raise
  843. def connections_between(model_name, source_element, target_element):
  844. """Gets a list of all allowed connections between the source and target element in the model."""
  845. _goto_mode(MODE_MODIFY, model_name)
  846. _input(["connections_between", source_element, target_element])
  847. output = _handle_output("Success: ", split=True)
  848. return set(output)
  849. def define_attribute(model_name, node, attr_name, attr_type):
  850. """Create a new attribute, which can be instantiated one meta-level below."""
  851. _goto_mode(MODE_MODIFY, model_name)
  852. _input(["define_attribute", node, attr_name, attr_type])
  853. return _handle_output("Success: ", split=True)[0]
  854. def all_instances(model_name, type_name):
  855. """Returns a list of all elements of a specific type."""
  856. _goto_mode(MODE_MODIFY, model_name)
  857. _input(["all_instances", type_name])
  858. output = _handle_output("Success: ", split=True)
  859. return set(output)
  860. def service_poll(port):
  861. """Checks whether or not the Modelverse side has any input ready to be processed."""
  862. raise NotImplementedError()
  863. def user_name(user, username):
  864. """Change a user's name."""
  865. raise NotImplementedError()
  866. def remove_conformance(model_name, metamodel_name):
  867. """Remove a metamodel for a model."""
  868. _goto_mode(MODE_MODELLING)
  869. _input(["remove_conformance", model_name, metamodel_name])
  870. _handle_output("Success")
  871. def add_conformance(model_name, metamodel_name, partial_type_mapping=None):
  872. """Add a metamodel for a model."""
  873. raise NotImplementedError()
  874. _goto_mode(MODE_MODELLING)
  875. _input(["add_conformance", model_name, metamodel_name])
  876. _handle_output("Success")
  877. def folder_create(folder_name):
  878. _goto_mode(MODE_MODELLING)
  879. _input(["folder_create", folder_name])
  880. _handle_output("Success")