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()));
}
}
}
}
}
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;
}
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());
}
}
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));
}
}
Aggregations