Search in sources :

Example 26 with RuleSet

use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.

the class BackwardChainingTest method Test2.

/**
 * Test 2
 *
 * @throws IteratorException
 * @throws ParseException
 */
@Theory
public void Test2(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X,Y) :- q(X,Y)."));
    rules.add(DlgpParser.parseRule("q(X,Y) :- p(Y,X)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y) :- p(X,Y).");
    compilation.compile(rules.iterator());
    PureRewriter bc = new PureRewriter(operator, true);
    CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
    int i = Iterators.count(it);
    Assert.assertEquals(4, i);
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Example 27 with RuleSet

use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.

the class RuleSetTest method test.

@Test
public void test() {
    RuleSet rs = new LinkedListRuleSet();
    rs.add(rule1);
    rs.add(rule2);
    Iterator<Rule> it = rs.iterator();
    Assert.assertTrue(it.hasNext());
    it.next();
    Assert.assertTrue(it.hasNext());
}
Also used : LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) Test(org.junit.Test)

Example 28 with RuleSet

use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.

the class IndexedByHeadPredicatesRuleSet method add.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private void add(Predicate p, Rule r) {
    RuleSet rules = this.map.get(p);
    if (rules == null) {
        rules = new LinkedListRuleSet();
        this.map.put(p, rules);
    }
    rules.add(r);
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet)

Example 29 with RuleSet

use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.

the class MFAProperty method check.

/**
 */
@Override
public int check(AnalyserRuleSet ruleSet) {
    RuleSet R = translateToMFA(ruleSet);
    AtomSet A = Rules.criticalInstance(ruleSet);
    Chase chase = new ChaseWithGRD<AtomSet>(new DefaultGraphOfRuleDependencies(R), A, new DefaultRuleApplier<AtomSet>(new FrontierRestrictedChaseHaltingCondition()));
    DefaultConjunctiveQuery Q = new DefaultConjunctiveQuery();
    DefaultAtom q = new DefaultAtom(C);
    q.setTerm(0, FAKE);
    Q.getAtomSet().add(q);
    try {
        while (chase.hasNext()) {
            chase.next();
            if (SmartHomomorphism.instance().exist(Q, A)) {
                return -1;
            }
        }
    } catch (ChaseException e) {
        LOGGER.warn("An error occurs during the chase: ", e);
        return 0;
    } catch (HomomorphismException e) {
        LOGGER.warn("An error occurs during the homomorphism: ", e);
        return 0;
    }
    return 1;
}
Also used : AnalyserRuleSet(fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet) RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) FrontierRestrictedChaseHaltingCondition(fr.lirmm.graphik.graal.forward_chaining.halting_condition.FrontierRestrictedChaseHaltingCondition) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) DefaultGraphOfRuleDependencies(fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) ChaseWithGRD(fr.lirmm.graphik.graal.forward_chaining.ChaseWithGRD) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) Chase(fr.lirmm.graphik.graal.api.forward_chaining.Chase)

Example 30 with RuleSet

use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.

the class DefaultKnowledgeBase method query.

@Override
public CloseableIterator<Substitution> query(Query query, long timeout) throws KnowledgeBaseException, TimeoutException {
    long remainingTime = timeout;
    if (this.isSaturated) {
        try {
            return SmartHomomorphism.instance().execute(query, this.store);
        } catch (HomomorphismException e) {
            throw new KnowledgeBaseException(e);
        }
    } else if (query instanceof ConjunctiveQuery) {
        ConjunctiveQuery cq = (ConjunctiveQuery) query;
        if (this.approach != Approach.REWRITING_ONLY && this.approach != Approach.SATURATION_ONLY) {
            this.analyse();
        }
        if (this.approach == Approach.REWRITING_ONLY || this.approach == Approach.SATURATION_ONLY || this.analyse.isDecidable()) {
            try {
                long time = System.currentTimeMillis();
                this.fesSaturate(remainingTime);
                if (timeout > 0) {
                    remainingTime -= (System.currentTimeMillis() - time);
                    if (remainingTime <= 0) {
                        throw new TimeoutException(timeout);
                    }
                }
                this.compileRule();
                RuleSet fusRuleSet = this.getFUSRuleSet();
                PureRewriter pure = new PureRewriter(false);
                CloseableIteratorWithoutException<ConjunctiveQuery> it = pure.execute(cq, fusRuleSet, this.ruleCompilation, remainingTime);
                UnionOfConjunctiveQueries ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
                CloseableIterator<Substitution> resultIt = null;
                try {
                    resultIt = SmartHomomorphism.instance().execute(ucq, this.store, this.ruleCompilation);
                } catch (HomomorphismException e) {
                    if (this.getApproach().equals(Approach.REWRITING_FIRST)) {
                        it = PureRewriter.unfold(ucq, this.ruleCompilation);
                        ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
                    } else {
                        this.semiSaturate();
                    }
                    resultIt = SmartHomomorphism.instance().execute(ucq, this.store);
                }
                return new FilterIterator<Substitution, Substitution>(resultIt, new UniqFilter<Substitution>());
            } catch (ChaseException e) {
                throw new KnowledgeBaseException(e);
            } catch (HomomorphismException e) {
                throw new KnowledgeBaseException(e);
            }
        } else {
            throw new KnowledgeBaseException("No decidable combinaison found with the defined approach: " + this.getApproach());
        }
    } else {
        throw new KnowledgeBaseException("No implementation found for this kind of query: " + query.getClass());
    }
}
Also used : DefaultUnionOfConjunctiveQueries(fr.lirmm.graphik.graal.core.DefaultUnionOfConjunctiveQueries) AnalyserRuleSet(fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet) RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) CloseableIterator(fr.lirmm.graphik.util.stream.CloseableIterator) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) KnowledgeBaseException(fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException) UniqFilter(fr.lirmm.graphik.util.stream.filter.UniqFilter) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) DefaultUnionOfConjunctiveQueries(fr.lirmm.graphik.graal.core.DefaultUnionOfConjunctiveQueries) UnionOfConjunctiveQueries(fr.lirmm.graphik.graal.api.core.UnionOfConjunctiveQueries) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) TimeoutException(fr.lirmm.graphik.graal.api.util.TimeoutException)

Aggregations

RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)39 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)37 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)22 Theory (org.junit.experimental.theories.Theory)22 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)14 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)14 RulesCompilation (fr.lirmm.graphik.graal.api.core.RulesCompilation)13 Atom (fr.lirmm.graphik.graal.api.core.Atom)11 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)11 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)10 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)9 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)9 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)7 AnalyserRuleSet (fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet)7 Test (org.junit.Test)7 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)6 HomomorphismWithCompilation (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation)6 Rule (fr.lirmm.graphik.graal.api.core.Rule)5 ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)5 TripleStore (fr.lirmm.graphik.graal.api.store.TripleStore)5