typing.alc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. keys = dict_keys(model["type_mapping"])
  13. while (set_len(keys) > 0):
  14. key = set_pop(keys)
  15. m = model["model"][key]
  16. mm = model["metamodel"]["model"][model["type_mapping"][key]]
  17. edge = create_edge(m, mm)
  18. dict_add_fast(tm_model, cast_id(m), m)
  19. if (bool_not(dict_in(tm_model, cast_id(mm)))):
  20. dict_add_fast(tm_model, cast_id(mm), mm)
  21. dict_add_fast(tm_model, cast_id(edge), edge)
  22. return tm_model!
  23. Void function set_type_mapping(model : Element, type_mapping_model : Element):
  24. // Serialize model to dictionary
  25. Element type_mapping
  26. Element rev_model
  27. Element rev_metamodel
  28. Element keys
  29. String key
  30. type_mapping = dict_create()
  31. keys = dict_keys(type_mapping_model)
  32. rev_model = make_reverse_dictionary(model["model"])
  33. rev_metamodel = make_reverse_dictionary(model["metamodel"]["model"])
  34. while (set_len(keys) > 0):
  35. key = set_pop(keys)
  36. if (is_edge(type_mapping_model[key])):
  37. if (bool_not(bool_or(dict_in(rev_model, cast_id(type_mapping_model[key])), dict_in(rev_metamodel, cast_id(type_mapping_model[key]))))):
  38. // Element is in neither model or metamodel
  39. // Must be a typing link!
  40. // So add it
  41. dict_add_fast(type_mapping, rev_model[cast_id(read_edge_src(type_mapping_model[key]))], rev_metamodel[cast_id(read_edge_dst(type_mapping_model[key]))])
  42. dict_overwrite(model, "type_mapping", type_mapping)
  43. return!
  44. Element function elements_typed_by(model : Element, type_name : String):
  45. Element result
  46. Element temp_result
  47. String temp
  48. result = set_create()
  49. temp_result = reverseKeyLookupMulti(get_type_mapping_as_dict(model), type_name)
  50. while (set_len(temp_result) > 0):
  51. temp = set_pop(temp_result)
  52. if (dict_in(model["model"], temp)):
  53. set_add(result, temp)
  54. return result!
  55. Element function get_type_mapping_as_dict(model : Element):
  56. return model["type_mapping"]!
  57. String function read_type(model : Element, name : String):
  58. String result
  59. Element tm
  60. if (dict_in(model["model"], name)):
  61. if (dict_in(model["type_mapping"], name)):
  62. result = model["type_mapping"][name]
  63. if (dict_in(model["metamodel"]["model"], result)):
  64. return result!
  65. else:
  66. return ""!
  67. else:
  68. return ""!
  69. else:
  70. return ""!
  71. Void function retype(model : Element, element : String, type : String):
  72. // Retype a model, deleting any previous type the element had
  73. // The type string is evaluated in the metamodel previously specified
  74. dict_overwrite(model["type_mapping"], element, type)
  75. return!
  76. Void function new_type_mapping(model : Element):
  77. dict_overwrite(model, "type_mapping", dict_create())
  78. return !
  79. Void function remove_type(model : Element, name : String):
  80. if (dict_in(model["type_mapping"], name)):
  81. dict_delete(model["type_mapping"], name)
  82. String elem
  83. elem = cast_id(model["model"][name])
  84. if (dict_in(model["type_mapping"], elem)):
  85. dict_delete(model["type_mapping"], elem)
  86. return !