ramify.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. actioncode_type_id = state.read_dict(state.read_root(), "ActionCode")
  14. actioncode_type = UUID(state.read_value(actioncode_type_id))
  15. m_scd = scd.SCD(model, state)
  16. ramified = state.create_node()
  17. ramified_scd = scd.SCD(ramified, state)
  18. string_modelref = ramified_scd.create_model_ref("String", string_type)
  19. actioncode_modelref = ramified_scd.create_model_ref("ActionCode", actioncode_type)
  20. already_ramified = set() # for correct order of ramification
  21. classes = m_scd.get_classes()
  22. for class_name, class_node in classes.items():
  23. # For every class in our original model, create a class:
  24. # - abstract: False
  25. # - min-card: 0
  26. # - max-card: same as original
  27. upper_card = od.find_cardinality(bottom, class_node, od.get_scd_mm_class_uppercard_node(bottom))
  28. # print('creating class', class_name, "with card 0 ..", upper_card)
  29. ramified_class = ramified_scd.create_class(prefix+class_name, abstract=None, max_c=upper_card)
  30. # traceability link
  31. bottom.create_edge(ramified_class, class_node, RAMIFIES_LABEL)
  32. # We don't add a 'label' attribute (as described in literature on RAMification)
  33. # 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
  34. for (attr_name, attr_edge) in od.get_attributes(bottom, class_node):
  35. # print(' creating attribute', attr_name, "with type String")
  36. # Every attribute becomes 'string' type
  37. # The string will be a Python expression
  38. ramified_attr_link = ramified_scd._create_attribute_link(prefix+class_name, actioncode_modelref, prefix+attr_name, optional=True)
  39. # create traceability link
  40. bottom.create_edge(ramified_attr_link, attr_edge, RAMIFIES_LABEL)
  41. # Additional condition that can be specified
  42. # In LHS, this will be a boolean expression (pre-condition)
  43. # In RHS, this is just a piece of action code
  44. ramified_scd._create_attribute_link(prefix+class_name, actioncode_modelref, "condition", optional=True)
  45. # Optional: specify name of object to create
  46. ramified_scd._create_attribute_link(prefix+class_name, actioncode_modelref, "name", optional=True)
  47. already_ramified.add(class_name)
  48. glob_cond = ramified_scd.create_class("GlobalCondition", abstract=None)
  49. ramified_scd._create_attribute_link("GlobalCondition", actioncode_modelref, "condition", optional=False)
  50. assocs_to_ramify = m_scd.get_associations()
  51. while len(assocs_to_ramify) > 0:
  52. ramify_later = {}
  53. for assoc_name, assoc_node in assocs_to_ramify.items():
  54. # For every association in our original model, create an association:
  55. # - src-min-card: 0
  56. # - src-max-card: same as original
  57. # - tgt-min-card: 0
  58. # - tgt-max-card: same as original
  59. if assoc_name in already_ramified:
  60. raise Exception("Assertion failed: did not expect this to ever happen!")
  61. continue
  62. _, src_upper_card, _, tgt_upper_card = m_scd.get_assoc_cardinalities(assoc_node)
  63. src = m_scd.get_class_name(bottom.read_edge_source(assoc_node))
  64. tgt = m_scd.get_class_name(bottom.read_edge_target(assoc_node))
  65. if src not in already_ramified or tgt not in already_ramified:
  66. ramify_later[assoc_name] = assoc_node
  67. continue
  68. # print('creating assoc', src, "->", tgt, ", name =", assoc_name, ", src card = 0 ..", src_upper_card, "and tgt card = 0 ..", tgt_upper_card)
  69. ramified_assoc = ramified_scd.create_association(name=prefix+assoc_name,
  70. source=prefix+src, target=prefix+tgt,
  71. src_max_c=src_upper_card,
  72. tgt_max_c=tgt_upper_card)
  73. # create traceability link
  74. bottom.create_edge(ramified_assoc, assoc_node, RAMIFIES_LABEL)
  75. # Additional constraint that can be specified
  76. ramified_scd._create_attribute_link(prefix+assoc_name, actioncode_modelref, "condition", optional=True)
  77. # Optional: specify name of link to create
  78. ramified_scd._create_attribute_link(prefix+assoc_name, actioncode_modelref, "name", optional=True)
  79. already_ramified.add(assoc_name)
  80. # Associations can also have attributes...
  81. for (attr_name, attr_edge) in od.get_attributes(bottom, assoc_node):
  82. # print(' creating attribute', attr_name, "with type String")
  83. # Every attribute becomes 'string' type
  84. # The string will be a Python expression
  85. ramified_attr_link = ramified_scd._create_attribute_link(prefix+assoc_name, actioncode_modelref, prefix+attr_name, optional=True)
  86. # create traceability link
  87. bottom.create_edge(ramified_attr_link, attr_edge, RAMIFIES_LABEL)
  88. assocs_to_ramify = ramify_later
  89. for inh_name, inh_node in m_scd.get_inheritances().items():
  90. # Re-create inheritance links like in our original model:
  91. src = m_scd.get_class_name(bottom.read_edge_source(inh_node))
  92. tgt = m_scd.get_class_name(bottom.read_edge_target(inh_node))
  93. # print('creating inheritance link', prefix+src, '->', prefix+tgt)
  94. ramified_inh_link = ramified_scd.create_inheritance(prefix+src, prefix+tgt)
  95. # Double-check: The RAMified meta-model should also conform to 'SCD':
  96. conf = Conformance(state, ramified, scd_metamodel)
  97. if len(conf.check_nominal(log=True)) > 0:
  98. raise Exception("Unexpected error: RAMified MM does not conform to SCD MM")
  99. return ramified
  100. # Every RAMified type has a link to its original type
  101. def get_original_type(bottom, typ: UUID):
  102. original_types = bottom.read_outgoing_elements(typ, RAMIFIES_LABEL)
  103. if len(original_types) > 1:
  104. raise Exception("Expected at most 1 original type, got " + str(len(original_types)))
  105. elif len(original_types) == 1:
  106. return original_types[0]