typing.alc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. include "primitives.alh"
  2. include "utils.alh"
  3. Element function get_type_mapping_as_dict(model : Element):
  4. return OLD_get_type_mapping_as_dict(model)!
  5. String function read_type(model : Element, name : String):
  6. return OLD_read_type(model, name)!
  7. Void function retype(model : Element, element : String, type : String):
  8. OLD_retype(model, element, type)
  9. return!
  10. Void function new_type_mapping(model : Element):
  11. OLD_new_type_mapping(model)
  12. return!
  13. Void function remove_type(model : Element, name : String):
  14. OLD_remove_type(model, name)
  15. return!
  16. Element function get_type_mapping(model : Element):
  17. return model["type_mapping"]!
  18. Void function set_type_mapping(model : Element, type_mapping : Element):
  19. dict_overwrite(model, "type_mapping", type_mapping)
  20. return!
  21. Element function NEW_get_type_mapping_as_dict(model : Element):
  22. Element dict
  23. Element keys
  24. String key
  25. dict = dict_create()
  26. keys = dict_keys(model["type_mapping"])
  27. Element rev_model
  28. Element rev_metamodel
  29. rev_model = make_reverse_dictionary(model["model"])
  30. rev_metamodel = make_reverse_dictionary(model["metamodel"]["model"])
  31. while (set_len(keys) > 0):
  32. key = set_pop(key)
  33. if (bool_not(bool_or(dict_in_node(rev_model, model["type_mapping"][key]), dict_in_node(rev_metamodel, model["type_mapping"][key])))):
  34. // Element is in neither model or metamodel
  35. // Must be a typing link!
  36. // So add it
  37. dict_add_fast(dict, rev_model[model["type_mapping"][key]], rev_metamodel[model["type_mapping"][key]])
  38. return dict!
  39. String function NEW_read_type(model : Element, name : String):
  40. Element m_element
  41. Element link
  42. Integer nr
  43. Element mm_element
  44. if (dict_in(model["model"], name)):
  45. m_element = model["model"][name]
  46. nr = read_nr_out(m_element) - 1
  47. while (nr >= 0):
  48. link = read_out(m_element, nr)
  49. if (dict_in(model["type_mapping"], cast_id2s(link))):
  50. // Link is in our mapping, so we probably found it...
  51. // But ONLY if it is NOT in the model itself
  52. if (reverseKeyLookup(model["model"], link) == ""):
  53. // It is not, so we actually got it!
  54. // Now follow the edge to the type
  55. mm_element = read_edge_dst(link)
  56. return reverseKeyLookup(model["metamodel"]["model"], mm_element)!
  57. nr = nr - 1
  58. // Nothing found, so it must not be typed at all
  59. return ""!
  60. Void function NEW_retype(model : Element, element : String, type : String):
  61. // Remove previous type
  62. remove_type(model, element)
  63. // Add element of the model
  64. dict_add_fast(model["type_mapping"], cast_id2s(model["model"][element]), model["model"][element])
  65. // Add element of metamodel if not yet there
  66. Element type_entry
  67. type_entry = model["metamodel"]["model"][type]
  68. if (bool_not(dict_in(model["type_mapping"], cast_id2s(type_entry)))):
  69. dict_add_fast(model["type_mapping"], cast_id2s(type_entry), type_entry)
  70. // Draw a link between them: the typing link
  71. Element new_edge
  72. new_edge = create_edge(model["model"][element], type_entry)
  73. dict_add_fast(model["type_mapping"], cast_id2s(new_edge), new_edge)
  74. return!
  75. Void function NEW_new_type_mapping(model : Element):
  76. if (dict_in(model, "type_mapping")):
  77. dict_delete(model, "type_mapping")
  78. dict_add_fast(model, "type_mapping", dict_create())
  79. return !
  80. Void function NEW_remove_type(model : Element, name : String):
  81. String elem
  82. elem = cast_id2s(model["model"][name])
  83. if (dict_in(model["type_mapping"], elem)):
  84. dict_delete(model["type_mapping"], elem)
  85. return !
  86. Element function OLD_get_type_mapping_as_dict(model : Element):
  87. return model["type_mapping"]!
  88. Element function OLD_get_elements_typed_by(model : Element, type : String):
  89. return reverseKeyLookupMulti(model["type_mapping"], type)!
  90. String function OLD_read_type(model : Element, name : String):
  91. String result
  92. Element tm
  93. if (dict_in(model["model"], name)):
  94. if (dict_in(model["type_mapping"], name)):
  95. result = model["type_mapping"][name]
  96. if (dict_in(model["metamodel"]["model"], result)):
  97. return result!
  98. else:
  99. return ""!
  100. else:
  101. return ""!
  102. else:
  103. return ""!
  104. Void function OLD_retype(model : Element, element : String, type : String):
  105. // Retype a model, deleting any previous type the element had
  106. // The type string is evaluated in the metamodel previously specified
  107. if (dict_in(model["type_mapping"], element)):
  108. dict_delete(model["type_mapping"], element)
  109. dict_add_fast(model["type_mapping"], element, type)
  110. return!
  111. Void function OLD_new_type_mapping(model : Element):
  112. if (dict_in(model, "type_mapping")):
  113. dict_delete(model, "type_mapping")
  114. dict_add_fast(model, "type_mapping", dict_create())
  115. return !
  116. Void function OLD_remove_type(model : Element, name : String):
  117. dict_delete(model["type_mapping"], name)
  118. return !