verifier.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 in every example model.
  54. More specifically, if a node type appears occ = [n0, n1, ... n_i] times in example model i,
  55. it needs to occur min(occ) in the instance model.
  56. """
  57. total_occurences = {k: [] for k in self._available_types}
  58. for node, occ_list in total_occurences.iteritems():
  59. for exm in self._example_models:
  60. num_occ = commons.count_occurences(node, exm)
  61. occ_list.append(num_occ)
  62. elem_cardinality = {k: min(v) for k, v in total_occurences.iteritems()}
  63. for el, min_occ in elem_cardinality.iteritems():
  64. if not commons.count_occurences(el, self._instance_model) >= min_occ:
  65. return {"OK": False, "error": "Node with type {} needs to occur at least {} times".format(el, min_occ)}
  66. return {"OK": True, "error": None, "affected":[]}
  67. def _get_attributes_of_all_types(self):
  68. """
  69. Helper for attribute check that returns a dictionary of the form {type:[attr_key_1, attr_key_2, ...], ...},
  70. listing all attributes of every type from example models
  71. """
  72. attrs_of_types = {node_type:[] for node_type in self._available_types}
  73. for exm in self._example_models:
  74. for node_type, attrs in attrs_of_types.iteritems():
  75. attrs_of_type_in_exm = [x.key for x in commons.get_all_attributes_of_type(exm, node_type)]
  76. for attr in attrs_of_type_in_exm:
  77. if not attr in attrs:
  78. attrs.append(attr)
  79. return attrs_of_types
  80. def _is_attribute_mandatory_in(self, model, node_type, attr_key):
  81. """
  82. Helper for attribute check that returns True if the attribute attr_key of type node_type is mandatory (i.e
  83. occurs in every node of the type) in model.
  84. """
  85. nodes_of_type = commons.all_nodes_with_type(model, node_type)
  86. attrs_of_type = commons.get_all_attributes_of_type(model, node_type)
  87. ctr = 0
  88. for attr in attrs_of_type:
  89. if attr.key == attr_key:
  90. ctr += 1
  91. return len(nodes_of_type) == ctr
  92. def _is_attribute_mandatory(self, node_type, attr_key):
  93. """
  94. Helper for attribute check that returns True if the attribute attr_key of type node_type is mandatory by
  95. looking at all example models.
  96. """
  97. mand_list = []
  98. for exm in self._example_models:
  99. is_mandatory = self._is_attribute_mandatory_in(exm, node_type, attr_key)
  100. mand_list.append(is_mandatory)
  101. return all(mand_list)
  102. def verify_attributes(self):
  103. """
  104. 1. For every attribute key of a typed node in the instance model, there must be a corresponding
  105. attribute key in some example model.
  106. 2. An attribute for a type is mandatory if it occurs in every example model.
  107. """
  108. # Check attribute keys for every node in instance model
  109. all_nodes = mv.all_instances(self._instance_model, "Node")
  110. for node in all_nodes:
  111. attrs = commons.get_attributes_of_node(self._instance_model, node)
  112. if not attrs:
  113. continue
  114. node_typ = commons.get_node_type(self._instance_model, node)
  115. for attr in attrs:
  116. # check every example model if such an attribute key is present for the specific type
  117. for exm in self._example_models:
  118. exm_attrs = commons.get_all_attributes_of_type(exm, node_typ)
  119. if attr.key in [x.key for x in exm_attrs]:
  120. break
  121. else:
  122. # loop completed without break, so we did not find any key in example models
  123. return {"OK": False, "error": "No key {} for type {} in example models".format(attr.key, node_typ),
  124. "affected":[node]}
  125. # Check if mandatory attributes are present in instance model
  126. attr_mandatory = {node_type:{} for node_type in self._available_types}
  127. all_attrs = self._get_attributes_of_all_types()
  128. for typ_i, attr_list in all_attrs.iteritems():
  129. for typ_j, dic in attr_mandatory.iteritems():
  130. if typ_j == typ_i:
  131. for attr in attr_list:
  132. dic[attr] = False
  133. # have dict like {"PC": {"owner":False}, "Router": {"IP":False}, ...}
  134. # now need to check which one is mandatory and set the boolean accordingly
  135. for node_type, attr_dict in attr_mandatory.iteritems():
  136. for attr, _ in attr_dict.iteritems():
  137. if self._is_attribute_mandatory(node_type, attr):
  138. attr_dict[attr] = True
  139. # for every node in instance model, check if it has the mandatory attributes
  140. for node in mv.all_instances(self._instance_model, "Node"):
  141. node_type = commons.get_node_type(self._instance_model, node)
  142. node_attrs = commons.get_attributes_of_node(self._instance_model, node)
  143. attr_mand = attr_mandatory[node_type]
  144. for attr, mand in attr_mand.iteritems():
  145. if mand:
  146. if not attr in [x.key for x in node_attrs]:
  147. return {"OK": False, "error":"Attribute {} for type {} mandatory".format(attr, node_type),
  148. "affected":[node]}
  149. return {"OK": True, "error": None, "affected":[]}
  150. def verify_associations(self):
  151. """
  152. 1. If an association between two types is present in all example models, it is mandatory and must
  153. therefore be present in the instance model (if it contains the same two types).
  154. 2. For every association in the instance model, the types of the source and target must correspond
  155. to the types of an association in some example model. This check should not be necessary since this
  156. is already enforced while instance modeling (see im_scene.py, draw_edge() which checks this when trying
  157. to connect two nodes).
  158. """
  159. # get a set of mandatory edges (= edges that occur in every example model)
  160. all_edges = {model: set() for model in self._example_models}
  161. for exm in self._example_models:
  162. all_links = mv.all_instances(exm, "Edge")
  163. for link in all_links:
  164. src_id = mv.read_association_source(exm, link)[0]
  165. dest_id = mv.read_association_destination(exm, link)[0]
  166. src_type = commons.get_node_type(exm, src_id)
  167. dest_type = commons.get_node_type(exm, dest_id)
  168. all_edges[exm].add(Edge(src_type, dest_type))
  169. mandatory_edges = set.intersection(*all_edges.values())
  170. # check if instance model contains the types and the edge
  171. for edge in mandatory_edges:
  172. if not commons.model_contains_type(self._instance_model, edge.n1):
  173. continue
  174. if not commons.model_contains_type(self._instance_model, edge.n2):
  175. continue
  176. # instance model contains both types
  177. for node in commons.all_nodes_with_type(self._instance_model, edge.n1):
  178. if not commons.has_edge_to_type(self._instance_model, node, edge.n2):
  179. return {"OK":False, "error": "Edge between {} and {} mandatory".format(edge.n1, edge.n2),
  180. "affected":[]}
  181. # lastly, check if all edges in the instance model are actually supported
  182. all_edges = mv.all_instances(self._instance_model, "Edge")
  183. for edge in all_edges:
  184. src_id = mv.read_association_source(self._instance_model, edge)[0]
  185. dest_id = mv.read_association_destination(self._instance_model, edge)[0]
  186. src_type = commons.get_node_type(self._instance_model, src_id)
  187. dest_type = commons.get_node_type(self._instance_model, dest_id)
  188. if not commons.is_edge_supported(src_type, dest_type):
  189. return {"OK":False, "error": "Edge between {} and {} not supported".format(edge.n1, edge.n2),
  190. "affected":[src_id, dest_id]}
  191. return {"OK": True, "error": None, "affected":[]}