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 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 findAttribute(source : Element, attr_name : Element, types : Element, inheritance_link : Element): if (dict_in(source, attr_name)): return dict_read_edge(source, attr_name) else: Integer counter Integer i Element edge counter = read_nr_out(source) i = 0 while (i < counter): edge = read_out(source, i) if (element_eq(dict_read_node(types, edge), inheritance_link)): return find_attribute(read_edge_dst(edge), attr_name, types, inheritance_link) i = i + 1 // No return at the moment, as this crashes the MvK log("ERROR: could not find attribute") Element function readAttribute(model : Element, source : Element, attr_name : String): // Read out the edge we are talking about Element edge edge = findAttribute(dict_read_node(model["type_mapping"], source), attr_name, model["type_mapping"], model["inheritance"]) return read_edge_dst(set_pop(allOutgoingAssociationInstances(model, source, edge))) Element function deleteAttribute(model : Element, source : Element, attr_name : Element): Element edge edge = findAttribute(dict_read_node(model["type_mapping"], source), attr_name, model["type_mapping"], model["inheritance"]) Element found_elements found_elements = allOutgoingAssociationInstances(model, source, edge) Element rm_element if (list_len(found_elements) > 0): rm_element = read_edge_dst(set_pop(found_elements)) dict_delete(model["type_mapping"], rm_element) delete_element(rm_element) return edge Element function getAttributeList(model : Element, mm : Element): Element result result = create_node() // Get all outgoing "dictionary" links Element set_own set_own = dict_keys(mm) // Filter them Element e while (0 < read_nr_out(set_own)): e = set_pop(set_own) if (is_physical_string(e)): if (has_value(mm[e])): // This is a primitive type, so add to the list dict_add(result, e, mm[e]) // And go up to the possible supertypes Element found_elements found_elements = allOutgoingAssociationInstances(model, mm, model["inheritance"]) Element dict_super Integer i Integer max Element set_super Element found_key while (0 < read_nr_out(found_elements)): // Now find the destination of these associations, which are the superclasses of this model dict_super = getAttributeList(model, read_edge_dst(set_pop(found_elements))) set_super = dict_keys(dict_super) // Merge the two while (0 < read_nr_out(set_super)): found_key = set_pop(set_super) dict_add(result, found_key, dict_super[found_key]) 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 (bool_and(is_physical_string(e), has_value(element[e]))): // This is a primitive type, so add to the list dict_add(result, e, 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 reverseNameLookup(s : Element, e : Element): Element element_keys 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)), " )")