test_constructors_models.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import unittest
  2. import sys
  3. import os
  4. from utils import execute, kill, run_file, run_barebone
  5. def flatten(lst):
  6. new_lst = []
  7. for f in lst:
  8. if isinstance(f, (list, tuple)):
  9. new_lst.extend(flatten(f))
  10. else:
  11. new_lst.append(f)
  12. return new_lst
  13. bottom = [
  14. '"model"',
  15. '"instantiate_bottom"', 1,
  16. '"add_node"', 1, '"Class"',
  17. '"add_node"', 1, '"Any"',
  18. '"add_node"', 1, '"String"',
  19. '"add_node"', 1, '"Integer"',
  20. '"add_value"', 1, '"inheritance"', '"inheritance"',
  21. '"add_value"', 1, '"link"', '"link"',
  22. '"add_value"', 1, '"name"', '"name"',
  23. '"add_edge"', 1, '"l1"', '"Class"', '"Any"',
  24. '"add_edge"', 1, '"l2"', '"String"', '"Any"',
  25. '"add_edge"', 1, '"Inheritance"', '"Any"', '"Any"',
  26. '"add_edge"', 1, '"l4"', '"Inheritance"', '"inheritance"',
  27. '"add_edge"', 1, '"Association"', '"Any"', '"Any"',
  28. '"add_edge"', 1, '"l6"', '"Association"', '"Any"',
  29. '"add_edge"', 1, '"l7"', '"Association"', '"link"',
  30. '"add_edge"', 1, '"l8"', '"Association"', '"String"',
  31. '"add_edge"', 1, '"l9"', '"l8"', '"name"',
  32. '"add_edge"', 1, '"l10"', '"Integer"', '"Any"',
  33. '"exit"',
  34. ]
  35. retype = [
  36. '"model"',
  37. '"retype_model"', 1, 1,
  38. '"define_inheritance"', 1, '"Inheritance"',
  39. '"retype"', 1, '"Class"', '"Class"',
  40. '"retype"', 1, '"String"', '"Class"',
  41. '"retype"', 1, '"Any"', '"Class"',
  42. '"retype"', 1, '"Integer"', '"Class"',
  43. '"retype"', 1, '"inheritance"', '"String"',
  44. '"retype"', 1, '"link"', '"String"',
  45. '"retype"', 1, '"name"', '"String"',
  46. '"retype"', 1, '"l1"', '"Inheritance"',
  47. '"retype"', 1, '"l2"', '"Inheritance"',
  48. '"retype"', 1, '"Inheritance"', '"Association"',
  49. '"retype"', 1, '"l4"', '"l8"',
  50. '"retype"', 1, '"Association"', '"Association"',
  51. '"retype"', 1, '"l6"', '"Inheritance"',
  52. '"retype"', 1, '"l7"', '"l8"',
  53. '"retype"', 1, '"l8"', '"Association"',
  54. '"retype"', 1, '"l9"', '"l8"',
  55. '"exit"',
  56. ]
  57. action_language = [
  58. '"model"',
  59. '"instantiate_node"', 1, '"Class"', '"Action"',
  60. '"instantiate_node"', 1, '"Class"', '"Statement"',
  61. '"instantiate_node"', 1, '"Class"', '"Expression"',
  62. '"instantiate_node"', 1, '"Class"', '"funcdef"',
  63. '"instantiate_node"', 1, '"Class"', '"param"',
  64. '"instantiate_node"', 1, '"Class"', '"if"',
  65. '"instantiate_node"', 1, '"Class"', '"break"',
  66. '"instantiate_node"', 1, '"Class"', '"while"',
  67. '"instantiate_node"', 1, '"Class"', '"continue"',
  68. '"instantiate_node"', 1, '"Class"', '"assign"',
  69. '"instantiate_node"', 1, '"Class"', '"return"',
  70. '"instantiate_node"', 1, '"Class"', '"output"',
  71. '"instantiate_node"', 1, '"Class"', '"declare"',
  72. '"instantiate_node"', 1, '"Class"', '"global"',
  73. '"instantiate_node"', 1, '"Class"', '"access"',
  74. '"instantiate_node"', 1, '"Class"', '"constant"',
  75. '"instantiate_node"', 1, '"Class"', '"input"',
  76. '"instantiate_node"', 1, '"Class"', '"resolve"',
  77. '"instantiate_node"', 1, '"Class"', '"call"',
  78. '"instantiate_link"', 1, '"Association"', '"dict_link"', '"Action"', '"Any"',
  79. '"instantiate_link"', 1, '"Association"', '"to_str"', '"dict_link"', '"String"',
  80. '"instantiate_attribute"', 1, '"to_str"', '"name"', '"name"',
  81. '"instantiate_link"', 1, '"Inheritance"', '""', '"Action"', '"Any"',
  82. '"instantiate_link"', 1, '"Inheritance"', '""', '"funcdef"', '"Action"',
  83. '"instantiate_link"', 1, '"Inheritance"', '""', '"param"', '"Action"',
  84. '"instantiate_link"', 1, '"Inheritance"', '""', '"Statement"', '"Action"',
  85. '"instantiate_link"', 1, '"Inheritance"', '""', '"Expression"', '"Action"',
  86. '"instantiate_link"', 1, '"Inheritance"', '""', '"resolve"', '"Statement"',
  87. '"instantiate_link"', 1, '"Inheritance"', '""', '"if"', '"Statement"',
  88. '"instantiate_link"', 1, '"Inheritance"', '""', '"break"', '"Statement"',
  89. '"instantiate_link"', 1, '"Inheritance"', '""', '"continue"', '"Statement"',
  90. '"instantiate_link"', 1, '"Inheritance"', '""', '"global"', '"Statement"',
  91. '"instantiate_link"', 1, '"Inheritance"', '""', '"while"', '"Statement"',
  92. '"instantiate_link"', 1, '"Inheritance"', '""', '"assign"', '"Statement"',
  93. '"instantiate_link"', 1, '"Inheritance"', '""', '"return"', '"Statement"',
  94. '"instantiate_link"', 1, '"Inheritance"', '""', '"call"', '"Statement"',
  95. '"instantiate_link"', 1, '"Inheritance"', '""', '"declare"', '"Statement"',
  96. '"instantiate_link"', 1, '"Inheritance"', '""', '"call"', '"Expression"',
  97. '"instantiate_link"', 1, '"Inheritance"', '""', '"access"', '"Expression"',
  98. '"instantiate_link"', 1, '"Inheritance"', '""', '"constant"', '"Expression"',
  99. '"instantiate_link"', 1, '"Inheritance"', '""', '"input"', '"Expression"',
  100. '"instantiate_link"', 1, '"Association"', '"statement_next"', '"Statement"', '"Statement"',
  101. '"instantiate_attribute"', 1, '"statement_next"', '"name"', '"next"',
  102. '"instantiate_link"', 1, '"Association"', '"if_cond"', '"if"', '"Expression"',
  103. '"instantiate_attribute"', 1, '"if_cond"', '"name"', '"cond"',
  104. '"instantiate_link"', 1, '"Association"', '"if_true"', '"if"', '"Statement"',
  105. '"instantiate_attribute"', 1, '"if_true"', '"name"', '"true"',
  106. '"instantiate_link"', 1, '"Association"', '"if_false"', '"if"', '"Statement"',
  107. '"instantiate_attribute"', 1, '"if_false"', '"name"', '"false"',
  108. '"instantiate_link"', 1, '"Association"', '"while_cond"', '"while"', '"Expression"',
  109. '"instantiate_attribute"', 1, '"while_cond"', '"name"', '"cond"',
  110. '"instantiate_link"', 1, '"Association"', '"while_body"', '"while"', '"Statement"',
  111. '"instantiate_attribute"', 1, '"while_body"', '"name"', '"body"',
  112. '"instantiate_link"', 1, '"Association"', '"assign_var"', '"assign"', '"Any"',
  113. '"instantiate_attribute"', 1, '"assign_var"', '"name"', '"var"',
  114. '"instantiate_link"', 1, '"Association"', '"assign_value"', '"assign"', '"Expression"',
  115. '"instantiate_attribute"', 1, '"assign_value"', '"name"', '"value"',
  116. '"instantiate_link"', 1, '"Association"', '"break_while"', '"break"', '"while"',
  117. '"instantiate_attribute"', 1, '"break_while"', '"name"', '"while"',
  118. '"instantiate_link"', 1, '"Association"', '"continue_while"', '"continue"', '"while"',
  119. '"instantiate_attribute"', 1, '"continue_while"', '"name"', '"while"',
  120. '"instantiate_link"', 1, '"Association"', '"return_value"', '"return"', '"Expression"',
  121. '"instantiate_attribute"', 1, '"return_value"', '"name"', '"value"',
  122. '"instantiate_link"', 1, '"Association"', '"resolve_var"', '"resolve"', '"Any"',
  123. '"instantiate_attribute"', 1, '"resolve_var"', '"name"', '"var"',
  124. '"instantiate_link"', 1, '"Association"', '"access_var"', '"access"', '"Any"',
  125. '"instantiate_attribute"', 1, '"access_var"', '"name"', '"var"',
  126. '"instantiate_link"', 1, '"Association"', '"constant_node"', '"constant"', '"Any"',
  127. '"instantiate_attribute"', 1, '"constant_node"', '"name"', '"node"',
  128. '"instantiate_link"', 1, '"Association"', '"output_node"', '"output"', '"Expression"',
  129. '"instantiate_attribute"', 1, '"output_node"', '"name"', '"node"',
  130. '"instantiate_link"', 1, '"Association"', '"global_var"', '"global"', '"String"',
  131. '"instantiate_attribute"', 1, '"global_var"', '"name"', '"var"',
  132. '"instantiate_link"', 1, '"Association"', '"param_name"', '"param"', '"String"',
  133. '"instantiate_attribute"', 1, '"param_name"', '"name"', '"name"',
  134. '"instantiate_link"', 1, '"Association"', '"param_value"', '"param"', '"Expression"',
  135. '"instantiate_attribute"', 1, '"param_value"', '"name"', '"value"',
  136. '"instantiate_link"', 1, '"Association"', '"param_next_param"', '"param"', '"param"',
  137. '"instantiate_attribute"', 1, '"param_next_param"', '"name"', '"next_param"',
  138. '"instantiate_link"', 1, '"Association"', '"funcdef_body"', '"funcdef"', '"Statement"',
  139. '"instantiate_attribute"', 1, '"funcdef_body"', '"name"', '"body"',
  140. '"instantiate_link"', 1, '"Association"', '"call_func"', '"call"', '"Expression"',
  141. '"instantiate_attribute"', 1, '"call_func"', '"name"', '"func"',
  142. '"instantiate_link"', 1, '"Association"', '"call_params"', '"call"', '"param"',
  143. '"instantiate_attribute"', 1, '"call_params"', '"name"', '"params"',
  144. '"instantiate_link"', 1, '"Association"', '"call_last_param"', '"call"', '"param"',
  145. '"instantiate_attribute"', 1, '"call_last_param"', '"name"', '"last_param"',
  146. '"exit"',
  147. ]
  148. bottom_attributes = [
  149. '"model"',
  150. '"instantiate_link"', 1, '"Association"', '"lc"', '"Class"', '"Integer"',
  151. '"instantiate_attribute"', 1, '"lc"', '"name"', '"lower_cardinality"',
  152. '"instantiate_link"', 1, '"Association"', '"uc"', '"Class"', '"Integer"',
  153. '"instantiate_attribute"', 1, '"uc"', '"name"', '"upper_cardinality"',
  154. '"instantiate_link"', 1, '"Association"', '"slc"', '"Association"', '"Integer"',
  155. '"instantiate_attribute"', 1, '"slc"', '"name"', '"source_lower_cardinality"',
  156. '"instantiate_link"', 1, '"Association"', '"suc"', '"Association"', '"Integer"',
  157. '"instantiate_attribute"', 1, '"suc"', '"name"', '"source_upper_cardinality"',
  158. '"instantiate_link"', 1, '"Association"', '"tlc"', '"Association"', '"Integer"',
  159. '"instantiate_attribute"', 1, '"tlc"', '"name"', '"target_lower_cardinality"',
  160. '"instantiate_link"', 1, '"Association"', '"tuc"', '"Association"', '"Integer"',
  161. '"instantiate_attribute"', 1, '"tuc"', '"name"', '"target_upper_cardinality"',
  162. '"instantiate_link"', 1, '"Association"', '"constraint"', '"Any"', '"funcdef"',
  163. '"instantiate_attribute"', 1, '"constraint"', '"name"', '"constraint"',
  164. '"exit"',
  165. ]
  166. add_constraints = [
  167. '"model"',
  168. '"add_constraint"', 1, '"Integer"',
  169. '2', 10, 11,
  170. '"return"', 'true',
  171. '"call"',
  172. '"access"', '"resolve"', '"is_physical_int"',
  173. '1',
  174. '"call"',
  175. '"access"', '"resolve"', '"dict_read"',
  176. '2',
  177. '"call"',
  178. '"access"', '"resolve"', '"dict_read"',
  179. '2',
  180. '"const"', 10,
  181. '"const"', '"model"',
  182. 'false',
  183. '"const"', 11,
  184. 'false',
  185. 'false',
  186. '"exit"',
  187. ]
  188. instantiate_scd = [
  189. '"model"',
  190. '"instantiate_model"', 1, 2,
  191. '"define_inheritance"', 2, '"Inheritance"',
  192. '"instantiate_node"', 2, '"Class"', '"Place"',
  193. '"instantiate_node"', 2, '"Class"', '"Transition"',
  194. '"instantiate_node"', 2, '"Class"', '"Integer"',
  195. '"instantiate_link"', 2, '"Association"', '"P2T"', '"Place"', '"Transition"',
  196. '"instantiate_link"', 2, '"Association"', '"T2P"', '"Transition"', '"Place"',
  197. '"instantiate_link"', 2, '"Association"', '"Place_tokens"', '"Place"', '"Integer"',
  198. '"instantiate_attribute"', 2, '"Place_tokens"', '"name"', '"tokens"',
  199. '"instantiate_link"', 2, '"Association"', '"P2T_weight"', '"P2T"', '"Integer"',
  200. '"instantiate_attribute"', 2, '"P2T_weight"', '"name"', '"weight"',
  201. '"instantiate_link"', 2, '"Association"', '"T2P_weight"', '"T2P"', '"Integer"',
  202. '"instantiate_attribute"', 2, '"T2P_weight"', '"name"', '"weight"',
  203. '"exit"',
  204. ]
  205. instantiate_pn = [
  206. '"model"',
  207. '"instantiate_model"', 2, 3,
  208. '"instantiate_node"', 3, '"Place"', '"p1"',
  209. '"instantiate_node"', 3, '"Place"', '"p2"',
  210. '"instantiate_node"', 3, '"Transition"', '"t1"',
  211. '"instantiate_node"', 3, '"Transition"', '"t2"',
  212. '"instantiate_link"', 3, '"P2T"', '"p1_t1"', '"p1"', '"t1"',
  213. '"instantiate_link"', 3, '"T2P"', '"t1_p2"', '"t1"', '"p2"',
  214. '"instantiate_link"', 3, '"P2T"', '"p2_t2"', '"p2"', '"t2"',
  215. '"instantiate_link"', 3, '"T2P"', '"t2_p1"', '"t2"', '"p1"',
  216. '"instantiate_attribute"', 3, '"p1_t1"', '"weight"', '1',
  217. '"instantiate_attribute"', 3, '"t1_p2"', '"weight"', '2',
  218. '"instantiate_attribute"', 3, '"p2_t2"', '"weight"', '3',
  219. '"instantiate_attribute"', 3, '"t2_p1"', '"weight"', '4',
  220. '"instantiate_attribute"', 3, '"p1"', '"tokens"', '5',
  221. '"instantiate_attribute"', 3, '"p2"', '"tokens"', '6',
  222. '"exit"',
  223. ]
  224. instantiate_example = [
  225. '"model"',
  226. '"instantiate_model"', 1, 2,
  227. '"define_inheritance"', 2, '"Inheritance"',
  228. '"instantiate_node"', 2, '"Class"', '"A"',
  229. '"instantiate_node"', 2, '"Class"', '"B"',
  230. '"instantiate_node"', 2, '"Class"', '"C"',
  231. '"instantiate_link"', 2, '"Inheritance"', '"b_inherits_a"', '"B"', '"A"',
  232. '"instantiate_link"', 2, '"Association"', '"A_tokens"', '"A"', '"B"',
  233. '"instantiate_attribute"', 2, '"A_tokens"', '"name"', '"tokens"',
  234. '"instantiate_link"', 2, '"Association"', '"C_tokens"', '"C"', '"B"',
  235. '"instantiate_attribute"', 2, '"C_tokens"', '"name"', '"tokens"',
  236. '"exit"',
  237. '"model"',
  238. '"instantiate_model"', 2, 3,
  239. '"instantiate_node"', 3, '"A"', '"a"',
  240. '"instantiate_attribute"', 3, '"a"', '"tokens"', '"b"',
  241. '"instantiate_node"', 3, '"B"', '"b"',
  242. '"instantiate_node"', 3, '"C"', '"c"',
  243. '"instantiate_attribute"', 3, '"c"', '"tokens"', '"b"',
  244. '"exit"',
  245. ]
  246. def conformance_call(operation, model, metamodel):
  247. return [
  248. '"output"',
  249. '"call"',
  250. '"access"', '"resolve"', '"%s"' % operation,
  251. '3',
  252. '"const"', 3,
  253. '"const"', '"%s"' % model,
  254. '"const"', '"%s"' % metamodel,
  255. 'false',
  256. 'true',
  257. ]
  258. is_direct_instance = \
  259. conformance_call("is_direct_instance", "a", "A") + \
  260. conformance_call("is_direct_instance", "b", "A") + \
  261. conformance_call("is_direct_instance", "c", "A") + \
  262. conformance_call("is_direct_instance", "a", "B") + \
  263. conformance_call("is_direct_instance", "b", "B") + \
  264. conformance_call("is_direct_instance", "c", "B") + \
  265. conformance_call("is_direct_instance", "a", "C") + \
  266. conformance_call("is_direct_instance", "b", "C") + \
  267. conformance_call("is_direct_instance", "c", "C")
  268. is_nominal_instance = \
  269. conformance_call("is_nominal_instance", "a", "A") + \
  270. conformance_call("is_nominal_instance", "b", "A") + \
  271. conformance_call("is_nominal_instance", "c", "A") + \
  272. conformance_call("is_nominal_instance", "a", "B") + \
  273. conformance_call("is_nominal_instance", "b", "B") + \
  274. conformance_call("is_nominal_instance", "c", "B") + \
  275. conformance_call("is_nominal_instance", "a", "C") + \
  276. conformance_call("is_nominal_instance", "b", "C") + \
  277. conformance_call("is_nominal_instance", "c", "C")
  278. def conformance_check(node):
  279. return [
  280. '"output"',
  281. '"call"',
  282. '"access"', '"resolve"', '"conformance_scd"',
  283. '1',
  284. '"const"', node,
  285. 'false',
  286. 'true',
  287. ]
  288. class TestConstructorsModels(unittest.TestCase):
  289. def test_constructors_instantiate_bottom(self):
  290. commands = bottom + retype + bottom_attributes + conformance_check(1) + ['"return"', 'false']
  291. self.assertTrue(run_barebone(commands, ["OK"], 1))
  292. def test_constructors_action_language(self):
  293. commands = bottom + retype + action_language + bottom_attributes + conformance_check(1) + ['"return"', 'false']
  294. self.assertTrue(run_barebone(commands, ["OK"], 1))
  295. def test_constructors_constraints(self):
  296. commands = bottom + retype + action_language + bottom_attributes + add_constraints + conformance_check(1) + ['"return"', 'false']
  297. self.assertTrue(run_barebone(commands, ["OK"], 1))
  298. def test_constructors_instantiate_scd(self):
  299. commands = bottom + retype + bottom_attributes + instantiate_scd + conformance_check(2) + ['"return"', 'false']
  300. self.assertTrue(run_barebone(commands, ["OK"], 1))
  301. def test_constructors_instantiate_pn(self):
  302. commands = bottom + retype + bottom_attributes + instantiate_scd + instantiate_pn + conformance_check(3) + ['"return"', 'false']
  303. self.assertTrue(run_barebone(commands, ["OK"], 1))
  304. def test_constructors_instantiate_example(self):
  305. commands = bottom + retype + bottom_attributes + instantiate_example + conformance_check(2) + conformance_check(3) + ['"return"', 'false']
  306. self.assertTrue(run_barebone(commands, ["OK", "OK"], 1))
  307. def test_constructors_is_direct_instance(self):
  308. commands = bottom + retype + bottom_attributes + instantiate_example + is_direct_instance + ['"return"', 'false']
  309. expected = ['True', 'False', 'False',
  310. 'False', 'True', 'False',
  311. 'False', 'False', 'True']
  312. self.assertTrue(run_barebone(commands, expected, 1))
  313. def test_constructors_is_nominal_instance(self):
  314. commands = bottom + retype + bottom_attributes + instantiate_example + is_nominal_instance + ['"return"', 'false']
  315. expected = ['True', 'True', 'False',
  316. 'False', 'True', 'False',
  317. 'False', 'False', 'True']
  318. self.assertTrue(run_barebone(commands, expected, 1))