test_constructors_models.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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, '"Type"',
  18. '"add_node"', 1, '"Any"',
  19. '"add_node"', 1, '"String"',
  20. '"add_node"', 1, '"Integer"',
  21. '"add_value"', 1, '"inheritance"', '"inheritance"',
  22. '"add_value"', 1, '"link"', '"link"',
  23. '"add_value"', 1, '"name"', '"name"',
  24. '"add_edge"', 1, '"l1"', '"Class"', '"Any"',
  25. '"add_edge"', 1, '"l2"', '"Type"', '"Any"',
  26. '"add_edge"', 1, '"Inheritance"', '"Any"', '"Any"',
  27. '"add_edge"', 1, '"l4"', '"Inheritance"', '"inheritance"',
  28. '"add_edge"', 1, '"Association"', '"Any"', '"Any"',
  29. '"add_edge"', 1, '"l6"', '"Association"', '"Any"',
  30. '"add_edge"', 1, '"l7"', '"Association"', '"link"',
  31. '"add_edge"', 1, '"l8"', '"Association"', '"String"',
  32. '"add_edge"', 1, '"l9"', '"l8"', '"name"',
  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, '"Type"', '"Class"',
  41. '"retype"', 1, '"Any"', '"Class"',
  42. '"retype"', 1, '"String"', '"Type"',
  43. '"retype"', 1, '"Integer"', '"Type"',
  44. '"retype"', 1, '"inheritance"', '"String"',
  45. '"retype"', 1, '"link"', '"String"',
  46. '"retype"', 1, '"name"', '"String"',
  47. '"retype"', 1, '"l1"', '"Inheritance"',
  48. '"retype"', 1, '"l2"', '"Inheritance"',
  49. '"retype"', 1, '"Inheritance"', '"Association"',
  50. '"retype"', 1, '"l4"', '"l8"',
  51. '"retype"', 1, '"Association"', '"Association"',
  52. '"retype"', 1, '"l6"', '"Inheritance"',
  53. '"retype"', 1, '"l7"', '"l8"',
  54. '"retype"', 1, '"l8"', '"Association"',
  55. '"retype"', 1, '"l9"', '"l8"',
  56. '"exit"',
  57. ]
  58. action_language = [
  59. '"model"',
  60. '"instantiate_node"', 1, '"Class"', '"Action"',
  61. '"instantiate_node"', 1, '"Class"', '"Statement"',
  62. '"instantiate_node"', 1, '"Class"', '"Expression"',
  63. '"instantiate_node"', 1, '"Class"', '"Funcdef"',
  64. '"instantiate_node"', 1, '"Class"', '"Param"',
  65. '"instantiate_node"', 1, '"Class"', '"If"',
  66. '"instantiate_node"', 1, '"Class"', '"Break"',
  67. '"instantiate_node"', 1, '"Class"', '"While"',
  68. '"instantiate_node"', 1, '"Class"', '"Continue"',
  69. '"instantiate_node"', 1, '"Class"', '"Assign"',
  70. '"instantiate_node"', 1, '"Class"', '"Return"',
  71. '"instantiate_node"', 1, '"Class"', '"Output"',
  72. '"instantiate_node"', 1, '"Class"', '"Declare"',
  73. '"instantiate_node"', 1, '"Class"', '"Global"',
  74. '"instantiate_node"', 1, '"Class"', '"Access"',
  75. '"instantiate_node"', 1, '"Class"', '"Constant"',
  76. '"instantiate_node"', 1, '"Class"', '"Input"',
  77. '"instantiate_node"', 1, '"Class"', '"Resolve"',
  78. '"instantiate_node"', 1, '"Class"', '"Call"',
  79. '"instantiate_link"', 1, '"Inheritance"', '""', '"Funcdef"', '"Any"',
  80. '"instantiate_link"', 1, '"Inheritance"', '""', '"Action"', '"Any"',
  81. '"instantiate_link"', 1, '"Inheritance"', '""', '"Param"', '"Any"',
  82. '"instantiate_link"', 1, '"Inheritance"', '""', '"Statement"', '"Action"',
  83. '"instantiate_link"', 1, '"Inheritance"', '""', '"Expression"', '"Action"',
  84. '"instantiate_link"', 1, '"Inheritance"', '""', '"Resolve"', '"Statement"',
  85. '"instantiate_link"', 1, '"Inheritance"', '""', '"If"', '"Statement"',
  86. '"instantiate_link"', 1, '"Inheritance"', '""', '"Break"', '"Statement"',
  87. '"instantiate_link"', 1, '"Inheritance"', '""', '"Continue"', '"Statement"',
  88. '"instantiate_link"', 1, '"Inheritance"', '""', '"Global"', '"Statement"',
  89. '"instantiate_link"', 1, '"Inheritance"', '""', '"While"', '"Statement"',
  90. '"instantiate_link"', 1, '"Inheritance"', '""', '"Assign"', '"Statement"',
  91. '"instantiate_link"', 1, '"Inheritance"', '""', '"Return"', '"Statement"',
  92. '"instantiate_link"', 1, '"Inheritance"', '""', '"Call"', '"Statement"',
  93. '"instantiate_link"', 1, '"Inheritance"', '""', '"Declare"', '"Statement"',
  94. '"instantiate_link"', 1, '"Inheritance"', '""', '"Call"', '"Expression"',
  95. '"instantiate_link"', 1, '"Inheritance"', '""', '"Access"', '"Expression"',
  96. '"instantiate_link"', 1, '"Inheritance"', '""', '"Constant"', '"Expression"',
  97. '"instantiate_link"', 1, '"Inheritance"', '""', '"Input"', '"Expression"',
  98. '"instantiate_link"', 1, '"Association"', '"statement_next"', '"Statement"', '"Statement"',
  99. '"instantiate_attribute"', 1, '"statement_next"', '"name"', '"next"',
  100. '"instantiate_link"', 1, '"Association"', '"if_cond"', '"If"', '"Expression"',
  101. '"instantiate_attribute"', 1, '"if_cond"', '"name"', '"cond"',
  102. '"instantiate_link"', 1, '"Association"', '"if_true"', '"If"', '"Statement"',
  103. '"instantiate_attribute"', 1, '"if_true"', '"name"', '"true"',
  104. '"instantiate_link"', 1, '"Association"', '"if_false"', '"If"', '"Statement"',
  105. '"instantiate_attribute"', 1, '"if_false"', '"name"', '"false"',
  106. '"instantiate_link"', 1, '"Association"', '"while_cond"', '"While"', '"Expression"',
  107. '"instantiate_attribute"', 1, '"while_cond"', '"name"', '"cond"',
  108. '"instantiate_link"', 1, '"Association"', '"while_body"', '"While"', '"Statement"',
  109. '"instantiate_attribute"', 1, '"while_body"', '"name"', '"body"',
  110. '"instantiate_link"', 1, '"Association"', '"assign_var"', '"Assign"', '"Any"',
  111. '"instantiate_attribute"', 1, '"assign_var"', '"name"', '"var"',
  112. '"instantiate_link"', 1, '"Association"', '"assign_value"', '"Assign"', '"Expression"',
  113. '"instantiate_attribute"', 1, '"assign_value"', '"name"', '"value"',
  114. '"instantiate_link"', 1, '"Association"', '"break_while"', '"Break"', '"While"',
  115. '"instantiate_attribute"', 1, '"break_while"', '"name"', '"while"',
  116. '"instantiate_link"', 1, '"Association"', '"continue_while"', '"Continue"', '"While"',
  117. '"instantiate_attribute"', 1, '"continue_while"', '"name"', '"while"',
  118. '"instantiate_link"', 1, '"Association"', '"return_value"', '"Return"', '"Expression"',
  119. '"instantiate_attribute"', 1, '"return_value"', '"name"', '"value"',
  120. '"instantiate_link"', 1, '"Association"', '"resolve_var"', '"Resolve"', '"Any"',
  121. '"instantiate_attribute"', 1, '"resolve_var"', '"name"', '"var"',
  122. '"instantiate_link"', 1, '"Association"', '"access_var"', '"Access"', '"Any"',
  123. '"instantiate_attribute"', 1, '"access_var"', '"name"', '"var"',
  124. '"instantiate_link"', 1, '"Association"', '"constant_node"', '"Constant"', '"Any"',
  125. '"instantiate_attribute"', 1, '"constant_node"', '"name"', '"node"',
  126. '"instantiate_link"', 1, '"Association"', '"output_node"', '"Output"', '"Expression"',
  127. '"instantiate_attribute"', 1, '"output_node"', '"name"', '"node"',
  128. '"instantiate_link"', 1, '"Association"', '"global_var"', '"Global"', '"String"',
  129. '"instantiate_attribute"', 1, '"global_var"', '"name"', '"var"',
  130. '"instantiate_link"', 1, '"Association"', '"param_name"', '"Param"', '"String"',
  131. '"instantiate_attribute"', 1, '"param_name"', '"name"', '"name"',
  132. '"instantiate_link"', 1, '"Association"', '"param_value"', '"Param"', '"Expression"',
  133. '"instantiate_attribute"', 1, '"param_value"', '"name"', '"value"',
  134. '"instantiate_link"', 1, '"Association"', '"param_next_param"', '"Param"', '"Param"',
  135. '"instantiate_attribute"', 1, '"param_next_param"', '"name"', '"next_param"',
  136. '"instantiate_link"', 1, '"Association"', '"funcdef_body"', '"Funcdef"', '"Statement"',
  137. '"instantiate_attribute"', 1, '"funcdef_body"', '"name"', '"body"',
  138. '"instantiate_link"', 1, '"Association"', '"call_func"', '"Call"', '"Expression"',
  139. '"instantiate_attribute"', 1, '"call_func"', '"name"', '"func"',
  140. '"instantiate_link"', 1, '"Association"', '"call_params"', '"Call"', '"Param"',
  141. '"instantiate_attribute"', 1, '"call_params"', '"name"', '"params"',
  142. '"instantiate_link"', 1, '"Association"', '"call_last_param"', '"Call"', '"Param"',
  143. '"instantiate_attribute"', 1, '"call_last_param"', '"name"', '"last_param"',
  144. '"exit"',
  145. ]
  146. bottom_attributes = [
  147. '"model"',
  148. '"instantiate_link"', 1, '"Association"', '"lc"', '"Class"', '"Integer"',
  149. '"instantiate_attribute"', 1, '"lc"', '"name"', '"lower_cardinality"',
  150. '"instantiate_link"', 1, '"Association"', '"uc"', '"Class"', '"Integer"',
  151. '"instantiate_attribute"', 1, '"uc"', '"name"', '"upper_cardinality"',
  152. '"instantiate_link"', 1, '"Association"', '"slc"', '"Association"', '"Integer"',
  153. '"instantiate_attribute"', 1, '"slc"', '"name"', '"source_lower_cardinality"',
  154. '"instantiate_link"', 1, '"Association"', '"suc"', '"Association"', '"Integer"',
  155. '"instantiate_attribute"', 1, '"suc"', '"name"', '"source_upper_cardinality"',
  156. '"instantiate_link"', 1, '"Association"', '"tlc"', '"Association"', '"Integer"',
  157. '"instantiate_attribute"', 1, '"tlc"', '"name"', '"target_lower_cardinality"',
  158. '"instantiate_link"', 1, '"Association"', '"tuc"', '"Association"', '"Integer"',
  159. '"instantiate_attribute"', 1, '"tuc"', '"name"', '"target_upper_cardinality"',
  160. '"exit"',
  161. ]
  162. instantiate_scd = [
  163. '"model"',
  164. '"instantiate_model"', 1, 2,
  165. '"define_inheritance"', 2, '"Inheritance"',
  166. '"instantiate_node"', 2, '"Class"', '"Place"',
  167. '"instantiate_node"', 2, '"Class"', '"Transition"',
  168. '"instantiate_node"', 2, '"Type"', '"Integer"',
  169. '"instantiate_link"', 2, '"Association"', '"P2T"', '"Place"', '"Transition"',
  170. '"instantiate_link"', 2, '"Association"', '"T2P"', '"Transition"', '"Place"',
  171. '"instantiate_link"', 2, '"Association"', '"Place_tokens"', '"Place"', '"Integer"',
  172. '"instantiate_attribute"', 2, '"Place_tokens"', '"name"', '"tokens"',
  173. '"instantiate_link"', 2, '"Association"', '"P2T_weight"', '"P2T"', '"Integer"',
  174. '"instantiate_attribute"', 2, '"P2T_weight"', '"name"', '"weight"',
  175. '"instantiate_link"', 2, '"Association"', '"T2P_weight"', '"T2P"', '"Integer"',
  176. '"instantiate_attribute"', 2, '"T2P_weight"', '"name"', '"weight"',
  177. '"exit"',
  178. ]
  179. instantiate_pn = [
  180. '"model"',
  181. '"instantiate_model"', 2, 3,
  182. '"instantiate_node"', 3, '"Place"', '"p1"',
  183. '"instantiate_node"', 3, '"Place"', '"p2"',
  184. '"instantiate_node"', 3, '"Transition"', '"t1"',
  185. '"instantiate_node"', 3, '"Transition"', '"t2"',
  186. '"instantiate_link"', 3, '"P2T"', '"p1_t1"', '"p1"', '"t1"',
  187. '"instantiate_link"', 3, '"T2P"', '"t1_p2"', '"t1"', '"p2"',
  188. '"instantiate_link"', 3, '"P2T"', '"p2_t2"', '"p2"', '"t2"',
  189. '"instantiate_link"', 3, '"T2P"', '"t2_p1"', '"t2"', '"p1"',
  190. '"instantiate_attribute"', 3, '"p1_t1"', '"weight"', '1',
  191. '"instantiate_attribute"', 3, '"t1_p2"', '"weight"', '2',
  192. '"instantiate_attribute"', 3, '"p2_t2"', '"weight"', '3',
  193. '"instantiate_attribute"', 3, '"t2_p1"', '"weight"', '4',
  194. '"instantiate_attribute"', 3, '"p1"', '"tokens"', '5',
  195. '"instantiate_attribute"', 3, '"p2"', '"tokens"', '6',
  196. '"exit"',
  197. ]
  198. instantiate_example = [
  199. '"model"',
  200. '"instantiate_model"', 1, 2,
  201. '"define_inheritance"', 2, '"Inheritance"',
  202. '"instantiate_node"', 2, '"Class"', '"A"',
  203. '"instantiate_node"', 2, '"Class"', '"B"',
  204. '"instantiate_node"', 2, '"Class"', '"C"',
  205. '"instantiate_link"', 2, '"Inheritance"', '"b_inherits_a"', '"B"', '"A"',
  206. '"instantiate_link"', 2, '"Association"', '"A_tokens"', '"A"', '"B"',
  207. '"instantiate_attribute"', 2, '"A_tokens"', '"name"', '"tokens"',
  208. '"instantiate_link"', 2, '"Association"', '"C_tokens"', '"C"', '"B"',
  209. '"instantiate_attribute"', 2, '"C_tokens"', '"name"', '"tokens"',
  210. '"exit"',
  211. '"model"',
  212. '"instantiate_model"', 2, 3,
  213. '"instantiate_node"', 3, '"A"', '"a"',
  214. '"instantiate_attribute"', 3, '"a"', '"tokens"', '"b"',
  215. '"instantiate_node"', 3, '"B"', '"b"',
  216. '"instantiate_node"', 3, '"C"', '"c"',
  217. '"instantiate_attribute"', 3, '"c"', '"tokens"', '"b"',
  218. '"exit"',
  219. ]
  220. def conformance_call(operation, model, metamodel):
  221. return [
  222. '"output"',
  223. '"call"',
  224. '"access"', '"resolve"', '"%s"' % operation,
  225. '3',
  226. '"const"', 3,
  227. '"const"', '"%s"' % model,
  228. '"const"', '"%s"' % metamodel,
  229. 'false',
  230. 'true',
  231. ]
  232. is_direct_instance = \
  233. conformance_call("is_direct_instance", "a", "A") + \
  234. conformance_call("is_direct_instance", "b", "A") + \
  235. conformance_call("is_direct_instance", "c", "A") + \
  236. conformance_call("is_direct_instance", "a", "B") + \
  237. conformance_call("is_direct_instance", "b", "B") + \
  238. conformance_call("is_direct_instance", "c", "B") + \
  239. conformance_call("is_direct_instance", "a", "C") + \
  240. conformance_call("is_direct_instance", "b", "C") + \
  241. conformance_call("is_direct_instance", "c", "C")
  242. is_nominal_instance = \
  243. conformance_call("is_nominal_instance", "a", "A") + \
  244. conformance_call("is_nominal_instance", "b", "A") + \
  245. conformance_call("is_nominal_instance", "c", "A") + \
  246. conformance_call("is_nominal_instance", "a", "B") + \
  247. conformance_call("is_nominal_instance", "b", "B") + \
  248. conformance_call("is_nominal_instance", "c", "B") + \
  249. conformance_call("is_nominal_instance", "a", "C") + \
  250. conformance_call("is_nominal_instance", "b", "C") + \
  251. conformance_call("is_nominal_instance", "c", "C")
  252. def conformance_check(node):
  253. return [
  254. '"output"',
  255. '"call"',
  256. '"access"', '"resolve"', '"conformance_scd"',
  257. '1',
  258. '"const"', node,
  259. 'false',
  260. 'true',
  261. ]
  262. class TestConstructorsModels(unittest.TestCase):
  263. def test_constructors_instantiate_bottom(self):
  264. commands = bottom + retype + bottom_attributes + conformance_check(1) + ['"return"', 'false']
  265. self.assertTrue(run_barebone(commands, ["OK"], 1))
  266. def test_constructors_action_language(self):
  267. commands = bottom + retype + bottom_attributes + action_language + conformance_check(1) + ['"return"', 'false']
  268. self.assertTrue(run_barebone(commands, ["OK"], 1))
  269. def test_constructors_instantiate_scd(self):
  270. commands = bottom + retype + bottom_attributes + instantiate_scd + conformance_check(2) + ['"return"', 'false']
  271. self.assertTrue(run_barebone(commands, ["OK"], 1))
  272. def test_constructors_instantiate_pn(self):
  273. commands = bottom + retype + bottom_attributes + instantiate_scd + instantiate_pn + conformance_check(3) + ['"return"', 'false']
  274. self.assertTrue(run_barebone(commands, ["OK"], 1))
  275. def test_constructors_instantiate_example(self):
  276. commands = bottom + retype + bottom_attributes + instantiate_example + conformance_check(2) + conformance_check(3) + ['"return"', 'false']
  277. self.assertTrue(run_barebone(commands, ["OK", "OK"], 1))
  278. def test_constructors_is_direct_instance(self):
  279. commands = bottom + retype + bottom_attributes + instantiate_example + is_direct_instance + ['"return"', 'false']
  280. expected = ['True', 'False', 'False',
  281. 'False', 'True', 'False',
  282. 'False', 'False', 'True']
  283. self.assertTrue(run_barebone(commands, expected, 1))
  284. def test_constructors_is_nominal_instance(self):
  285. commands = bottom + retype + bottom_attributes + instantiate_example + is_nominal_instance + ['"return"', 'false']
  286. expected = ['True', 'True', 'False',
  287. 'False', 'True', 'False',
  288. 'False', 'False', 'True']
  289. self.assertTrue(run_barebone(commands, expected, 1))