main.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. from Tkinter import *
  2. import tkSimpleDialog
  3. import urllib
  4. import urllib2
  5. JUMP = 40
  6. MAX_WIDTH = 20 * JUMP
  7. MAX_HEIGHT = 20 * JUMP
  8. address = "http://127.0.0.1:8001"
  9. root = Tk()
  10. canvas = Canvas(root, width=MAX_WIDTH, height=MAX_HEIGHT, bg="white")
  11. canvas.pack()
  12. name = 0
  13. class FakeLayer():
  14. def __init__(self, address):
  15. self.types = {}
  16. self.sources = {}
  17. self.targets = {}
  18. self.attrs = {}
  19. def read_available_attributes(self, name):
  20. if self.types[name] == "const":
  21. return ["value"]
  22. else:
  23. return []
  24. def read_attribute(self, name, attr):
  25. return self.attr.get(name, {}).get(attr, None)
  26. def set_attribute(self, name, attr, value):
  27. self.attrs[name][attr] = value
  28. def instantiate_block(self, name, block_type):
  29. self.types[name] = block_type
  30. def instantiate_link(self, name, link_type, source, target):
  31. self.types[name] = link_type
  32. self.sources[name] = source
  33. self.targets[name] = target
  34. self.attrs[name] = {}
  35. def simulate(self):
  36. pass
  37. def step(self):
  38. pass
  39. def pause(self):
  40. pass
  41. attribute = []
  42. available_attrs = []
  43. simulation = []
  44. def poll(address):
  45. working_attribute = []
  46. working_available_attrs = []
  47. working_simulation = []
  48. while 1:
  49. returnvalue = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": "CBD_env"}))).read()
  50. if returnvalue.startswith("AVAILABLE:"):
  51. working_available_attrs.append(returnvalue.split(":", 1)[1])
  52. elif returnvalue.startswith("ATTRIBUTE:"):
  53. working_attribute.append(returnvalue.split(":", 1)[1])
  54. elif returnvalue.startswith("SIMULATION:"):
  55. working_simulation.append(returnvalue.split(":", 1)[1])
  56. elif returnvalue.startswith("FINISH_AVAILABLE"):
  57. available_attrs.append(working_available_attrs)
  58. working_available_attrs = []
  59. elif returnvalue.startswith("FINISH_ATTRIBUTE"):
  60. attributes.append(working_attributes)
  61. working_attributes = []
  62. elif returnvalue.startswith("FINISH_SIMULATION"):
  63. simulation.append(working_simulation)
  64. working_simulation = []
  65. class MvLayer():
  66. def __init__(self, address):
  67. import threading
  68. self.address = address
  69. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"CBD_env"', "username": "user_manager"}))).read()
  70. threading.Thread(target=poll, args=[address])
  71. def read_available_attributes(self, name):
  72. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"read_available_attributes"', "username": "CBD_env"}))).read()
  73. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % name, "username": "CBD_env"}))).read()
  74. while not available_attrs:
  75. time.sleep(0.1)
  76. return available_attrs.pop(0)
  77. def read_attribute(self, name, attr):
  78. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"read_attribute"', "username": "CBD_env"}))).read()
  79. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % name, "username": "CBD_env"}))).read()
  80. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % attr, "username": "CBD_env"}))).read()
  81. while not attribute:
  82. time.sleep(0.1)
  83. return attribute.pop(0)
  84. def set_attribute(self, name, attr, value):
  85. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"set_attribute"', "username": "CBD_env"}))).read()
  86. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % name, "username": "CBD_env"}))).read()
  87. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % attr, "username": "CBD_env"}))).read()
  88. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % value, "username": "CBD_env"}))).read()
  89. def instantiate_block(self, name, block_type):
  90. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"instantiate"', "username": "CBD_env"}))).read()
  91. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % (block_type), "username": "CBD_env"}))).read()
  92. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % (name), "username": "CBD_env"}))).read()
  93. def instantiate_link(self, name, link_type, source, target):
  94. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"instantiate"', "username": "CBD_env"}))).read()
  95. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % (link_type), "username": "CBD_env"}))).read()
  96. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % (name), "username": "CBD_env"}))).read()
  97. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % (source), "username": "CBD_env"}))).read()
  98. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % (target), "username": "CBD_env"}))).read()
  99. def simulate(self):
  100. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"simulate"', "username": "CBD_env"}))).read()
  101. def step(self):
  102. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"step"', "username": "CBD_env"}))).read()
  103. def pause(self):
  104. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"pause"', "username": "CBD_env"}))).read()
  105. def lower(value):
  106. return value / JUMP * JUMP
  107. def upper(value):
  108. return (value / JUMP + 1) * JUMP
  109. def avg(a, b):
  110. return float(a + b) / 2
  111. class InterfaceCore():
  112. mode = ""
  113. drawn = set()
  114. refs = dict()
  115. mappings = {"+": "AdditionBlock",
  116. "-": "NegationBlock",
  117. }
  118. #mv = MvLayer(address)
  119. mv = FakeLayer(address)
  120. def set_mode(self, mode):
  121. self.mode = mode
  122. def clicked(self, event):
  123. if self.mode not in ["+", "-"]:
  124. print("Cannot create something not guaranteed to be block type!")
  125. else:
  126. if self.find((event.x, event.y)):
  127. # Something already there, so don't add, but modify
  128. lname = self.find((event.x, event.y))
  129. attrs = self.mv.read_available_attributes(lname)
  130. if not attrs:
  131. print("No attrs to manage!")
  132. for attr in attrs:
  133. old_value = self.mv.read_attribute(lname, attr)
  134. new_value = tkSimpleDialog.askstring("Attribute modification", attr, initialvalue=old_value)
  135. self.mv.set_attribute(lname, attr, new_value)
  136. else:
  137. global name
  138. x = event.x
  139. y = event.y
  140. self.mv.instantiate_block(str(name), self.mappings[self.mode])
  141. r = canvas.create_rectangle(lower(x), lower(y), upper(x), upper(y), fill="white")
  142. t = canvas.create_text(avg(lower(x), upper(x)), avg(lower(y), upper(y)), text=self.mode, fill="black")
  143. b = (lower(x), lower(y), upper(x), upper(y), str(name))
  144. self.drawn.add(b)
  145. self.refs[str(name)] = [r, t]
  146. name += 1
  147. def find(self, location):
  148. x, y = location
  149. for e in self.drawn:
  150. if (e[0] <= x and
  151. e[1] <= y and
  152. e[2] >= x and
  153. e[3] >= y):
  154. return e[4]
  155. print("Found nothing at that location!")
  156. return []
  157. def draw(self, start, end):
  158. source = self.find(start)
  159. target = self.find(end)
  160. print("Connect from %s to %s" % (source, target))
  161. if source and target:
  162. if self.mode not in ["Link", "IC"]:
  163. print("Cannot create something not guaranteed to be link type!")
  164. else:
  165. global name
  166. self.mv.instantiate_link(str(name), self.mode, source, target)
  167. self.refs[str(name)] = [canvas.create_line(start[0], start[1], end[0], end[1], fill="black", arrow=LAST)]
  168. name += 1
  169. core = InterfaceCore()
  170. def addition():
  171. core.set_mode("+")
  172. def negation():
  173. core.set_mode("-")
  174. def link():
  175. core.set_mode("Link")
  176. def clicked(event):
  177. core.clicked(event)
  178. def draw(event):
  179. global start_location
  180. start_location = (event.x, event.y)
  181. def release(event):
  182. core.draw(start_location, (event.x, event.y))
  183. def simulate():
  184. core.mv.simulate()
  185. def step():
  186. core.mv.step()
  187. def pause():
  188. core.mv.pause()
  189. Button(root, text="+", command=addition).pack()
  190. Button(root, text="-", command=negation).pack()
  191. Button(root, text="Link", command=link).pack()
  192. Button(root, text="SIM", command=simulate).pack()
  193. Button(root, text="STEP", command=step).pack()
  194. Button(root, text="PAUSE", command=pause).pack()
  195. core.canvas = canvas
  196. for i in range(JUMP, MAX_HEIGHT, JUMP):
  197. canvas.create_line(0, i, MAX_HEIGHT, i, fill="grey")
  198. for i in range(JUMP, MAX_WIDTH, JUMP):
  199. canvas.create_line(i, 0, i, MAX_WIDTH, fill="grey")
  200. canvas.bind("<Button-1>", clicked)
  201. canvas.bind("<Button-3>", draw)
  202. canvas.bind("<ButtonRelease-3>", release)
  203. root.mainloop()