@@ -30,15 +30,15 @@ public class DagTraversalTask<T> {
3030 private final Dag <T > dag ;
3131 private final Consumer <T > task ;
3232 private final ListeningExecutorService executorService ;
33- private final Map <T , Set <T >> parents ;
33+ private final Map <T , Set <T >> outgoingNodes ;
3434 private final Lock lock ;
3535 private final AtomicBoolean failed ;
3636
3737 /**
3838 * Create a task that traverses a DAG with an {@link java.util.concurrent.ExecutorService}
3939 * <p>
40- * The nodes will be traversed in reverse- topological order,
41- * such that no node is visited until all its children have been visited.
40+ * The nodes will be traversed in topological order,
41+ * such that no node is visited until all its incoming nodes have been visited.
4242 *
4343 * @param dag the DAG to traverse
4444 * @param task the task to apply to each node
@@ -49,21 +49,21 @@ public DagTraversalTask(Dag<T> dag, Consumer<T> task, ExecutorService executorSe
4949 this .dag = dag .clone ();
5050 this .task = task ;
5151 this .executorService = MoreExecutors .listeningDecorator (executorService );
52- this .parents = new HashMap <>();
52+ this .outgoingNodes = new HashMap <>();
5353 this .lock = new ReentrantLock (true );
5454 this .failed = new AtomicBoolean ();
5555
56- // Cache the parents of each node for this DAG
57- this .dag .getNodes ().forEach (node -> this .parents .put (node , this .dag .getIncoming (node )));
56+ // Cache each node's outgoing nodes for this DAG
57+ this .dag .getNodes ().forEach (node -> this .outgoingNodes .put (node , this .dag .getOutgoing (node )));
5858
5959 // Get the set of leaves for this dag
60- Set <T > leaves = this .dag .getLeaves ();
60+ Set <T > roots = this .dag .getRoots ();
6161
6262 // If there are no leaves, then there are no nodes to visit
63- if (leaves .isEmpty ()) {
63+ if (roots .isEmpty ()) {
6464 executorService .shutdown ();
6565 } else {
66- visit (leaves );
66+ visit (roots );
6767 }
6868
6969 }
@@ -101,13 +101,13 @@ private void visit(Collection<T> nodes) {
101101 executorService .shutdown ();
102102 }
103103
104- Set <T > parents = this .parents .get (node );
105- parents .retainAll (dag .getNodes ());
106- parents .removeIf (p -> !dag .getOutgoing (p ).isEmpty ());
104+ Set <T > outgoing = this .outgoingNodes .get (node );
105+ outgoing .retainAll (dag .getNodes ());
106+ outgoing .removeIf (p -> !dag .getIncoming (p ).isEmpty ());
107107
108108 lock .unlock ();
109109
110- visit (parents );
110+ visit (outgoing );
111111
112112 }, MoreExecutors .directExecutor ());
113113 } catch (Throwable ignore ) {
0 commit comments