瀏覽代碼

relaxed type mandatory constraint a bit: lower bound can only be 0 or 1

Lucas Heer 7 年之前
父節點
當前提交
9f8d050b80
共有 2 個文件被更改,包括 22 次插入17 次删除
  1. 11 0
      commons.py
  2. 11 17
      verifier.py

+ 11 - 0
commons.py

@@ -82,6 +82,17 @@ def count_occurences(node_type, model):
             ctr += 1
     return ctr
 
+def is_type_mandatory(node_type):
+    """
+    True if node_type is a mandatory type (i.e. it occurs at least once in every example model),
+    False otherwise.
+    """
+    for exm in all_example_models():
+        nodes_with_type = all_nodes_with_type(exm, node_type)
+        if not nodes_with_type:
+            return False
+    return True
+
 def get_associations_between(model, node1, node2):
     """ Returns a list of association IDs between the nodes node1 and node2 """
     edges_n1 = mv.read_outgoing(model, node1, "Edge")

+ 11 - 17
verifier.py

@@ -63,22 +63,18 @@ class Verifier(object):
 
     def verify_node_multiplicity(self):
         """
-        A node is mandatory in the instance model if it occurs in every example model.
-        More specifically, if a node type appears occ = [n0, n1, ... n_i] times in example model i,
-        it needs to occur min(occ) in the instance model.
+        A node is mandatory in the instance model if it occurs at least once in every example model.
         """
-        total_occurences = {k: [] for k in self._available_types}
-        for node, occ_list in total_occurences.iteritems():
-            for exm in self._example_models:
-                num_occ = commons.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 commons.count_occurences(el, self._instance_model) >= min_occ:
-                return {"OK": False, "error": "Node with type {} needs to occur at least {} times".format(el, min_occ)}
+        type_mandatory = {t:False for t in self._available_types}
+        for node_type,_ in type_mandatory.iteritems():
+            if commons.is_type_mandatory(node_type):
+                type_mandatory[node_type] = True
 
-        return {"OK": True, "error": None, "affected":[]}
+        mandatory_types = [node_type for node_type, mandatory in type_mandatory.iteritems() if mandatory]
+        for mand_type in mandatory_types:
+            all_of_type = commons.all_nodes_with_type(self._instance_model, mand_type)
+            if not all_of_type:
+                return {"OK": False, "error": "Mandatory node of type {} not found".format(mand_type), "affected":[]}
 
     def _get_attributes_of_all_types(self):
         """
@@ -123,9 +119,7 @@ class Verifier(object):
         """
         1. For every attribute key of a typed node in the instance model, there must be a corresponding
         attribute key in some example model.
-        2. An attribute for a type is mandatory if it occurs in every example model.
-        TODO: Would make more sense to change to: An attribute is mandatory if it occurs in every example model where the
-        associated typed node occurs (as explained in the thesis text).
+        2. An attribute for a type is mandatory if it occurs in every example model where the associated type node occurs.
         """
 
         # Check attribute keys for every node in instance model