client.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. print("SCCD NetLogo CLI interface")
  2. import sys
  3. import time
  4. from sccd.runtime.statecharts_core import Event
  5. def set_defaults(inp, defaultlist):
  6. for i, v in enumerate(defaultlist):
  7. if len(inp) == i + 1:
  8. inp.append(v)
  9. def get_bool(val):
  10. if val.lower() in ["true", "yes", "1"]:
  11. return True
  12. else:
  13. return False
  14. def poll_port(port):
  15. while 1:
  16. try:
  17. print(port.fetch(-1))
  18. except:
  19. pass
  20. def raw_inputter():
  21. print("READY for input")
  22. print("Type 'help' for information")
  23. while 1:
  24. inp = raw_input().split(" ")
  25. action = inp[0]
  26. if inp[0] == "god_event":
  27. if len(inp) != 4:
  28. print("God Events require 3 parameters!")
  29. continue
  30. params = [{"turtle": inp[1], "attribute": inp[2], "value": inp[3]}]
  31. elif inp[0] in ["pause", "step", "continuous", "reset"]:
  32. params = []
  33. elif inp[0] == "realtime":
  34. set_defaults(inp, [1.0])
  35. params = [{"realtime_scale": float(inp[1])}]
  36. elif inp[0] == "exit" or inp[0] == "quit":
  37. break
  38. elif inp[0] == "add_breakpoint":
  39. if len(inp) < 5:
  40. print("Breakpoint removal requires 2 parameters!")
  41. continue
  42. # Merge together the final part again
  43. inp = [inp[0], inp[1], " ".join(inp[2:-2]).replace("\\n", "\n"), get_bool(inp[-2]), get_bool(inp[-1])]
  44. print("Generated parameters: " + str(inp))
  45. params = inp[1:]
  46. elif inp[0] == "del_breakpoint":
  47. if len(inp) < 2:
  48. print("Breakpoint removal requires 1 parameter!")
  49. continue
  50. params = [inp[1]]
  51. elif inp[0] == "enable_breakpoint":
  52. action = "toggle_breakpoint"
  53. params = [inp[1], True]
  54. elif inp[0] == "disable_breakpoint":
  55. action = "toggle_breakpoint"
  56. params = [inp[1], False]
  57. elif inp[0] == "help":
  58. print("Supported operations:")
  59. print(" continuous")
  60. print(" --> Simulate continuously")
  61. print(" step")
  62. print(" --> Simulate a single step")
  63. print(" god_event turtle_id attribute_name new_value")
  64. print(" --> Modify the internal state of an arbitrary turtle")
  65. print(" turtle_id should be a turtle id")
  66. print(" attribute_name is the name of the attribute to alter")
  67. print(" new_value is the value to assign")
  68. print(" new_value can only be a string due to string-only input")
  69. print(" add_breakpoint id function enabled disable_on_trigger")
  70. print(" --> Add a breakpoint that will pause simulation when function returns True")
  71. print(" the function should contain a definition of the 'breakpoint' function")
  72. print(" and must take 3 parameters: time, model and transitioned")
  73. print(" enabled indicates whether or not the breakpoint should be checked")
  74. print(" disable_on_trigger determines if the breakpoint should auto-disable")
  75. print(" after being triggered")
  76. print(" del_breakpoint id")
  77. print(" --> Remove a breakpoint")
  78. print(" enable_breakpoint id")
  79. print(" --> Enable the provided breakpoint")
  80. print(" disable_breakpoint id")
  81. print(" --> Disable the provided breakpoint")
  82. print(" reset")
  83. print(" --> Reset the simulation")
  84. print(" exit")
  85. print(" --> Stop the client")
  86. print(" pause")
  87. print(" --> Pause the simulation")
  88. print(" quit")
  89. print(" --> Stop the client")
  90. continue
  91. else:
  92. print("Command not understood: " + str(inp))
  93. continue
  94. controller.addInput(Event(action, "request", params))
  95. import simulator
  96. controller = simulator.Controller("BouncingBalls.nlogo")
  97. reply_port = controller.addOutputListener('reply')
  98. try:
  99. import threading
  100. thrd_o = threading.Thread(target=poll_port,args=[reply_port])
  101. thrd_o.daemon = True
  102. thrd_o.start()
  103. thrd_i = threading.Thread(target=raw_inputter)
  104. thrd_i.daemon = True
  105. thrd_i.start()
  106. controller.start()
  107. finally:
  108. controller.stop()