Skip to content

Commit

Permalink
introduced hidden edges to not create cycles in the graph (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
claxn committed Jun 29, 2020
1 parent 1b6bdc2 commit eb6ad11
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/openeo_pg_parser/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ def __eq__(self, other):
return self.id == other.id


class Edge(object):
class Edge:
""" An edge connects two nodes. The connection has a label/name. """

def __init__(self, id=None, name=None, nodes=None):
def __init__(self, id=None, name=None, nodes=None, hidden=False):
"""
Constructor of `graph.Edge`.
Expand All @@ -219,6 +219,8 @@ def __init__(self, id=None, name=None, nodes=None):
Name of the edge/connection.
nodes : list of graph.Nodes, optional
List containing two nodes comprising an edge.
hidden : bool, optional
True if edge should be ignored, e.g. for sorting (defaults to False).
"""

if nodes is not None:
Expand All @@ -230,6 +232,7 @@ def __init__(self, id=None, name=None, nodes=None):
self.id = id
self.name = name
self.nodes = nodes
self.hidden = hidden

@property
def node_ids(self):
Expand All @@ -256,7 +259,7 @@ def __eq__(self, other):
(self.name == other.name)


class Graph(object):
class Graph:
""" Represents an arbitrary graph containing `graph.Node` instances as nodes. """

def __init__(self, nodes):
Expand Down Expand Up @@ -585,7 +588,7 @@ def to_igraph(self):
edges = []
for node in self.nodes:
for edge in node.edges:
if edge not in edges:
if not edge.hidden and edge not in edges:
# ignore nodes, which are not contained in the graph
if edge.nodes[0].id not in self.ids or edge.nodes[1].id not in self.ids:
continue
Expand Down
8 changes: 5 additions & 3 deletions src/openeo_pg_parser/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def adjust_from_nodes(process_graph):
return process_graph


def create_edge(node_from, node_to, name="data"):
def create_edge(node_from, node_to, name="data", hidden=False):
"""
Creates a directed edge of type `graph.Edge` between the nodes `node_from` and `node_to`.
Expand All @@ -158,6 +158,8 @@ def create_edge(node_from, node_to, name="data"):
End node of the edge.
name : str, optional
Name of the edge (default is "data")
hidden : bool, optional
True if edge should be ignored, e.g. for sorting (defaults to False).
Returns
-------
Expand All @@ -167,7 +169,7 @@ def create_edge(node_from, node_to, name="data"):
"""
edge_nodes = [node_from, node_to]
edge_id = "_".join([edge_node.id for edge_node in edge_nodes])
edge = Edge(id=edge_id, name=name, nodes=edge_nodes)
edge = Edge(id=edge_id, name=name, nodes=edge_nodes, hidden=hidden)
node_to.add_edge(edge)
node_from.add_edge(edge)

Expand Down Expand Up @@ -212,7 +214,7 @@ def adjust_from_parameters(process_graph, parameters=None):
node_relatives = parent_node.relatives(link="data", ancestor=True)
# parameter is required, but the start node has no input data -> take parent node as data reference
if parameter.is_required and not node_relatives:
create_edge(parent_node, node)
create_edge(parent_node, node, hidden=True)
node_argument = {'from_node': '{}'.format(parent_node.id)}
set_obj_elem_from_keys(node.content['arguments'], key_lineage[:-1], node_argument)
# parameter is required and parent node has input data -> add all data relatives of parent node later
Expand Down

0 comments on commit eb6ad11

Please sign in to comment.