Java Code Examples for org.jgrapht.alg.CycleDetector#detectCycles()

The following examples show how to use org.jgrapht.alg.CycleDetector#detectCycles() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DagChecker.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
private boolean tryAdd(Set<? extends T> roots, Graph<T, Pair> graph, Set<T> knownNodes,
                       CycleDetector<T, Pair> cycleDetector, Deque<T> nodePath)
{
    for (T node : roots) {
        nodePath.addLast(node);
        graph.addVertex(node);
        if (knownNodes.add(node)) {
            Set<? extends T> nodesFrom = nodesFrom(node);
            for (T from : nodesFrom) {
                graph.addVertex(from);
                Pair edge = new Pair(from, node);
                graph.addEdge(from, node, edge);
                nodePath.addLast(from);
                if (cycleDetector.detectCycles())
                    return false;
                nodePath.removeLast();
            }
            if (!tryAdd(nodesFrom, graph, knownNodes, cycleDetector, nodePath))
                return false;
        }
        nodePath.removeLast();
    }
    return true;
}
 
Example 2
Source File: RunnableTaskDagBuilder.java    From workflow with Apache License 2.0 6 votes vote down vote up
private void build(Task task, ImmutableList.Builder<RunnableTaskDag> entriesBuilder, ImmutableMap.Builder<TaskId, Task> tasksBuilder)
{
    DefaultDirectedGraph<TaskId, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class);
    worker(graph, task, null, tasksBuilder, Sets.newHashSet());

    CycleDetector<TaskId, DefaultEdge> cycleDetector = new CycleDetector<>(graph);
    if ( cycleDetector.detectCycles() )
    {
        throw new RuntimeException("The Task DAG contains cycles: " + task);
    }

    TopologicalOrderIterator<TaskId, DefaultEdge> orderIterator = new TopologicalOrderIterator<>(graph);
    while ( orderIterator.hasNext() )
    {
        TaskId taskId = orderIterator.next();
        Set<DefaultEdge> taskIdEdges = graph.edgesOf(taskId);
        Set<TaskId> processed = taskIdEdges
            .stream()
            .map(graph::getEdgeSource)
            .filter(edge -> !edge.equals(taskId) && !edge.getId().equals(""))
            .collect(Collectors.toSet());
        entriesBuilder.add(new RunnableTaskDag(taskId, processed));
    }
}
 
Example 3
Source File: GraphUtilities.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public static boolean isCyclic(DirectedGraph G) {
	CycleDetector cd = new CycleDetector(G);
	return cd.detectCycles();
}