exp_scd.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Simple Class Diagram experiment
  2. from state.devstate import DevState
  3. from bootstrap.scd import bootstrap_scd
  4. from uuid import UUID
  5. from services.scd import SCD
  6. from framework.conformance import Conformance
  7. from services.od import OD
  8. from transformation.ramify import ramify
  9. from services.primitives.integer_type import Integer
  10. import sys
  11. def create_integer_node(state, i: int):
  12. node = state.create_node()
  13. integer_t = Integer(node, state)
  14. integer_t.create(i)
  15. return node
  16. def main():
  17. state = DevState()
  18. root = state.read_root() # id: 0
  19. scd_node = bootstrap_scd(state)
  20. scd_node2 = state.read_dict(root, "SCD")
  21. # print(root, scd_node, scd_node2)
  22. def print_tree(root, max_depth, depth=0):
  23. print(" "*depth, "root=", root, "value=", state.read_value(root))
  24. src,tgt = state.read_edge(root)
  25. if src != None:
  26. print(" "*depth, "src...")
  27. print_tree(src, max_depth, depth+1)
  28. if tgt != None:
  29. print(" "*depth, "tgt...")
  30. print_tree(tgt, max_depth, depth+1)
  31. for edge in state.read_outgoing(root):
  32. for edge_label in state.read_outgoing(edge):
  33. [_,tgt] = state.read_edge(edge_label)
  34. label = state.read_value(tgt)
  35. print(" "*depth, " key:", label)
  36. [_, tgt] = state.read_edge(edge)
  37. value = state.read_value(tgt)
  38. if value != None:
  39. print(" "*depth, " ->", tgt, " (value:", value, ")")
  40. else:
  41. print(" "*depth, " ->", tgt)
  42. if depth < max_depth:
  43. if isinstance(value, str) and len(value) == 36:
  44. i = None
  45. try:
  46. i = UUID(value)
  47. except ValueError as e:
  48. # print("invalid UUID:", value)
  49. pass
  50. if i != None:
  51. print_tree(i, max_depth, depth+1)
  52. print_tree(tgt, max_depth, depth+1)
  53. print("explore...")
  54. # print_tree(root, 2)
  55. int_type_id = state.read_dict(state.read_root(), "Integer")
  56. int_type = UUID(state.read_value(int_type_id))
  57. # scd2 = SCD(scd_node, state)
  58. # for el in scd2.list_elements():
  59. # print(el)
  60. model_id = state.create_node()
  61. scd = SCD(model_id, state)
  62. scd.create_class("Abstract", abstract=True)
  63. scd.create_class("A", min_c=1, max_c=10)
  64. scd.create_inheritance("A", "Abstract")
  65. scd.create_model_ref("Integer", int_type)
  66. scd.create_attribute_link("A", "Integer", "size", False)
  67. scd.create_class("B")
  68. scd.create_association("A2B", "A", "B",
  69. src_min_c=1,
  70. src_max_c=1,
  71. tgt_min_c=1,
  72. tgt_max_c=2,
  73. )
  74. # print_tree(model_id, 3)
  75. conf = Conformance(state, model_id, scd_node)
  76. print("Check nominal conformance...")
  77. print(conf.check_nominal(log=True))
  78. # print("Check structural conformance...")
  79. # print(conf.check_structural(log=True))
  80. # print("Check nominal conformance (again)...")
  81. # print(conf.check_nominal(log=True))
  82. inst_id = state.create_node()
  83. od = OD(model_id, inst_id, state)
  84. od.create_object("a", "A")
  85. od.create_object("b", "B")
  86. od.create_link("A2B", "a", "b")
  87. od.create_slot("size", "a", od.create_integer_value(42))
  88. print("checking conformance....")
  89. conf2 = Conformance(state, inst_id, model_id)
  90. print("conforms?", conf2.check_nominal(log=True))
  91. ramify(state, model_id)
  92. if __name__ == "__main__":
  93. main()