scd.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from state.base import State, UUID
  2. from services.bottom.V0 import Bottom
  3. def create_model_root(bottom: Bottom, model_name: str) -> UUID:
  4. model_root = bottom.create_node()
  5. mcl_root_id = bottom.create_node(value=str(model_root))
  6. bottom.create_edge(bottom.model, mcl_root_id, label=model_name)
  7. return model_root
  8. def bootstrap_scd(state: State) -> UUID:
  9. # init model roots and store their UUIDs attached to state root
  10. state_root = state.read_root()
  11. bottom = Bottom(state_root, state)
  12. mcl_root = create_model_root(bottom, "SCD")
  13. mcl_morphism_root = create_model_root(bottom, "phi(SCD,SCD)")
  14. # Create model roots for primitive types
  15. integer_type_root = create_model_root(bottom, "Integer")
  16. boolean_type_root = create_model_root(bottom, "Boolean")
  17. string_type_root = create_model_root(bottom, "String")
  18. float_type_root = create_model_root(bottom, "Float")
  19. type_type_root = create_model_root(bottom, "Type")
  20. # create MCL, without morphism links
  21. bottom = Bottom(mcl_root, state)
  22. def add_node_element(element_name, node_value=None):
  23. """ Helper function, adds node to model with given name and value """
  24. _node = bottom.create_node(value=node_value)
  25. bottom.create_edge(bottom.model, _node, element_name)
  26. return _node
  27. def add_edge_element(element_name, source, target):
  28. """ Helper function, adds edge to model with given name """
  29. _edge = bottom.create_edge(source, target)
  30. bottom.create_edge(bottom.model, _edge, element_name)
  31. return _edge
  32. def add_attribute_attributes(attribute_element_name, attribute_element, _name, _optional):
  33. _name_node = add_node_element(f"{attribute_element_name}.name", _name)
  34. _name_edge = add_edge_element(f"{attribute_element_name}01", attribute_element, _name_node)
  35. _optional_node = add_node_element(f"{attribute_element_name}.optional", _optional)
  36. _optional_edge = add_edge_element(f"{attribute_element_name}02", attribute_element, _optional_node)
  37. return _name_node, _name_edge, _optional_node, _optional_edge
  38. # # CLASSES, i.e. elements typed by Class
  39. # # Element
  40. element_node = add_node_element("Element")
  41. # # Class
  42. class_node = add_node_element("Class")
  43. # # Attribute
  44. attr_node = add_node_element("Attribute")
  45. # # ModelRef
  46. model_ref_node = add_node_element("ModelRef")
  47. # # Global Constraint
  48. glob_constr_node = add_node_element("GlobalConstraint")
  49. # # ASSOCIATIONS, i.e. elements typed by Association
  50. # # Association
  51. assoc_edge = add_edge_element("Association", class_node, class_node)
  52. # # Inheritance
  53. inh_edge = add_edge_element("Inheritance", element_node, element_node)
  54. # # Attribute Link
  55. attr_link_edge = add_edge_element("AttributeLink", element_node, attr_node)
  56. # # INHERITANCES, i.e. elements typed by Inheritance
  57. # # Class inherits from Element
  58. class_inh_element_edge = add_edge_element("class_inh_element", class_node, element_node)
  59. # # Attribute inherits from Element
  60. attr_inh_element_edge = add_edge_element("attr_inh_element", attr_node, element_node)
  61. # # Association inherits from Element
  62. assoc_inh_element_edge = add_edge_element("assoc_inh_element", assoc_edge, element_node)
  63. # # AttributeLink inherits from Element
  64. attr_link_inh_element_edge = add_edge_element("attr_link_inh_element", attr_link_edge, element_node)
  65. # # ModelRef inherits from Attribute
  66. model_ref_inh_attr_edge = add_edge_element("model_ref_inh_attr", model_ref_node, attr_node)
  67. # # ATTRIBUTES, i.e. elements typed by Attribute
  68. # # Action Code # TODO: Update to ModelRef when action code is explicitly modelled
  69. action_code_node = add_node_element("ActionCode")
  70. # # MODELREFS, i.e. elements typed by ModelRef
  71. # # Integer
  72. integer_node = add_node_element("Integer", str(integer_type_root))
  73. # # String
  74. string_node = add_node_element("String", str(string_type_root))
  75. # # Boolean
  76. boolean_node = add_node_element("Boolean", str(boolean_type_root))
  77. # # ATTRIBUTE LINKS, i.e. elements typed by AttributeLink
  78. # # name attribute of AttributeLink
  79. attr_name_edge = add_edge_element("AttributeLink_name", attr_link_edge, string_node)
  80. # # optional attribute of AttributeLink
  81. attr_opt_edge = add_edge_element("AttributeLink_optional", attr_link_edge, boolean_node)
  82. # # constraint attribute of Element
  83. elem_constr_edge = add_edge_element("Element_constraint", element_node, action_code_node)
  84. # # abstract attribute of Class
  85. class_abs_edge = add_edge_element("Class_abstract", class_node, boolean_node)
  86. # # multiplicity attributes of Class
  87. class_l_c_edge = add_edge_element("Class_lower_cardinality", class_node, integer_node)
  88. class_u_c_edge = add_edge_element("Class_upper_cardinality", class_node, integer_node)
  89. # # multiplicity attributes of Association
  90. assoc_s_l_c_edge = add_edge_element("Association_source_lower_cardinality", assoc_edge, integer_node)
  91. assoc_s_u_c_edge = add_edge_element("Association_source_upper_cardinality", assoc_edge, integer_node)
  92. assoc_t_l_c_edge = add_edge_element("Association_target_lower_cardinality", assoc_edge, integer_node)
  93. assoc_t_u_c_edge = add_edge_element("Association_target_upper_cardinality", assoc_edge, integer_node)
  94. # # ATTRIBUTE ATTRIBUTES, assign 'name' and 'optional' attributes to all AttributeLinks
  95. # # AttributeLink_name
  96. attr_name_name_node, attr_name_name_edge, attr_name_optional_node, attr_name_optional_edge = \
  97. add_attribute_attributes("AttributeLink_name", attr_name_edge, "name", False)
  98. # # AttributeLink_opt
  99. attr_opt_name_node, attr_opt_name_edge, attr_opt_optional_node, attr_opt_optional_edge = \
  100. add_attribute_attributes("AttributeLink_optional", attr_opt_edge, "optional", False)
  101. # # Element_constraint
  102. elem_constr_name_node, elem_constr_name_edge, elem_constr_optional_node, elem_constr_optional_edge = \
  103. add_attribute_attributes("Element_constraint", elem_constr_edge, "constraint", True)
  104. # # Class_abstract
  105. class_abs_name_node, class_abs_name_edge, class_abs_optional_node, class_abs_optional_edge = \
  106. add_attribute_attributes("Class_abstract", class_abs_edge, "abstract", True)
  107. # # Class_lower_cardinality
  108. class_l_c_name_node, class_l_c_name_edge, class_l_c_optional_node, class_l_c_optional_edge = \
  109. add_attribute_attributes("Class_lower_cardinality", class_l_c_edge, "lower_cardinality", True)
  110. # # Class_upper_cardinality
  111. class_u_c_name_node, class_u_c_name_edge, class_u_c_optional_node, class_u_c_optional_edge = \
  112. add_attribute_attributes("Class_upper_cardinality", class_u_c_edge, "upper_cardinality", True)
  113. # # Association_source_lower_cardinality
  114. assoc_s_l_c_name_node, assoc_s_l_c_name_edge, assoc_s_l_c_optional_node, assoc_s_l_c_optional_edge = \
  115. add_attribute_attributes("Association_source_lower_cardinality", assoc_s_l_c_edge, "source_lower_cardinality", True)
  116. # # Association_source_upper_cardinality
  117. assoc_s_u_c_name_node, assoc_s_u_c_name_edge, assoc_s_u_c_optional_node, assoc_s_u_c_optional_edge = \
  118. add_attribute_attributes("Association_source_upper_cardinality", assoc_s_u_c_edge, "source_upper_cardinality", True)
  119. # # Association_target_lower_cardinality
  120. assoc_t_l_c_name_node, assoc_t_l_c_name_edge, assoc_t_l_c_optional_node, assoc_t_l_c_optional_edge = \
  121. add_attribute_attributes("Association_target_lower_cardinality", assoc_t_l_c_edge, "target_lower_cardinality", True)
  122. # # Association_target_upper_cardinality
  123. assoc_t_u_c_name_node, assoc_t_u_c_name_edge, assoc_t_u_c_optional_node, assoc_t_u_c_optional_edge = \
  124. add_attribute_attributes("Association_target_upper_cardinality", assoc_t_u_c_edge, "target_upper_cardinality", True)
  125. # create phi(SCD,SCD) to type MCL with itself
  126. def bootstrap_type_type(model_root: UUID, state: State) -> UUID:
  127. pass
  128. def bootstrap_boolean_type(model_root: UUID, state: State) -> UUID:
  129. pass
  130. def bootstrap_integer_type(model_root: UUID, state: State) -> UUID:
  131. pass
  132. def bootstrap_float_type(model_root: UUID, state: State) -> UUID:
  133. pass
  134. def bootstrap_string_type(model_root: UUID, state: State) -> UUID:
  135. pass