primitive.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from state.base import State, UUID
  2. from services.bottom.V0 import Bottom
  3. from services.primitives.integer_type import Integer
  4. from services.primitives.actioncode_type import ActionCode
  5. def bootstrap_type(type_name: str, scd_root: UUID, model_root: UUID, integer_type: UUID, state: State):
  6. bottom = Bottom(state)
  7. # create class
  8. class_node = bottom.create_node() # create class node
  9. bottom.create_edge(model_root, class_node, type_name) # attach to model
  10. scd_node, = bottom.read_outgoing_elements(scd_root, "Class") # retrieve type
  11. bottom.create_edge(class_node, scd_node, "Morphism") # create morphism link
  12. # set min_cardinality
  13. min_c_model = bottom.create_node()
  14. Integer(min_c_model, state).create(1)
  15. min_c_node = bottom.create_node(str(min_c_model))
  16. bottom.create_edge(model_root, min_c_node, f"{type_name}.lower_cardinality")
  17. min_c_link = bottom.create_edge(class_node, min_c_node)
  18. bottom.create_edge(model_root, min_c_link, f"{type_name}_lower_cardinality")
  19. scd_node = integer_type
  20. scd_link, = bottom.read_outgoing_elements(scd_root, "Class_lower_cardinality")
  21. bottom.create_edge(min_c_node, scd_node, "Morphism")
  22. bottom.create_edge(min_c_link, scd_link, "Morphism")
  23. # set max_cardinality
  24. max_c_model = bottom.create_node()
  25. Integer(max_c_model, state).create(1)
  26. max_c_node = bottom.create_node(str(max_c_model))
  27. bottom.create_edge(model_root, max_c_node, f"{type_name}.upper_cardinality")
  28. max_c_link = bottom.create_edge(class_node, max_c_node)
  29. bottom.create_edge(model_root, max_c_link, f"{type_name}_upper_cardinality")
  30. scd_node = integer_type
  31. scd_link, = bottom.read_outgoing_elements(scd_root, "Class_upper_cardinality")
  32. bottom.create_edge(max_c_node, scd_node, "Morphism")
  33. bottom.create_edge(max_c_link, scd_link, "Morphism")
  34. return class_node
  35. def bootstrap_constraint(class_node, type_name: str, python_type: str, scd_root: UUID, model_root: UUID, actioncode_type: UUID, state: State):
  36. bottom = Bottom(state)
  37. constraint_model = bottom.create_node()
  38. ActionCode(constraint_model, state).create(f"isinstance(read_value(this),{python_type})")
  39. constraint_node = bottom.create_node(str(constraint_model))
  40. bottom.create_edge(model_root, constraint_node, f"{type_name}.constraint")
  41. constraint_link = bottom.create_edge(class_node, constraint_node)
  42. bottom.create_edge(model_root, constraint_link, f"{type_name}_constraint")
  43. scd_node = actioncode_type
  44. scd_link, = bottom.read_outgoing_elements(scd_root, "Element_constraint")
  45. bottom.create_edge(constraint_node, scd_node, "Morphism")
  46. bottom.create_edge(constraint_link, scd_link, "Morphism")
  47. def bootstrap_primitive_types(scd_root, state, integer_type, boolean_type, float_type, string_type, type_type, actioncode_type):
  48. # Order is important: Integer must come first
  49. class_integer = bootstrap_type("Integer", scd_root, integer_type, integer_type, state)
  50. class_type = bootstrap_type("Type", scd_root, type_type, integer_type, state)
  51. class_boolean = bootstrap_type("Boolean", scd_root, boolean_type, integer_type, state)
  52. class_float = bootstrap_type("Float", scd_root, float_type, integer_type, state)
  53. class_string = bootstrap_type("String", scd_root, string_type, integer_type, state)
  54. class_actioncode = bootstrap_type("ActionCode", scd_root, actioncode_type, integer_type, state)
  55. # Can only create constraints after ActionCode type has been created:
  56. bootstrap_constraint(class_integer, "Integer", "int", scd_root, integer_type, actioncode_type, state)
  57. bootstrap_constraint(class_type, "Type", "tuple", scd_root, type_type, actioncode_type, state)
  58. bootstrap_constraint(class_boolean, "Boolean", "bool", scd_root, boolean_type, actioncode_type, state)
  59. bootstrap_constraint(class_float, "Float", "float", scd_root, float_type, actioncode_type, state)
  60. bootstrap_constraint(class_string, "String", "str", scd_root, string_type, actioncode_type, state)
  61. bootstrap_constraint(class_actioncode, "ActionCode", "str", scd_root, actioncode_type, actioncode_type, state)