Search in sources :

Example 1 with CycleDetector

use of org.jgrapht.alg.cycle.CycleDetector in project pgcodekeeper by pgcodekeeper.

the class DepcyGraph method removeCycles.

private void removeCycles() {
    CycleDetector<PgStatement, DefaultEdge> detector = new CycleDetector<>(graph);
    for (PgStatement st : detector.findCycles()) {
        if (!(st instanceof AbstractPgFunction)) {
            continue;
        }
        for (PgStatement vertex : detector.findCyclesContainingVertex(st)) {
            if (vertex.getStatementType() == DbObjType.COLUMN) {
                graph.removeEdge(st, vertex);
                Log.log(Log.LOG_INFO, MessageFormat.format(REMOVE_DEP, st.getQualifiedName(), vertex.getQualifiedName()));
                PgStatement table = vertex.getParent();
                if (graph.removeEdge(st, table) != null) {
                    Log.log(Log.LOG_INFO, MessageFormat.format(REMOVE_DEP, st.getQualifiedName(), table.getQualifiedName()));
                }
            }
        }
    }
}
Also used : PgStatement(cz.startnet.utils.pgdiff.schema.PgStatement) AbstractPgFunction(cz.startnet.utils.pgdiff.schema.AbstractPgFunction) DefaultEdge(org.jgrapht.graph.DefaultEdge) CycleDetector(org.jgrapht.alg.cycle.CycleDetector)

Example 2 with CycleDetector

use of org.jgrapht.alg.cycle.CycleDetector in project enunciate by stoicflame.

the class Enunciate method buildModuleGraph.

protected Graph<String, DefaultEdge> buildModuleGraph(Map<String, ? extends EnunciateModule> modules) {
    Graph<String, DefaultEdge> graph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
    for (String moduleName : modules.keySet()) {
        graph.addVertex(moduleName);
    }
    for (EnunciateModule module : modules.values()) {
        List<DependencySpec> dependencies = module.getDependencySpecifications();
        if (dependencies != null && !dependencies.isEmpty()) {
            for (DependencySpec dependency : dependencies) {
                for (EnunciateModule other : modules.values()) {
                    if (dependency.accept(other)) {
                        graph.addEdge(other.getName(), module.getName());
                    }
                }
                if (!dependency.isFulfilled()) {
                    throw new EnunciateException(String.format("Unfulfilled dependency %s of module %s.", dependency.toString(), module.getName()));
                }
            }
        }
    }
    for (EnunciateModule module : modules.values()) {
        if (module instanceof DependingModuleAwareModule) {
            Set<DefaultEdge> edges = graph.outgoingEdgesOf(module.getName());
            Set<String> dependingModules = new TreeSet<String>();
            for (DefaultEdge edge : edges) {
                dependingModules.add(graph.getEdgeTarget(edge));
            }
            ((DependingModuleAwareModule) module).acknowledgeDependingModules(dependingModules);
        }
        if (module instanceof ApiRegistryAwareModule) {
            ((ApiRegistryAwareModule) module).setApiRegistry(this.apiRegistry);
        }
    }
    CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<String, DefaultEdge>(graph);
    Set<String> modulesInACycle = cycleDetector.findCycles();
    if (!modulesInACycle.isEmpty()) {
        StringBuilder errorMessage = new StringBuilder("Module cycle detected: ");
        java.util.Iterator<String> subcycle = cycleDetector.findCyclesContainingVertex(modulesInACycle.iterator().next()).iterator();
        while (subcycle.hasNext()) {
            String next = subcycle.next();
            errorMessage.append(next);
            if (subcycle.hasNext()) {
                errorMessage.append(" --> ");
            }
        }
        throw new EnunciateException(errorMessage.toString());
    }
    return graph;
}
Also used : EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) DefaultDirectedGraph(org.jgrapht.graph.DefaultDirectedGraph) DefaultEdge(org.jgrapht.graph.DefaultEdge) ApiRegistryAwareModule(com.webcohesion.enunciate.module.ApiRegistryAwareModule) CycleDetector(org.jgrapht.alg.cycle.CycleDetector) DependingModuleAwareModule(com.webcohesion.enunciate.module.DependingModuleAwareModule) TreeSet(java.util.TreeSet) DependencySpec(com.webcohesion.enunciate.module.DependencySpec)

Example 3 with CycleDetector

use of org.jgrapht.alg.cycle.CycleDetector in project windup by windup.

the class RuleProviderSorter method checkForCycles.

/**
 * Use the jgrapht cycle checker to detect any cycles in the provided dependency graph.
 */
