|
|
@@ -0,0 +1,184 @@
|
|
|
+import wrappers.modelverse as mv
|
|
|
+
|
|
|
+
|
|
|
+class Edge(object):
|
|
|
+ """
|
|
|
+ Small helper class for association validation.
|
|
|
+ Stores an edge as a tuple of (source, dest)
|
|
|
+ """
|
|
|
+ def __init__(self, src, dest):
|
|
|
+ self.src = src
|
|
|
+ self.dest = dest
|
|
|
+
|
|
|
+ def __eq__(self, other):
|
|
|
+ if other.src == self.src and other.dest == self.dest:
|
|
|
+ return True
|
|
|
+ return False
|
|
|
+
|
|
|
+ def __repr__(self):
|
|
|
+ return "{}->{}".format(self.src, self.dest)
|
|
|
+
|
|
|
+
|
|
|
+class Attribute(object):
|
|
|
+ """
|
|
|
+ Helper class for attribute representation as key value pair
|
|
|
+ """
|
|
|
+ def __init__(self, key, val):
|
|
|
+ self.key = key
|
|
|
+ self.val = val
|
|
|
+
|
|
|
+ def __eq__(self, other):
|
|
|
+ if other.key == self.key and other.val == self.val:
|
|
|
+ return True
|
|
|
+ return False
|
|
|
+
|
|
|
+ def __repr__(self):
|
|
|
+ return "{}={}".format(self.key, self.val)
|
|
|
+
|
|
|
+# returns a list of attributes of a node with type node_type in a model
|
|
|
+def _get_attributes_of(node_type, model):
|
|
|
+ ret = []
|
|
|
+ node_attribute_links = mv.all_instances(model, "NodeAttribute")
|
|
|
+ for link in node_attribute_links:
|
|
|
+ src_node = mv.read_association_source(model, link)[0]
|
|
|
+ src_typ = mv.read_attrs(model, src_node)["type"]
|
|
|
+ if src_typ != node_type:
|
|
|
+ continue
|
|
|
+ attr_node_name = mv.read_association_destination(model, link)[0]
|
|
|
+ attr_dict = mv.read_attrs(model, attr_node_name)
|
|
|
+ attr = Attribute(attr_dict["key"], attr_dict["value"])
|
|
|
+ ret.append(attr)
|
|
|
+ return ret
|
|
|
+
|
|
|
+# returns the number of occurences of a node with type node_type in a model
|
|
|
+def _count_occurences(node_type, model):
|
|
|
+ ctr = 0
|
|
|
+ all_nodes = mv.all_instances(model, "Node")
|
|
|
+ for node in all_nodes:
|
|
|
+ typ = mv.read_attrs(model, node)["type"]
|
|
|
+ if typ == node_type:
|
|
|
+ ctr += 1
|
|
|
+ return ctr
|
|
|
+
|
|
|
+
|
|
|
+def verify(instance_model_path):
|
|
|
+ example_models = mv.model_list("models/example")
|
|
|
+ example_models_full = ["models/example/"+exm for exm in example_models]
|
|
|
+
|
|
|
+ # 1. Check if all nodes in instance model are typed by a node in the example models
|
|
|
+ # For that, first get all available types from example models
|
|
|
+ print("Verify: checking node typing ...")
|
|
|
+ all_types = []
|
|
|
+ for exm in example_models_full:
|
|
|
+ all_nodes = mv.all_instances(exm, "Node")
|
|
|
+ for node in all_nodes:
|
|
|
+ typ = mv.read_attrs(exm, node)["type"]
|
|
|
+ if not typ in all_types:
|
|
|
+ all_types.append(typ)
|
|
|
+ #print("Verify: types found in example models: {}".format(all_types))
|
|
|
+
|
|
|
+ # Now, check for every element in instance model if it is a valid type
|
|
|
+ all_nodes = mv.all_instances(instance_model_path, "Node")
|
|
|
+ for node in all_nodes:
|
|
|
+ node_typ = mv.read_attrs(instance_model_path, node)["type"]
|
|
|
+ if node_typ not in all_types:
|
|
|
+ return {"OK":False, "error":"Type {} from instance model not in example models".format(node_typ)}
|
|
|
+
|
|
|
+ # 2. Check node cardinality: a node is mandatory in the instance model if it occurs in every
|
|
|
+ # example model. Additionally, it needs to occur at least n times if it occurs at least n times in every example model.
|
|
|
+ print("Verify: checking node multiplicity ...")
|
|
|
+ total_occurences = {k:[] for k in all_types}
|
|
|
+ for node, occ_list in total_occurences.iteritems():
|
|
|
+ for exm in example_models_full:
|
|
|
+ num_occ = _count_occurences(node, exm)
|
|
|
+ occ_list.append(num_occ)
|
|
|
+
|
|
|
+ elem_cardinality = {k:min(v) for k, v in total_occurences.iteritems()}
|
|
|
+ for el, min_occ in elem_cardinality.iteritems():
|
|
|
+ if not _count_occurences(el, instance_model_path) >= min_occ:
|
|
|
+ return {"OK":False, "error":"Node with type {} needs to occur at least {} times".format(el, min_occ)}
|
|
|
+
|
|
|
+ # 3. Check attribute types: Is every attribute in instance model present in some example model?
|
|
|
+ print("Verify: checking attributes ...")
|
|
|
+ all_nodes = mv.all_instances(instance_model_path, "Node")
|
|
|
+ for node in all_nodes:
|
|
|
+ node_typ = mv.read_attrs(instance_model_path, node)["type"]
|
|
|
+ attrs = _get_attributes_of(node_typ, instance_model_path)
|
|
|
+ if len(attrs) == 0:
|
|
|
+ continue
|
|
|
+ #print("Attributes of node {} of type {} are: {}".format(node, node_typ, attrs))
|
|
|
+ # check example models for such an attribute key
|
|
|
+ for im_attribute in attrs:
|
|
|
+ found = False
|
|
|
+ for exm in example_models_full:
|
|
|
+ if found:
|
|
|
+ break
|
|
|
+ exm_attrs = _get_attributes_of(node_typ, exm)
|
|
|
+ for exm_attr in exm_attrs:
|
|
|
+ if exm_attr.key == im_attribute.key:
|
|
|
+ #print("Found attribute {} in model {}".format(im_attribute, exm))
|
|
|
+ found = True
|
|
|
+ break
|
|
|
+ if not found:
|
|
|
+ return {"OK":False, "error":"Attribute {} not found in example models".format(im_attribute)}
|
|
|
+
|
|
|
+ # TODO: Attribute mandatory if present in all example model nodes
|
|
|
+
|
|
|
+
|
|
|
+ # 4. Associations
|
|
|
+ # Check multiplicity: an association needs to occur n>0 times if it occurs n>0 times in every example model
|
|
|
+ # Check assocation type validity as well
|
|
|
+ print("Verify: checking associations ...")
|
|
|
+ all_edges = {k:[] for k in example_models_full}
|
|
|
+ for exm in example_models_full:
|
|
|
+ all_nodes = mv.all_instances(exm, "Node")
|
|
|
+ for node in all_nodes:
|
|
|
+ node_typ = mv.read_attrs(exm, node)["type"]
|
|
|
+ out_associations = mv.read_outgoing(exm, node, "Edge")
|
|
|
+ for assoc in out_associations:
|
|
|
+ if assoc.startswith("__"):
|
|
|
+ continue
|
|
|
+ target = mv.read_association_destination(exm, assoc)[0]
|
|
|
+ target_typ = mv.read_attrs(exm, target)["type"]
|
|
|
+ all_edges[exm].append(Edge(node_typ, target_typ))
|
|
|
+
|
|
|
+ unique_edges = []
|
|
|
+ for edge_lst in all_edges.values():
|
|
|
+ for edge in edge_lst:
|
|
|
+ if not edge in unique_edges:
|
|
|
+ unique_edges.append(edge)
|
|
|
+
|
|
|
+ edge_ctr = {k:[] for k in unique_edges}
|
|
|
+ for edge, ctr_lst in edge_ctr.iteritems():
|
|
|
+ for exm, edges in all_edges.iteritems():
|
|
|
+ ctr_lst.append(edges.count(edge))
|
|
|
+
|
|
|
+ for k, v in edge_ctr.iteritems():
|
|
|
+ edge_ctr[k] = min(v)
|
|
|
+
|
|
|
+ #print(edge_ctr) # {Edge("AP", "Tablet"): 0, Edge("Router", "PC"): 1, ...}
|
|
|
+
|
|
|
+ # check if mandatory edges are present in instance model
|
|
|
+ # first, get all edges in instance model
|
|
|
+ all_edges = mv.all_instances(instance_model_path, "Edge")
|
|
|
+ im_edges = []
|
|
|
+ for edge in all_edges:
|
|
|
+ if edge.startswith("__"):
|
|
|
+ continue
|
|
|
+ edge_src = mv.read_association_source(instance_model_path, edge)[0]
|
|
|
+ edge_dest = mv.read_association_destination(instance_model_path, edge)[0]
|
|
|
+ edge_src_typ = mv.read_attrs(instance_model_path, edge_src)["type"]
|
|
|
+ edge_dest_typ = mv.read_attrs(instance_model_path, edge_dest)["type"]
|
|
|
+ im_edges.append(Edge(edge_src_typ, edge_dest_typ))
|
|
|
+
|
|
|
+ # Check if all edges in instance models are valid (connecting types correspond to example models)
|
|
|
+ for edge in im_edges:
|
|
|
+ if edge not in edge_ctr.keys():
|
|
|
+ return {"OK":False, "error":"Edge {} not valid according to example models".format(edge)}
|
|
|
+
|
|
|
+ # now, check if for every mandatory edge, it occurs sufficiently enough
|
|
|
+ for edge, cnt in edge_ctr.iteritems():
|
|
|
+ if im_edges.count(edge) < cnt:
|
|
|
+ return {"OK":False, "error":"Edge {} needs to appear {} times in instance model".format(edge, cnt)}
|
|
|
+
|
|
|
+ return {"OK":True, "error":None}
|