verifier.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import wrappers.modelverse as mv
  2. import commons
  3. class Edge(object):
  4. """
  5. Small helper class for association validation.
  6. Represents an edge as a connection between two nodes (n1, n2).
  7. Does not assume any direction. As such, the eq and hash implementations
  8. do not care about the ordering and an Edge("a", "b") object is equal to Edge("b", "a").
  9. """
  10. def __init__(self, n1, n2):
  11. self.n1 = n1
  12. self.n2 = n2
  13. def __eq__(self, other):
  14. # type: (Edge) -> bool
  15. if other.n1 == self.n1 or other.n1 == self.n2 and other.n2 == self.n1 or other.n2 == self.n2:
  16. return True
  17. return False
  18. def __hash__(self):
  19. return hash(self.n1) ^ hash(self.n2)
  20. def __repr__(self):
  21. return "{}-{}".format(self.n1, self.n2)
  22. class Verifier(object):
  23. def __init__(self, instance_model):
  24. self._instance_model = instance_model
  25. self._example_models = None
  26. self._available_types = None
  27. def set_instance_model(self, model):
  28. self._instance_model = model
  29. def get_instance_model(self):
  30. return self._instance_model
  31. def init(self):
  32. """
  33. Inits the verifier. Loads required data from the modelverse
  34. to avoid reloading it for every step.
  35. """
  36. self._example_models = commons.all_example_models()
  37. self._available_types = commons.get_available_types()
  38. def verify_node_typing(self):
  39. """
  40. Checks if every node in instance model is typed by a node in any example model.
  41. Should actually not be neccessary since instance modeling only allows types from
  42. example models.
  43. """
  44. # Check for every node in instance model if it has a valid type
  45. all_nodes = mv.all_instances(self._instance_model, "Node")
  46. for node in all_nodes:
  47. node_typ = commons.get_node_type(self._instance_model, node)
  48. if node_typ not in self._available_types:
  49. return {"OK": False, "error": "Type {} from instance model not in example models".format(node_typ)}
  50. return {"OK":True, "error":None, "affected":[]}
  51. def verify_node_multiplicity(self):
  52. """
  53. A node is mandatory in the instance model if it occurs at least once in every example model.
  54. """
  55. type_mandatory = {t:False for t in self._available_types}
  56. for node_type,_ in type_mandatory.iteritems():
  57. if commons.is_type_mandatory(node_type):
  58. type_mandatory[node_type] = True
  59. mandatory_types = [node_type for node_type, mandatory in type_mandatory.iteritems() if mandatory]
  60. for mand_type in mandatory_types:
  61. all_of_type = commons.all_nodes_with_type(self._instance_model, mand_type)
  62. if not all_of_type:
  63. return {"OK": False, "error": "Mandatory node of type {} not found".format(mand_type), "affected":[]}
  64. def _get_attributes_of_all_types(self):
  65. """
  66. Helper for attribute check that returns a dictionary of the form {type:[attr_key_1, attr_key_2, ...], ...},
  67. listing all attributes of every type from example models
  68. """
  69. attrs_of_types = {node_type:[] for node_type in self._available_types}
  70. for exm in self._example_models:
  71. for node_type, attrs in attrs_of_types.iteritems():
  72. attrs_of_type_in_exm = [x.key for x in commons.get_all_attributes_of_type(exm, node_type)]
  73. for attr in attrs_of_type_in_exm:
  74. if not attr in attrs:
  75. attrs.append(attr)
  76. return attrs_of_types
  77. def _has_attribute_key(self, model, node_id, attr_key):
  78. """
  79. True if node with node_id has an attribute with key attr_key, False otherwise.
  80. """
  81. attrs = commons.get_attributes_of_node(model, node_id)
  82. for attr in attrs:
  83. if attr.key == attr_key:
  84. return True
  85. return False
  86. def _is_attribute_mandatory(self, node_type, attr_key):
  87. """
  88. Helper for attribute check that returns True if the attribute attr_key of type node_type is mandatory by
  89. looking at all example models.
  90. """
  91. # iterate through all example models: If every node with type node_type has the attribute with attr_key,
  92. # it is mandatory
  93. for exm in self._example_models:
  94. nodes_of_type = commons.all_nodes_with_type(exm, node_type)
  95. for node in nodes_of_type:
  96. if not self._has_attribute_key(exm, node, attr_key):
  97. return False
  98. return True
  99. def verify_attributes(self):
  100. """
  101. 1. For every attribute key of a typed node in the instance model, there must be a corresponding
  102. attribute key in some example model.
  103. 2. An attribute for a type is mandatory if it occurs in every example model where the associated type node occurs.
  104. """
  105. # Check attribute keys for every node in instance model
  106. all_nodes = mv.all_instances(self._instance_model, "Node")
  107. for node in all_nodes:
  108. attrs = commons.get_attributes_of_node(self._instance_model, node)
  109. if not attrs:
  110. continue
  111. node_typ = commons.get_node_type(self._instance_model, node)
  112. for attr in attrs:
  113. # check every example model if such an attribute key is present for the specific type
  114. for exm in self._example_models:
  115. exm_attrs = commons.get_all_attributes_of_type(exm, node_typ)
  116. if attr.key in [x.key for x in exm_attrs]:
  117. break
  118. else:
  119. # loop completed without break, so we did not find any key in example models
  120. return {"OK": False, "error": "No key {} for type {} in example models".format(attr.key, node_typ),
  121. "affected":[node]}
  122. # Check if mandatory attributes are present in instance model
  123. attr_mandatory = {node_type:{} for node_type in self._available_types}
  124. all_attrs = self._get_attributes_of_all_types()
  125. for typ_i, attr_list in all_attrs.iteritems():
  126. for typ_j, dic in attr_mandatory.iteritems():
  127. if typ_j == typ_i:
  128. for attr in attr_list:
  129. dic[attr] = False
  130. # have dict like {"PC": {"owner":False}, "Router": {"IP":False}, ...}
  131. # now need to check which one is mandatory and set the boolean accordingly
  132. for node_type, attr_dict in attr_mandatory.iteritems():
  133. for attr, _ in attr_dict.iteritems():
  134. if self._is_attribute_mandatory(node_type, attr):
  135. attr_dict[attr] = True
  136. # for every node in instance model, check if it has the mandatory attributes
  137. for node in mv.all_instances(self._instance_model, "Node"):
  138. node_type = commons.get_node_type(self._instance_model, node)
  139. node_attrs = commons.get_attributes_of_node(self._instance_model, node)
  140. attr_mand = attr_mandatory[node_type]
  141. for attr, mand in attr_mand.iteritems():
  142. if mand:
  143. if not attr in [x.key for x in node_attrs]:
  144. return {"OK": False, "error":"Attribute {} for type {} mandatory".format(attr, node_type),
  145. "affected":[node]}
  146. return {"OK": True, "error": None, "affected":[]}
  147. def verify_associations(self):
  148. """
  149. 1. An edge between two types is mandatory if in every example model that contains nodes of both types, every
  150. node is connected with an edge to the other type.
  151. 2. For every association in the instance model, the types of the source and target must correspond
  152. to the types of an association in some example model. This check should not be necessary since this
  153. is already enforced while instance modeling (see im_scene.py, draw_edge() which checks this when trying
  154. to connect two nodes).
  155. """
  156. # construct the set of mandatory edges
  157. all_edges = set()
  158. for exm in self._example_models:
  159. all_links = mv.all_instances(exm, "Edge")
  160. for link in all_links:
  161. src_id = mv.read_association_source(exm, link)[0]
  162. dest_id = mv.read_association_destination(exm, link)[0]
  163. src_type = commons.get_node_type(exm, src_id)
  164. dest_type = commons.get_node_type(exm, dest_id)
  165. all_edges.add(Edge(src_type, dest_type))
  166. edge_mandatory = {e:True for e in all_edges}
  167. for cand_edge, _ in edge_mandatory.iteritems():
  168. # check every example model if it contains the required types
  169. # and if there are two noes of the type which are not connected -> not mandatory
  170. found = False
  171. for exm in self._example_models:
  172. if found:
  173. break
  174. typed_nodes_a = commons.all_nodes_with_type(exm, cand_edge.n1)
  175. typed_nodes_b = commons.all_nodes_with_type(exm, cand_edge.n2)
  176. if not typed_nodes_a or not typed_nodes_b:
  177. # example model does not contain the two types
  178. continue
  179. for src_node in typed_nodes_a:
  180. # if this node is not connected to a node typed by the required type, the edge is not mandatory
  181. if not commons.has_edge_to_type(exm, src_node, cand_edge.n2):
  182. edge_mandatory[cand_edge] = False
  183. found = True
  184. mandatory_edges = [edge for edge,mandatory in edge_mandatory.iteritems() if mandatory]
  185. # check if instance model contains the types and the edge
  186. for edge in mandatory_edges:
  187. if not commons.model_contains_type(self._instance_model, edge.n1):
  188. continue
  189. if not commons.model_contains_type(self._instance_model, edge.n2):
  190. continue
  191. # instance model contains both types -> are they connected?
  192. for node in commons.all_nodes_with_type(self._instance_model, edge.n1):
  193. if not commons.has_edge_to_type(self._instance_model, node, edge.n2):
  194. return {"OK":False, "error": "Edge between {} and {} mandatory".format(edge.n1, edge.n2),
  195. "affected":[]}
  196. # other way round
  197. for node in commons.all_nodes_with_type(self._instance_model, edge.n2):
  198. if not commons.has_edge_to_type(self._instance_model, node, edge.n1):
  199. return {"OK":False, "error": "Edge between {} and {} mandatory".format(edge.n2, edge.n1),
  200. "affected":[]}
  201. # lastly, check if all edges in the instance model are actually valid
  202. all_edges = mv.all_instances(self._instance_model, "Edge")
  203. for edge in all_edges:
  204. src_id = mv.read_association_source(self._instance_model, edge)[0]
  205. dest_id = mv.read_association_destination(self._instance_model, edge)[0]
  206. src_type = commons.get_node_type(self._instance_model, src_id)
  207. dest_type = commons.get_node_type(self._instance_model, dest_id)
  208. if not commons.is_edge_supported(src_type, dest_type):
  209. return {"OK":False, "error": "Edge between {} and {} not valid".format(edge.n1, edge.n2),
  210. "affected":[src_id, dest_id]}
  211. return {"OK": True, "error": None, "affected":[]}