typing.alc 4.8 KB

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