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 the jgrapht cycle checker to detect any cycles in the provided dependency graph .