123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- 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 setAttribute(model : Element, source : Element, attr_name : String, value : Element):
- // Read out which edge we are talking about
- Element edge
- edge = deleteAttribute(model, source, attr_name)
- // Now make the element
- Element created_edge
- created_edge = create_edge(source, value)
- dict_add(model["type_mapping"], created_edge, edge)
- dict_add(model["type_mapping"], value, read_edge_dst(edge))
- add_to_model(model, "", value)
- add_to_model(model, "", created_edge)
- return True
- 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)), " )")
|