primitives.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. import time as python_time
  2. import json
  3. import sys
  4. class InterpretedFunctionFinished(Exception):
  5. """Exception to indicate the result value of an interpreted function, as a return
  6. cannot be used."""
  7. def __init__(self, value):
  8. Exception.__init__(self)
  9. self.result = value
  10. class SleepKernel(Exception):
  11. """Exception to indicate the kernel to sleep for some time."""
  12. def __init__(self, timeout, interruptable):
  13. Exception.__init__(self)
  14. self.timeout = timeout
  15. self.interruptable = interruptable
  16. # Functions annotated with __exception_return use the JIT's calling convention instead of
  17. # returnvalue is not modified.
  18. #
  19. # ### Rationale for __exception_return
  20. #
  21. # __exception_return is a useful mechanism because it allows us to have a __call_function
  22. # implementation that has O(1) state read overhead. A previous implementation of
  23. # __call_function checked if the caller's frame had been popped whenever
  24. # ModelverseKernel.execute_yield threw a StopIteration exception. However, that incurs O(n) overhead
  25. # _per call,_ where n is the number of StopIteration exceptions that are thrown during the call.
  26. # O(n) is pretty bad, but this actually becomes O(n * m) when m calls to __call_function are
  27. # nested. And that's just not acceptable.
  28. # __exception_return requires kernel support, but I think the complexity gains are well worth it;
  29. # I reckon JIT-to-interpreter switches aren't going to get a whole lot cheaper than this.
  30. EXCEPTION_RETURN_KEY = "__exception_return"
  31. """A dictionary key for functions which request that the kernel throw a InterpretedFunctionFinished
  32. exception with the return value instead of injecting the return value in the caller's frame."""
  33. def integer_subtraction(a, b, **remainder):
  34. if 'value' not in a:
  35. a['value'], = yield [("RV", [a['id']])]
  36. if 'value' not in b:
  37. b['value'], = yield [("RV", [b['id']])]
  38. yield [("RETURN", [{'value': a['value'] - b['value']}])]
  39. def integer_addition(a, b, **remainder):
  40. if 'value' not in a:
  41. a['value'], = yield [("RV", [a['id']])]
  42. if 'value' not in b:
  43. b['value'], = yield [("RV", [b['id']])]
  44. yield [("RETURN", [{'value': a['value'] + b['value']}])]
  45. def integer_multiplication(a, b, **remainder):
  46. if 'value' not in a:
  47. a['value'], = yield [("RV", [a['id']])]
  48. if 'value' not in b:
  49. b['value'], = yield [("RV", [b['id']])]
  50. yield [("RETURN", [{'value': a['value'] * b['value']}])]
  51. def integer_division(a, b, **remainder):
  52. if 'value' not in a:
  53. a['value'], = yield [("RV", [a['id']])]
  54. if 'value' not in b:
  55. b['value'], = yield [("RV", [b['id']])]
  56. yield [("RETURN", [{'value': int(a['value']) // b['value']}])]
  57. def integer_lt(a, b, **remainder):
  58. if 'value' not in a:
  59. a['value'], = yield [("RV", [a['id']])]
  60. if 'value' not in b:
  61. b['value'], = yield [("RV", [b['id']])]
  62. yield [("RETURN", [{'value': a['value'] < b['value']}])]
  63. def bool_and(a, b, **remainder):
  64. if 'value' not in a:
  65. a['value'], = yield [("RV", [a['id']])]
  66. if 'value' not in b:
  67. b['value'], = yield [("RV", [b['id']])]
  68. yield [("RETURN", [{'value': a['value'] and b['value']}])]
  69. def bool_or(a, b, **remainder):
  70. if 'value' not in a:
  71. a['value'], = yield [("RV", [a['id']])]
  72. if 'value' not in b:
  73. b['value'], = yield [("RV", [b['id']])]
  74. yield [("RETURN", [{'value': a['value'] or b['value']}])]
  75. def float_subtraction(a, b, **remainder):
  76. if 'value' not in a:
  77. a['value'], = yield [("RV", [a['id']])]
  78. if 'value' not in b:
  79. b['value'], = yield [("RV", [b['id']])]
  80. yield [("RETURN", [{'value': a['value'] - b['value']}])]
  81. def float_addition(a, b, **remainder):
  82. if 'value' not in a:
  83. a['value'], = yield [("RV", [a['id']])]
  84. if 'value' not in b:
  85. b['value'], = yield [("RV", [b['id']])]
  86. yield [("RETURN", [{'value': a['value'] + b['value']}])]
  87. def float_multiplication(a, b, **remainder):
  88. if 'value' not in a:
  89. a['value'], = yield [("RV", [a['id']])]
  90. if 'value' not in b:
  91. b['value'], = yield [("RV", [b['id']])]
  92. yield [("RETURN", [{'value': a['value'] * b['value']}])]
  93. def float_division(a, b, **remainder):
  94. if 'value' not in a:
  95. a['value'], = yield [("RV", [a['id']])]
  96. if 'value' not in b:
  97. b['value'], = yield [("RV", [b['id']])]
  98. yield [("RETURN", [{'value': float(a['value']) / b['value']}])]
  99. def float_lt(a, b, **remainder):
  100. if 'value' not in a:
  101. a['value'], = yield [("RV", [a['id']])]
  102. if 'value' not in b:
  103. b['value'], = yield [("RV", [b['id']])]
  104. yield [("RETURN", [{'value': a['value'] < b['value']}])]
  105. def string_join(a, b, **remainder):
  106. if 'value' not in a:
  107. a['value'], = yield [("RV", [a['id']])]
  108. if 'value' not in b:
  109. b['value'], = yield [("RV", [b['id']])]
  110. yield [("RETURN", [{'value': str(a['value']) + str(b['value'])}])]
  111. def string_split(a, b, **remainder):
  112. # TODO make non-primitive, though compiled
  113. if 'value' not in a:
  114. a['value'], = yield [("RV", [a['id']])]
  115. if 'value' not in b:
  116. b['value'], = yield [("RV", [b['id']])]
  117. result = a['value'].split(b['value'])
  118. elems = yield [("CN", [])] + [("CNV", [v]) for v in result]
  119. new_val = elems[0]
  120. yield [("CD", [new_val, i, v]) for i, v in enumerate(elems[1:])]
  121. yield [("RETURN", [{'id': new_val}])]
  122. def string_get(a, b, **remainder):
  123. if 'value' not in a:
  124. a['value'], = yield [("RV", [a['id']])]
  125. if 'value' not in b:
  126. b['value'], = yield [("RV", [b['id']])]
  127. yield [("RETURN", [{'value': a['value'][b['value']]}])]
  128. def string_len(a, **remainder):
  129. if 'value' not in a:
  130. a['value'], = yield [("RV", [a['id']])]
  131. yield [("RETURN", [{'value': len(a['value'])}])]
  132. def value_eq(a, b, **remainder):
  133. if 'value' not in a:
  134. a['value'], = yield [("RV", [a['id']])]
  135. if 'value' not in b:
  136. b['value'], = yield [("RV", [b['id']])]
  137. yield [("RETURN", [{'value': a['value'] == b['value']}])]
  138. def element_eq(a, b, **remainder):
  139. if "id" not in a:
  140. #print("MATERIALIZING A element_eq")
  141. a['id'], = yield [("CNV", [a['value']])]
  142. if "id" not in b:
  143. #print("MATERIALIZING B element_eq")
  144. b['id'], = yield [("CNV", [b['value']])]
  145. yield [("RETURN", [{'value': a['id'] == b['id']}])]
  146. def cast_string(a, **remainder):
  147. if 'value' not in a:
  148. a['value'], = yield [("RV", [a['id']])]
  149. if isinstance(a['value'], dict):
  150. yield [("RETURN", [{'value': str(a['value']['value'])}])]
  151. else:
  152. yield [("RETURN", [{'value': str(a['value'])}])]
  153. def cast_float(a, **remainder):
  154. if 'value' not in a:
  155. a['value'], = yield [("RV", [a['id']])]
  156. try:
  157. yield [("RETURN", [{'value': float(a['value'])}])]
  158. except ValueError:
  159. yield [("RETURN", [{'value': -1.0}])]
  160. def cast_boolean(a, **remainder):
  161. if 'value' not in a:
  162. a['value'], = yield [("RV", [a['id']])]
  163. try:
  164. yield [("RETURN", [{'value': bool(a['value'])}])]
  165. except ValueError:
  166. yield [("RETURN", [{'value': False}])]
  167. def cast_integer(a, **remainder):
  168. if 'value' not in a:
  169. a['value'], = yield [("RV", [a['id']])]
  170. try:
  171. yield [("RETURN", [{'value': int(a['value'])}])]
  172. except ValueError:
  173. yield [("RETURN", [{'value': -1}])]
  174. def cast_value(a, **remainder):
  175. if 'value' not in a:
  176. a['value'], = yield [("RV", [a['id']])]
  177. if isinstance(a['value'], dict):
  178. yield [("RETURN", [{'value': str(a['value']['value'])}])]
  179. else:
  180. yield [("RETURN", [{'value': json.dumps(a['value'])}])]
  181. def cast_id(a, **remainder):
  182. if "id" not in a:
  183. #print("MATERIALIZING A cast_id")
  184. a['id'], = yield [("CNV", [a['value']])]
  185. yield [("RETURN", [{'value': str(a['id'])}])]
  186. def dict_add_fast(a, b, c, **remainder):
  187. # TODO deprecate, as dict_add is now also efficient
  188. if "value" not in b:
  189. b['value'], = yield [("RV", [b['id']])]
  190. if "id" not in c:
  191. #print("MATERIALIZING C dict_add_fast")
  192. c['id'], = yield [("CNV", [c['value']])]
  193. yield [("CD", [a['id'], b['value'], c['id']])]
  194. yield [("RETURN", [a])]
  195. def dict_delete(a, b, **remainder):
  196. if "value" not in b:
  197. b['value'], = yield [("RV", [b['id']])]
  198. edge, = yield [("RDE", [a['id'], b['value']])]
  199. if edge is None:
  200. print("Failed dict_delete for value '%s'!" % b['value'])
  201. keys, = yield [("RDK", [a['id']])]
  202. keys = yield [("RV", [i]) for i in keys]
  203. print("Keys: " + str(keys))
  204. raise Exception()
  205. yield [("DE", [edge])]
  206. yield [("RETURN", [a])]
  207. def dict_delete_node(a, b, **remainder):
  208. edge, = yield [("RDNE", [a['id'], b['id']])]
  209. if edge is None:
  210. print("Failed dict_delete_node!")
  211. yield [("DE", [edge])]
  212. yield [("RETURN", [a])]
  213. def dict_read(a, b, **remainder):
  214. if "value" not in b:
  215. b['value'], = yield [("RV", [b['id']])]
  216. result, = yield [("RD", [a['id'], b['value']])]
  217. yield [("RETURN", [{'id': result}])]
  218. def dict_read_edge(a, b, **remainder):
  219. if "value" not in b:
  220. b['value'], = yield [("RV", [b['id']])]
  221. result, = yield [("RDE", [a['id'], b['value']])]
  222. yield [("RETURN", [{'id': result}])]
  223. def dict_read_node(a, b, **remainder):
  224. result, = yield [("RDN", [a['id'], b['id']])]
  225. yield [("RETURN", [{'id': result}])]
  226. def dict_in(a, b, **remainder):
  227. if "value" not in b:
  228. b['value'], = yield [("RV", [b['id']])]
  229. value, = yield [("RD", [a['id'], b['value']])]
  230. yield [("RETURN", [{'value': value is not None}])]
  231. def dict_in_node(a, b, **remainder):
  232. if "id" not in b:
  233. # Not even allocated the node, so it is certain not to be in the dictionary
  234. yield [("RETURN", [{'value': False}])]
  235. value, = yield [("RDN", [a['id'], b['id']])]
  236. yield [("RETURN", [{'value': value is not None}])]
  237. def dict_keys(a, **remainder):
  238. keys, result = yield [("RDK", [a['id']]), ("CN", [])]
  239. edges = yield [("CE", [result, result]) for _ in keys]
  240. _ = yield [("CE", [edge, key]) for edge, key in zip(edges, keys)]
  241. yield [("RETURN", [{'id': result}])]
  242. def is_physical_int(a, **remainder):
  243. if "value" not in a:
  244. a['value'], = yield [("RV", [a['id']])]
  245. try:
  246. yield [("RETURN", [{'value': isinstance(a['value'], int) or isinstance(a['value'], long)}])]
  247. except NameError:
  248. yield [("RETURN", [{'value': isinstance(a['value'], int)}])]
  249. def is_physical_string(a, **remainder):
  250. if "value" not in a:
  251. a['value'], = yield [("RV", [a['id']])]
  252. try:
  253. yield [("RETURN", [{'value': isinstance(a['value'], str) or isinstance(a['value'], unicode)}])]
  254. except NameError:
  255. yield [("RETURN", [{'value': isinstance(a['value'], str)}])]
  256. def is_physical_float(a, **remainder):
  257. if "value" not in a:
  258. a['value'], = yield [("RV", [a['id']])]
  259. yield [("RETURN", [{'value': isinstance(a['value'], float)}])]
  260. def is_physical_boolean(a, **remainder):
  261. if "value" not in a:
  262. a['value'], = yield [("RV", [a['id']])]
  263. yield [("RETURN", [{'value': isinstance(a['value'], bool)}])]
  264. def is_physical_action(a, **remainder):
  265. if "value" not in a:
  266. a['value'], = yield [("RV", [a['id']])]
  267. yield [("RETURN", [{'value': isinstance(a['value'], dict) and a['value']["value"] in ["if", "while", "assign", "call", "break", "continue", "return", "resolve", "access", "constant", "global", "declare"]}])]
  268. def is_physical_none(a, **remainder):
  269. if a['id'] is None:
  270. yield [("RETURN", [{"value": True}])]
  271. if "value" not in a:
  272. a['value'], = yield [("RV", [a['id']])]
  273. elif a['value'] is None:
  274. yield [("RETURN", [{"value": True}])]
  275. yield [("RETURN", [{'value': isinstance(a['value'], dict) and a['value']["value"] == "none"}])]
  276. def create_node(**remainder):
  277. result, = yield [("CN", [])]
  278. yield [("RETURN", [{'id': result}])]
  279. def create_edge(a, b, **remainder):
  280. if "id" not in a:
  281. #print("MATERIALIZING A create_edge")
  282. a['id'], = yield [("CNV", [a['value']])]
  283. if "id" not in b:
  284. #print("MATERIALIZING B create_edge")
  285. b['id'], = yield [("CNV", [b['value']])]
  286. result, = yield [("CE", [a['id'], b['id']])]
  287. yield [("RETURN", [{'id': result}])]
  288. def create_value(a, **remainder):
  289. if "value" not in a:
  290. a['value'], = yield [("RV", [a['id']])]
  291. yield [("RETURN", [{'value': a['value']}])]
  292. def read_nr_out(a, **remainder):
  293. if "id" not in a:
  294. yield [("RETURN", [{'value': 0}])]
  295. else:
  296. outgoing, = yield [("RO", [a['id']])]
  297. yield [("RETURN", [{'value': len(outgoing)}])]
  298. def read_out(a, b, root, **remainder):
  299. if "id" not in a:
  300. a['id'], = yield [("CNV", [a['value']])]
  301. if "value" not in b:
  302. b['value'], = yield [("RV", [b['id']])]
  303. outgoing, = yield [("RO", [a['id']])]
  304. yield [("RETURN", [{'id': sorted(outgoing)[b['value']] if len(outgoing) > b['value'] else root}])]
  305. def read_nr_in(a, **remainder):
  306. if "id" not in a:
  307. yield [("RETURN", [{'value': 0}])]
  308. else:
  309. incoming, = yield [("RI", [a['id']])]
  310. yield [("RETURN", [{'value': len(incoming)}])]
  311. def read_in(a, b, root, **remainder):
  312. if "id" not in a:
  313. a['id'], = yield [("CNV", [a['value']])]
  314. if "value" not in b:
  315. b['value'], = yield [("RV", [b['id']])]
  316. incoming, = yield [("RI", [a['id']])]
  317. yield [("RETURN", [{'id': sorted(incoming)[b['value']] if len(incoming) > b['value'] else root}])]
  318. def read_edge_src(a, **remainder):
  319. result, = yield [("RE", [a['id']])]
  320. yield [("RETURN", [{'id': result[0]}])]
  321. def read_edge_dst(a, **remainder):
  322. result, = yield [("RE", [a['id']])]
  323. yield [("RETURN", [{'id': result[1]}])]
  324. def delete_element(a, **remainder):
  325. if "id" not in a:
  326. yield [("RETURN", [{'value': False}])]
  327. edge, = yield [("RE", [a['id']])]
  328. if edge[0] is None:
  329. # Not an edge:
  330. yield [("DN", [a['id']])]
  331. yield [("RETURN", [{'value': False}])]
  332. else:
  333. yield [("DE", [a['id']])]
  334. yield [("RETURN", [{'value': True}])]
  335. def read_root(root, **remainder):
  336. yield [("RETURN", [{'id': root}])]
  337. def is_edge(a, **remainder):
  338. if "id" not in a:
  339. yield [("RETURN", [{'value': False}])]
  340. edge, = yield [("RE", [a['id']])]
  341. yield [("RETURN", [{'value': edge[0] is not None}])]
  342. def log(a, **remainder):
  343. if "value" not in a:
  344. a['value'], = yield [("RV", [a['id']])]
  345. print("== LOG == " + str(a['value']))
  346. yield [("RETURN", [a])]
  347. def read_taskroot(task_root, **remainder):
  348. yield [("RETURN", [{'id': task_root}])]
  349. def time(**remainder):
  350. yield [("RETURN", [{'value': python_time.time()}])]
  351. def hash(a, **remainder):
  352. if "value" not in a:
  353. a['value'], = yield [("RV", [a['id']])]
  354. import hashlib
  355. try:
  356. value = hashlib.sha512(a['value']).hexdigest()
  357. except TypeError:
  358. value = hashlib.sha512(a['value'].encode()).hexdigest()
  359. yield [("RETURN", [{'value': value}])]
  360. def __sleep(a, b, **remainder):
  361. if "value" not in a:
  362. a['value'], = yield [("RV", [a['id']])]
  363. if "value" not in b:
  364. b['value'], = yield [("RV", [b['id']])]
  365. timeout = a['value']
  366. interruptable = b['value']
  367. yield [("SLEEP", [timeout, interruptable])]
  368. yield [("RETURN", [a])]
  369. def is_error(a, **remainder):
  370. if a['id'] is None:
  371. yield [("RETURN", [{'value': True}])]
  372. else:
  373. yield [("RETURN", [{'value': False}])]
  374. def list_sort(a, **remainder):
  375. elements, = yield [("RO", [a['id']])]
  376. values = yield [("RD", [a['id'], i]) for i in range(len(elements))]
  377. values = yield [("RV", [i]) for i in values]
  378. new_list, = yield [("CN", [])]
  379. values = yield [("CNV", [v]) for v in sorted(values)]
  380. yield [("CD", [new_list, i, v]) for i, v in enumerate(values)]
  381. yield [("RETURN", [{'id': new_list}])]
  382. def bool_not(a, **remainder):
  383. if 'value' not in a:
  384. a['value'], = yield [("RV", [a['id']])]
  385. yield [("RETURN", [{'value': not a['value']}])]