include "primitives.alh" include "utils.alh" Element function get_type_mapping(model : Element): return model["type_mapping"]! Void function set_type_mapping(model : Element, type_mapping : Element): dict_overwrite(model, "type_mapping", type_mapping) return! Element function get_type_mapping_as_dict(model : Element): Float start start = time() Element dict Element keys String key dict = dict_create() keys = dict_keys(model["type_mapping"]) Element rev_model Element rev_metamodel rev_model = make_reverse_dictionary(model["model"]) rev_metamodel = make_reverse_dictionary(model["metamodel"]["model"]) while (set_len(keys) > 0): key = set_pop(keys) if (bool_not(bool_or(dict_in_node(rev_model, model["type_mapping"][key]), dict_in_node(rev_metamodel, model["type_mapping"][key])))): // Element is in neither model or metamodel // Must be a typing link! // So add it dict_add_fast(dict, rev_model[cast_id2s(read_edge_src(model["type_mapping"][key]))], rev_metamodel[cast_id2s(read_edge_dst(model["type_mapping"][key]))]) log("get_type_mapping_as_dict : 0.0 : " + cast_v2s(time() - start)) return dict! String function read_type(model : Element, name : String): Float start start = time() Element m_element Element link Integer nr Element mm_element Element result if (dict_in(model["model"], name)): m_element = model["model"][name] nr = read_nr_out(m_element) - 1 while (nr >= 0): link = read_out(m_element, nr) if (dict_in(model["type_mapping"], cast_id2s(link))): // Link is in our mapping, so we probably found it... // But ONLY if it is NOT in the model itself if (reverseKeyLookup(model["model"], link) == ""): // It is not, so we actually got it! // Now follow the edge to the type mm_element = read_edge_dst(link) result = reverseKeyLookup(model["metamodel"]["model"], mm_element) log("read_type : 0.0 : " + cast_v2s(time() - start)) return result! nr = nr - 1 // Nothing found, so it must not be typed at all log("read_type : 0.0 : " + cast_v2s(time() - start)) return ""! Void function retype(model : Element, element : String, type : String): // Remove previous type Float start start = time() remove_type(model, element) // Add element of the model dict_add_fast(model["type_mapping"], cast_id2s(model["model"][element]), model["model"][element]) // Add element of metamodel if not yet there Element type_entry type_entry = model["metamodel"]["model"][type] if (bool_not(dict_in(model["type_mapping"], cast_id2s(type_entry)))): dict_add_fast(model["type_mapping"], cast_id2s(type_entry), type_entry) // Draw a link between them: the typing link Element new_edge new_edge = create_edge(model["model"][element], type_entry) dict_add_fast(model["type_mapping"], cast_id2s(new_edge), new_edge) log("retype : 0.0 : " + cast_v2s(time() - start)) return! Void function new_type_mapping(model : Element): if (dict_in(model, "type_mapping")): dict_delete(model, "type_mapping") dict_add_fast(model, "type_mapping", dict_create()) return ! Void function remove_type(model : Element, name : String): Float start start = time() String elem elem = cast_id2s(model["model"][name]) if (dict_in(model["type_mapping"], elem)): dict_delete(model["type_mapping"], elem) log("remove_type : 0.0 : " + cast_v2s(time() - start)) return ! Element function elements_typed_by(model : Element, type_name : String): Float start Element result start = time() result = reverseKeyLookupMulti(get_type_mapping_as_dict(model), type_name) log("elements_typed_by : 0.0 : " + cast_v2s(time() - start)) return result!