test_constructors_models.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 is_direct_instance_call(model, metamodel):
  106. return [
  107. '"output"',
  108. '"call"',
  109. '"access"', '"resolve"', '"is_direct_instance"',
  110. '3',
  111. '"const"', 3,
  112. '"call"',
  113. '"access"', '"resolve"', '"dict_read"',
  114. '2',
  115. '"const"', 3,
  116. '"const"', '"%s"' % model,
  117. 'false',
  118. '"call"',
  119. '"access"', '"resolve"', '"dict_read"',
  120. '2',
  121. '"const"', 2,
  122. '"const"', '"%s"' % metamodel,
  123. 'false',
  124. 'false',
  125. 'true',
  126. ]
  127. is_direct_instance = \
  128. is_direct_instance_call("a", "A") + \
  129. is_direct_instance_call("b", "A") + \
  130. is_direct_instance_call("c", "A") + \
  131. is_direct_instance_call("a", "B") + \
  132. is_direct_instance_call("b", "B") + \
  133. is_direct_instance_call("c", "B") + \
  134. is_direct_instance_call("a", "C") + \
  135. is_direct_instance_call("b", "C") + \
  136. is_direct_instance_call("c", "C")
  137. def conformance_check(node):
  138. return [
  139. '"output"',
  140. '"call"',
  141. '"access"', '"resolve"', '"conformance_scd"',
  142. '1',
  143. '"const"', node,
  144. 'false',
  145. 'true',
  146. ]
  147. class TestConstructorsModels(unittest.TestCase):
  148. def test_constructors_instantiate_bottom(self):
  149. commands = bottom + retype + conformance_check(1) + ['"return"', 'false']
  150. self.assertTrue(run_barebone(commands, ["OK"], 1))
  151. def test_constructors_instantiate_scd(self):
  152. commands = bottom + retype + instantiate_scd + conformance_check(2) + ['"return"', 'false']
  153. self.assertTrue(run_barebone(commands, ["OK"], 1))
  154. def test_constructors_instantiate_pn(self):
  155. commands = bottom + retype + instantiate_scd + instantiate_pn + conformance_check(3) + ['"return"', 'false']
  156. self.assertTrue(run_barebone(commands, ["OK"], 1))
  157. def test_constructors_instantiate_example(self):
  158. commands = bottom + retype + instantiate_example + conformance_check(2) + conformance_check(3) + ['"return"', 'false']
  159. self.assertTrue(run_barebone(commands, ["OK", "OK"], 1))
  160. def test_constructors_is_direct_instance(self):
  161. commands = bottom + retype + instantiate_example + is_direct_instance + ['"return"', 'false']
  162. expected = ['True', 'False', 'False',
  163. 'False', 'True', 'False',
  164. 'False', 'False', 'True']
  165. self.assertTrue(run_barebone(commands, expected, 1))