include "primitives.alh" include "library.alh" include "object_operations.alh" include "constructors.alh" include "modelling.alh" Boolean function is_direct_instance(model : Element, instance : Element, type : Element): // Just check whether or not the type mapping specifies the type as the type of the instance return element_eq(dict_read_node(model["type_mapping"], instance), type) Boolean function is_nominal_instance(model : Element, instance : Element, type : Element): return is_nominal_subtype(model["metamodel"], dict_read_node(model["type_mapping"], instance), type) Boolean function is_nominal_subtype(metamodel : Element, subclass : Element, superclass : Element): if (element_eq(subclass, superclass)): return True Element superclasses Element new_superclass Element result superclasses = get_superclasses(metamodel, subclass) while (0 < list_len(superclasses)): new_superclass = set_pop(superclasses) result = is_nominal_subtype(metamodel, new_superclass, superclass) if (result): return True return False Boolean function is_structural_instance(model : Element, instance : Element, type : Element): return is_structural_subtype(dict_read_node(model["type_mapping"], instance), type) Boolean function is_structural_subtype(subtype : Element, supertype : Element): // Determine whether it is just the exact type or not if (element_eq(subtype, supertype)): return True // Find all links that are required (name and type) from the specified type Element required_keys required_keys = dict_keys(supertype) Integer required_keys_len required_keys_len = dict_len(required_keys) String key Element equivalent Integer i i = 0 // Go over all keys that we require while (i < required_keys_len): key = set_pop(required_keys) // Check whether they exist in the instance if (dict_in(subtype, key)): // Normally, we should still check whether they don't violate the constraints imposed on the class (i.e., are actually present) // For now, we ignore this and simply require that it is always there in the metamodel (not necessarily in the instance) // TODO // Still check whether the types match if (bool_not(is_structural_subtype(subtype[key], supertype[key]))): return False // All clear, so pass on to the next attribute i = i + 1 else: return False // No violations found, so OK return True String function conformance_scd(model : Element): // Initialization Element keys Element metamodel Element typing String model_name Element src_model Element dst_model Element src_metamodel Element dst_metamodel Element element // Load in variables keys = dict_keys(model["model"]) metamodel = model["metamodel"] typing = model["type_mapping"] // Iterate over each element of the model, finding out whether everything is fine while (0 < list_len(keys)): model_name = set_pop(keys) element = model["model"][model_name] if (bool_not(dict_in_node(typing, element))): return "Model has no type specified: " + model_name if (bool_not(set_in_node(metamodel["model"], dict_read_node(typing, element)))): return "Type of element not in specified metamodel: " + model_name if (bool_not(is_nominal_instance(model, element, dict_read_node(typing, element)))): return "Element is not an instance of its specified type: " + model_name if (is_edge(element)): src_model = read_edge_src(element) dst_model = read_edge_dst(element) src_metamodel = read_edge_src(dict_read_node(typing, element)) dst_metamodel = read_edge_dst(dict_read_node(typing, element)) if (bool_not(is_nominal_instance(model, src_model, src_metamodel))): return "Source of model edge not typed by source of type: " + model_name if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))): return "Destination of model edge not typed by source of type: " + model_name // Structure seems fine, now do static semantics if (dict_in(metamodel, "constraints")): Element constraint_function constraint_function = metamodel["constraints"] return constraint_function(model) else: return "OK" Element function set_model_constraints(model : Element, func : Element): if (dict_in(model, "constraints")): dict_delete(model, "constraints") dict_add(model, "constraints", func) return model Element function generate_bottom_type_mapping(model : Element): Element mm mm = model["metamodel"]["model"] dict_delete(model, "type_mapping") Element tm tm = create_node() dict_add(model, "type_mapping", tm) // Iterate over every element Element elem_keys Element elem elem_keys = dict_keys(model["model"]) while (0 < read_nr_out(elem_keys)): elem = model["model"][set_pop(elem_keys)] if (is_edge(elem)): dict_add(tm, elem, mm["Edge"]) else: dict_add(tm, elem, mm["Node"]) return model