[graph] add nodesFromNode method based on reverse dfs visit

Get the whole node chain from a start node to the graph leaves following graph edges
This commit is contained in:
Yann Lanthony 2018-01-08 13:08:55 +01:00
parent 924dbc8d32
commit 516f909db4
2 changed files with 52 additions and 0 deletions

View file

@ -1492,6 +1492,30 @@ class Graph(BaseObject):
flowEdges.append(link)
return flowEdges
def nodesFromNode(self, startNode, filterType=None):
"""
Return the node chain from startNode to the graph leaves.
Args:
startNode (Node): the node to start the visit from.
filterType (str): (optional) only return the nodes of the given type
(does not stop the visit, this is a post-process only)
Returns:
The list of nodes from startNode to the graph leaves following edges.
"""
nodes = []
edges = []
visitor = Visitor()
def discoverVertex(vertex, graph):
if not filterType or vertex.nodeType == filterType:
nodes.append(vertex)
visitor.discoverVertex = discoverVertex
visitor.examineEdge = lambda edge, graph: edges.append(edge)
self.dfs(visitor=visitor, startNodes=[startNode], reverse=True)
return nodes, edges
def _applyExpr(self):
with GraphModification(self):
for node in self._nodes: