[core] Graph: add getRootNodes()

This commit is contained in:
Julien-Haudegond 2020-09-04 15:21:03 +02:00
parent 44d019b7f0
commit 8499431dff

View file

@ -578,10 +578,14 @@ class Graph(BaseObject):
def edge(self, dstAttributeName):
return self._edges.get(dstAttributeName)
def getLeaves(self):
def getLeafNodes(self):
nodesWithOutput = set([edge.src.node for edge in self.edges])
return set(self._nodes) - nodesWithOutput
def getRootNodes(self):
nodesWithInput = set([edge.dst.node for edge in self.edges])
return set(self._nodes) - nodesWithInput
@changeTopology
def addEdge(self, srcAttr, dstAttr):
assert isinstance(srcAttr, Attribute)
@ -661,7 +665,7 @@ class Graph(BaseObject):
# it is not possible to handle this case at the moment
raise NotImplementedError("Graph.dfs(): longestPathFirst=True and reverse=True are not compatible yet.")
nodes = startNodes or self.getLeaves()
nodes = startNodes or self.getLeafNodes()
if longestPathFirst:
# Graph topology must be known and node depths up-to-date
@ -859,7 +863,7 @@ class Graph(BaseObject):
# propagate inputVertex computability
self._computationBlocked[currentVertex] |= self._computationBlocked[inputVertex]
leaves = self.getLeaves()
leaves = self.getLeafNodes()
visitor.finishEdge = finishEdge
visitor.discoverVertex = discoverVertex
self.dfs(visitor=visitor, startNodes=leaves)