use of org.apache.rya.forwardchain.rule.Ruleset in project incubator-rya by apache.
the class RoundRobinStrategy method getNextRule.
private Rule getNextRule() {
if (activeNow.isEmpty()) {
return null;
}
Ruleset subset = new Ruleset(activeNow);
SortedSet<Rule> sorted = new TreeSet<>(new Comparator<Rule>() {
@Override
public int compare(Rule r1, Rule r2) {
// If one rule triggers the other (directly or indirectly) but
// not the other way around, the one that triggers the other
// should come first.
boolean forwardPath = subset.pathExists(r1, r2);
boolean backwardPath = subset.pathExists(r2, r1);
if (forwardPath && !backwardPath) {
return -1;
}
if (backwardPath && !forwardPath) {
return 1;
}
return 0;
}
}.thenComparingInt(rule -> {
// and defer rules that can be triggered by many remaining rules.
return remainingPredecessors(rule).size() - remainingSuccessors(rule).size();
}).thenComparing(// Fall back on string comparison
Rule::toString));
sorted.addAll(activeNow);
Rule next = sorted.first();
activeNow.remove(next);
return next;
}