console.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import sys
  2. from modelverse import *
  3. import random, re, json, subprocess
  4. from interface import Controller
  5. print("Init")
  6. init("msdl.uantwerpen.be:8001")
  7. print("Login")
  8. login("admin", "admin")
  9. print("Remove ProductionSystem metamodel")
  10. try:
  11. model_delete("formalisms/ProductionSystem")
  12. except ModelverseException:
  13. pass
  14. # Add the metamodel for ProductionSystem
  15. print("Add ProductionSystem metamodel")
  16. try:
  17. model_add("formalisms/ProductionSystem", "formalisms/SimpleClassDiagrams", open("../../models/production_system_design.mvc").read())
  18. except ModelExists:
  19. pass
  20. print("Remove model")
  21. try:
  22. model_delete("models/example_PS")
  23. except ModelverseException:
  24. pass
  25. print("Add model")
  26. try:
  27. model_add("models/example_PS", "formalisms/ProductionSystem", open("../../models/example_PS.mvc").read())
  28. except ModelExists:
  29. pass
  30. print("Remove ParallelDEVS metamodel")
  31. try:
  32. model_delete("formalisms/ParallelDEVS")
  33. except ModelverseException:
  34. pass
  35. # Add the metamodel for Parallel DEVS
  36. print("Add ParallelDEVS metamodel")
  37. try:
  38. model_add("formalisms/ParallelDEVS", "formalisms/SimpleClassDiagrams", open("../../models/paralleldevs_design.mvc").read())
  39. except ModelExists:
  40. pass
  41. def traceability_PS2DEVS():
  42. instantiate(None, "Association", ("ProductionSystem/Machine", "ParallelDEVS/BaseDEVSBlock"), ID="PS2DEVS_typelink")
  43. instantiate(None, "Association", ("ProductionSystem/Machine", "ParallelDEVS/DEVSInstance"), ID="PS2DEVS_instancelink")
  44. print("Remove PS->DEVS transformation model")
  45. try:
  46. model_delete("models/ps_to_devs")
  47. except ModelverseException:
  48. pass
  49. print("Add PS->DEVS transformation model")
  50. transformation_add_MT({"ProductionSystem": "formalisms/ProductionSystem"}, {"ParallelDEVS": "formalisms/ParallelDEVS"}, "models/ps_to_devs", open("../../models/ps_to_devs.mvc", 'r').read(), traceability_PS2DEVS)
  51. print("Execute PS->DEVS transformation")
  52. transformation_execute_MT("models/ps_to_devs", {"ProductionSystem": "models/example_PS"}, {"ParallelDEVS": "models/example_PS_DEVS"})
  53. print("Remove MyString metamodel")
  54. try:
  55. model_delete("formalisms/MyString")
  56. except ModelverseException:
  57. pass
  58. # Add the metamodel for String
  59. print("Add MyString metamodel")
  60. try:
  61. model_add("formalisms/MyString", "formalisms/SimpleClassDiagrams", open("../../models/string.mvc").read())
  62. except ModelExists:
  63. pass
  64. """
  65. print("Remove PDEVS model")
  66. try:
  67. model_delete("models/produce_consume_pdevs")
  68. except ModelverseException:
  69. pass
  70. print("Add PDEVS model")
  71. try:
  72. model_add("models/produce_consume_pdevs", "formalisms/ParallelDEVS", open("../../models/produce_consume_PDEVS.mvc").read())
  73. except ModelExists:
  74. pass
  75. """
  76. # Remove the action language code to transform between DEVS->String
  77. print("Remove DEVS->String transformation model")
  78. try:
  79. model_delete("models/paralleldevs_to_string")
  80. except ModelverseException:
  81. pass
  82. # Add the action language code to transform between DEVS->String
  83. print("Add DEVS->String transformation model")
  84. try:
  85. transformation_add_AL({"ParallelDEVS": "formalisms/ParallelDEVS"}, {"MyString": "formalisms/MyString"}, "models/paralleldevs_to_string", open("../../models/devs_to_string.alc", "r").read())
  86. except ModelExists:
  87. pass
  88. print("Execute DEVS->String transformation")
  89. transformation_execute_AL("models/paralleldevs_to_string", {"ParallelDEVS": "models/example_PS_DEVS"}, {"MyString": "models/example_PS_DEVS_String"})
  90. '''
  91. print("Remove ParallelDEVS simulator service")
  92. try:
  93. model_delete("models/paralleldevs_simulator")
  94. except ModelverseException:
  95. pass
  96. # TODO: Make this a transformation to a trace metamodel
  97. print("Add ParallelDEVS simulator service")
  98. try:
  99. transformation_add_AL({"MyString": "formalisms/MyString"}, {}, "models/paralleldevs_simulator", open("../../integration/code/pdevs_client.alc", "r").read())
  100. except ModelExists:
  101. pass
  102. controller = Controller()
  103. def set_defaults(inp, defaultlist):
  104. for i, v in enumerate(defaultlist):
  105. if len(inp) == i + 1:
  106. inp.append(v)
  107. def get_bool(val):
  108. if val.lower() in ["true", "yes", "1"]:
  109. return True
  110. else:
  111. return False
  112. def raw_inputter():
  113. while 1:
  114. inp = raw_input().split(" ")
  115. action = inp[0]
  116. if inp[0] == "simulate":
  117. set_defaults(inp, ['inf'])
  118. params = [{"termination_time": float(inp[1])}]
  119. elif inp[0] == "big_step":
  120. set_defaults(inp, ['inf'])
  121. params = [{"termination_time": float(inp[1])}]
  122. elif inp[0] == "realtime":
  123. set_defaults(inp, [1.0, 'inf'])
  124. params = [{"realtime_scale": float(inp[1]), "termination_time": float(inp[2])}]
  125. elif inp[0] == "small_step":
  126. set_defaults(inp, ['inf'])
  127. params = [{"termination_time": float(inp[1])}]
  128. elif inp[0] == "god_event":
  129. if len(inp) != 4:
  130. print("God Events require 3 parameters!")
  131. continue
  132. params = [{"model": inp[1], "attribute": inp[2], "value": inp[3]}]
  133. elif inp[0] == "inject":
  134. if len(inp) != 4:
  135. print("Injects require 3 parameters!")
  136. continue
  137. params = [{"time": float(inp[1]), "port": inp[2], "event": inp[3]}]
  138. elif inp[0] == "trace":
  139. set_defaults(inp, [None])
  140. params = [inp[1]]
  141. elif inp[0] == "pause":
  142. params = []
  143. elif inp[0] == "exit" or inp[0] == "quit":
  144. break
  145. elif inp[0] == "add_breakpoint":
  146. if len(inp) < 5:
  147. print("Breakpoint removal requires 2 parameters!")
  148. continue
  149. # Merge together the final part again
  150. inp = [inp[0], inp[1], " ".join(inp[2:-2]).replace("\\n", "\n").replace('\\t', '\t'), get_bool(inp[-2]), get_bool(inp[-1])]
  151. print("Generated parameters: " + str(inp))
  152. params = inp[1:]
  153. elif inp[0] == "del_breakpoint":
  154. if len(inp) < 2:
  155. print("Breakpoint removal requires 1 parameter!")
  156. continue
  157. params = [inp[1]]
  158. elif inp[0] == "enable_breakpoint":
  159. action = "toggle_breakpoint"
  160. params = [inp[1], True]
  161. elif inp[0] == "disable_breakpoint":
  162. action = "toggle_breakpoint"
  163. params = [inp[1], False]
  164. elif inp[0] == "reset":
  165. params = []
  166. elif inp[0] == "backwards_step":
  167. params = []
  168. elif inp[0] == "help":
  169. print("Supported operations:")
  170. print(" simulate [termination_time]")
  171. print(" --> Simulate until termination time is reached")
  172. print(" big_step [termination_time]")
  173. print(" --> Simulate a single step, unless termination time is reached")
  174. print(" small_step [termination_time]")
  175. print(" --> Simulate a single internal simulation step")
  176. print(" Termination time is ONLY checked at the")
  177. print(" check_termination phase")
  178. print(" backwards_step")
  179. print(" --> Step back to the previous time")
  180. print(" realtime [termination_time [realtime_scale]]")
  181. print(" --> Simulate in realtime until simulation time is reached")
  182. print(" realtime_scale can speed up or slow down the pace")
  183. print(" god_event model_name attribute_name new_value")
  184. print(" --> Modify the internal state of an arbitrary model")
  185. print(" model_name should be the fully qualified name")
  186. print(" attribute_name is the name of the attribute to alter")
  187. print(" new_value is the value to assign")
  188. print(" new_value can only be a string due to string-only input")
  189. print(" inject time port_name event")
  190. print(" --> Put a user-defined event on an input port")
  191. print(" port_name should be a fully qualified port name")
  192. print(" event should be the event to put on it, string only")
  193. print(" trace [filename]")
  194. print(" --> Write out trace information to the specified file.")
  195. print(" Leave empty to disable tracing.")
  196. print(" add_breakpoint id function enabled disable_on_trigger")
  197. print(" --> Add a breakpoint that will pause simulation when function returns True")
  198. print(" the function should contain a definition of the 'breakpoint' function")
  199. print(" and must take 3 parameters: time, model and transitioned")
  200. print(" enabled indicates whether or not the breakpoint should be checked")
  201. print(" disable_on_trigger determines if the breakpoint should auto-disable")
  202. print(" after being triggered")
  203. print(" del_breakpoint id")
  204. print(" --> Remove a breakpoint")
  205. print(" enable_breakpoint id")
  206. print(" --> Enable the provided breakpoint")
  207. print(" disable_breakpoint id")
  208. print(" --> Disable the provided breakpoint")
  209. print(" reset")
  210. print(" --> Reset the simulation")
  211. print(" exit")
  212. print(" --> Stop the client")
  213. print(" pause")
  214. print(" --> Pause the simulation")
  215. print(" quit")
  216. print(" --> Stop the client")
  217. print("")
  218. print("Defaults: ")
  219. print(" termination_time --> 'inf'")
  220. print(" realtime_scale --> 1.0")
  221. continue
  222. else:
  223. print("Command not understood: " + str(inp))
  224. continue
  225. params.insert(0, action)
  226. print "Console sending input to the Modelverse! %s" % params
  227. controller.addInput(Event("mv_input", "input", [json.dumps(params)]))
  228. input_thread = threading.Thread(target=raw_inputter)
  229. input_thread.daemon = True
  230. input_thread.start()
  231. output_listener = controller.addOutputListener(["output"])
  232. def outputter():
  233. while 1:
  234. print "Console got output from the Modelverse! %s" % output_listener.fetch(-1)
  235. output_thread = threading.Thread(target=outputter)
  236. output_thread.daemon = True
  237. output_thread.start()
  238. print("Starting PyPDEVS service...")
  239. transformation_execute_AL("models/paralleldevs_simulator", {"MyString": "models/produce_consume_string"}, {}, statechart=(controller, "mv_input", "mv_output"))
  240. print("Starting controller")
  241. controller.start()
  242. '''