scd.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. # # order is important, integer must be first
  104. bootstrap_integer_type(mcl_root, integer_type_root, state)
  105. bootstrap_boolean_type(mcl_root, boolean_type_root, state)
  106. bootstrap_float_type(mcl_root, float_type_root, state)
  107. bootstrap_string_type(mcl_root, string_type_root, state)
  108. bootstrap_type_type(mcl_root, type_type_root, state)
  109. # # ATTRIBUTE ATTRIBUTES, assign 'name' and 'optional' attributes to all AttributeLinks
  110. # # AttributeLink_name
  111. m_name, m_opt = add_attribute_attributes("AttributeLink_name", attr_name_edge)
  112. String(m_name, state).create("name")
  113. Boolean(m_opt, state).create(False)
  114. # # AttributeLink_opt
  115. m_name, m_opt = add_attribute_attributes("AttributeLink_optional", attr_opt_edge)
  116. String(m_name, state).create("optional")
  117. Boolean(m_opt, state).create(False)
  118. # # Element_constraint
  119. m_name, m_opt = add_attribute_attributes("Element_constraint", elem_constr_edge)
  120. String(m_name, state).create("constraint")
  121. Boolean(m_opt, state).create(True)
  122. # # Class_abstract
  123. m_name, m_opt = add_attribute_attributes("Class_abstract", class_abs_edge)
  124. String(m_name, state).create("abstract")
  125. Boolean(m_opt, state).create(True)
  126. # # Class_lower_cardinality
  127. m_name, m_opt = add_attribute_attributes("Class_lower_cardinality", class_l_c_edge)
  128. String(m_name, state).create("lower_cardinality")
  129. Boolean(m_opt, state).create(True)
  130. # # Class_upper_cardinality
  131. m_name, m_opt = add_attribute_attributes("Class_upper_cardinality", class_u_c_edge)
  132. String(m_name, state).create("upper_cardinality")
  133. Boolean(m_opt, state).create(True)
  134. # # Association_source_lower_cardinality
  135. m_name, m_opt = add_attribute_attributes("Association_source_lower_cardinality", assoc_s_l_c_edge)
  136. String(m_name, state).create("source_lower_cardinality")
  137. Boolean(m_opt, state).create(True)
  138. # # Association_source_upper_cardinality
  139. m_name, m_opt = add_attribute_attributes("Association_source_upper_cardinality", assoc_s_u_c_edge)
  140. String(m_name, state).create("source_upper_cardinality")
  141. Boolean(m_opt, state).create(True)
  142. # # Association_target_lower_cardinality
  143. m_name, m_opt = add_attribute_attributes("Association_target_lower_cardinality", assoc_t_l_c_edge)
  144. String(m_name, state).create("target_lower_cardinality")
  145. Boolean(m_opt, state).create(True)
  146. # # Association_target_upper_cardinality
  147. m_name, m_opt = add_attribute_attributes("Association_target_upper_cardinality", assoc_t_u_c_edge)
  148. String(m_name, state).create("target_upper_cardinality")
  149. Boolean(m_opt, state).create(True)
  150. # # Make Element abstract
  151. abs_model = bottom.create_node()
  152. abs_node = add_node_element(f"Element.abstract", str(abs_model))
  153. abs_edge = add_edge_element(f"Element.abstract_link", element_node, abs_node)
  154. Boolean(abs_model, state).create(True)
  155. # create phi(SCD,SCD) to type MCL with itself
  156. def add_mcl_morphism(element_name, type_name):
  157. # get elements from mcl by name
  158. _element_edge, = bottom.read_outgoing_edges(mcl_root, element_name)
  159. _element_node = bottom.read_edge_target(_element_edge)
  160. _type_edge, = bottom.read_outgoing_edges(mcl_root, type_name)
  161. _type_node = bottom.read_edge_target(_type_edge)
  162. # create morphism link
  163. bottom.create_edge(_element_node, _type_node, "Morphism")
  164. # Class
  165. add_mcl_morphism("Element", "Class")
  166. add_mcl_morphism("Class", "Class")
  167. add_mcl_morphism("Attribute", "Class")
  168. add_mcl_morphism("ModelRef", "Class")
  169. add_mcl_morphism("GlobalConstraint", "Class")
  170. # Association
  171. add_mcl_morphism("Association", "Association")
  172. add_mcl_morphism("Inheritance", "Association")
  173. add_mcl_morphism("AttributeLink", "Association")
  174. # Inheritance
  175. add_mcl_morphism("class_inh_element", "Inheritance")
  176. add_mcl_morphism("attr_inh_element", "Inheritance")
  177. add_mcl_morphism("assoc_inh_element", "Inheritance")
  178. add_mcl_morphism("attr_link_inh_element", "Inheritance")
  179. add_mcl_morphism("model_ref_inh_attr", "Inheritance")
  180. # Attribute
  181. add_mcl_morphism("ActionCode", "Attribute")
  182. # ModelRef
  183. add_mcl_morphism("Integer", "ModelRef")
  184. add_mcl_morphism("String", "ModelRef")
  185. add_mcl_morphism("Boolean", "ModelRef")
  186. # AttributeLink
  187. add_mcl_morphism("AttributeLink_name", "AttributeLink")
  188. add_mcl_morphism("AttributeLink_optional", "AttributeLink")
  189. add_mcl_morphism("Element_constraint", "AttributeLink")
  190. add_mcl_morphism("Class_abstract", "AttributeLink")
  191. add_mcl_morphism("Class_lower_cardinality", "AttributeLink")
  192. add_mcl_morphism("Class_upper_cardinality", "AttributeLink")
  193. add_mcl_morphism("Association_source_lower_cardinality", "AttributeLink")
  194. add_mcl_morphism("Association_source_upper_cardinality", "AttributeLink")
  195. add_mcl_morphism("Association_target_lower_cardinality", "AttributeLink")
  196. add_mcl_morphism("Association_target_upper_cardinality", "AttributeLink")
  197. # AttributeLink_name
  198. add_mcl_morphism("AttributeLink_name.name_link", "AttributeLink_name")
  199. add_mcl_morphism("AttributeLink_optional.name_link", "AttributeLink_name")
  200. add_mcl_morphism("Element_constraint.name_link", "AttributeLink_name")
  201. add_mcl_morphism("Class_abstract.name_link", "AttributeLink_name")
  202. add_mcl_morphism("Class_lower_cardinality.name_link", "AttributeLink_name")
  203. add_mcl_morphism("Class_upper_cardinality.name_link", "AttributeLink_name")
  204. add_mcl_morphism("Association_source_lower_cardinality.name_link", "AttributeLink_name")
  205. add_mcl_morphism("Association_source_upper_cardinality.name_link", "AttributeLink_name")
  206. add_mcl_morphism("Association_target_lower_cardinality.name_link", "AttributeLink_name")
  207. add_mcl_morphism("Association_target_upper_cardinality.name_link", "AttributeLink_name")
  208. # AttributeLink_optional
  209. add_mcl_morphism("AttributeLink_name.optional_link", "AttributeLink_optional")
  210. add_mcl_morphism("AttributeLink_optional.optional_link", "AttributeLink_optional")
  211. add_mcl_morphism("Element_constraint.optional_link", "AttributeLink_optional")
  212. add_mcl_morphism("Class_abstract.optional_link", "AttributeLink_optional")
  213. add_mcl_morphism("Class_lower_cardinality.optional_link", "AttributeLink_optional")
  214. add_mcl_morphism("Class_upper_cardinality.optional_link", "AttributeLink_optional")
  215. add_mcl_morphism("Association_source_lower_cardinality.optional_link", "AttributeLink_optional")
  216. add_mcl_morphism("Association_source_upper_cardinality.optional_link", "AttributeLink_optional")
  217. add_mcl_morphism("Association_target_lower_cardinality.optional_link", "AttributeLink_optional")
  218. add_mcl_morphism("Association_target_upper_cardinality.optional_link", "AttributeLink_optional")
  219. # String
  220. add_mcl_morphism("AttributeLink_name.name", "String")
  221. add_mcl_morphism("AttributeLink_optional.name", "String")
  222. add_mcl_morphism("Element_constraint.name", "String")
  223. add_mcl_morphism("Class_abstract.name", "String")
  224. add_mcl_morphism("Class_lower_cardinality.name", "String")
  225. add_mcl_morphism("Class_upper_cardinality.name", "String")
  226. add_mcl_morphism("Association_source_lower_cardinality.name", "String")
  227. add_mcl_morphism("Association_source_upper_cardinality.name", "String")
  228. add_mcl_morphism("Association_target_lower_cardinality.name", "String")
  229. add_mcl_morphism("Association_target_upper_cardinality.name", "String")
  230. # Boolean
  231. add_mcl_morphism("AttributeLink_name.optional", "Boolean")
  232. add_mcl_morphism("AttributeLink_optional.optional", "Boolean")
  233. add_mcl_morphism("Element_constraint.optional", "Boolean")
  234. add_mcl_morphism("Class_abstract.optional", "Boolean")
  235. add_mcl_morphism("Class_lower_cardinality.optional", "Boolean")
  236. add_mcl_morphism("Class_upper_cardinality.optional", "Boolean")
  237. add_mcl_morphism("Association_source_lower_cardinality.optional", "Boolean")
  238. add_mcl_morphism("Association_source_upper_cardinality.optional", "Boolean")
  239. add_mcl_morphism("Association_target_lower_cardinality.optional", "Boolean")
  240. add_mcl_morphism("Association_target_upper_cardinality.optional", "Boolean")
  241. add_mcl_morphism("Element.abstract", "Boolean")
  242. # Class_abstract
  243. add_mcl_morphism("Element.abstract_link", "Class_abstract")
  244. return mcl_root
  245. if __name__ == '__main__':
  246. from state.devstate import DevState as State
  247. s = State()
  248. bootstrap_scd(s)
  249. r = s.read_root()
  250. for n in s.read_dict_keys(r):
  251. print(s.read_value(n))