Add ActionCode primitive type. Fix constraint checking.

This commit is contained in:
Joeri Exelmans 2024-10-07 16:08:23 +02:00
parent 0785b9218e
commit 59de61d0a3
11 changed files with 256 additions and 82 deletions

View file

@ -1,9 +1,10 @@
from state.base import State, UUID
from services.bottom.V0 import Bottom
from services.primitives.integer_type import Integer
from services.primitives.actioncode_type import ActionCode
def bootstrap_type(type_name: str, python_type: str, scd_root: UUID, model_root: UUID, state: State):
def bootstrap_type(type_name: str, scd_root: UUID, model_root: UUID, integer_type: UUID, state: State):
bottom = Bottom(state)
# create class
class_node = bottom.create_node() # create class node
@ -17,7 +18,7 @@ def bootstrap_type(type_name: str, python_type: str, scd_root: UUID, model_root:
bottom.create_edge(model_root, min_c_node, f"{type_name}.lower_cardinality")
min_c_link = bottom.create_edge(class_node, min_c_node)
bottom.create_edge(model_root, min_c_link, f"{type_name}_lower_cardinality")
scd_node, = bottom.read_outgoing_elements(scd_root, "Integer")
scd_node = integer_type
scd_link, = bottom.read_outgoing_elements(scd_root, "Class_lower_cardinality")
bottom.create_edge(min_c_node, scd_node, "Morphism")
bottom.create_edge(min_c_link, scd_link, "Morphism")
@ -28,36 +29,61 @@ def bootstrap_type(type_name: str, python_type: str, scd_root: UUID, model_root:
bottom.create_edge(model_root, max_c_node, f"{type_name}.upper_cardinality")
max_c_link = bottom.create_edge(class_node, max_c_node)
bottom.create_edge(model_root, max_c_link, f"{type_name}_upper_cardinality")
scd_node, = bottom.read_outgoing_elements(scd_root, "Integer")
scd_node = integer_type
scd_link, = bottom.read_outgoing_elements(scd_root, "Class_upper_cardinality")
bottom.create_edge(max_c_node, scd_node, "Morphism")
bottom.create_edge(max_c_link, scd_link, "Morphism")
return class_node
def bootstrap_constraint(class_node, type_name: str, python_type: str, scd_root: UUID, model_root: UUID, actioncode_type: UUID, state: State):
# set constraint
constraint_node = bottom.create_node(f"isinstance(read_value(element),{python_type})")
# chicken-and-egg problem: we cannot create an action-code constraint because the action-code MM doesn't exist yet
bottom = Bottom(state)
constraint_model = bottom.create_node()
ActionCode(constraint_model, state).create(f"isinstance(read_value(element),{python_type})")
constraint_node = bottom.create_node(str(constraint_model))
bottom.create_edge(model_root, constraint_node, f"{type_name}.constraint")
constraint_link = bottom.create_edge(class_node, constraint_node)
bottom.create_edge(model_root, constraint_link, f"{type_name}_constraint")
scd_node, = bottom.read_outgoing_elements(scd_root, "ActionCode")
scd_node = actioncode_type
scd_link, = bottom.read_outgoing_elements(scd_root, "Element_constraint")
bottom.create_edge(constraint_node, scd_node, "Morphism")
bottom.create_edge(constraint_link, scd_link, "Morphism")
def bootstrap_type_type(scd_root: UUID, model_root: UUID, state: State):
bootstrap_type("Type", "tuple", scd_root, model_root, state)
# def bootstrap_type_type(scd_root: UUID, model_root: UUID, integer_type: UUID, actioncode_type: UUID, state: State):
def bootstrap_boolean_type(scd_root: UUID, model_root: UUID, state: State):
bootstrap_type("Boolean", "bool", scd_root, model_root, state)
# def bootstrap_boolean_type(scd_root: UUID, model_root: UUID, integer_type: UUID, actioncode_type: UUID, state: State):
def bootstrap_integer_type(scd_root: UUID, model_root: UUID, state: State):
bootstrap_type("Integer", "int", scd_root, model_root, state)
# def bootstrap_integer_type(scd_root: UUID, model_root: UUID, integer_type: UUID, actioncode_type: UUID, state: State):
def bootstrap_float_type(scd_root: UUID, model_root: UUID, state: State):
bootstrap_type("Float", "float", scd_root, model_root, state)
# def bootstrap_float_type(scd_root: UUID, model_root: UUID, integer_type: UUID, actioncode_type: UUID, state: State):
def bootstrap_string_type(scd_root: UUID, model_root: UUID, state: State):
bootstrap_type("String", "str", scd_root, model_root, state)
# def bootstrap_string_type(scd_root: UUID, model_root: UUID, integer_type: UUID, actioncode_type: UUID, state: State):
# def bootstrap_actioncode_type(scd_root: UUID, model_root: UUID, integer_type: UUID, actioncode_type: UUID, state: State):
# # we store action code as Python string:
def bootstrap_primitive_types(scd_root, state, integer_type, boolean_type, float_type, string_type, type_type, actioncode_type):
# Order is important: Integer must come first
class_integer = bootstrap_type("Integer", scd_root, integer_type, integer_type, state)
class_type = bootstrap_type("Type", scd_root, type_type, integer_type, state)
class_boolean = bootstrap_type("Boolean", scd_root, boolean_type, integer_type, state)
class_float = bootstrap_type("Float", scd_root, float_type, integer_type, state)
class_string = bootstrap_type("String", scd_root, string_type, integer_type, state)
class_actioncode = bootstrap_type("ActionCode", scd_root, actioncode_type, integer_type, state)
# Can only create constraints after ActionCode type has been created:
bootstrap_constraint(class_integer, "Integer", "int", scd_root, integer_type, actioncode_type, state)
bootstrap_constraint(class_type, "Type", "tuple", scd_root, type_type, actioncode_type, state)
bootstrap_constraint(class_boolean, "Boolean", "bool", scd_root, boolean_type, actioncode_type, state)
bootstrap_constraint(class_float, "Float", "float", scd_root, float_type, actioncode_type, state)
bootstrap_constraint(class_string, "String", "str", scd_root, string_type, actioncode_type, state)
bootstrap_constraint(class_actioncode, "ActionCode", "str", scd_root, actioncode_type, actioncode_type, state)

View file

@ -3,11 +3,13 @@ from services.bottom.V0 import Bottom
from services.primitives.boolean_type import Boolean
from services.primitives.string_type import String
from bootstrap.primitive import (
bootstrap_boolean_type,
bootstrap_float_type,
bootstrap_integer_type,
bootstrap_string_type,
bootstrap_type_type
bootstrap_primitive_types
# bootstrap_boolean_type,
# bootstrap_float_type,
# bootstrap_integer_type,
# bootstrap_string_type,
# bootstrap_type_type,
# bootstrap_actioncode_type
)
@ -29,6 +31,7 @@ def bootstrap_scd(state: State) -> UUID:
string_type_root = create_model_root(bottom, "String")
float_type_root = create_model_root(bottom, "Float")
type_type_root = create_model_root(bottom, "Type")
actioncode_type_root = create_model_root(bottom, "ActionCode")
# create MCL, without morphism links
@ -91,7 +94,7 @@ def bootstrap_scd(state: State) -> UUID:
# # 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")
# action_code_node = add_node_element("ActionCode")
# # MODELREFS, i.e. elements typed by ModelRef
# # Integer
@ -100,6 +103,8 @@ def bootstrap_scd(state: State) -> UUID:
string_node = add_node_element("String", str(string_type_root))
# # Boolean
boolean_node = add_node_element("Boolean", str(boolean_type_root))
# # ActionCode
actioncode_node = add_node_element("ActionCode", str(actioncode_type_root))
# # ATTRIBUTE LINKS, i.e. elements typed by AttributeLink
# # name attribute of AttributeLink
@ -107,7 +112,7 @@ def bootstrap_scd(state: State) -> UUID:
# # optional attribute of AttributeLink
attr_opt_edge = add_edge_element("AttributeLink_optional", attr_link_edge, boolean_node)
# # constraint attribute of Element
elem_constr_edge = add_edge_element("Element_constraint", element_node, action_code_node)
elem_constr_edge = add_edge_element("Element_constraint", element_node, actioncode_node)
# # abstract attribute of Class
class_abs_edge = add_edge_element("Class_abstract", class_node, boolean_node)
# # multiplicity attributes of Class
@ -120,12 +125,19 @@ def bootstrap_scd(state: State) -> UUID:
assoc_t_u_c_edge = add_edge_element("Association_target_upper_cardinality", assoc_edge, integer_node)
# # bootstrap primitive types
# # order is important, integer must be first
bootstrap_integer_type(mcl_root, integer_type_root, state)
bootstrap_boolean_type(mcl_root, boolean_type_root, state)
bootstrap_float_type(mcl_root, float_type_root, state)
bootstrap_string_type(mcl_root, string_type_root, state)
bootstrap_type_type(mcl_root, type_type_root, state)
bootstrap_primitive_types(mcl_root, state,
integer_type_root,
boolean_type_root,
float_type_root,
string_type_root,
type_type_root,
actioncode_type_root)
# bootstrap_integer_type(mcl_root, integer_type_root, integer_type_root, actioncode_type_root, state)
# bootstrap_boolean_type(mcl_root, boolean_type_root, integer_type_root, actioncode_type_root, state)
# bootstrap_float_type(mcl_root, float_type_root, integer_type_root, actioncode_type_root, state)
# bootstrap_string_type(mcl_root, string_type_root, integer_type_root, actioncode_type_root, state)
# bootstrap_type_type(mcl_root, type_type_root, integer_type_root, actioncode_type_root, state)
# bootstrap_actioncode_type(mcl_root, actioncode_type_root, integer_type_root, actioncode_type_root, state)
# # ATTRIBUTE ATTRIBUTES, assign 'name' and 'optional' attributes to all AttributeLinks
# # AttributeLink_name
@ -203,11 +215,12 @@ def bootstrap_scd(state: State) -> UUID:
add_mcl_morphism("attr_link_inh_element", "Inheritance")
add_mcl_morphism("model_ref_inh_attr", "Inheritance")
# Attribute
add_mcl_morphism("ActionCode", "Attribute")
# add_mcl_morphism("ActionCode", "Attribute")
# ModelRef
add_mcl_morphism("Integer", "ModelRef")
add_mcl_morphism("String", "ModelRef")
add_mcl_morphism("Boolean", "ModelRef")
add_mcl_morphism("ActionCode", "ModelRef")
# AttributeLink
add_mcl_morphism("AttributeLink_name", "AttributeLink")
add_mcl_morphism("AttributeLink_optional", "AttributeLink")