Refactored some stuff
This commit is contained in:
parent
9b3a4c5a44
commit
f9aa6a8adf
3 changed files with 37 additions and 54 deletions
|
|
@ -5,16 +5,14 @@ from services.bottom.V0 import Bottom
|
|||
def create_model_root(bottom: Bottom, model_name: str) -> UUID:
|
||||
model_root = bottom.create_node()
|
||||
mcl_root_id = bottom.create_node(value=str(model_root))
|
||||
bottom.create_edge(bottom.model, mcl_root_id, label=model_name)
|
||||
bottom.create_edge(bottom.state.read_root(), mcl_root_id, label=model_name)
|
||||
return model_root
|
||||
|
||||
|
||||
def bootstrap_scd(state: State) -> UUID:
|
||||
# init model roots and store their UUIDs attached to state root
|
||||
state_root = state.read_root()
|
||||
bottom = Bottom(state_root, state)
|
||||
bottom = Bottom(state)
|
||||
mcl_root = create_model_root(bottom, "SCD")
|
||||
mcl_morphism_root = create_model_root(bottom, "phi(SCD,SCD)")
|
||||
|
||||
# Create model roots for primitive types
|
||||
integer_type_root = create_model_root(bottom, "Integer")
|
||||
|
|
@ -24,18 +22,17 @@ def bootstrap_scd(state: State) -> UUID:
|
|||
type_type_root = create_model_root(bottom, "Type")
|
||||
|
||||
# create MCL, without morphism links
|
||||
bottom = Bottom(mcl_root, state)
|
||||
|
||||
def add_node_element(element_name, node_value=None):
|
||||
""" Helper function, adds node to model with given name and value """
|
||||
_node = bottom.create_node(value=node_value)
|
||||
bottom.create_edge(bottom.model, _node, element_name)
|
||||
bottom.create_edge(mcl_root, _node, element_name)
|
||||
return _node
|
||||
|
||||
def add_edge_element(element_name, source, target):
|
||||
""" Helper function, adds edge to model with given name """
|
||||
_edge = bottom.create_edge(source, target)
|
||||
bottom.create_edge(bottom.model, _edge, element_name)
|
||||
bottom.create_edge(mcl_root, _edge, element_name)
|
||||
return _edge
|
||||
|
||||
def add_attribute_attributes(attribute_element_name, attribute_element, _name, _optional):
|
||||
|
|
@ -65,15 +62,15 @@ def bootstrap_scd(state: State) -> UUID:
|
|||
attr_link_edge = add_edge_element("AttributeLink", element_node, attr_node)
|
||||
# # INHERITANCES, i.e. elements typed by Inheritance
|
||||
# # Class inherits from Element
|
||||
class_inh_element_edge = add_edge_element("class_inh_element", class_node, element_node)
|
||||
add_edge_element("class_inh_element", class_node, element_node)
|
||||
# # Attribute inherits from Element
|
||||
attr_inh_element_edge = add_edge_element("attr_inh_element", attr_node, element_node)
|
||||
add_edge_element("attr_inh_element", attr_node, element_node)
|
||||
# # Association inherits from Element
|
||||
assoc_inh_element_edge = add_edge_element("assoc_inh_element", assoc_edge, element_node)
|
||||
add_edge_element("assoc_inh_element", assoc_edge, element_node)
|
||||
# # AttributeLink inherits from Element
|
||||
attr_link_inh_element_edge = add_edge_element("attr_link_inh_element", attr_link_edge, element_node)
|
||||
add_edge_element("attr_link_inh_element", attr_link_edge, element_node)
|
||||
# # ModelRef inherits from Attribute
|
||||
model_ref_inh_attr_edge = add_edge_element("model_ref_inh_attr", model_ref_node, attr_node)
|
||||
add_edge_element("model_ref_inh_attr", model_ref_node, attr_node)
|
||||
# # ATTRIBUTES, i.e. elements typed by Attribute
|
||||
# # Action Code # TODO: Update to ModelRef when action code is explicitly modelled
|
||||
action_code_node = add_node_element("ActionCode")
|
||||
|
|
@ -103,38 +100,27 @@ def bootstrap_scd(state: State) -> UUID:
|
|||
assoc_t_u_c_edge = add_edge_element("Association_target_upper_cardinality", assoc_edge, integer_node)
|
||||
# # ATTRIBUTE ATTRIBUTES, assign 'name' and 'optional' attributes to all AttributeLinks
|
||||
# # AttributeLink_name
|
||||
attr_name_name_node, attr_name_name_edge, attr_name_optional_node, attr_name_optional_edge = \
|
||||
add_attribute_attributes("AttributeLink_name", attr_name_edge, "name", False)
|
||||
# # AttributeLink_opt
|
||||
attr_opt_name_node, attr_opt_name_edge, attr_opt_optional_node, attr_opt_optional_edge = \
|
||||
add_attribute_attributes("AttributeLink_optional", attr_opt_edge, "optional", False)
|
||||
# # Element_constraint
|
||||
elem_constr_name_node, elem_constr_name_edge, elem_constr_optional_node, elem_constr_optional_edge = \
|
||||
add_attribute_attributes("Element_constraint", elem_constr_edge, "constraint", True)
|
||||
# # Class_abstract
|
||||
class_abs_name_node, class_abs_name_edge, class_abs_optional_node, class_abs_optional_edge = \
|
||||
add_attribute_attributes("Class_abstract", class_abs_edge, "abstract", True)
|
||||
# # Class_lower_cardinality
|
||||
class_l_c_name_node, class_l_c_name_edge, class_l_c_optional_node, class_l_c_optional_edge = \
|
||||
add_attribute_attributes("Class_lower_cardinality", class_l_c_edge, "lower_cardinality", True)
|
||||
# # Class_upper_cardinality
|
||||
class_u_c_name_node, class_u_c_name_edge, class_u_c_optional_node, class_u_c_optional_edge = \
|
||||
add_attribute_attributes("Class_upper_cardinality", class_u_c_edge, "upper_cardinality", True)
|
||||
# # Association_source_lower_cardinality
|
||||
assoc_s_l_c_name_node, assoc_s_l_c_name_edge, assoc_s_l_c_optional_node, assoc_s_l_c_optional_edge = \
|
||||
add_attribute_attributes("Association_source_lower_cardinality", assoc_s_l_c_edge, "source_lower_cardinality", True)
|
||||
# # Association_source_upper_cardinality
|
||||
assoc_s_u_c_name_node, assoc_s_u_c_name_edge, assoc_s_u_c_optional_node, assoc_s_u_c_optional_edge = \
|
||||
add_attribute_attributes("Association_source_upper_cardinality", assoc_s_u_c_edge, "source_upper_cardinality", True)
|
||||
# # Association_target_lower_cardinality
|
||||
assoc_t_l_c_name_node, assoc_t_l_c_name_edge, assoc_t_l_c_optional_node, assoc_t_l_c_optional_edge = \
|
||||
add_attribute_attributes("Association_target_lower_cardinality", assoc_t_l_c_edge, "target_lower_cardinality", True)
|
||||
# # Association_target_upper_cardinality
|
||||
assoc_t_u_c_name_node, assoc_t_u_c_name_edge, assoc_t_u_c_optional_node, assoc_t_u_c_optional_edge = \
|
||||
add_attribute_attributes("Association_target_upper_cardinality", assoc_t_u_c_edge, "target_upper_cardinality", True)
|
||||
|
||||
# create phi(SCD,SCD) to type MCL with itself
|
||||
bottom.model = mcl_morphism_root
|
||||
|
||||
def add_mcl_morphism(element_name, type_name):
|
||||
# get elements from mcl by name
|
||||
|
|
@ -142,14 +128,8 @@ def bootstrap_scd(state: State) -> UUID:
|
|||
_element_node = bottom.read_edge_target(_element_edge)
|
||||
_type_edge, = bottom.read_outgoing_edges(mcl_root, type_name)
|
||||
_type_node = bottom.read_edge_target(_type_edge)
|
||||
# add elements to morphism model
|
||||
if element_name not in bottom.read_keys(bottom.model):
|
||||
bottom.create_edge(bottom.model, _element_node, element_name)
|
||||
if type_name not in bottom.read_keys(bottom.model):
|
||||
bottom.create_edge(bottom.model, _type_node, type_name)
|
||||
# create morphism link
|
||||
morphism_edge = bottom.create_edge(_element_node, _type_node)
|
||||
bottom.create_edge(bottom.model, morphism_edge, f"{element_name}_is_a_{type_name}")
|
||||
bottom.create_edge(_element_node, _type_node, "Morphism")
|
||||
|
||||
# Class
|
||||
add_mcl_morphism("Element", "Class")
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
from abc import ABC, abstractmethod
|
||||
from uuid import UUID
|
||||
|
||||
|
||||
class Service(ABC):
|
||||
def __init__(self, model: UUID):
|
||||
self.model = model
|
||||
|
|
@ -1,11 +1,10 @@
|
|||
from services.base import Service, UUID
|
||||
from uuid import UUID
|
||||
from state.base import State
|
||||
from typing import Any, List
|
||||
|
||||
|
||||
class Bottom(Service):
|
||||
def __init__(self, model: UUID, state: State):
|
||||
super().__init__(model)
|
||||
class Bottom:
|
||||
def __init__(self, state: State):
|
||||
self.state = state
|
||||
|
||||
def create_node(self, value=None) -> UUID:
|
||||
|
|
@ -20,9 +19,6 @@ class Bottom(Service):
|
|||
else:
|
||||
return self.state.create_dict(source, label, target)
|
||||
|
||||
def read_model_root(self) -> UUID:
|
||||
return self.model
|
||||
|
||||
def read_value(self, node: UUID) -> Any:
|
||||
return self.state.read_value(node)
|
||||
|
||||
|
|
@ -68,6 +64,20 @@ class Bottom(Service):
|
|||
edges = [e for e in edges if read_label(e) == label]
|
||||
return edges
|
||||
|
||||
def read_incoming_nodes(self, target: UUID, label=None) -> List[UUID]:
|
||||
edges = self.read_incoming_edges(target, label)
|
||||
if edges is None or len(edges) == 0:
|
||||
return []
|
||||
else:
|
||||
return [self.read_edge_source(e) for e in edges]
|
||||
|
||||
def read_outgoing_nodes(self, source: UUID, label=None) -> List[UUID]:
|
||||
edges = self.read_outgoing_edges(source, label)
|
||||
if edges is None or len(edges) == 0:
|
||||
return []
|
||||
else:
|
||||
return [self.read_edge_target(e) for e in edges]
|
||||
|
||||
def read_keys(self, element: UUID) -> List[str]:
|
||||
key_nodes = self.state.read_dict_keys(element)
|
||||
unique_keys = {self.state.read_value(node) for node in key_nodes}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue