ramify.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. already_ramified.add(class_name)
  46. glob_cond = ramified_scd.create_class("GlobalCondition", abstract=None)
  47. ramified_scd._create_attribute_link("GlobalCondition", actioncode_modelref, "condition", optional=False)
  48. assocs_to_ramify = m_scd.get_associations()
  49. while len(assocs_to_ramify) > 0:
  50. ramify_later = {}
  51. for assoc_name, assoc_node in assocs_to_ramify.items():
  52. # For every association in our original model, create an association:
  53. # - src-min-card: 0
  54. # - src-max-card: same as original
  55. # - tgt-min-card: 0
  56. # - tgt-max-card: same as original
  57. if assoc_name in already_ramified:
  58. raise Exception("Assertion failed: did not expect this to ever happen!")
  59. continue
  60. _, src_upper_card, _, tgt_upper_card = m_scd.get_assoc_cardinalities(assoc_node)
  61. src = m_scd.get_class_name(bottom.read_edge_source(assoc_node))
  62. tgt = m_scd.get_class_name(bottom.read_edge_target(assoc_node))
  63. if src not in already_ramified or tgt not in already_ramified:
  64. ramify_later[assoc_name] = assoc_node
  65. continue
  66. # print('creating assoc', src, "->", tgt, ", name =", assoc_name, ", src card = 0 ..", src_upper_card, "and tgt card = 0 ..", tgt_upper_card)
  67. ramified_assoc = ramified_scd.create_association(name=prefix+assoc_name,
  68. source=prefix+src, target=prefix+tgt,
  69. src_max_c=src_upper_card,
  70. tgt_max_c=tgt_upper_card)
  71. # create traceability link
  72. bottom.create_edge(ramified_assoc, assoc_node, RAMIFIES_LABEL)
  73. # Additional constraint that can be specified
  74. ramified_scd._create_attribute_link(prefix+assoc_name, actioncode_modelref, "condition", optional=True)
  75. already_ramified.add(assoc_name)
  76. # Associations can also have attributes...
  77. for (attr_name, attr_edge) in od.get_attributes(bottom, assoc_node):
  78. # print(' creating attribute', attr_name, "with type String")
  79. # Every attribute becomes 'string' type
  80. # The string will be a Python expression
  81. ramified_attr_link = ramified_scd._create_attribute_link(prefix+assoc_name, actioncode_modelref, prefix+attr_name, optional=True)
  82. # create traceability link
  83. bottom.create_edge(ramified_attr_link, attr_edge, RAMIFIES_LABEL)
  84. assocs_to_ramify = ramify_later
  85. for inh_name, inh_node in m_scd.get_inheritances().items():
  86. # Re-create inheritance links like in our original model:
  87. src = m_scd.get_class_name(bottom.read_edge_source(inh_node))
  88. tgt = m_scd.get_class_name(bottom.read_edge_target(inh_node))
  89. # print('creating inheritance link', prefix+src, '->', prefix+tgt)
  90. ramified_inh_link = ramified_scd.create_inheritance(prefix+src, prefix+tgt)
  91. # Double-check: The RAMified meta-model should also conform to 'SCD':
  92. conf = Conformance(state, ramified, scd_metamodel)
  93. if len(conf.check_nominal(log=True)) > 0:
  94. raise Exception("Unexpected error: RAMified MM does not conform to SCD MM")
  95. return ramified
  96. # Every RAMified type has a link to its original type
  97. def get_original_type(bottom, typ: UUID):
  98. original_types = bottom.read_outgoing_elements(typ, RAMIFIES_LABEL)
  99. if len(original_types) > 1:
  100. raise Exception("Expected at most 1 original type, got " + str(len(original_types)))
  101. elif len(original_types) == 1:
  102. return original_types[0]