use of fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException 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());
}
}
use of fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException 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());
}
}
use of fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException 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");
}
}
}
use of fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException in project graal by graphik-team.
the class DefaultKnowledgeBase method semiSaturate.
@Override
public void semiSaturate() throws KnowledgeBaseException {
if (!this.isSemiSaturated) {
this.compileRule();
Chase chase = new BreadthFirstChase(this.ruleCompilation.getSaturation(), this.store);
chase.setProfiler(this.getProfiler());
try {
chase.next();
} catch (ChaseException e) {
throw new KnowledgeBaseException(e);
}
this.isSemiSaturated = true;
}
}
use of fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException in project graal by graphik-team.
the class DefaultKnowledgeBase method isConsistent.
@Override
public boolean isConsistent() throws KnowledgeBaseException {
try {
CloseableIterator<Substitution> results = this.query(DefaultConjunctiveQueryFactory.instance().BOOLEAN_BOTTOM_QUERY);
boolean res = !results.hasNext();
results.close();
return res;
} catch (IteratorException e) {
throw new KnowledgeBaseException(e);
}
}
Aggregations