test_constructors_models.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_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"', '"Type"', '"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. '"exit"',
  33. ]
  34. retype = [
  35. '"model"',
  36. '"retype_model"', 1, 1,
  37. '"define_inheritance"', 1, '"Inheritance"',
  38. '"retype"', 1, '"Class"', '"Class"',
  39. '"retype"', 1, '"Type"', '"Class"',
  40. '"retype"', 1, '"Any"', '"Class"',
  41. '"retype"', 1, '"String"', '"Type"',
  42. '"retype"', 1, '"inheritance"', '"String"',
  43. '"retype"', 1, '"link"', '"String"',
  44. '"retype"', 1, '"name"', '"String"',
  45. '"retype"', 1, '"l1"', '"Inheritance"',
  46. '"retype"', 1, '"l2"', '"Inheritance"',
  47. '"retype"', 1, '"Inheritance"', '"Association"',
  48. '"retype"', 1, '"l4"', '"l8"',
  49. '"retype"', 1, '"Association"', '"Association"',
  50. '"retype"', 1, '"l6"', '"Inheritance"',
  51. '"retype"', 1, '"l7"', '"l8"',
  52. '"retype"', 1, '"l8"', '"Association"',
  53. '"retype"', 1, '"l9"', '"l8"',
  54. '"exit"',
  55. ]
  56. bottom_attributes = [
  57. '"model"',
  58. '"instantiate_link"', 1, '"Association"', '"lc"', '"Class"', '"Integer"',
  59. '"instantiate_attribute"', 1, '"lc"', '"name"', '"lower_cardinality"',
  60. '"instantiate_link"', 1, '"Association"', '"uc"', '"Class"', '"Integer"',
  61. '"instantiate_attribute"', 1, '"lc"', '"name"', '"upper_cardinality"',
  62. '"instantiate_link"', 1, '"Association"', '"slc"', '"Association"', '"Integer"',
  63. '"instantiate_attribute"', 1, '"slc"', '"name"', '"source_lower_cardinality"',
  64. '"instantiate_link"', 1, '"Association"', '"suc"', '"Association"', '"Integer"',
  65. '"instantiate_attribute"', 1, '"suc"', '"name"', '"source_upper_cardinality"',
  66. '"instantiate_link"', 1, '"Association"', '"tlc"', '"Association"', '"Integer"',
  67. '"instantiate_attribute"', 1, '"tlc"', '"name"', '"target_lower_cardinality"',
  68. '"instantiate_link"', 1, '"Association"', '"tuc"', '"Association"', '"Integer"',
  69. '"instantiate_attribute"', 1, '"tuc"', '"name"', '"target_upper_cardinality"',
  70. '"exit"',
  71. ]
  72. instantiate_scd = [
  73. '"model"',
  74. '"instantiate_model"', 1, 2,
  75. '"define_inheritance"', 2, '"Inheritance"',
  76. '"instantiate_node"', 2, '"Class"', '"Place"',
  77. '"instantiate_node"', 2, '"Class"', '"Transition"',
  78. '"instantiate_node"', 2, '"Type"', '"Integer"',
  79. '"instantiate_link"', 2, '"Association"', '"P2T"', '"Place"', '"Transition"',
  80. '"instantiate_link"', 2, '"Association"', '"T2P"', '"Transition"', '"Place"',
  81. '"instantiate_link"', 2, '"Association"', '"Place_tokens"', '"Place"', '"Integer"',
  82. '"instantiate_attribute"', 2, '"Place_tokens"', '"name"', '"tokens"',
  83. '"instantiate_link"', 2, '"Association"', '"weight"', '"P2T"', '"Integer"',
  84. '"instantiate_attribute"', 2, '"P2T_weight"', '"name"', '"weight"',
  85. '"instantiate_link"', 2, '"Association"', '"weight"', '"T2P"', '"Integer"',
  86. '"instantiate_attribute"', 2, '"T2P_weight"', '"name"', '"weight"',
  87. '"exit"',
  88. ]
  89. instantiate_pn = [
  90. '"model"',
  91. '"instantiate_model"', 2, 3,
  92. '"instantiate_node"', 3, '"Place"', '"p1"',
  93. '"instantiate_node"', 3, '"Place"', '"p2"',
  94. '"instantiate_node"', 3, '"Transition"', '"t1"',
  95. '"instantiate_node"', 3, '"Transition"', '"t2"',
  96. '"instantiate_link"', 3, '"P2T"', '"p1_t1"', '"p1"', '"t1"',
  97. '"instantiate_link"', 3, '"T2P"', '"t1_p2"', '"t1"', '"p2"',
  98. '"instantiate_link"', 3, '"P2T"', '"p2_t2"', '"p2"', '"t2"',
  99. '"instantiate_link"', 3, '"T2P"', '"t2_p1"', '"t2"', '"p1"',
  100. '"instantiate_attribute"', 3, '"p1_t1"', '"weight"', '1',
  101. '"instantiate_attribute"', 3, '"t1_p2"', '"weight"', '2',
  102. '"instantiate_attribute"', 3, '"p2_t2"', '"weight"', '3',
  103. '"instantiate_attribute"', 3, '"t2_p1"', '"weight"', '4',
  104. '"instantiate_attribute"', 3, '"p1"', '"tokens"', '5',
  105. '"instantiate_attribute"', 3, '"p2"', '"tokens"', '6',
  106. '"exit"',
  107. ]
  108. instantiate_example = [
  109. '"model"',
  110. '"instantiate_model"', 1, 2,
  111. '"define_inheritance"', 2, '"Inheritance"',
  112. '"instantiate_node"', 2, '"Class"', '"A"',
  113. '"instantiate_node"', 2, '"Class"', '"B"',
  114. '"instantiate_node"', 2, '"Class"', '"C"',
  115. '"instantiate_link"', 2, '"Inheritance"', '"b_inherits_a"', '"B"', '"A"',
  116. '"instantiate_link"', 2, '"Association"', '"A_tokens"', '"A"', '"B"',
  117. '"instantiate_attribute"', 2, '"A_tokens"', '"name"', '"tokens"',
  118. '"instantiate_link"', 2, '"Association"', '"C_tokens"', '"C"', '"B"',
  119. '"instantiate_attribute"', 2, '"C_tokens"', '"name"', '"tokens"',
  120. '"exit"',
  121. '"model"',
  122. '"instantiate_model"', 2, 3,
  123. '"instantiate_node"', 3, '"A"', '"a"',
  124. '"instantiate_attribute"', 3, '"a"', '"tokens"', '"b"',
  125. '"instantiate_node"', 3, '"B"', '"b"',
  126. '"instantiate_node"', 3, '"C"', '"c"',
  127. '"instantiate_attribute"', 3, '"c"', '"tokens"', '"b"',
  128. '"exit"',
  129. ]
  130. def conformance_call(operation, model, metamodel):
  131. return [
  132. '"output"',
  133. '"call"',
  134. '"access"', '"resolve"', '"%s"' % operation,
  135. '3',
  136. '"const"', 3,
  137. '"call"',
  138. '"access"', '"resolve"', '"dict_read"',
  139. '2',
  140. '"call"',
  141. '"access"', '"resolve"', '"dict_read"',
  142. '2',
  143. '"const"', 3,
  144. '"const"', '"model"',
  145. 'false',
  146. '"const"', '"%s"' % model,
  147. 'false',
  148. '"call"',
  149. '"access"', '"resolve"', '"dict_read"',
  150. '2',
  151. '"call"',
  152. '"access"', '"resolve"', '"dict_read"',
  153. '2',
  154. '"const"', 2,
  155. '"const"', '"model"',
  156. 'false',
  157. '"const"', '"%s"' % metamodel,
  158. 'false',
  159. 'false',
  160. 'true',
  161. ]
  162. is_direct_instance = \
  163. conformance_call("is_direct_instance", "a", "A") + \
  164. conformance_call("is_direct_instance", "b", "A") + \
  165. conformance_call("is_direct_instance", "c", "A") + \
  166. conformance_call("is_direct_instance", "a", "B") + \
  167. conformance_call("is_direct_instance", "b", "B") + \
  168. conformance_call("is_direct_instance", "c", "B") + \
  169. conformance_call("is_direct_instance", "a", "C") + \
  170. conformance_call("is_direct_instance", "b", "C") + \
  171. conformance_call("is_direct_instance", "c", "C")
  172. is_nominal_instance = \
  173. conformance_call("is_nominal_instance", "a", "A") + \
  174. conformance_call("is_nominal_instance", "b", "A") + \
  175. conformance_call("is_nominal_instance", "c", "A") + \
  176. conformance_call("is_nominal_instance", "a", "B") + \
  177. conformance_call("is_nominal_instance", "b", "B") + \
  178. conformance_call("is_nominal_instance", "c", "B") + \
  179. conformance_call("is_nominal_instance", "a", "C") + \
  180. conformance_call("is_nominal_instance", "b", "C") + \
  181. conformance_call("is_nominal_instance", "c", "C")
  182. is_structural_instance = \
  183. conformance_call("is_structural_instance", "a", "A") + \
  184. conformance_call("is_structural_instance", "b", "A") + \
  185. conformance_call("is_structural_instance", "c", "A") + \
  186. conformance_call("is_structural_instance", "a", "B") + \
  187. conformance_call("is_structural_instance", "b", "B") + \
  188. conformance_call("is_structural_instance", "c", "B") + \
  189. conformance_call("is_structural_instance", "a", "C") + \
  190. conformance_call("is_structural_instance", "b", "C") + \
  191. conformance_call("is_structural_instance", "c", "C")
  192. def conformance_check(node):
  193. return [
  194. '"output"',
  195. '"call"',
  196. '"access"', '"resolve"', '"conformance_scd"',
  197. '1',
  198. '"const"', node,
  199. 'false',
  200. 'true',
  201. ]
  202. class TestConstructorsModels(unittest.TestCase):
  203. def test_constructors_instantiate_bottom(self):
  204. commands = bottom + retype + bottom_attributes + conformance_check(1) + ['"return"', 'false']
  205. self.assertTrue(run_barebone(commands, ["OK"], 1))
  206. def test_constructors_instantiate_scd(self):
  207. commands = bottom + retype + bottom_attributes + instantiate_scd + conformance_check(2) + ['"return"', 'false']
  208. self.assertTrue(run_barebone(commands, ["OK"], 1))
  209. def test_constructors_instantiate_pn(self):
  210. commands = bottom + retype + bottom_attributes + instantiate_scd + instantiate_pn + conformance_check(3) + ['"return"', 'false']
  211. self.assertTrue(run_barebone(commands, ["OK"], 1))
  212. def test_constructors_instantiate_example(self):
  213. commands = bottom + retype + bottom_attributes + instantiate_example + conformance_check(2) + conformance_check(3) + ['"return"', 'false']
  214. self.assertTrue(run_barebone(commands, ["OK", "OK"], 1))
  215. def test_constructors_is_direct_instance(self):
  216. commands = bottom + retype + bottom_attributes + instantiate_example + is_direct_instance + ['"return"', 'false']
  217. expected = ['True', 'False', 'False',
  218. 'False', 'True', 'False',
  219. 'False', 'False', 'True']
  220. self.assertTrue(run_barebone(commands, expected, 1))
  221. def test_constructors_is_nominal_instance(self):
  222. commands = bottom + retype + bottom_attributes + instantiate_example + is_nominal_instance + ['"return"', 'false']
  223. expected = ['True', 'True', 'False',
  224. 'False', 'True', 'False',
  225. 'False', 'False', 'True']
  226. self.assertTrue(run_barebone(commands, expected, 1))
  227. def test_constructors_is_structural_instance(self):
  228. commands = bottom + retype + bottom_attributes + instantiate_example + is_structural_instance + ['"return"', 'false']
  229. expected = ['True', 'False', 'True',
  230. 'True', 'True', 'True',
  231. 'True', 'False', 'True']
  232. self.assertTrue(run_barebone(commands, expected, 1))