exp_scd.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. for edge in state.read_outgoing(root):
  25. for edge_label in state.read_outgoing(edge):
  26. [_,tgt] = state.read_edge(edge_label)
  27. label = state.read_value(tgt)
  28. print(" "*depth, " key:", label)
  29. [_, tgt] = state.read_edge(edge)
  30. value = state.read_value(tgt)
  31. if value != None:
  32. print(" "*depth, " ->", tgt, " (value:", value, ")")
  33. else:
  34. print(" "*depth, " ->", tgt)
  35. if depth < max_depth:
  36. if isinstance(value, str) and len(value) == 36:
  37. i = None
  38. try:
  39. i = UUID(value)
  40. except ValueError as e:
  41. # print("invalid UUID:", value)
  42. pass
  43. if i != None:
  44. print_tree(i, max_depth, depth+1)
  45. print_tree(tgt, max_depth, depth+1)
  46. print("explore...")
  47. print_tree(root, 2)
  48. int_type_id = state.read_dict(state.read_root(), "Integer")
  49. int_type = UUID(state.read_value(int_type_id))
  50. scd2 = SCD(scd_node, state)
  51. for el in scd2.list_elements():
  52. print(el)
  53. model_id = state.create_node()
  54. scd = SCD(model_id, state)
  55. scd.create_class("A")
  56. scd.create_model_ref("Integer", int_type)
  57. scd.create_attribute_link("A", "Integer", "size", False)
  58. scd.create_class("B")
  59. scd.create_association("A2B", "A", "B",
  60. src_min_c=1,
  61. src_max_c=1,
  62. tgt_min_c=1,
  63. tgt_max_c=2,
  64. )
  65. print_tree(model_id, 2)
  66. conf = Conformance(state, model_id, scd_node)
  67. print("Check nominal conformance...")
  68. print(conf.check_nominal(log=True))
  69. # print("Check structural conformance...")
  70. # print(conf.check_structural(log=True))
  71. # print("Check nominal conformance (again)...")
  72. # print(conf.check_nominal(log=True))
  73. inst_id = state.create_node()
  74. od = OD(model_id, inst_id, state)
  75. od.create_object("a", "A")
  76. od.create_object("b", "B")
  77. od.create_link("A2B", "a", "b")
  78. od.create_slot("size", "a", od.create_integer_value(42))
  79. print("checking conformance....")
  80. conf2 = Conformance(state, inst_id, model_id)
  81. print(conf2.check_nominal(log=True))
  82. # ramify(state, model_id)
  83. if __name__ == "__main__":
  84. main()