typing.alc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. if (bool_not(dict_in(tm_model, cast_id2s(mm)))):
  20. dict_add_fast(tm_model, cast_id2s(mm), mm)
  21. dict_add_fast(tm_model, cast_id2s(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_id2s(type_mapping_model[key])), dict_in(rev_metamodel, cast_id2s(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_id2s(read_edge_src(type_mapping_model[key]))], rev_metamodel[cast_id2s(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. result = reverseKeyLookupMulti(get_type_mapping_as_dict(model), type_name)
  47. return result!
  48. Element function get_type_mapping_as_dict(model : Element):
  49. return model["type_mapping"]!
  50. Element function get_elements_typed_by(model : Element, type : String):
  51. return reverseKeyLookupMulti(model["type_mapping"], type)!
  52. String function read_type(model : Element, name : String):
  53. String result
  54. Element tm
  55. if (dict_in(model["model"], name)):
  56. if (dict_in(model["type_mapping"], name)):
  57. result = model["type_mapping"][name]
  58. if (dict_in(model["metamodel"]["model"], result)):
  59. return result!
  60. else:
  61. return ""!
  62. else:
  63. return ""!
  64. else:
  65. return ""!
  66. Void function retype(model : Element, element : String, type : String):
  67. // Retype a model, deleting any previous type the element had
  68. // The type string is evaluated in the metamodel previously specified
  69. if (dict_in(model["type_mapping"], element)):
  70. dict_delete(model["type_mapping"], element)
  71. dict_add_fast(model["type_mapping"], element, type)
  72. return!
  73. Void function new_type_mapping(model : Element):
  74. if (dict_in(model, "type_mapping")):
  75. dict_delete(model, "type_mapping")
  76. dict_add_fast(model, "type_mapping", dict_create())
  77. return !
  78. Void function remove_type(model : Element, name : String):
  79. dict_delete(model["type_mapping"], name)
  80. return !