Search in sources :

Example 11 with PureRewriter

use of fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter in project graal by graphik-team.

the class BackwardChainingTest method queriesCover.

@Theory
public void queriesCover(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X) :- r(Y, X)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- p(X), r(a,X).");
    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(1, 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 12 with PureRewriter

use of fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter in project graal by graphik-team.

the class BackwardChainingTest method issue22.

/**
 * Given p(X,Y) :- q(X,Y). q(X,Y) :- a(X), p(X,Y). Then rewrite(?(X) :-
 * q(X,Y), p(Y,Z).) Return ?(X) :- q(X,Y), p(Y,Z). ?(X) :- a(X), p(X,Y),
 * p(Y,Z). ?(X) :- q(X,Y), q(Y,Z). ?(X) :- a(X), p(X,Y), q(Y,Z).
 *
 * @param compilation
 * @param operator
 * @throws IteratorException
 * @throws ParseException
 */
@Theory
public void issue22(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) :- a(X), p(X,Y)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- q(X,Y), p(Y,Z).");
    compilation.compile(rules.iterator());
    PureRewriter bc = new PureRewriter(operator, true);
    CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
    int i = 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 13 with PureRewriter

use of fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter 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)

Example 14 with PureRewriter

use of fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter in project graal by graphik-team.

the class DefaultKnowledgeBase method query.

@Override
public CloseableIterator<Substitution> query(Query query) throws KnowledgeBaseException {
    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;
        this.analyse();
        if (this.analyse.isDecidable() && (!this.getApproach().equals(Approach.REWRITING_ONLY) || this.getFESRuleSet().isEmpty()) && (!this.getApproach().equals(Approach.SATURATION_ONLY) || this.getFUSRuleSet().isEmpty())) {
            try {
                this.fesSaturate();
                this.compileRule();
                RuleSet fusRuleSet = this.getFUSRuleSet();
                PureRewriter pure = new PureRewriter(false);
                CloseableIteratorWithoutException<ConjunctiveQuery> it = pure.execute(cq, fusRuleSet, this.ruleCompilation);
                UnionOfConjunctiveQueries ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
                CloseableIterator<Substitution> resultIt = null;
                try {
                    if (this.isSemiSaturated) {
                        resultIt = SmartHomomorphism.instance().execute(query, this.store);
                    } else {
                        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)

Aggregations

ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)14 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)14 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)14 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)14 Theory (org.junit.experimental.theories.Theory)12 Atom (fr.lirmm.graphik.graal.api.core.Atom)3 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)3 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)3 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)2 Term (fr.lirmm.graphik.graal.api.core.Term)2 UnionOfConjunctiveQueries (fr.lirmm.graphik.graal.api.core.UnionOfConjunctiveQueries)2 ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)2 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)2 KnowledgeBaseException (fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException)2 DefaultUnionOfConjunctiveQueries (fr.lirmm.graphik.graal.core.DefaultUnionOfConjunctiveQueries)2 AnalyserRuleSet (fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet)2 CloseableIterator (fr.lirmm.graphik.util.stream.CloseableIterator)2 UniqFilter (fr.lirmm.graphik.util.stream.filter.UniqFilter)2 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)1 ParseException (fr.lirmm.graphik.graal.api.io.ParseException)1