123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- 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):
- 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)
- 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)
- def _callback(self, model):
- mv.instantiate(model, "gm/Edge", ("gm/"+self._from_node, "gm/"+self._to_node))
- def repair(self):
- """
- Check if the operation invalidated any instance models by making an edge mandatory.
- If so, repair it by adding the edge there.
- """
- 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)
- class EdgeDel(object):
- def __init__(self):
- self._edge = ""
- self._from_node = ""
- self._to_node = ""
- self._from_type = ""
- self._to_type = ""
- def execute(self, model, edge, local):
- self._edge = edge
- self._from_node = mv.read_association_source(model, edge)[0]
- self._to_node = mv.read_association_destination(model, edge)[0]
- if not self._from_type:
- self._from_type = commons.get_node_type(model, self._from_node)
- if not self._to_type:
- self._to_type = commons.get_node_type(model, self._to_node)
- if local:
- mv.transformation_execute_MANUAL("graph_ops/del_edge", {"gm":model}, {"gm":model},
- callback=self._callback)
- 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:
- edges = commons.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)
- def repair(self):
- """
- Check if this operation invalidated any instance models by leaving an edge untyped.
- If so, delete untyped edges.
- """
- if not commons.is_edge_supported(self._from_type, self._to_type):
- print("Edge between {} and {} not supported anymore, deleting from all instance models ...".format(
- self._from_type, self._to_type))
- 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:
- edges = commons.get_associations_between(im, nf, nt)
- for edge in edges:
- self.execute(im, edge, local=True)
|