client.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. print("SCCD PythonPDEVS CLI interface")
  2. import sys
  3. import time
  4. from pprint import pprint
  5. sys.path.append("../src/")
  6. from sccd.runtime.statecharts_core import Event
  7. import services.DEVS.pypdevs.src.simulator as simulator
  8. from services.DEVS.pypdevs.examples.ps_model import Root
  9. controller = simulator.Controller(Root())
  10. reply_port = controller.addOutputListener('reply')
  11. def set_defaults(inp, defaultlist):
  12. for i, v in enumerate(defaultlist):
  13. if len(inp) == i + 1:
  14. inp.append(v)
  15. def get_bool(val):
  16. if val.lower() in ["true", "yes", "1"]:
  17. return True
  18. else:
  19. return False
  20. def poll_port(port):
  21. while 1:
  22. try:
  23. event = port.fetch(-1)
  24. print("%s" % event.getName())
  25. pprint(event.getParameters())
  26. except:
  27. pass
  28. def generate_input():
  29. print("READY for input")
  30. print("Type 'help' for information")
  31. while 1:
  32. if sys.version_info[0] < 3:
  33. inp = raw_input().split(" ")
  34. else:
  35. inp = input().split(" ")
  36. action = inp[0]
  37. if inp[0] == "simulate":
  38. set_defaults(inp, ['inf'])
  39. params = [{"termination_time": float(inp[1])}]
  40. elif inp[0] == "big_step":
  41. set_defaults(inp, ['inf'])
  42. params = [{"termination_time": float(inp[1])}]
  43. elif inp[0] == "realtime":
  44. set_defaults(inp, ['inf', 1.0])
  45. params = [{"termination_time": float(inp[1]), "realtime_scale": float(inp[2])}]
  46. elif inp[0] == "small_step":
  47. set_defaults(inp, ['inf'])
  48. params = [{"termination_time": float(inp[1])}]
  49. elif inp[0] == "god_event":
  50. if len(inp) != 4:
  51. print("God Events require 3 parameters!")
  52. continue
  53. params = [{"model": inp[1], "attribute": inp[2], "value": inp[3]}]
  54. elif inp[0] == "inject":
  55. if len(inp) != 4:
  56. print("Injects require 3 parameters!")
  57. continue
  58. params = [{"time": float(inp[1]), "port": inp[2], "event": inp[3]}]
  59. elif inp[0] == "trace":
  60. set_defaults(inp, [None])
  61. params = [inp[1]]
  62. elif inp[0] == "pause":
  63. params = []
  64. elif inp[0] == "exit" or inp[0] == "quit":
  65. break
  66. elif inp[0] == "add_breakpoint":
  67. if len(inp) < 5:
  68. print("Breakpoint removal requires 2 parameters!")
  69. continue
  70. # Merge together the final part again
  71. inp = [inp[0], inp[1], " ".join(inp[2:-2]).replace("\\n", "\n"), get_bool(inp[-2]), get_bool(inp[-1])]
  72. print("Generated parameters: " + str(inp))
  73. params = inp[1:]
  74. elif inp[0] == "del_breakpoint":
  75. if len(inp) < 2:
  76. print("Breakpoint removal requires 1 parameter!")
  77. continue
  78. params = [inp[1]]
  79. elif inp[0] == "enable_breakpoint":
  80. action = "toggle_breakpoint"
  81. params = [inp[1], True]
  82. elif inp[0] == "disable_breakpoint":
  83. action = "toggle_breakpoint"
  84. params = [inp[1], False]
  85. elif inp[0] == "reset":
  86. params = []
  87. elif inp[0] == "backwards_step":
  88. params = []
  89. elif inp[0] == "help":
  90. print("Supported operations:")
  91. print(" simulate [termination_time]")
  92. print(" --> Simulate until termination time is reached")
  93. print(" big_step [termination_time]")
  94. print(" --> Simulate a single step, unless termination time is reached")
  95. print(" small_step [termination_time]")
  96. print(" --> Simulate a single internal simulation step")
  97. print(" Termination time is ONLY checked at the")
  98. print(" check_termination phase")
  99. print(" backwards_step")
  100. print(" --> Step back to the previous time")
  101. print(" realtime [termination_time [realtime_scale]]")
  102. print(" --> Simulate in realtime until simulation time is reached")
  103. print(" realtime_scale can speed up or slow down the pace")
  104. print(" god_event model_name attribute_name new_value")
  105. print(" --> Modify the internal state of an arbitrary model")
  106. print(" model_name should be the fully qualified name")
  107. print(" attribute_name is the name of the attribute to alter")
  108. print(" new_value is the value to assign")
  109. print(" new_value can only be a string due to string-only input")
  110. print(" inject time port_name event")
  111. print(" --> Put a user-defined event on an input port")
  112. print(" port_name should be a fully qualified port name")
  113. print(" event should be the event to put on it, string only")
  114. print(" trace [filename]")
  115. print(" --> Write out trace information to the specified file.")
  116. print(" Leave empty to disable tracing.")
  117. print(" add_breakpoint id function enabled disable_on_trigger")
  118. print(" --> Add a breakpoint that will pause simulation when function returns True")
  119. print(" the function should contain a definition of the 'breakpoint' function")
  120. print(" and must take 3 parameters: time, model and transitioned")
  121. print(" enabled indicates whether or not the breakpoint should be checked")
  122. print(" disable_on_trigger determines if the breakpoint should auto-disable")
  123. print(" after being triggered")
  124. print(" del_breakpoint id")
  125. print(" --> Remove a breakpoint")
  126. print(" enable_breakpoint id")
  127. print(" --> Enable the provided breakpoint")
  128. print(" disable_breakpoint id")
  129. print(" --> Disable the provided breakpoint")
  130. print(" reset")
  131. print(" --> Reset the simulation")
  132. print(" exit")
  133. print(" --> Stop the client")
  134. print(" pause")
  135. print(" --> Pause the simulation")
  136. print(" quit")
  137. print(" --> Stop the client")
  138. print("")
  139. print("Defaults: ")
  140. print(" termination_time --> 'inf'")
  141. print(" realtime_scale --> 1.0")
  142. continue
  143. else:
  144. print("Command not understood: " + str(inp))
  145. continue
  146. controller.addInput(Event(action, "request", params))
  147. import threading
  148. poll_thrd = threading.Thread(target=poll_port,args=[reply_port])
  149. poll_thrd.daemon = True
  150. poll_thrd.start()
  151. inp_thrd = threading.Thread(target=generate_input)
  152. inp_thrd.daemon = True
  153. inp_thrd.start()
  154. try:
  155. controller.start()
  156. finally:
  157. controller.stop()