import sys sys.path.append("../wrappers") from wrappers import modelverse as mv from commons import * class EdgeAdd(object): def __init__(self): self._from_node = "" self._to_node = "" def execute(self, model, from_node, to_node, local): self._from_node = from_node self._to_node = to_node if local: mv.transformation_execute_MANUAL("graph_ops/add_edge", {"gm":model}, {"gm":model}, callback=self._callback) else: from_type = get_node_type(model, self._from_node) to_type = get_node_type(model, self._to_node) for m in all_models(): nodes_from = all_nodes_with_type(m, from_type) nodes_to = all_nodes_with_type(m, to_type) for nf in nodes_from: for nt in nodes_to: self.execute(m, nf, nt, local=True) def _callback(self, model): mv.instantiate(model, "gm/Edge", ("gm/"+self._from_node, "gm/"+self._to_node), ID="e") class EdgeDel(object): def __init__(self): self._edge = "" def execute(self, model, edge, local, check_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 = get_node_type(model, from_node) to_type = 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_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 all_example_models(): nodes_from = all_nodes_with_type(m, from_type) nodes_to = all_nodes_with_type(m, to_type) for nf in nodes_from: for nt in nodes_to: edges = get_associations_between(m, nf, nt) remaining_instances += len(edges) if remaining_instances == 0: for m in all_instance_models(): nodes_from = all_nodes_with_type(m, from_type) nodes_to = all_nodes_with_type(m, to_type) for nf in nodes_from: for nt in nodes_to: edges = get_associations_between(m, nf, nt) for edge in edges: self.execute(m, edge, local=True) else: for m in all_models(): nodes_from = all_nodes_with_type(m, from_type) nodes_to = all_nodes_with_type(m, to_type) for nf in nodes_from: for nt in nodes_to: edges = get_associations_between(m, nf, nt) for edge in edges: self.execute(m, edge, local=True) def _callback(self, model): mv.delete_element(model, "gm/"+self._edge)