client.py 6.3 KB

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