import sys sys.path.append("../wrappers") from wrappers import modelverse as mv import commons class EdgeAdd(object): def __init__(self): self._from_node = "" self._to_node = "" self._from_type = "" self._to_type = "" def execute(self, model, from_node, to_node, local, check_if_last=False): self._from_node = from_node self._to_node = to_node if not self._from_type: self._from_type = commons.get_node_type(model, from_node) if not self._to_type: self._to_type = commons.get_node_type(model, to_node) if local: mv.transformation_execute_MANUAL("graph_ops/add_edge", {"gm":model}, {"gm":model}, callback=self._callback) if check_if_last: mandatory_edges = commons.get_mandtory_edges() this_edge = commons.Edge(self._from_type, self._to_type) if this_edge in mandatory_edges: print("Edge {} became mandatory, adding to instance models ...".format(this_edge)) for im in commons.all_instance_models(): nodes_from = commons.all_nodes_with_type(im, self._from_type) nodes_to = commons.all_nodes_with_type(im, self._to_type) for nf in nodes_from: for nt in nodes_to: self.execute(im, nf, nt, local=True, check_if_last=False) else: for m in commons.all_models(): nodes_from = commons.all_nodes_with_type(m, self._from_type) nodes_to = commons.all_nodes_with_type(m, self._to_type) for nf in nodes_from: for nt in nodes_to: self.execute(m, nf, nt, local=True, check_if_last=False) def _callback(self, model): mv.instantiate(model, "gm/Edge", ("gm/"+self._from_node, "gm/"+self._to_node)) class EdgeDel(object): def __init__(self): self._edge = "" def execute(self, model, edge, local, check_if_last=False): self._edge = edge from_node = mv.read_association_source(model, edge)[0] to_node = mv.read_association_destination(model, edge)[0] from_type = commons.get_node_type(model, from_node) to_type = commons.get_node_type(model, to_node) if local: mv.transformation_execute_MANUAL("graph_ops/del_edge", {"gm":model}, {"gm":model}, callback=self._callback) if check_if_last: # check if this association was the last one in all example models # if yes, delete it from all instance models as well to preserve their validity remaining_instances = 0 for m in commons.all_example_models(): nodes_from = commons.all_nodes_with_type(m, from_type) nodes_to = commons.all_nodes_with_type(m, to_type) for nf in nodes_from: for nt in nodes_to: edges = commons.get_associations_between(m, nf, nt) remaining_instances += len(edges) if remaining_instances == 0: for m in commons.all_instance_models(): nodes_from = commons.all_nodes_with_type(m, from_type) nodes_to = commons.all_nodes_with_type(m, to_type) for nf in nodes_from: for nt in nodes_to: edges = commons.get_associations_between(m, nf, nt) for edge in edges: self.execute(m, edge, local=True, check_if_last=False) else: for m in commons.all_models(): nodes_from = commons.all_nodes_with_type(m, from_type) nodes_to = commons.all_nodes_with_type(m, to_type) for nf in nodes_from: for nt in nodes_to: edges = commons.get_associations_between(m, nf, nt) for edge in edges: self.execute(m, edge, local=True, check_if_last=False) def _callback(self, model): mv.delete_element(model, "gm/"+self._edge)