modelverse.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. from sccd.runtime.statecharts_core import Event
  2. import sccd.runtime.socket2event as socket2event
  3. import modelverse_SCCD
  4. import time
  5. import threading
  6. # Exceptions
  7. class ModelverseException(Exception):
  8. pass
  9. class UnknownError(ModelverseException):
  10. pass
  11. class UnknownIdentifier(ModelverseException):
  12. pass
  13. class CompilationError(ModelverseException):
  14. pass
  15. class NoSuchAttribute(ModelverseException):
  16. pass
  17. class UnknownModel(ModelverseException):
  18. pass
  19. class ConnectionError(ModelverseException):
  20. pass
  21. class ModelExists(ModelverseException):
  22. pass
  23. class PermissionDenied(ModelverseException):
  24. pass
  25. class InvalidMode(ModelverseException):
  26. pass
  27. class InterfaceMismatch(ModelverseException):
  28. pass
  29. class UnknownMetamodellingHierarchy(ModelverseException):
  30. pass
  31. def run_controller():
  32. try:
  33. controller.start()
  34. finally:
  35. controller.stop()
  36. def _next_ID():
  37. global ID
  38. ID += 1
  39. return ID
  40. def __run_new_modelverse(address, username, password, callback, model):
  41. init(address)
  42. login(username, password)
  43. if callback is not None:
  44. callback(model)
  45. exit_save(model)
  46. def __run_new_modelverse_activity(address, username, password, taskname, pipe, callback):
  47. init(address, taskname=taskname)
  48. controller.username = username
  49. controller.password = password
  50. t = OUTPUT()
  51. if t == "OP":
  52. model = OUTPUT()
  53. __invoke(callback, model)
  54. controller.addInput(Event("data_input", "action_in", [None, None]))
  55. time.sleep(2)
  56. elif t == "SC":
  57. while 1:
  58. empty = True
  59. # Fetch output from the MV
  60. response = responses.fetch(0)
  61. if response is not None:
  62. print("Got response: " + str(response))
  63. if response.name == "data_output":
  64. # Got output of MV, so forward to SCCD
  65. if pipe is not None:
  66. pipe.send(("input", response.parameters))
  67. elif response.name == "result":
  68. # Finished execution, so continue and return result
  69. if pipe is not None:
  70. print("Sending over pipe!")
  71. pipe.send(("terminate", []))
  72. pipe.close()
  73. print("TERMINATE")
  74. return
  75. else:
  76. raise Exception("Unknown data from MV to SC: " + str(response))
  77. empty = False
  78. # Fetch output from the SC
  79. if pipe is not None and pipe.poll():
  80. response = pipe.recv()
  81. if response.name == "output":
  82. controller.addInput(Event("data_input", "action_in", [response.parameters, context]))
  83. else:
  84. raise Exception("Unknown data from SC to MV: " + str(response))
  85. empty = False
  86. if empty:
  87. time.sleep(0.05)
  88. def __invoke(callback, model):
  89. import multiprocessing
  90. p = multiprocessing.Process(target=__run_new_modelverse, args=[controller.address, controller.username, controller.password, callback, model])
  91. p.start()
  92. p.join()
  93. def _process_SC(statechart, port_sc, taskname):
  94. import multiprocessing
  95. p2c_pipe, c2p_pipe = multiprocessing.Pipe()
  96. p = multiprocessing.Process(target=__run_new_modelverse_activity, args=[controller.address, controller.username, controller.password, taskname, c2p_pipe, None])
  97. p.start()
  98. while 1:
  99. empty = True
  100. if p2c_pipe.poll():
  101. response = p2c_pipe.recv()
  102. print("Got SC response: " + str(response))
  103. statechart[0].addInput(Event(response[0], statechart[1], response[1]))
  104. if response[0] == "terminate":
  105. print("Got break")
  106. p2c_pipe.close()
  107. break
  108. empty = False
  109. response = port_sc.fetch(0)
  110. if response is not None:
  111. p2c_pipe.send(response)
  112. empty = False
  113. if empty:
  114. time.sleep(0.05)
  115. print("Wait for join")
  116. p.join()
  117. print("Joined SC")
  118. def _process_OP(callback, taskname):
  119. import multiprocessing
  120. p = multiprocessing.Process(target=__run_new_modelverse_activity, args=[controller.address, controller.username, controller.password, taskname, None, callback])
  121. p.start()
  122. p.join()
  123. def INPUT(action, context, parameters):
  124. controller.addInput(Event("action", "action_in", [action, _next_ID(), context, parameters]))
  125. def OUTPUT():
  126. while 1:
  127. response = responses.fetch(-1)
  128. if response.name == "result":
  129. return response.parameters[1]
  130. elif response.name == "exception":
  131. if response.parameters[1] == "UnknownIdentifier":
  132. raise UnknownIdentifier()
  133. elif response.parameters[1] == "UnknownMetamodellingHierarchy":
  134. raise UnknownMetamodellingHierarchy()
  135. else:
  136. print("Unknown error: " + str(response.parameters))
  137. raise UnknownError()
  138. def init(address_param="127.0.0.1:8001", timeout=20.0, taskname=None):
  139. global controller
  140. global ID
  141. global responses
  142. controller = modelverse_SCCD.Controller(taskname)
  143. socket2event.boot_translation_service(controller)
  144. ID = 0
  145. thrd = threading.Thread(target=run_controller)
  146. thrd.daemon = True
  147. thrd.start()
  148. responses = controller.addOutputListener("action_out")
  149. controller.addOutputListener("ready").fetch(-1)
  150. INPUT("init", None, [address_param, timeout])
  151. controller.address = address_param
  152. return OUTPUT()
  153. def login(username, password):
  154. controller.username = username
  155. controller.password = password
  156. INPUT("login", None, [username, password])
  157. return OUTPUT()
  158. def model_list(location):
  159. INPUT("model_list", None, [location])
  160. return OUTPUT()
  161. def model_add(model_name, metamodel_name, model_code=""):
  162. INPUT("model_add", None, [model_name, metamodel_name, model_code])
  163. return OUTPUT()
  164. def model_delete(model_name):
  165. INPUT("model_delete", None, [model_name])
  166. return OUTPUT()
  167. def model_list_full(location):
  168. INPUT("model_list_full", None, [location])
  169. return OUTPUT()
  170. def verify(model_name, metamodel_name):
  171. INPUT("verify", None, [model_name, metamodel_name])
  172. return OUTPUT()
  173. def model_overwrite(model_name, new_model, context=None):
  174. INPUT("model_overwrite", context, [model_name, new_model])
  175. return OUTPUT()
  176. def disconnect():
  177. INPUT("disconnect", None, [])
  178. return OUTPUT()
  179. def user_logout():
  180. INPUT("user_logout", None, [])
  181. return OUTPUT()
  182. def user_delete():
  183. INPUT("user_delete", None, [])
  184. return OUTPUT()
  185. def model_render(model_name, mapper_name, rendered_name):
  186. INPUT("model_render", None, [model_name, mapper_name, rendered_name])
  187. return OUTPUT()
  188. def transformation_between(sources, targets):
  189. INPUT("transformation_between", None, [source, target])
  190. return OUTPUT()
  191. def transformation_add_MT(source_metamodels, target_metamodels, operation_name, code, callback=None):
  192. INPUT("transformation_add_MT", None, [source_metamodels, target_metamodels, operation_name, code, True])
  193. model = OUTPUT()
  194. if callback is not None:
  195. __invoke(callback, model)
  196. controller.addInput(Event("data_input", "action_in", [None, None]))
  197. return OUTPUT()
  198. def transformation_add_AL(source_metamodels, target_metamodels, operation_name, code, callback=None):
  199. INPUT("transformation_add_AL", None, [source_metamodels, target_metamodels, operation_name, code, True])
  200. model = OUTPUT()
  201. if model is None:
  202. # In case the source and target metamodels are empty, the context will be None, indicating that we are finished already (no callbacks allowed)
  203. return
  204. if callback is not None:
  205. __invoke(callback, model)
  206. controller.addInput(Event("data_input", "action_in", [None, None]))
  207. return OUTPUT()
  208. def transformation_add_MANUAL(source_metamodels, target_metamodels, operation_name, callback=None):
  209. INPUT("transformation_add_MANUAL", None, [source_metamodels, target_metamodels, operation_name, True])
  210. model = OUTPUT()
  211. if callback is not None:
  212. __invoke(callback, model)
  213. controller.addInput(Event("data_input", "action_in", [None, None]))
  214. return OUTPUT()
  215. def __transformation_execute(operation_name, input_models_dict, output_models_dict, statechart, tracability_model, fetch_output):
  216. if statechart is not None:
  217. port_sc = statechart[0].addOutputListener(statechart[2])
  218. INPUT("transformation_execute", None, [operation_name, input_models_dict, output_models_dict, tracability_model, fetch_output])
  219. taskname = OUTPUT()
  220. if statechart is not None:
  221. threading.Thread(target=_process_SC, args=[statechart, port_sc, taskname]).start()
  222. return OUTPUT()
  223. def transformation_execute_MT(operation_name, input_models_dict, output_models_dict, statechart=None, tracability_model="", fetch_output=True):
  224. return __transformation_execute(operation_name, input_models_dict, output_models_dict, statechart, tracability_model, fetch_output)
  225. def transformation_execute_AL(operation_name, input_models_dict, output_models_dict, statechart=None, tracability_model="", fetch_output=True):
  226. return __transformation_execute(operation_name, input_models_dict, output_models_dict, statechart, tracability_model, fetch_output)
  227. def transformation_execute_MANUAL(operation_name, input_models_dict, output_models_dict, callback=None, tracability_model=""):
  228. INPUT("transformation_execute", None, [operation_name, input_models_dict, output_models_dict, tracability_model])
  229. taskname = OUTPUT()
  230. _process_OP(callback, taskname)
  231. return OUTPUT()
  232. def transformation_signature(operation_name):
  233. INPUT("transformation_signature", None, [operation_name])
  234. return OUTPUT()
  235. def process_signature(process_name):
  236. INPUT("process_signature", None, [operation_name])
  237. return OUTPUT()
  238. def permission_modify(model_name, permissions):
  239. INPUT("permission_modify", None, [model_name, permissions])
  240. return OUTPUT()
  241. def permission_owner(model_name, permission):
  242. INPUT("permission_owner", None, [model_name, permission])
  243. return OUTPUT()
  244. def permission_group(model_name, group):
  245. INPUT("permission_group", None, [model_name, group])
  246. return OUTPUT()
  247. def group_create(group_name):
  248. INPUT("group_create", None, [group_name])
  249. return OUTPUT()
  250. def group_delete(group_name):
  251. INPUT("group_delete", None, [group_name])
  252. return OUTPUT()
  253. def group_owner_add(group_name, user_name):
  254. INPUT("group_owner_add", None, [group_name, user_name])
  255. return OUTPUT()
  256. def group_owner_delete(group_name, user_name):
  257. INPUT("group_owner_delete", None, [group_name, user_name])
  258. return OUTPUT()
  259. def group_join(group_name, user_name):
  260. INPUT("group_join", None, [group_name, user_name])
  261. return OUTPUT()
  262. def group_kick(group_name, user_name):
  263. INPUT("group_kick", None, [group_name, user_name])
  264. return OUTPUT()
  265. def group_list():
  266. INPUT("group_list", None, [])
  267. return OUTPUT()
  268. def admin_promote(user_name):
  269. INPUT("admin_promote", None, [user_name])
  270. return OUTPUT()
  271. def admin_demote(user_name):
  272. INPUT("admin_demote", None, [user_name])
  273. return OUTPUT()
  274. def conformance_delete(model_name, metamodel_name):
  275. INPUT("conformance_delete", None, [model_name, metamodel_name])
  276. return OUTPUT()
  277. def conformance_add(model_name, metamodel_name):
  278. INPUT("conformance_add", None, [model_name, metamodel_name])
  279. return OUTPUT()
  280. def folder_create(folder_name):
  281. INPUT("folder_create", None, [folder_name])
  282. return OUTPUT()
  283. def model_types(model_name):
  284. INPUT("model_types", None, [model_name])
  285. return OUTPUT()
  286. def alter_context(model_name, metamodel_name):
  287. INPUT("alter_context", None, [model_name, metamodel_name])
  288. def element_list(model_name, context=None):
  289. INPUT("element_list", context, [model_name])
  290. return OUTPUT()
  291. def element_list_nice(model_name, context=None):
  292. INPUT("element_list_nice", context, [model_name])
  293. return OUTPUT()
  294. def types(model_name, context=None):
  295. INPUT("types", context, [model_name])
  296. return OUTPUT()
  297. def types_full(model_name, context=None):
  298. INPUT("types_full", context, [model_name])
  299. return OUTPUT()
  300. def read_info(model_name, ID, context=None):
  301. INPUT("read_info", context, [model_name, ID])
  302. return OUTPUT()
  303. def read_attrs(model_name, ID, context=None):
  304. INPUT("read_attrs", context, [model_name, ID])
  305. return OUTPUT()
  306. def instantiate(model_name, typename, edge=None, ID="", context=None):
  307. INPUT("instantiate", context, [model_name, typename, edge, ID])
  308. return OUTPUT()
  309. def delete_element(model_name, ID, context=None):
  310. INPUT("delete_element", context, [model_name, ID])
  311. return OUTPUT()
  312. def attr_assign(model_name, ID, attr, value, context=None):
  313. INPUT("attr_assign", context, [model_name, ID, attr, value])
  314. return OUTPUT()
  315. def attr_assign_code(model_name, ID, attr, code, context=None):
  316. INPUT("attr_assign_code", context, [model_name, ID, attr, code])
  317. return OUTPUT()
  318. def attr_delete(model_name, ID, attr, context=None):
  319. INPUT("attr_delete", context, [model_name, ID, attr])
  320. return OUTPUT()
  321. def read_outgoing(model_name, ID, typename, context=None):
  322. INPUT("read_outgoing", context, [model_name, ID, typename])
  323. return OUTPUT()
  324. def read_incoming(model_name, ID, typename, context=None):
  325. INPUT("read_incoming", context, [model_name, ID, typename])
  326. return OUTPUT()
  327. def read_association_source(model_name, ID, context=None):
  328. INPUT("read_association_source", context, [model_name, ID])
  329. return OUTPUT()
  330. def read_association_destination(model_name, ID, context=None):
  331. INPUT("read_association_destination", context, [model_name, ID])
  332. return OUTPUT()
  333. def connections_between(model_name, source, target, context=None):
  334. INPUT("connections_between", context, [model_name, source, target])
  335. return OUTPUT()
  336. def define_attribute(model_name, node, attr_name, attr_type, context=None):
  337. INPUT("define_attribute", context, [model_name, node, attr_name, attr_type])
  338. return OUTPUT()
  339. def all_instances(model_name, type_name, context=None):
  340. INPUT("all_instances", context, [model_name, type_name])
  341. return OUTPUT()
  342. def process_execute(process_name, prefix, callbacks=None):
  343. # for all callbacks to SCs, start up the output port already
  344. sc_ports = {}
  345. for k, v in callbacks.items():
  346. if isinstance(v, (tuple, list)):
  347. # Is a statechart, so register already
  348. sc_ports[k] = v[0].addOutputListener(v[2])
  349. INPUT("process_execute", None, [process_name, prefix])
  350. while 1:
  351. print("Waiting for output...")
  352. result = OUTPUT()
  353. print("Got result: " + str(result))
  354. if result == "Success":
  355. # Finished
  356. print("FINISHING")
  357. return None
  358. else:
  359. taskname, operation = result
  360. if (operation in callbacks):
  361. data = callbacks[operation]
  362. if isinstance(data, (tuple, list)):
  363. # Statechart, so consider like that
  364. print("A")
  365. threading.Thread(target=_process_SC, args=[data, sc_ports[operation], taskname]).start()
  366. print("AE")
  367. else:
  368. # Assume function
  369. print("B")
  370. threading.Thread(target=_process_OP, args=[data, taskname]).start()
  371. print("BE")
  372. else:
  373. # Assume empty function
  374. print("C")
  375. threading.Thread(target=_process_OP, args=[None, taskname]).start()
  376. print("CE")
  377. def get_taskname():
  378. """Fetch the taskname of the current connection."""
  379. return controller.taskname
  380. def exit_save(model_name):
  381. INPUT("exit_save", None, [model_name])
  382. return OUTPUT()
  383. """ Some hardcoded functions... Way easier to express them with code than with statecharts!"""
  384. import json
  385. import urllib
  386. import urllib2
  387. def service_register(name, function):
  388. """Register a function as a service with a specific name."""
  389. INPUT("service_register", None, [name, function])
  390. port = OUTPUT()
  391. return port
  392. def service_stop():
  393. """Stop the currently executing process."""
  394. INPUT("service_stop", None, [])
  395. return OUTPUT()
  396. def service_get(port):
  397. """Get the values on the specified port."""
  398. val = json.loads(urllib2.urlopen(urllib2.Request("http://%s" % controller.address, urllib.urlencode({"op": "get_output", "taskname": port}))).read())
  399. return val
  400. def service_set(port, value):
  401. """Set a value on a specified port."""
  402. if isinstance(value, type([])):
  403. value = json.dumps(value)
  404. urllib2.urlopen(urllib2.Request("http://%s" % controller.address, urllib.urlencode({"op": "set_input", "data": value, "taskname": port}))).read()
  405. else:
  406. value = json.dumps(value)
  407. urllib2.urlopen(urllib2.Request("http://%s" % controller.address, urllib.urlencode({"op": "set_input", "value": value, "taskname": port}))).read()
  408. def service_poll(port):
  409. """Checks whether or not the Modelverse side has any input ready to be processed."""
  410. raise NotImplementedError()