use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.
the class PureHomomorphismTest method issue34.
@Theory
public void issue34(ExistentialHomomorphismWithCompilation<InMemoryAtomSet, AtomSet> h) throws HomomorphismException, IteratorException, ParseException {
InMemoryAtomSet query1 = new LinkedListAtomSet();
query1.add(DlgpParser.parseAtom("p(a,Y)."));
InMemoryAtomSet facts = new LinkedListAtomSet();
facts.add(DlgpParser.parseAtom("q(a,b)."));
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("p(X,Y) :- q(Y,X)."));
RulesCompilation comp = new IDCompilation();
comp.compile(rules.iterator());
Assert.assertFalse(h.exist(query1, facts, comp));
}
use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.
the class HomomorphismTest method test6Compilation.
@Test
public void test6Compilation() throws HomomorphismException, IteratorException, AtomSetException {
InMemoryAtomSet data = new DefaultInMemoryGraphStore();
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("r(X,Y) :- s(X,Y)."));
RulesCompilation comp = new IDCompilation();
comp.compile(rules.iterator());
data.addAll(DlgpParser.parseAtomSet("p(a,b), q(b), p(a,c), s(a,d), q(d), s(a,e)."));
Variable y = DefaultTermFactory.instance().createVariable("Y");
Variable z = DefaultTermFactory.instance().createVariable("Z");
InMemoryAtomSet positivePart = new LinkedListAtomSet();
positivePart.addAll(DlgpParser.parseAtomSet("p(X,Y),r(X,Z)."));
CloseableIteratorWithoutException<Atom> it = positivePart.iterator();
it.next().setTerm(1, y);
it.next().setTerm(1, z);
LinkedList<InMemoryAtomSet> parts = new LinkedList<InMemoryAtomSet>();
InMemoryAtomSet negatedPart = new LinkedListAtomSet();
negatedPart.addAll(DlgpParser.parseAtomSet("q(Y)."));
negatedPart.iterator().next().setTerm(0, y);
parts.add(negatedPart);
negatedPart = new LinkedListAtomSet();
negatedPart.addAll(DlgpParser.parseAtomSet("q(Z)."));
negatedPart.iterator().next().setTerm(0, z);
parts.add(negatedPart);
DefaultConjunctiveQueryWithNegatedParts query = new DefaultConjunctiveQueryWithNegatedParts(positivePart, parts);
BacktrackHomomorphismWithNegatedParts h = new BacktrackHomomorphismWithNegatedParts();
CloseableIterator<Substitution> res = h.execute(query, data, comp);
Assert.assertTrue(res.hasNext());
res.next();
Assert.assertFalse(res.hasNext());
res.close();
}
use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.
the class BackwardChainingTest method Test1.
/**
* Test 1
*
* @throws IteratorException
* @throws ParseException
*/
@Theory
public void Test1(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("q(X1,X2), ppp(X2) :- r(X1)."));
rules.add(DlgpParser.parseRule("pp(X) :- ppp(X)."));
rules.add(DlgpParser.parseRule("p(X) :- pp(X)."));
ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- q(X, Y), p(Y).");
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(4, i);
}
use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet 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.core.ruleset.LinkedListRuleSet 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);
}
Aggregations