Test_meta_model.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. import io
  2. import os
  3. import sys
  4. import unittest
  5. sys.path.insert(
  6. 0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../"))
  7. )
  8. from api.od import ODAPI
  9. from bootstrap.scd import bootstrap_scd
  10. from transformation.schedule.rule_scheduler import RuleScheduler
  11. from state.devstate import DevState
  12. from transformation.ramify import ramify
  13. from util import loader
  14. class Test_Meta_Model(unittest.TestCase):
  15. @classmethod
  16. def setUpClass(cls):
  17. cls.dir = os.path.dirname(__file__)
  18. state = DevState()
  19. scd_mmm = bootstrap_scd(state)
  20. with open(f"{cls.dir}/models/mm_petrinet.od") as file:
  21. mm_s = file.read()
  22. with open(f"{cls.dir}/models/m_petrinet.od") as file:
  23. m_s = file.read()
  24. mm = loader.parse_and_check(state, mm_s, scd_mmm, "mm")
  25. m = loader.parse_and_check(state, m_s, mm, "m")
  26. mm_rt_ramified = ramify(state, mm)
  27. cls.model_param = (state, m, mm)
  28. cls.generator_param = (state, mm, mm_rt_ramified)
  29. def setUp(self):
  30. self.model = ODAPI(*self.model_param)
  31. self.out = io.StringIO()
  32. self.generator = RuleScheduler(
  33. *self.generator_param,
  34. directory=self.dir + "/models",
  35. verbose=True,
  36. outstream=self.out,
  37. )
  38. def _test_conformance(
  39. self, file: str, expected_substr_err: dict[tuple[str, str], list[list[str]]]
  40. ) -> None:
  41. try:
  42. self.generator.load_schedule(f"schedule/{file}")
  43. errors = self.out.getvalue().split("\u25b8")[1:]
  44. if len(errors) != len(expected_substr_err.keys()):
  45. assert len(errors) == len(expected_substr_err.keys())
  46. for err in errors:
  47. error_lines = err.strip().split("\n")
  48. line = error_lines[0]
  49. for key_pattern in expected_substr_err.keys():
  50. if (key_pattern[0] in line) and (key_pattern[1] in line):
  51. key = key_pattern
  52. break
  53. else:
  54. assert False
  55. expected = expected_substr_err[key]
  56. if (len(error_lines) - 1) != len(expected):
  57. assert (len(error_lines) - 1) == len(expected)
  58. it = error_lines.__iter__()
  59. it.__next__()
  60. for err_line in it:
  61. if not any(
  62. all(exp in err_line for exp in line_exp)
  63. for line_exp in expected
  64. ):
  65. assert False
  66. expected_substr_err.pop(key)
  67. except AssertionError:
  68. raise
  69. except Exception as e:
  70. assert False
  71. def test_no_start(self):
  72. self._test_conformance("no_start.od", {("Start", "Cardinality"): []})
  73. def test_no_end(self):
  74. self._test_conformance("no_end.od", {("End", "Cardinality"): []})
  75. def test_multiple_start(self):
  76. self._test_conformance("multiple_start.od", {("Start", "Cardinality"): []})
  77. def test_multiple_end(self):
  78. self._test_conformance("multiple_end.od", {("End", "Cardinality"): []})
  79. def test_connections_start(self):
  80. # try to load the following schedule.
  81. # The schedules contains happy day nodes and faulty nodes.
  82. # Use the error messages to select error location and further validate the multiple reasons of failure.
  83. self._test_conformance(
  84. "connections_start.od",
  85. {
  86. ("Start", "start"): [ # locate failure (contains these two substrings), make sure other do not fully overlap -> flakey test
  87. ["input exec", "foo_in", "exist"], # 4 total reasons, a reason contains these three substrings
  88. ["output exec", "out", "multiple"], # a reason will match to exactly one subnstring list
  89. ["output exec", "foo_out", "exist"],
  90. ["input data", "in", "exist"],
  91. ]
  92. },
  93. )
  94. def test_connections_end(self):
  95. self._test_conformance(
  96. "connections_end.od",
  97. {
  98. ("End", "end"): [
  99. ["input exec", "foo_in", "exist"],
  100. ["output exec", "foo_out", "exist"],
  101. ["input data", "in", "multiple"],
  102. ["input data", "out2", "exist"],
  103. ["output data", "out", "exist"],
  104. ]
  105. },
  106. )
  107. def test_connections_match(self):
  108. self._test_conformance(
  109. "connections_match.od",
  110. {
  111. ("Match", "m_foo"): [
  112. ["input exec", "foo_in", "exist"],
  113. ["output exec", "foo", "exist"],
  114. ["output exec", "fail", "multiple"],
  115. ["input data", "foo_in", "exist"],
  116. ["input data", "in", "multiple"],
  117. ["output data", "foo_out", "exist"],
  118. ]
  119. },
  120. )
  121. def test_connections_rewrite(self):
  122. self._test_conformance(
  123. "connections_rewrite.od",
  124. {
  125. ("Rewrite", "r_foo1"): [
  126. ["input exec", "foo_in", "exist"],
  127. ["output exec", "foo", "exist"],
  128. ],
  129. ("Rewrite", "r_foo2"): [
  130. ["output exec", "out", "multiple"],
  131. ["input data", "foo_in", "exist"],
  132. ["input data", "in", "multiple"],
  133. ["output data", "foo_out", "exist"],
  134. ],
  135. },
  136. )
  137. def test_connections_action(self):
  138. self._test_conformance(
  139. "connections_action.od",
  140. {
  141. ("Action", "a_foo1"): [
  142. ["input exec", "foo_in", "exist"],
  143. ["output exec", "out", "multiple"],
  144. ["output exec", "foo", "exist"],
  145. ["input data", "in1", "multiple"],
  146. ],
  147. ("Action", "a_foo2"): [
  148. ["input exec", "in", "exist"],
  149. ["output exec", "out3", "multiple"],
  150. ["output exec", "out", "exist"],
  151. ["input data", "in", "exist"],
  152. ["output data", "out", "exist"],
  153. ],
  154. },
  155. )
  156. def test_connections_modify(self):
  157. #TODO:
  158. # see test_connections_merge
  159. self._test_conformance(
  160. "connections_modify.od",
  161. {
  162. ("Invalid source", "Conn_exec"): [],
  163. ("Invalid target", "Conn_exec"): [],
  164. ("Modify", "m_foo"): [
  165. ["input data", "foo_in", "exist"],
  166. ["output data", "foo_out", "exist"],
  167. ["input data", "in", "multiple"],
  168. ],
  169. ("Modify", "m_exec"): [
  170. ["input exec", "in", "exist"],
  171. ["input exec", "in", "exist"],
  172. ["output exec", "out", "exist"],
  173. ]
  174. },
  175. )
  176. def test_connections_merge(self):
  177. #TODO:
  178. # mm:
  179. # association Conn_exec [0..*] Exec -> Exec [0..*] {
  180. # ...;
  181. # }
  182. # m:
  183. # Conn_exec ( Data -> Exec) {...;} -> Invalid source type 'Merge' for link '__Conn_exec_3:Conn_exec' (1)
  184. # -> Invalid target type 'End' for link '__Conn_exec_3:Conn_exec' (2)
  185. # Conn_exec ( Exec -> Data) {...;} -> No error at all, inconsistent and unexpected behaviour (3)
  186. # different combinations behave unexpected
  187. self._test_conformance(
  188. "connections_merge.od",
  189. {
  190. ("Invalid source", "Conn_exec"): [], # (1), expected
  191. ("Invalid target", "Conn_exec"): [], # (2), invalid error, should not be shown
  192. ("Merge", "m_foo"): [
  193. ["input data", "foo_in", "exist"],
  194. ["input data", "in2", "multiple"],
  195. ["output data", "foo_out", "exist"],
  196. ],
  197. ("Merge", "m_exec"): [ # (3), checked in Merge itself
  198. ["input exec", "in", "exist"],
  199. ["output exec", "out", "exist"],
  200. ],
  201. },
  202. )
  203. def test_connections_store(self):
  204. self._test_conformance(
  205. "connections_store.od",
  206. {
  207. ("Store", "s_foo"): [
  208. ["input exec", "foo", "exist"],
  209. ["output exec", "out", "multiple"],
  210. ["output exec", "foo", "exist"],
  211. ["input data", "foo_in", "exist"],
  212. ["output data", "foo_out", "exist"],
  213. ["input data", "2", "multiple"],
  214. ],
  215. },
  216. )
  217. def test_connections_schedule(self):
  218. self._test_conformance(
  219. "connections_schedule.od",
  220. {
  221. ("Schedule", "s_foo"): [
  222. ["output exec", "out", "multiple"],
  223. ["input data", "in2", "multiple"],
  224. ]
  225. },
  226. )
  227. def test_connections_loop(self):
  228. self._test_conformance(
  229. "connections_loop.od",
  230. {
  231. ("Loop", "l_foo"): [
  232. ["input exec", "foo_in", "exist"],
  233. ["output exec", "out", "multiple"],
  234. ["output exec", "foo", "exist"],
  235. ["input data", "foo_in", "exist"],
  236. ["output data", "foo_out", "exist"],
  237. ["input data", "in", "multiple"],
  238. ]
  239. },
  240. )
  241. def test_connections_print(self):
  242. self._test_conformance(
  243. "connections_print.od",
  244. {
  245. ("Print", "p_foo"): [
  246. ["input exec", "foo_in", "exist"],
  247. ["output exec", "out", "multiple"],
  248. ["output exec", "foo", "exist"],
  249. ["input data", "foo_in", "exist"],
  250. ["output data", "out", "exist"],
  251. ["input data", "in", "multiple"],
  252. ]
  253. },
  254. )
  255. def test_fields_start(self):
  256. self._test_conformance(
  257. "fields_start.od",
  258. {
  259. ("Start", "Cardinality"): [],
  260. ("Start", "string"): [
  261. ["Unexpected type", "ports_exec_out", "str"],
  262. ["Unexpected type", "ports_data_out", "str"],
  263. ],
  264. ("Start", '"int"'): [ # included " to avoid flakey test
  265. ["Unexpected type", "ports_exec_out", "int"],
  266. ["Unexpected type", "ports_data_out", "int"],
  267. ],
  268. ("Start", "tuple"): [
  269. ["Unexpected type", "ports_exec_out", "tuple"],
  270. ["Unexpected type", "ports_data_out", "tuple"],
  271. ],
  272. ("Start", "dict"): [
  273. ["Unexpected type", "ports_exec_out", "dict"],
  274. ["Unexpected type", "ports_data_out", "dict"],
  275. ],
  276. ("Start", "none"): [
  277. ["Unexpected type", "ports_exec_out", "NoneType"],
  278. ["Unexpected type", "ports_data_out", "NoneType"],
  279. ],
  280. ("Start", "invalid"): [
  281. ["Invalid python", "ports_exec_out"],
  282. ["Invalid python", "ports_data_out"],
  283. ],
  284. ("Start", "subtype"): [
  285. ["Unexpected type", "ports_exec_out", "list"],
  286. ["Unexpected type", "ports_data_out", "list"],
  287. ],
  288. ("Start", "code"): [
  289. ["Unexpected type", "ports_exec_out"],
  290. ["Unexpected type", "ports_data_out"],
  291. ],
  292. },
  293. )
  294. def test_fields_end(self):
  295. self._test_conformance(
  296. "fields_end.od",
  297. {
  298. ("End", "Cardinality"): [],
  299. ("End", "string"): [
  300. ["Unexpected type", "ports_exec_in", "str"],
  301. ["Unexpected type", "ports_data_in", "str"],
  302. ],
  303. ("End", '"int"'): [
  304. ["Unexpected type", "ports_exec_in", "int"],
  305. ["Unexpected type", "ports_data_in", "int"],
  306. ],
  307. ("End", "tuple"): [
  308. ["Unexpected type", "ports_exec_in", "tuple"],
  309. ["Unexpected type", "ports_data_in", "tuple"],
  310. ],
  311. ("End", "dict"): [
  312. ["Unexpected type", "ports_exec_in", "dict"],
  313. ["Unexpected type", "ports_data_in", "dict"],
  314. ],
  315. ("End", "none"): [
  316. ["Unexpected type", "ports_exec_in", "NoneType"],
  317. ["Unexpected type", "ports_data_in", "NoneType"],
  318. ],
  319. ("End", "invalid"): [
  320. ["Invalid python", "ports_exec_in"],
  321. ["Invalid python", "ports_data_in"],
  322. ],
  323. ("End", "subtype"): [
  324. ["Unexpected type", "ports_exec_in", "list"],
  325. ["Unexpected type", "ports_data_in", "list"],
  326. ],
  327. ("End", "code"): [
  328. ["Unexpected type", "ports_exec_in"],
  329. ["Unexpected type", "ports_data_in"],
  330. ],
  331. },
  332. )
  333. def test_fields_action(self):
  334. self._test_conformance(
  335. "fields_action.od",
  336. {
  337. ("cardinality", "Action_action"): [],
  338. ("Action", "string"): [
  339. ["Unexpected type", "ports_exec_out", "str"],
  340. ["Unexpected type", "ports_exec_in", "str"],
  341. ["Unexpected type", "ports_data_out", "str"],
  342. ["Unexpected type", "ports_data_in", "str"],
  343. ],
  344. ("Action", '"int"'): [
  345. ["Unexpected type", "ports_exec_out", "int"],
  346. ["Unexpected type", "ports_exec_in", "int"],
  347. ["Unexpected type", "ports_data_out", "int"],
  348. ["Unexpected type", "ports_data_in", "int"],
  349. ],
  350. ("Action", "tuple"): [
  351. ["Unexpected type", "ports_exec_out", "tuple"],
  352. ["Unexpected type", "ports_exec_in", "tuple"],
  353. ["Unexpected type", "ports_data_out", "tuple"],
  354. ["Unexpected type", "ports_data_in", "tuple"],
  355. ],
  356. ("Action", "dict"): [
  357. ["Unexpected type", "ports_exec_out", "dict"],
  358. ["Unexpected type", "ports_exec_in", "dict"],
  359. ["Unexpected type", "ports_data_out", "dict"],
  360. ["Unexpected type", "ports_data_in", "dict"],
  361. ],
  362. ("Action", "none"): [
  363. ["Unexpected type", "ports_exec_out", "NoneType"],
  364. ["Unexpected type", "ports_exec_in", "NoneType"],
  365. ["Unexpected type", "ports_data_out", "NoneType"],
  366. ["Unexpected type", "ports_data_in", "NoneType"],
  367. ],
  368. ('"Action"', '"invalid"'): [
  369. ["Invalid python", "ports_exec_out"],
  370. ["Invalid python", "ports_exec_in"],
  371. ["Invalid python", "ports_data_out"],
  372. ["Invalid python", "ports_data_in"],
  373. ],
  374. ('"Action_action"', '"invalid_action"'): [
  375. ["Invalid python code"],
  376. ["line"],
  377. ],
  378. ("Action", "subtype"): [
  379. ["Unexpected type", "ports_exec_out", "list"],
  380. ["Unexpected type", "ports_exec_in", "list"],
  381. ["Unexpected type", "ports_data_out", "list"],
  382. ["Unexpected type", "ports_data_in", "list"],
  383. ],
  384. ("Action", "code"): [
  385. ["Unexpected type", "ports_exec_out"],
  386. ["Unexpected type", "ports_exec_in"],
  387. ["Unexpected type", "ports_data_out"],
  388. ["Unexpected type", "ports_data_in"],
  389. ],
  390. },
  391. )
  392. def test_fields_modify(self):
  393. self._test_conformance(
  394. "fields_modify.od",
  395. {
  396. ("Modify", "string"): [
  397. ["Unexpected type", "rename", "str"],
  398. ["Unexpected type", "delete", "str"],
  399. ],
  400. ("Modify", "list"): [["Unexpected type", "rename", "list"]],
  401. ("Modify", "set"): [["Unexpected type", "rename", "set"]],
  402. ("Modify", "tuple"): [
  403. ["Unexpected type", "rename", "tuple"],
  404. ["Unexpected type", "delete", "tuple"],
  405. ],
  406. ("Modify", "dict"): [["Unexpected type", "delete", "dict"]],
  407. ("Modify", "none"): [
  408. ["Unexpected type", "rename", "NoneType"],
  409. ["Unexpected type", "delete", "NoneType"],
  410. ],
  411. ("Modify", "invalid"): [
  412. ["Invalid python", "rename"],
  413. ["Invalid python", "delete"],
  414. ],
  415. ("Modify", "subtype"): [
  416. ["Unexpected type", "rename", "dict"],
  417. ["Unexpected type", "delete", "list"],
  418. ],
  419. ("Modify", "code"): [
  420. ["Unexpected type", "rename"],
  421. ["Unexpected type", "delete"],
  422. ],
  423. ("Modify", "joined"): [["rename", "delete", "disjoint"]],
  424. },
  425. )
  426. def test_fields_merge(self):
  427. self._test_conformance(
  428. "fields_merge.od",
  429. {
  430. ("cardinality", "Merge_ports_data_in"): [],
  431. ("Merge", "string"): [["Unexpected type", "ports_data_in", "str"]],
  432. ("Merge", "tuple"): [["Unexpected type", "ports_data_in", "tuple"]],
  433. ("Merge", "dict"): [["Unexpected type", "ports_data_in", "dict"]],
  434. ("Merge", "none"): [["Unexpected type", "ports_data_in", "NoneType"]],
  435. ("Merge", "invalid"): [["Invalid python", "ports_data_in"]],
  436. ("Merge", "subtype"): [["Unexpected type", "ports_data_in", "list"]],
  437. ("Merge", "code"): [["Unexpected type", "ports_data_in"]],
  438. ("Merge", "no"): [["Missing", "slot", "ports_data_in"]],
  439. },
  440. )
  441. def test_fields_store(self):
  442. self._test_conformance(
  443. "fields_store.od",
  444. {
  445. ("cardinality", "Store_ports"): [],
  446. ("Store", "string"): [["Unexpected type", "ports", "str"]],
  447. ("Store", "tuple"): [["Unexpected type", "ports", "tuple"]],
  448. ("Store", "dict"): [["Unexpected type", "ports", "dict"]],
  449. ("Store", "none"): [["Unexpected type", "ports", "NoneType"]],
  450. ("Store", "invalid"): [["Invalid python", "ports"]],
  451. ("Store", "subtype"): [["Unexpected type", "ports", "list"]],
  452. ("Store", "code"): [["Unexpected type", "ports"]],
  453. ("Store", "no"): [["Missing", "slot", "ports"]],
  454. },
  455. )
  456. def test_fields_print(self):
  457. self._test_conformance(
  458. "fields_print.od",
  459. {
  460. ("Print_custom", "list_custom"): [["Unexpected type", "custom", "list"]],
  461. ("Print_custom", "set_custom"): [["Unexpected type", "custom", "set"]],
  462. ("Print_custom", "tuple_custom"): [["Unexpected type", "custom", "tuple"]],
  463. ("Print_custom", "dict_custom"): [["Unexpected type", "custom", "dict"]],
  464. ("Print_custom", "none_custom"): [["Unexpected type", "custom", "NoneType"]],
  465. ("Print_custom", "invalid_custom"): [["Invalid python", "custom"]],
  466. ("Print_custom", "subtype_custom"): [["Unexpected type", "custom", "list"]],
  467. ("Print_custom", "code_custom"): [["Unexpected type", "custom"]],
  468. },
  469. )
  470. if __name__ == "__main__":
  471. unittest.main()