include "primitives.alh" include "conformance_scd.alh" include "constructors.alh" Element function allInstances(model : Element, type : Element): Element type_mapping Element result type_mapping = model["type_mapping"] result = create_node() Integer counter counter = 0 Integer length length = read_nr_out(type_mapping) Element edge while (counter < length): edge = read_out(type_mapping, counter) if (element_eq(read_edge_dst(edge), type)): // Found an element of the specified type set_add(result, read_edge_dst(read_out(edge, 0))) counter = counter + 1 return result Element function allPossibleIncoming(model : Element, target : String): // Find all possible incoming link types for the target model // Should also include those specified on the superclass(es) Element all_elems String type Element metamodel Element elem Element result result = create_node() metamodel = model["metamodel"] all_elems = dict_keys(metamodel["model"]) while (0 < list_len(all_elems)): type = set_pop(all_elems) elem = metamodel["model"][type] if (is_edge(elem)): if (is_nominal_instance(model, model["model"][target], read_edge_src(elem))): set_add(result, elem) return result Element function allPossibleOutgoing(model : Element, source : String): // Find all possible outgoing link types for the source model // Should also include those specified on the superclass(es) Element all_elems String type Element metamodel Element elem Element result result = create_node() metamodel = model["metamodel"] all_elems = dict_keys(model["metamodel"]["model"]) while (0 < list_len(all_elems)): type = set_pop(all_elems) elem = metamodel["model"][type] if (is_edge(elem)): if (is_nominal_instance(model, model["model"][source], read_edge_dst(elem))): set_add(result, elem) return result Element function allOutgoingAssociationInstances(model : Element, source : Element, assoc : Element): // Read out all outgoing edges of the model and select those that are typed by the specified association // TODO for some reason this crashes if allInstances is used! Integer length length = read_nr_out(source) Integer counter counter = 0 Element result result = create_node() Element edge while (counter < length): edge = read_out(source, counter) if (element_eq(dict_read_node(model["type_mapping"], edge), assoc)): set_add(result, edge) counter = counter + 1 return result Element function allIncomingAssociationInstances(model : Element, source : Element, assoc : Element): // Read out all outgoing edges of the model and select those that are typed by the specified association Element result result = create_node() Element allinsts allinsts = allInstances(model, assoc) Element understudy while (0 < read_nr_out(allinsts)): understudy = set_pop(allinsts) if (element_eq(read_edge_dst(understudy), source)): set_add(result, understudy) return result Element function readElementByName(model : Element, name : String): return model["model"][name] Element function getAttributeList(model : Element, element : String): Element result Element keys Element type Element attr_name String attr_type result = create_node() type = dict_read_node(model["type_mapping"], model["model"][element]) keys = dict_keys(type) // Add our own attributes while (0 < list_len(keys)): attr_name = set_pop(keys) if (is_physical_string(attr_name)): attr_type = getName(model["metamodel"], type[attr_name]) dict_add(result, attr_name, attr_type) // Go on to the metalevel // TODO return result Element function getInstantiatableAttributes(model : Element, element : Element): Element result result = create_node() // Get all outgoing "dictionary" links Element set_own set_own = dict_keys(element) // Filter them Element e while (0 < read_nr_out(set_own)): e = set_pop(set_own) if (is_physical_string(e)): dict_add(result, e, getName(model, element[e])) return result String function getName(m : Element, e : Element): Element element_keys Element s s = m["model"] element_keys = dict_keys(s) Element key while (0 < read_nr_out(element_keys)): key = set_pop(element_keys) if (element_eq(dict_read_node(s, key), e)): return key return string_join(string_join("(unknown: ", cast_e2s(e)), " )") String function reverseKeyLookup(dict : Element, element : Element): Element elements String name elements = dict_keys(dict) while (0 < list_len(elements)): name = set_pop(elements) if (element_eq(dict[name], element)): return name return string_join(string_join("(unknown: ", cast_e2s(element)), " )") String function print_dict(dict : Element): Element keys Element key String result keys = dict_keys(dict) result = "" while (0 < list_len(keys)): key = set_pop(keys) result = result + cast_v2s(key) result = result + ": " result = result + cast_v2s(dict[key]) result = result + "\n" return result