modelverse.py 17 KB

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