Search in sources :

Example 1 with Ruleset

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;
}
Also used : Ruleset(org.apache.rya.forwardchain.rule.Ruleset) TreeSet(java.util.TreeSet) Rule(org.apache.rya.forwardchain.rule.Rule)

Aggregations

TreeSet (java.util.TreeSet)1 Rule (org.apache.rya.forwardchain.rule.Rule)1 Ruleset (org.apache.rya.forwardchain.rule.Ruleset)1