ramify.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from state.base import State
  2. from uuid import UUID
  3. from services.bottom.V0 import Bottom
  4. from services import scd, od
  5. from framework.conformance import Conformance
  6. RAMIFIES_LABEL = "RAMifies"
  7. def ramify(state: State, model: UUID, prefix = "RAM_") -> UUID:
  8. bottom = Bottom(state)
  9. scd_metamodel_id = state.read_dict(state.read_root(), "SCD")
  10. scd_metamodel = UUID(state.read_value(scd_metamodel_id))
  11. string_type_id = state.read_dict(state.read_root(), "String")
  12. string_type = UUID(state.read_value(string_type_id))
  13. m_scd = scd.SCD(model, state)
  14. ramified = state.create_node()
  15. ramified_scd = scd.SCD(ramified, state)
  16. string_modelref = ramified_scd.create_model_ref("String", string_type)
  17. classes = m_scd.get_classes()
  18. for class_name, class_node in classes.items():
  19. # For every class in our original model, create a class:
  20. # - abstract: False
  21. # - min-card: 0
  22. # - max-card: same as original
  23. upper_card = od.find_cardinality(bottom, class_node, od.get_scd_mm_class_uppercard_node(bottom))
  24. # print('creating class', class_name, "with card 0 ..", upper_card)
  25. ramified_class = ramified_scd.create_class(prefix+class_name, abstract=None, max_c=upper_card)
  26. # traceability link
  27. bottom.create_edge(ramified_class, class_node, RAMIFIES_LABEL)
  28. # We don't add a 'label' attribute (as described in literature on RAMification)
  29. # Instead, the names of the objects (which only exist in the scope of the object diagram 'model', and are not visible to the matcher) are used as labels
  30. # Optional constraint on the object
  31. # ramified_scd._create_attribute_link(prefix+class_name, string_modelref, "constraint", optional=True)
  32. for (attr_name, attr_edge) in od.get_attributes(bottom, class_node):
  33. # print(' creating attribute', attr_name, "with type String")
  34. # Every attribute becomes 'string' type
  35. # The string will be a Python expression
  36. ramified_attr_link = ramified_scd._create_attribute_link(prefix+class_name, string_modelref, prefix+attr_name, optional=True)
  37. # traceability link
  38. bottom.create_edge(ramified_attr_link, attr_edge, RAMIFIES_LABEL)
  39. associations = m_scd.get_associations()
  40. for assoc_name, assoc_node in associations.items():
  41. # For every association in our original model, create an association:
  42. # - src-min-card: 0
  43. # - src-max-card: same as original
  44. # - tgt-min-card: 0
  45. # - tgt-max-card: same as original
  46. _, src_upper_card, _, tgt_upper_card = m_scd.get_assoc_cardinalities(assoc_node)
  47. src = m_scd.get_class_name(bottom.read_edge_source(assoc_node))
  48. tgt = m_scd.get_class_name(bottom.read_edge_target(assoc_node))
  49. # print('creating assoc', src, "->", tgt, ", name =", assoc_name, ", src card = 0 ..", src_upper_card, "and tgt card = 0 ..", tgt_upper_card)
  50. ramified_assoc = ramified_scd.create_association(
  51. prefix+assoc_name, prefix+src, prefix+tgt,
  52. src_max_c=src_upper_card,
  53. tgt_max_c=tgt_upper_card)
  54. # traceability link
  55. bottom.create_edge(ramified_assoc, assoc_node, RAMIFIES_LABEL)
  56. for inh_name, inh_node in m_scd.get_inheritances().items():
  57. # Re-create inheritance links like in our original model:
  58. src = m_scd.get_class_name(bottom.read_edge_source(inh_node))
  59. tgt = m_scd.get_class_name(bottom.read_edge_target(inh_node))
  60. # print('creating inheritance link', prefix+src, '->', prefix+tgt)
  61. ramified_inh_link = ramified_scd.create_inheritance(prefix+src, prefix+tgt)
  62. # Double-check: The RAMified meta-model should also conform to 'SCD':
  63. conf = Conformance(state, ramified, scd_metamodel)
  64. if not conf.check_nominal(log=True):
  65. raise Exception("Unexpected error: RAMified MM does not conform to SCD MM")
  66. return ramified
  67. # Every RAMified type has a link to its original type
  68. def get_original_type(bottom, typ: UUID):
  69. original_types = bottom.read_outgoing_elements(typ, RAMIFIES_LABEL)
  70. if len(original_types) > 1:
  71. raise Exception("Expected at most 1 original type, got " + str(len(original_types)))
  72. elif len(original_types) == 1:
  73. return original_types[0]