private void fillDag(Collection<? extends Task> tasks) {
Set<Task> visiting = new HashSet<Task>();
List<Task> queue = new ArrayList<Task>();
queue.addAll(tasks);
CachingTaskDependencyResolveContext context = new CachingTaskDependencyResolveContext();
while (!queue.isEmpty()) {
Task task = queue.get(0);
if (!filter.isSatisfiedBy(task)) {
// Filtered - skip
queue.remove(0);
continue;
}
if (executionPlan.contains(task)) {
// Already in plan - skip
queue.remove(0);
continue;
}
if (visiting.add(task)) {
// Have not seen this task before - add its dependencies to the head of the queue and leave this
// task in the queue
Set<Task> dependsOnTasks = new TreeSet<Task>(Collections.reverseOrder());
dependsOnTasks.addAll(context.getDependencies(task));
for (Task dependsOnTask : dependsOnTasks) {
if (visiting.contains(dependsOnTask)) {
throw new CircularReferenceException(String.format(
"Circular dependency between tasks. Cycle includes [%s, %s].", task, dependsOnTask));
}