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) 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.core.RuleSet 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;
}
}
use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.
the class MSAProperty method translateToMSA.
public static RuleSet translateToMSA(Iterable<Rule> rules) {
RuleSet R = new LinkedListRuleSet();
for (Rule r : rules) {
for (Rule r2 : translateRuleToMSA(r)) R.add(r2);
}
DefaultRule rule = new DefaultRule();
Atom s = new DefaultAtom(S);
s.setTerm(0, DefaultTermFactory.instance().createVariable("X1"));
s.setTerm(1, DefaultTermFactory.instance().createVariable("X2"));
Atom d = new DefaultAtom(D);
d.setTerm(0, DefaultTermFactory.instance().createVariable("X1"));
d.setTerm(1, DefaultTermFactory.instance().createVariable("X2"));
rule.getBody().add(s);
rule.getHead().add(d);
R.add(rule);
s = new DefaultAtom(S);
d = new DefaultAtom(D);
Atom d2 = new DefaultAtom(D);
d.setTerm(0, DefaultTermFactory.instance().createVariable("X1"));
d.setTerm(1, DefaultTermFactory.instance().createVariable("X2"));
s.setTerm(0, DefaultTermFactory.instance().createVariable("X2"));
s.setTerm(1, DefaultTermFactory.instance().createVariable("X3"));
d2.setTerm(0, DefaultTermFactory.instance().createVariable("X1"));
d2.setTerm(1, DefaultTermFactory.instance().createVariable("X3"));
rule = new DefaultRule();
rule.getBody().add(d);
rule.getBody().add(s);
rule.getHead().add(d2);
R.add(rule);
return R;
}
use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.
the class BackwardChainingTest method getBody1.
/**
* c(X) :- b(X,Y,Y).
*
* getBody([X]) => b(X, Y, Y).
*
* @throws ParseException
*/
@Theory
public void getBody1(RulesCompilation compilation, RewritingOperator operator) throws ParseException {
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("p(X) :- q(X,Y,Y)."));
ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- p(X).");
compilation.compile(rules.iterator());
PureRewriter bc = new PureRewriter(operator, true);
CloseableIteratorWithoutException<? extends ConjunctiveQuery> rewIt = bc.execute(query, rules, compilation);
boolean isFound = false;
while (rewIt.hasNext()) {
ConjunctiveQuery rew = rewIt.next();
CloseableIteratorWithoutException<Atom> it = rew.getAtomSet().iterator();
if (it.hasNext()) {
Atom a = it.next();
if (a.getPredicate().equals(new Predicate("q", 3))) {
isFound = true;
List<Term> terms = a.getTerms();
Assert.assertEquals(terms.get(1), terms.get(2));
}
}
}
Assert.assertTrue("Rewrite not found", isFound);
}
use of fr.lirmm.graphik.graal.api.core.RuleSet 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);
}
Aggregations