Search in sources :

Example 6 with ChaseException

use of fr.lirmm.graphik.graal.api.forward_chaining.ChaseException 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 7 with ChaseException

use of fr.lirmm.graphik.graal.api.forward_chaining.ChaseException in project graal by graphik-team.

the class MSAProperty method check.

@Override
public int check(AnalyserRuleSet ruleSet) {
    RuleSet R = translateToMSA(ruleSet);
    AtomSet A = Rules.criticalInstance(ruleSet);
    try {
        StaticChase.executeChase(A, R);
    } catch (ChaseException e) {
        LOGGER.warn("An error occurs during the chase: ", e);
        return 0;
    }
    DefaultConjunctiveQuery Q = new DefaultConjunctiveQuery();
    DefaultAtom q = new DefaultAtom(C);
    q.setTerm(0, FAKE);
    Q.getAtomSet().add(q);
    try {
        if (SmartHomomorphism.instance().exist(Q, A))
            return -1;
        return 1;
    } catch (HomomorphismException e) {
        LOGGER.warn("An error occurs during the homomorphism: ", e);
        return 0;
    }
}
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) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)

Example 8 with ChaseException

use of fr.lirmm.graphik.graal.api.forward_chaining.ChaseException 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 9 with ChaseException

use of fr.lirmm.graphik.graal.api.forward_chaining.ChaseException 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)

Example 10 with ChaseException

use of fr.lirmm.graphik.graal.api.forward_chaining.ChaseException in project graal by graphik-team.

the class DefaultKnowledgeBase method saturate.

@Override
public void saturate() throws KnowledgeBaseException {
    if (!this.isSaturated) {
        boolean run = this.approach == Approach.SATURATION_ONLY;
        if (!run) {
            this.analyse();
            run = this.analyse.isFES();
        }
        if (run) {
            GraphOfRuleDependencies grd = this.analysedRuleSet.getGraphOfRuleDependencies();
            ChaseWithGRD<AtomSet> chase = new ChaseWithGRD<>(grd, this.store);
            chase.setProfiler(this.getProfiler());
            try {
                chase.execute();
            } catch (ChaseException e) {
                throw new KnowledgeBaseException(e);
            }
            this.isSaturated = true;
            this.isFESSaturated = true;
            this.isSemiSaturated = true;
        } else {
            throw new KnowledgeBaseException("There is no proof for FES decidability");
        }
    }
}
Also used : AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) ChaseWithGRD(fr.lirmm.graphik.graal.forward_chaining.ChaseWithGRD) KnowledgeBaseException(fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException) GraphOfRuleDependencies(fr.lirmm.graphik.graal.api.core.GraphOfRuleDependencies) DefaultGraphOfRuleDependencies(fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)

Aggregations

ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)13 Atom (fr.lirmm.graphik.graal.api.core.Atom)6 Rule (fr.lirmm.graphik.graal.api.core.Rule)6 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)5 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)5 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)5 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)5 Chase (fr.lirmm.graphik.graal.api.forward_chaining.Chase)4 KnowledgeBaseException (fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException)4 AnalyserRuleSet (fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet)4 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)3 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)3 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)3 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)3 DefaultGraphOfRuleDependencies (fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies)3 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)3 GraphOfRuleDependencies (fr.lirmm.graphik.graal.api.core.GraphOfRuleDependencies)2 UnionOfConjunctiveQueries (fr.lirmm.graphik.graal.api.core.UnionOfConjunctiveQueries)2 RuleApplicationException (fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException)2 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)2