typing.alc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. include "primitives.alh"
  2. include "utils.alh"
  3. Element function get_type_mapping(model : Element):
  4. // Deserialize dictionary as model
  5. Element tm_model
  6. tm_model = dict_create()
  7. Element m
  8. Element mm
  9. Element edge
  10. Element keys
  11. String key
  12. String m_name
  13. String mm_name
  14. Element m_model
  15. Element mm_model
  16. Element tm
  17. m_model = model["model"]
  18. mm_model = model["metamodel"]["model"]
  19. tm = model["type_mapping"]
  20. keys = dict_keys(model["type_mapping"])
  21. while (set_len(keys) > 0):
  22. key = set_pop(keys)
  23. m = m_model[key]
  24. mm = mm_model[tm[key]]
  25. edge = create_edge(m, mm)
  26. m_name = "instance_" + key
  27. mm_name = "type_" + cast_string(tm[key])
  28. dict_add_fast(tm_model, m_name, m)
  29. if (bool_not(dict_in(tm_model, mm_name))):
  30. dict_add_fast(tm_model, mm_name, mm)
  31. dict_add_fast(tm_model, cast_id(edge), edge)
  32. return tm_model!
  33. Void function set_type_mapping(model : Element, type_mapping_model : Element):
  34. // Serialize model to dictionary
  35. Element type_mapping
  36. Element keys
  37. String key
  38. String source_name
  39. String destination_name
  40. String tmp_source_name
  41. String tmp_destination_name
  42. Element lookups
  43. String value
  44. String src_id
  45. String dst_id
  46. Element mm_model
  47. Element m_model
  48. m_model = model["model"]
  49. mm_model = model["metamodel"]["model"]
  50. type_mapping = dict_create()
  51. keys = dict_keys(type_mapping_model)
  52. Element reverse_mapping
  53. reverse_mapping = dict_create()
  54. while (set_len(keys) > 0):
  55. key = set_pop(keys)
  56. value = cast_id(type_mapping_model[key])
  57. if (bool_not(dict_in(reverse_mapping, value))):
  58. dict_add(reverse_mapping, value, set_create())
  59. set_add(reverse_mapping[value], key)
  60. keys = dict_keys(type_mapping_model)
  61. while (set_len(keys) > 0):
  62. key = set_pop(keys)
  63. if (is_edge(type_mapping_model[key])):
  64. src_id = cast_id(read_edge_src(type_mapping_model[key]))
  65. if (bool_not(dict_in(reverse_mapping, src_id))):
  66. continue!
  67. lookups = set_copy(reverse_mapping[src_id])
  68. source_name = ""
  69. while (set_len(lookups) > 0):
  70. tmp_source_name = set_pop(lookups)
  71. if (string_startswith(tmp_source_name, "instance_")):
  72. source_name = string_replace(tmp_source_name, "instance_", "")
  73. break!
  74. dst_id = cast_id(read_edge_dst(type_mapping_model[key]))
  75. if (bool_not(dict_in(reverse_mapping, dst_id))):
  76. continue!
  77. lookups = set_copy(reverse_mapping[dst_id])
  78. destination_name = ""
  79. while (set_len(lookups) > 0):
  80. tmp_destination_name = set_pop(lookups)
  81. if (string_startswith(tmp_destination_name, "type_")):
  82. destination_name = string_replace(tmp_destination_name, "type_", "")
  83. break!
  84. if (bool_and(bool_and(dict_in(m_model, source_name), dict_in(mm_model, destination_name)), read_nr_out(type_mapping_model[key]) == 0)):
  85. // Element is in neither model or metamodel
  86. // Must be a typing link!
  87. // So add it
  88. dict_add_fast(type_mapping, source_name, destination_name)
  89. dict_overwrite(model, "type_mapping", type_mapping)
  90. return!
  91. Element function elements_typed_by(model : Element, type_name : String):
  92. Element result
  93. Element temp_result
  94. String temp
  95. Element m_model
  96. m_model = model["model"]
  97. result = set_create()
  98. temp_result = reverseKeyLookupMultiValue(get_type_mapping_as_dict(model), type_name)
  99. while (set_len(temp_result) > 0):
  100. temp = set_pop(temp_result)
  101. if (dict_in(m_model, temp)):
  102. set_add(result, temp)
  103. return result!
  104. Element function get_type_mapping_as_dict(model : Element):
  105. return model["type_mapping"]!
  106. String function read_type(model : Element, name : String):
  107. String result
  108. Element tm
  109. if (dict_in(model["model"], name)):
  110. if (dict_in(model["type_mapping"], name)):
  111. result = model["type_mapping"][name]
  112. if (dict_in(model["metamodel"]["model"], result)):
  113. return result!
  114. else:
  115. return ""!
  116. else:
  117. return ""!
  118. else:
  119. return ""!
  120. Void function retype(model : Element, element : String, type : String):
  121. // Retype a model, deleting any previous type the element had
  122. // The type string is evaluated in the metamodel previously specified
  123. dict_overwrite(model["type_mapping"], element, type)
  124. return!
  125. Void function new_type_mapping(model : Element):
  126. dict_overwrite(model, "type_mapping", dict_create())
  127. return !
  128. Void function remove_type(model : Element, name : String):
  129. if (dict_in(model["type_mapping"], name)):
  130. dict_delete(model["type_mapping"], name)
  131. String elem
  132. elem = cast_id(model["model"][name])
  133. if (dict_in(model["type_mapping"], elem)):
  134. dict_delete(model["type_mapping"], elem)
  135. return !