Search in sources :

Example 1 with Rule

use of org.apache.rya.forwardchain.rule.Rule 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)

Example 2 with Rule

use of org.apache.rya.forwardchain.rule.Rule in project incubator-rya by apache.

the class RoundRobinStrategy method executeNext.

@Override
public long executeNext() throws ForwardChainException {
    if (!initialized.get()) {
        return 0;
    }
    Rule rule = getNextRule();
    if (rule == null) {
        return 0;
    }
    StatementMetadata metadata = new StatementMetadata();
    metadata.addMetadata(ForwardChainConstants.RYA_DERIVATION_TIME, new RyaType(XMLSchema.INT, Integer.toString(iteration)));
    long inferences = rule.execute(ruleStrategy, metadata);
    inferencesThisIteration += inferences;
    if (inferences > 0) {
        for (Rule successor : ruleset.getSuccessorsOf(rule)) {
            // it may not need  to be checked in the next one.
            if (!activeNow.contains(successor)) {
                activeNextIteration.add(successor);
            }
        }
    }
    prepareQueue();
    return inferences;
}
Also used : StatementMetadata(org.apache.rya.api.domain.StatementMetadata) Rule(org.apache.rya.forwardchain.rule.Rule) RyaType(org.apache.rya.api.domain.RyaType)

Aggregations

Rule (org.apache.rya.forwardchain.rule.Rule)2 TreeSet (java.util.TreeSet)1 RyaType (org.apache.rya.api.domain.RyaType)1 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)1 Ruleset (org.apache.rya.forwardchain.rule.Ruleset)1