Browse Source

Primitives are now bootstrapped as well

Andrei Bondarenko 4 years ago
parent
commit
6dcbdbe775
2 changed files with 71 additions and 20 deletions
  1. 57 20
      bootstrap/primitive.py
  2. 14 0
      bootstrap/scd.py

+ 57 - 20
bootstrap/primitive.py

@@ -1,21 +1,58 @@
 from state.base import State, UUID
-
-
-def bootstrap_type_type(model_root: UUID, state: State) -> UUID:
-    pass
-
-
-def bootstrap_boolean_type(model_root: UUID, state: State) -> UUID:
-    pass
-
-
-def bootstrap_integer_type(model_root: UUID, state: State) -> UUID:
-    pass
-
-
-def bootstrap_float_type(model_root: UUID, state: State) -> UUID:
-    pass
-
-
-def bootstrap_string_type(model_root: UUID, state: State) -> UUID:
-    pass
+from services.bottom.V0 import Bottom
+
+
+def bootstrap_type(type_name: str, python_type: str, scd_root: UUID, model_root: UUID, state: State):
+    bottom = Bottom(state)
+    # create class
+    class_node = bottom.create_node()  # create class node
+    bottom.create_edge(model_root, class_node, type_name)  # attach to model
+    scd_node, = bottom.read_outgoing_nodes(scd_root, "Class")  # retrieve type
+    bottom.create_edge(class_node, scd_node, "Morphism")  # create morphism link
+    # set min_cardinality
+    min_c_node = bottom.create_node(1)
+    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_link")
+    scd_node, = bottom.read_outgoing_nodes(scd_root, "Integer")
+    scd_link, = bottom.read_outgoing_nodes(scd_root, "Class_lower_cardinality")
+    bottom.create_edge(min_c_node, scd_node, "Morphism")
+    bottom.create_edge(min_c_link, scd_link, "Morphism")
+    # set max_cardinality
+    max_c_node = bottom.create_node(1)
+    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_link")
+    scd_node, = bottom.read_outgoing_nodes(scd_root, "Integer")
+    scd_link, = bottom.read_outgoing_nodes(scd_root, "Class_upper_cardinality")
+    bottom.create_edge(max_c_node, scd_node, "Morphism")
+    bottom.create_edge(max_c_link, scd_link, "Morphism")
+    # set constraint
+    constraint_node = bottom.create_node(f"isinstance(read_value(x),{python_type})")
+    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_link")
+    scd_node, = bottom.read_outgoing_nodes(scd_root, "ActionCode")
+    scd_link, = bottom.read_outgoing_nodes(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_boolean_type(scd_root: UUID, model_root: UUID, state: State):
+    bootstrap_type("Boolean", "bool", scd_root, model_root, state)
+
+
+def bootstrap_integer_type(scd_root: UUID, model_root: UUID, state: State):
+    bootstrap_type("Integer", "int", scd_root, model_root, state)
+
+
+def bootstrap_float_type(scd_root: UUID, model_root: UUID, state: State):
+    bootstrap_type("Float", "float", scd_root, model_root, state)
+
+
+def bootstrap_string_type(scd_root: UUID, model_root: UUID, state: State):
+    bootstrap_type("String", "str", scd_root, model_root, state)

+ 14 - 0
bootstrap/scd.py

@@ -1,5 +1,12 @@
 from state.base import State, UUID
 from services.bottom.V0 import Bottom
+from bootstrap.primitive import (
+    bootstrap_boolean_type,
+    bootstrap_float_type,
+    bootstrap_integer_type,
+    bootstrap_string_type,
+    bootstrap_type_type
+)
 
 
 def create_model_root(bottom: Bottom, model_name: str) -> UUID:
@@ -209,6 +216,13 @@ def bootstrap_scd(state: State) -> UUID:
     add_mcl_morphism("Association_target_lower_cardinality.optional", "Boolean")
     add_mcl_morphism("Association_target_upper_cardinality.optional", "Boolean")
 
+    # bootstrap primitive types
+    bootstrap_boolean_type(mcl_root, boolean_type_root, state)
+    bootstrap_float_type(mcl_root, float_type_root, state)
+    bootstrap_integer_type(mcl_root, integer_type_root, state)
+    bootstrap_string_type(mcl_root, string_type_root, state)
+    bootstrap_type_type(mcl_root, type_type_root, state)
+
     return mcl_root