client.py 6.6 KB

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