typing.alc 2.9 KB

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