test_constructors_models.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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, '"l3"', '"Any"', '"Any"',
  26. '"add_edge"', 1, '"l4"', '"l3"', '"inheritance"',
  27. '"add_edge"', 1, '"l5"', '"Any"', '"Any"',
  28. '"add_edge"', 1, '"l6"', '"l5"', '"Any"',
  29. '"add_edge"', 1, '"l7"', '"l5"', '"link"',
  30. '"add_edge"', 1, '"l8"', '"l5"', '"String"',
  31. '"add_edge"', 1, '"l9"', '"l8"', '"name"',
  32. '"exit"',
  33. ]
  34. retype = [
  35. '"model"',
  36. '"retype_model"', 1, 1,
  37. '"define_inheritance"', 1, '"l3"',
  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"', '"l3"',
  46. '"retype"', 1, '"l2"', '"l3"',
  47. '"retype"', 1, '"l3"', '"l5"',
  48. '"retype"', 1, '"l4"', '"l8"',
  49. '"retype"', 1, '"l5"', '"l5"',
  50. '"retype"', 1, '"l6"', '"l3"',
  51. '"retype"', 1, '"l7"', '"l8"',
  52. '"retype"', 1, '"l8"', '"l5"',
  53. '"retype"', 1, '"l9"', '"l8"',
  54. '"exit"',
  55. ]
  56. instantiate_scd = [
  57. '"model"',
  58. '"instantiate_model"', 1, 2,
  59. '"define_inheritance"', 2, '"l3"',
  60. '"instantiate_node"', 2, '"Class"', '"Place"',
  61. '"instantiate_node"', 2, '"Class"', '"Transition"',
  62. '"instantiate_node"', 2, '"Type"', '"Integer"',
  63. '"instantiate_link"', 2, '"l5"', '"P2T"', '"Place"', '"Transition"',
  64. '"instantiate_link"', 2, '"l5"', '"T2P"', '"Transition"', '"Place"',
  65. '"instantiate_named"', 2, '"l5"', '"tokens"', '"Place"', '"Integer"',
  66. '"instantiate_named"', 2, '"l5"', '"weight"', '"P2T"', '"Integer"',
  67. '"instantiate_named"', 2, '"l5"', '"weight"', '"T2P"', '"Integer"',
  68. '"exit"',
  69. ]
  70. instantiate_pn = [
  71. '"model"',
  72. '"instantiate_model"', 2, 3,
  73. '"instantiate_node"', 3, '"Place"', '"p1"',
  74. '"instantiate_node"', 3, '"Place"', '"p2"',
  75. '"instantiate_node"', 3, '"Transition"', '"t1"',
  76. '"instantiate_node"', 3, '"Transition"', '"t2"',
  77. '"instantiate_link"', 3, '"P2T"', '"p1_t1"', '"p1"', '"t1"',
  78. '"instantiate_link"', 3, '"T2P"', '"t1_p2"', '"t1"', '"p2"',
  79. '"instantiate_link"', 3, '"P2T"', '"p2_t2"', '"p2"', '"t2"',
  80. '"instantiate_link"', 3, '"T2P"', '"t2_p1"', '"t2"', '"p1"',
  81. '"instantiate_attribute"', 3, '"p1_t1"', '"weight"', '1',
  82. '"instantiate_attribute"', 3, '"t1_p2"', '"weight"', '2',
  83. '"instantiate_attribute"', 3, '"p2_t2"', '"weight"', '3',
  84. '"instantiate_attribute"', 3, '"t2_p1"', '"weight"', '4',
  85. '"instantiate_attribute"', 3, '"p1"', '"tokens"', '5',
  86. '"instantiate_attribute"', 3, '"p2"', '"tokens"', '6',
  87. '"exit"',
  88. ]
  89. instantiate_example = [
  90. '"model"',
  91. '"instantiate_model"', 1, 2,
  92. '"define_inheritance"', 2, '"l3"',
  93. '"instantiate_node"', 2, '"Class"', '"A"',
  94. '"instantiate_node"', 2, '"Class"', '"B"',
  95. '"instantiate_node"', 2, '"Class"', '"C"',
  96. '"instantiate_link"', 2, '"l3"', '"b_inherits_a"', '"B"', '"A"',
  97. '"exit"',
  98. '"model"',
  99. '"instantiate_model"', 2, 3,
  100. '"instantiate_node"', 3, '"A"', '"a"',
  101. '"instantiate_node"', 3, '"B"', '"b"',
  102. '"instantiate_node"', 3, '"C"', '"c"',
  103. '"exit"',
  104. ]
  105. def conformance_call(operation, model, metamodel):
  106. return [
  107. '"output"',
  108. '"call"',
  109. '"access"', '"resolve"', '"%s"' % operation,
  110. '3',
  111. '"const"', 3,
  112. '"call"',
  113. '"access"', '"resolve"', '"dict_read"',
  114. '2',
  115. '"call"',
  116. '"access"', '"resolve"', '"dict_read"',
  117. '2',
  118. '"const"', 3,
  119. '"const"', '"model"',
  120. 'false',
  121. '"const"', '"%s"' % model,
  122. 'false',
  123. '"call"',
  124. '"access"', '"resolve"', '"dict_read"',
  125. '2',
  126. '"call"',
  127. '"access"', '"resolve"', '"dict_read"',
  128. '2',
  129. '"const"', 2,
  130. '"const"', '"model"',
  131. 'false',
  132. '"const"', '"%s"' % metamodel,
  133. 'false',
  134. 'false',
  135. 'true',
  136. ]
  137. is_direct_instance = \
  138. conformance_call("is_direct_instance", "a", "A") + \
  139. conformance_call("is_direct_instance", "b", "A") + \
  140. conformance_call("is_direct_instance", "c", "A") + \
  141. conformance_call("is_direct_instance", "a", "B") + \
  142. conformance_call("is_direct_instance", "b", "B") + \
  143. conformance_call("is_direct_instance", "c", "B") + \
  144. conformance_call("is_direct_instance", "a", "C") + \
  145. conformance_call("is_direct_instance", "b", "C") + \
  146. conformance_call("is_direct_instance", "c", "C")
  147. is_nominal_instance = \
  148. conformance_call("is_nominal_instance", "a", "A") + \
  149. conformance_call("is_nominal_instance", "b", "A") + \
  150. conformance_call("is_nominal_instance", "c", "A") + \
  151. conformance_call("is_nominal_instance", "a", "B") + \
  152. conformance_call("is_nominal_instance", "b", "B") + \
  153. conformance_call("is_nominal_instance", "c", "B") + \
  154. conformance_call("is_nominal_instance", "a", "C") + \
  155. conformance_call("is_nominal_instance", "b", "C") + \
  156. conformance_call("is_nominal_instance", "c", "C")
  157. def conformance_check(node):
  158. return [
  159. '"output"',
  160. '"call"',
  161. '"access"', '"resolve"', '"conformance_scd"',
  162. '1',
  163. '"const"', node,
  164. 'false',
  165. 'true',
  166. ]
  167. class TestConstructorsModels(unittest.TestCase):
  168. def test_constructors_instantiate_bottom(self):
  169. commands = bottom + retype + conformance_check(1) + ['"return"', 'false']
  170. self.assertTrue(run_barebone(commands, ["OK"], 1))
  171. def test_constructors_instantiate_scd(self):
  172. commands = bottom + retype + instantiate_scd + conformance_check(2) + ['"return"', 'false']
  173. self.assertTrue(run_barebone(commands, ["OK"], 1))
  174. def test_constructors_instantiate_pn(self):
  175. commands = bottom + retype + instantiate_scd + instantiate_pn + conformance_check(3) + ['"return"', 'false']
  176. self.assertTrue(run_barebone(commands, ["OK"], 1))
  177. def test_constructors_instantiate_example(self):
  178. commands = bottom + retype + instantiate_example + conformance_check(2) + conformance_check(3) + ['"return"', 'false']
  179. self.assertTrue(run_barebone(commands, ["OK", "OK"], 1))
  180. def test_constructors_is_direct_instance(self):
  181. commands = bottom + retype + instantiate_example + is_direct_instance + ['"return"', 'false']
  182. expected = ['True', 'False', 'False',
  183. 'False', 'True', 'False',
  184. 'False', 'False', 'True']
  185. self.assertTrue(run_barebone(commands, expected, 1))
  186. def test_constructors_is_nominal_instance(self):
  187. commands = bottom + retype + instantiate_example + is_nominal_instance + ['"return"', 'false']
  188. expected = ['True', 'True', 'False',
  189. 'False', 'True', 'False',
  190. 'False', 'False', 'True']
  191. self.assertTrue(run_barebone(commands, expected, 1))