client.py 6.5 KB

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