private void checkForCycles(DefaultDirectedWeightedGraph<RuleProvider, DefaultEdge> graph) {
    CycleDetector<RuleProvider, DefaultEdge> cycleDetector = new CycleDetector<>(graph);
    if (cycleDetector.detectCycles()) {
        // if we have cycles, then try to throw an exception with some usable data
        Set<RuleProvider> cycles = cycleDetector.findCycles();
        StringBuilder errorSB = new StringBuilder();
        for (RuleProvider cycle : cycles) {
            errorSB.append("Found dependency cycle involving: " + cycle.getMetadata().getID()).append(System.lineSeparator());
            Set<RuleProvider> subCycleSet = cycleDetector.findCyclesContainingVertex(cycle);
            for (RuleProvider subCycle : subCycleSet) {
                errorSB.append("\tSubcycle: " + subCycle.getMetadata().getID()).append(System.lineSeparator());
            }
        }
        throw new RuntimeException("Dependency cycles detected: " + errorSB.toString());
    }
}
Also used : DefaultEdge(org.jgrapht.graph.DefaultEdge) AbstractRuleProvider(org.jboss.windup.config.AbstractRuleProvider) RuleProvider(org.jboss.windup.config.RuleProvider) CycleDetector(org.jgrapht.alg.cycle.CycleDetector)

Example 4 with CycleDetector

use of org.jgrapht.alg.cycle.CycleDetector in project workflow by pavansharma36.

the class RunnableTaskDagBuilder method build.

private void build(final Task task) {
    final DefaultDirectedGraph<TaskId, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class);
    worker(graph, task, null, new HashSet<>());
    final CycleDetector<TaskId, DefaultEdge> cycleDetector = new CycleDetector<>(graph);
    if (cycleDetector.detectCycles()) {
        throw new WorkflowException("The Task DAG contains cycles: " + task);
    }
    final TopologicalOrderIterator<TaskId, DefaultEdge> orderIterator = new TopologicalOrderIterator<>(graph);
    while (orderIterator.hasNext()) {
        final TaskId taskId = orderIterator.next();
        final Set<DefaultEdge> taskIdEdges = graph.edgesOf(taskId);
        final Set<TaskId> dependencies = taskIdEdges.stream().map(graph::getEdgeSource).filter(edge -> !edge.equals(taskId)).collect(Collectors.toSet());
        final Set<TaskId> childrens = taskIdEdges.stream().map(graph::getEdgeTarget).filter(edge -> !edge.equals(taskId)).collect(Collectors.toSet());
        entries.add(new RunnableTaskDag(taskId, dependencies, childrens));
    }
}
Also used : DefaultDirectedGraph(org.jgrapht.graph.DefaultDirectedGraph) DefaultEdge(org.jgrapht.graph.DefaultEdge) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) List(java.util.List) CycleDetector(org.jgrapht.alg.cycle.CycleDetector) WorkflowException(org.one.workflow.api.util.WorkflowException) Map(java.util.Map) Task(org.one.workflow.api.bean.task.Task) LinkedList(java.util.LinkedList) TaskId(org.one.workflow.api.bean.id.TaskId) TopologicalOrderIterator(org.jgrapht.traverse.TopologicalOrderIterator) TaskId(org.one.workflow.api.bean.id.TaskId) DefaultDirectedGraph(org.jgrapht.graph.DefaultDirectedGraph) WorkflowException(org.one.workflow.api.util.WorkflowException) DefaultEdge(org.jgrapht.graph.DefaultEdge) TopologicalOrderIterator(org.jgrapht.traverse.TopologicalOrderIterator) CycleDetector(org.jgrapht.alg.cycle.CycleDetector)

Aggregations

CycleDetector (org.jgrapht.alg.cycle.CycleDetector)4 DefaultEdge (org.jgrapht.graph.DefaultEdge)4 DefaultDirectedGraph (org.jgrapht.graph.DefaultDirectedGraph)2 ApiRegistryAwareModule (com.webcohesion.enunciate.module.ApiRegistryAwareModule)1 DependencySpec (com.webcohesion.enunciate.module.DependencySpec)1 DependingModuleAwareModule (com.webcohesion.enunciate.module.DependingModuleAwareModule)1 EnunciateModule (com.webcohesion.enunciate.module.EnunciateModule)1 AbstractPgFunction (cz.startnet.utils.pgdiff.schema.AbstractPgFunction)1 PgStatement (cz.startnet.utils.pgdiff.schema.PgStatement)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1 AbstractRuleProvider (org.jboss.windup.config.AbstractRuleProvider)1 RuleProvider (org.jboss.windup.config.RuleProvider)1 TopologicalOrderIterator (org.jgrapht.traverse.TopologicalOrderIterator)1