use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.
the class ConjunctiveQueryWithCompilation method issueWithAtom2SubstitutionConverter.
@Theory
public void issueWithAtom2SubstitutionConverter(RulesCompilationFactory factory, AtomSet store) throws Exception {
Assume.assumeFalse(store instanceof TripleStore);
HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = AtomicQueryHomomorphism.instance();
store.addAll(DlgpParser.parseAtomSet("<P>(a,a)."));
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("<Q>(X,Y,X) :- <P>(X,Y)."));
RulesCompilation comp = factory.create();
comp.compile(rules.iterator());
StaticChase.executeChase(store, rules);
ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- <Q>(X,Y,Y).");
CloseableIterator<Substitution> results = h.execute(new DefaultConjunctiveQuery(query), store, comp);
Assert.assertTrue(results.hasNext());
results.close();
query = DlgpParser.parseQuery("?(Y) :- <Q>(X,Y,Y).");
results = h.execute(new DefaultConjunctiveQuery(query), store, comp);
Assert.assertTrue(results.hasNext());
results.close();
}
use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.
the class ConjunctiveQueryWithCompilation method test1.
// /////////////////////////////////////////////////////////////////////////
// TEST CASES
// /////////////////////////////////////////////////////////////////////////
@Theory
public void test1(Homomorphism<ConjunctiveQuery, AtomSet> hh, RulesCompilationFactory factory, AtomSet store) throws Exception {
Assume.assumeFalse(store instanceof TripleStore);
Assume.assumeTrue(hh instanceof HomomorphismWithCompilation);
HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = (HomomorphismWithCompilation<ConjunctiveQuery, AtomSet>) hh;
store.add(DlgpParser.parseAtom("<P>(a)."));
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("<Q>(X) :- <P>(X)."));
RulesCompilation comp = factory.create();
comp.compile(rules.iterator());
StaticChase.executeChase(store, rules);
CloseableIterator<Substitution> results = h.execute(DlgpParser.parseQuery("?(X) :- <Q>(X)."), store, comp);
Assert.assertTrue(results.hasNext());
Substitution next = results.next();
Assert.assertEquals(DefaultTermFactory.instance().createConstant("a"), next.createImageOf(DefaultTermFactory.instance().createVariable("X")));
Assert.assertFalse(results.hasNext());
results.close();
}
use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.
the class BackwardChainingTest method getBody2.
/**
* c(X) :- b(X,X).
*
* getBody([X]) => b(X, X).
*
* @throws ParseException
*/
@Theory
public void getBody2(RulesCompilation compilation, RewritingOperator operator) throws ParseException {
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("p(X) :- q(X,X)."));
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", 2))) {
isFound = true;
List<Term> terms = a.getTerms();
Assert.assertEquals(terms.get(0), terms.get(1));
}
}
}
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 getUnification.
/**
* c(X) :- p(X,Y,X). p(X,Y,X) :- a(X).
*
* ?(X) :- c(X).
*
* @throws IteratorException
* @throws ParseException
*/
@Theory
public void getUnification(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("p(X) :- q(X,Y,X)."));
rules.add(DlgpParser.parseRule("q(X,Y,X) :- s(X)."));
ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- p(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(3, i);
}
use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.
the class BackwardChainingTest method issue34_2.
@Theory
public void issue34_2(RulesCompilation compilation, RewritingOperator operator) throws IteratorException {
try {
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("q(X,Y,X) :- p(X,Y)."));
rules.add(DlgpParser.parseRule("r(Z,T) :- q(X,Y,Z)."));
ConjunctiveQuery query = DlgpParser.parseQuery("? :- r(a,X),p(a,b).");
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);
} catch (Throwable t) {
Assert.assertFalse("There is an error.", true);
}
}
Aggregations