scd.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. from state.base import State, UUID
  2. from services.bottom.V0 import Bottom
  3. from services.primitives.boolean_type import Boolean
  4. from services.primitives.string_type import String
  5. from bootstrap.primitive import (
  6. bootstrap_boolean_type,
  7. bootstrap_float_type,
  8. bootstrap_integer_type,
  9. bootstrap_string_type,
  10. bootstrap_type_type
  11. )
  12. def create_model_root(bottom: Bottom, model_name: str) -> UUID:
  13. model_root = bottom.create_node()
  14. mcl_root_id = bottom.create_node(value=str(model_root))
  15. bottom.create_edge(bottom.state.read_root(), mcl_root_id, label=model_name)
  16. return model_root
  17. def bootstrap_scd(state: State) -> UUID:
  18. # init model roots and store their UUIDs attached to state root
  19. bottom = Bottom(state)
  20. mcl_root = create_model_root(bottom, "SCD")
  21. # Create model roots for primitive types
  22. integer_type_root = create_model_root(bottom, "Integer")
  23. boolean_type_root = create_model_root(bottom, "Boolean")
  24. string_type_root = create_model_root(bottom, "String")
  25. float_type_root = create_model_root(bottom, "Float")
  26. type_type_root = create_model_root(bottom, "Type")
  27. # create MCL, without morphism links
  28. def add_node_element(element_name, node_value=None):
  29. """ Helper function, adds node to model with given name and value """
  30. _node = bottom.create_node(value=node_value)
  31. bottom.create_edge(mcl_root, _node, element_name)
  32. return _node
  33. def add_edge_element(element_name, source, target):
  34. """ Helper function, adds edge to model with given name """
  35. _edge = bottom.create_edge(source, target)
  36. bottom.create_edge(mcl_root, _edge, element_name)
  37. return _edge
  38. def add_attribute_attributes(attribute_element_name, attribute_element):
  39. _name_model = bottom.create_node()
  40. _name_node = add_node_element(f"{attribute_element_name}.name", str(_name_model))
  41. _name_edge = add_edge_element(f"{attribute_element_name}.name_link", attribute_element, _name_node)
  42. _optional_model = bottom.create_node()
  43. _optional_node = add_node_element(f"{attribute_element_name}.optional", str(_optional_model))
  44. _optional_edge = add_edge_element(f"{attribute_element_name}.optional_link", attribute_element, _optional_node)
  45. return _name_model, _optional_model
  46. # # CLASSES, i.e. elements typed by Class
  47. # # Element
  48. element_node = add_node_element("Element")
  49. # # Class
  50. class_node = add_node_element("Class")
  51. # # Attribute
  52. attr_node = add_node_element("Attribute")
  53. # # ModelRef
  54. model_ref_node = add_node_element("ModelRef")
  55. # # Global Constraint
  56. glob_constr_node = add_node_element("GlobalConstraint")
  57. # # ASSOCIATIONS, i.e. elements typed by Association
  58. # # Association
  59. assoc_edge = add_edge_element("Association", class_node, class_node)
  60. # # Inheritance
  61. inh_edge = add_edge_element("Inheritance", element_node, element_node)
  62. # # Attribute Link
  63. attr_link_edge = add_edge_element("AttributeLink", element_node, attr_node)
  64. # # INHERITANCES, i.e. elements typed by Inheritance
  65. # # Class inherits from Element
  66. add_edge_element("class_inh_element", class_node, element_node)
  67. # # Attribute inherits from Element
  68. add_edge_element("attr_inh_element", attr_node, element_node)
  69. # # Association inherits from Element
  70. add_edge_element("assoc_inh_element", assoc_edge, element_node)
  71. # # AttributeLink inherits from Element
  72. add_edge_element("attr_link_inh_element", attr_link_edge, element_node)
  73. # # ModelRef inherits from Attribute
  74. add_edge_element("model_ref_inh_attr", model_ref_node, attr_node)
  75. # # ATTRIBUTES, i.e. elements typed by Attribute
  76. # # Action Code # TODO: Update to ModelRef when action code is explicitly modelled
  77. action_code_node = add_node_element("ActionCode")
  78. # # MODELREFS, i.e. elements typed by ModelRef
  79. # # Integer
  80. integer_node = add_node_element("Integer", str(integer_type_root))
  81. # # String
  82. string_node = add_node_element("String", str(string_type_root))
  83. # # Boolean
  84. boolean_node = add_node_element("Boolean", str(boolean_type_root))
  85. # # ATTRIBUTE LINKS, i.e. elements typed by AttributeLink
  86. # # name attribute of AttributeLink
  87. attr_name_edge = add_edge_element("AttributeLink_name", attr_link_edge, string_node)
  88. # # optional attribute of AttributeLink
  89. attr_opt_edge = add_edge_element("AttributeLink_optional", attr_link_edge, boolean_node)
  90. # # constraint attribute of Element
  91. elem_constr_edge = add_edge_element("Element_constraint", element_node, action_code_node)
  92. # # abstract attribute of Class
  93. class_abs_edge = add_edge_element("Class_abstract", class_node, boolean_node)
  94. # # multiplicity attributes of Class
  95. class_l_c_edge = add_edge_element("Class_lower_cardinality", class_node, integer_node)
  96. class_u_c_edge = add_edge_element("Class_upper_cardinality", class_node, integer_node)
  97. # # multiplicity attributes of Association
  98. assoc_s_l_c_edge = add_edge_element("Association_source_lower_cardinality", assoc_edge, integer_node)
  99. assoc_s_u_c_edge = add_edge_element("Association_source_upper_cardinality", assoc_edge, integer_node)
  100. assoc_t_l_c_edge = add_edge_element("Association_target_lower_cardinality", assoc_edge, integer_node)
  101. assoc_t_u_c_edge = add_edge_element("Association_target_upper_cardinality", assoc_edge, integer_node)
  102. # # bootstrap primitive types
  103. bootstrap_boolean_type(mcl_root, boolean_type_root, state)
  104. bootstrap_float_type(mcl_root, float_type_root, state)
  105. bootstrap_integer_type(mcl_root, integer_type_root, state)
  106. bootstrap_string_type(mcl_root, string_type_root, state)
  107. bootstrap_type_type(mcl_root, type_type_root, state)
  108. # # ATTRIBUTE ATTRIBUTES, assign 'name' and 'optional' attributes to all AttributeLinks
  109. # # AttributeLink_name
  110. m_name, m_opt = add_attribute_attributes("AttributeLink_name", attr_name_edge)
  111. String(m_name, state).create("name")
  112. Boolean(m_opt, state).create(False)
  113. # # AttributeLink_opt
  114. m_name, m_opt = add_attribute_attributes("AttributeLink_optional", attr_opt_edge)
  115. String(m_name, state).create("optional")
  116. Boolean(m_opt, state).create(False)
  117. # # Element_constraint
  118. m_name, m_opt = add_attribute_attributes("Element_constraint", elem_constr_edge)
  119. String(m_name, state).create("constraint")
  120. Boolean(m_opt, state).create(True)
  121. # # Class_abstract
  122. m_name, m_opt = add_attribute_attributes("Class_abstract", class_abs_edge)
  123. String(m_name, state).create("abstract")
  124. Boolean(m_opt, state).create(True)
  125. # # Class_lower_cardinality
  126. m_name, m_opt = add_attribute_attributes("Class_lower_cardinality", class_l_c_edge)
  127. String(m_name, state).create("lower_cardinality")
  128. Boolean(m_opt, state).create(True)
  129. # # Class_upper_cardinality
  130. m_name, m_opt = add_attribute_attributes("Class_upper_cardinality", class_u_c_edge)
  131. String(m_name, state).create("upper_cardinality")
  132. Boolean(m_opt, state).create(True)
  133. # # Association_source_lower_cardinality
  134. m_name, m_opt = add_attribute_attributes("Association_source_lower_cardinality", assoc_s_l_c_edge)
  135. String(m_name, state).create("source_lower_cardinality")
  136. Boolean(m_opt, state).create(True)
  137. # # Association_source_upper_cardinality
  138. m_name, m_opt = add_attribute_attributes("Association_source_upper_cardinality", assoc_s_u_c_edge)
  139. String(m_name, state).create("source_upper_cardinality")
  140. Boolean(m_opt, state).create(True)
  141. # # Association_target_lower_cardinality
  142. m_name, m_opt = add_attribute_attributes("Association_target_lower_cardinality", assoc_t_l_c_edge)
  143. String(m_name, state).create("target_lower_cardinality")
  144. Boolean(m_opt, state).create(True)
  145. # # Association_target_upper_cardinality
  146. m_name, m_opt = add_attribute_attributes("Association_target_upper_cardinality", assoc_t_u_c_edge)
  147. String(m_name, state).create("target_upper_cardinality")
  148. Boolean(m_opt, state).create(True)
  149. # create phi(SCD,SCD) to type MCL with itself
  150. def add_mcl_morphism(element_name, type_name):
  151. # get elements from mcl by name
  152. _element_edge, = bottom.read_outgoing_edges(mcl_root, element_name)
  153. _element_node = bottom.read_edge_target(_element_edge)
  154. _type_edge, = bottom.read_outgoing_edges(mcl_root, type_name)
  155. _type_node = bottom.read_edge_target(_type_edge)
  156. # create morphism link
  157. bottom.create_edge(_element_node, _type_node, "Morphism")
  158. # Class
  159. add_mcl_morphism("Element", "Class")
  160. add_mcl_morphism("Class", "Class")
  161. add_mcl_morphism("Attribute", "Class")
  162. add_mcl_morphism("ModelRef", "Class")
  163. add_mcl_morphism("GlobalConstraint", "Class")
  164. # Association
  165. add_mcl_morphism("Association", "Association")
  166. add_mcl_morphism("Inheritance", "Association")
  167. add_mcl_morphism("AttributeLink", "Association")
  168. # Inheritance
  169. add_mcl_morphism("class_inh_element", "Inheritance")
  170. add_mcl_morphism("attr_inh_element", "Inheritance")
  171. add_mcl_morphism("assoc_inh_element", "Inheritance")
  172. add_mcl_morphism("attr_link_inh_element", "Inheritance")
  173. add_mcl_morphism("model_ref_inh_attr", "Inheritance")
  174. # Attribute
  175. add_mcl_morphism("ActionCode", "Attribute")
  176. # ModelRef
  177. add_mcl_morphism("Integer", "ModelRef")
  178. add_mcl_morphism("String", "ModelRef")
  179. add_mcl_morphism("Boolean", "ModelRef")
  180. # AttributeLink
  181. add_mcl_morphism("AttributeLink_name", "AttributeLink")
  182. add_mcl_morphism("AttributeLink_optional", "AttributeLink")
  183. add_mcl_morphism("Element_constraint", "AttributeLink")
  184. add_mcl_morphism("Class_abstract", "AttributeLink")
  185. add_mcl_morphism("Class_lower_cardinality", "AttributeLink")
  186. add_mcl_morphism("Class_upper_cardinality", "AttributeLink")
  187. add_mcl_morphism("Association_source_lower_cardinality", "AttributeLink")
  188. add_mcl_morphism("Association_source_upper_cardinality", "AttributeLink")
  189. add_mcl_morphism("Association_target_lower_cardinality", "AttributeLink")
  190. add_mcl_morphism("Association_target_upper_cardinality", "AttributeLink")
  191. # AttributeLink_name
  192. add_mcl_morphism("AttributeLink_name.name_link", "AttributeLink_name")
  193. add_mcl_morphism("AttributeLink_optional.name_link", "AttributeLink_name")
  194. add_mcl_morphism("Element_constraint.name_link", "AttributeLink_name")
  195. add_mcl_morphism("Class_abstract.name_link", "AttributeLink_name")
  196. add_mcl_morphism("Class_lower_cardinality.name_link", "AttributeLink_name")
  197. add_mcl_morphism("Class_upper_cardinality.name_link", "AttributeLink_name")
  198. add_mcl_morphism("Association_source_lower_cardinality.name_link", "AttributeLink_name")
  199. add_mcl_morphism("Association_source_upper_cardinality.name_link", "AttributeLink_name")
  200. add_mcl_morphism("Association_target_lower_cardinality.name_link", "AttributeLink_name")
  201. add_mcl_morphism("Association_target_upper_cardinality.name_link", "AttributeLink_name")
  202. # AttributeLink_optional
  203. add_mcl_morphism("AttributeLink_name.optional_link", "AttributeLink_optional")
  204. add_mcl_morphism("AttributeLink_optional.optional_link", "AttributeLink_optional")
  205. add_mcl_morphism("Element_constraint.optional_link", "AttributeLink_optional")
  206. add_mcl_morphism("Class_abstract.optional_link", "AttributeLink_optional")
  207. add_mcl_morphism("Class_lower_cardinality.optional_link", "AttributeLink_optional")
  208. add_mcl_morphism("Class_upper_cardinality.optional_link", "AttributeLink_optional")
  209. add_mcl_morphism("Association_source_lower_cardinality.optional_link", "AttributeLink_optional")
  210. add_mcl_morphism("Association_source_upper_cardinality.optional_link", "AttributeLink_optional")
  211. add_mcl_morphism("Association_target_lower_cardinality.optional_link", "AttributeLink_optional")
  212. add_mcl_morphism("Association_target_upper_cardinality.optional_link", "AttributeLink_optional")
  213. # String
  214. add_mcl_morphism("AttributeLink_name.name", "String")
  215. add_mcl_morphism("AttributeLink_optional.name", "String")
  216. add_mcl_morphism("Element_constraint.name", "String")
  217. add_mcl_morphism("Class_abstract.name", "String")
  218. add_mcl_morphism("Class_lower_cardinality.name", "String")
  219. add_mcl_morphism("Class_upper_cardinality.name", "String")
  220. add_mcl_morphism("Association_source_lower_cardinality.name", "String")
  221. add_mcl_morphism("Association_source_upper_cardinality.name", "String")
  222. add_mcl_morphism("Association_target_lower_cardinality.name", "String")
  223. add_mcl_morphism("Association_target_upper_cardinality.name", "String")
  224. # Boolean
  225. add_mcl_morphism("AttributeLink_name.optional", "Boolean")
  226. add_mcl_morphism("AttributeLink_optional.optional", "Boolean")
  227. add_mcl_morphism("Element_constraint.optional", "Boolean")
  228. add_mcl_morphism("Class_abstract.optional", "Boolean")
  229. add_mcl_morphism("Class_lower_cardinality.optional", "Boolean")
  230. add_mcl_morphism("Class_upper_cardinality.optional", "Boolean")
  231. add_mcl_morphism("Association_source_lower_cardinality.optional", "Boolean")
  232. add_mcl_morphism("Association_source_upper_cardinality.optional", "Boolean")
  233. add_mcl_morphism("Association_target_lower_cardinality.optional", "Boolean")
  234. add_mcl_morphism("Association_target_upper_cardinality.optional", "Boolean")
  235. return mcl_root
  236. if __name__ == '__main__':
  237. from state.devstate import DevState as State
  238. s = State()
  239. bootstrap_scd(s)
  240. r = s.read_root()
  241. for n in s.read_dict_keys(r):
  242. print(s.read_value(n))