use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet 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);
}
}
use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.
the class BackwardChainingTest method constantInRulesIssue62.
@Theory
public void constantInRulesIssue62(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("p(X,a) :- q(X)."));
ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y) :- p(X,Y).");
compilation.compile(rules.iterator());
PureRewriter bc = new PureRewriter(operator, true);
CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
boolean found = false;
int i = 0;
while (it.hasNext()) {
ConjunctiveQuery next = it.next();
CloseableIteratorWithoutException<Atom> atomIt = next.getAtomSet().iterator();
while (atomIt.hasNext()) {
if (atomIt.next().getPredicate().equals(Predicate.EQUALITY)) {
found = true;
}
}
++i;
}
Assert.assertTrue(found);
Assert.assertEquals(2, i);
}
use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.
the class BackwardChainingTest method issue34_1.
@Theory
public void issue34_1(RulesCompilation compilation, RewritingOperator operator) throws IteratorException {
try {
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("p(X,Y) :- q(Y,X)."));
rules.add(DlgpParser.parseRule("r(X,Z) :- p(X,Y)."));
rules.add(DlgpParser.parseRule("r(X,Z) :- q(X,Y)."));
ConjunctiveQuery query = DlgpParser.parseQuery("? :- r(a,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);
} catch (Throwable t) {
Assert.assertFalse("There is an error.", true);
}
}
use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.
the class PureRewriter method execute.
// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIteratorWithoutException<ConjunctiveQuery> execute(ConjunctiveQuery query, Iterable<Rule> rules) {
RuleSet newRulSet = new LinkedListRuleSet(rules);
RulesCompilation compilation = new IDCompilation();
compilation.compile(newRulSet.iterator());
RewritinCloseableIterator it = new RewritinCloseableIterator(true, query, newRulSet, compilation, this.operator);
it.setProfiler(this.getProfiler());
return it;
}
use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.
the class ChaseTest method test2.
// @Theory
// public void coreChaseTest(AtomSet atomSet) throws AtomSetException,
// HomomorphismFactoryException,
// HomomorphismException, ChaseException {
// atomSet.addAll(DlgpParser.parseAtomSet("e(X,Y), e(Y,X)."));
//
// LinkedList<Rule> ruleSet = new LinkedList<Rule>();
// ruleSet.add(DlgpParser.parseRule("e(Z,Z) :- e(X,Y), e(Y,X)."));
// ruleSet.add(DlgpParser.parseRule("e(X,Z), e(Z,Y) :- e(X,Y)."));
//
// Chase chase = new DefaultChase(ruleSet, atomSet,
// RecursiveBacktrackHomomorphism.instance(),
// new CoreChaseStopCondition());
// chase.execute();
//
// int size = 0;
// for (Iterator<Atom> it = atomSet.iterator(); it.hasNext(); it.next()) {
// if (++size > 3)
// Assert.assertFalse(true);
// }
//
// Assert.assertTrue(true);
// }
//
// @Theory
// public void coreChaseTest2(AtomSet atomSet) throws AtomSetException,
// HomomorphismFactoryException,
// HomomorphismException, ChaseException {
// atomSet.addAll(DlgpParser.parseAtomSet("e(X,Y), e(Y,Z)."));
//
// LinkedList<Rule> ruleSet = new LinkedList<Rule>();
// ruleSet.add(DlgpParser.parseRule("e(X,Z) :- e(X,Y), e(Y,Z)."));
//
// Chase chase = new DefaultChase(ruleSet, atomSet,
// RecursiveBacktrackHomomorphism.instance(),
// new CoreChaseStopCondition());
// chase.execute();
//
// ConjunctiveQuery query = DlgpParser.parseQuery("? :-
// e(X,Y),e(Y,Z),e(X,Z).");
// Assert.assertTrue(StaticHomomorphism.instance().execute(query,
// atomSet).hasNext());
// }
@Theory
public void test2(InMemoryAtomSet atomSet) throws ChaseException, HomomorphismFactoryException, HomomorphismException, IteratorException, ParseException {
// add assertions into this atom set
atomSet.add(DlgpParser.parseAtom("<P>(a,a)."));
atomSet.add(DlgpParser.parseAtom("<P>(c,c)."));
atomSet.add(DlgpParser.parseAtom("<Q>(b,b)."));
atomSet.add(DlgpParser.parseAtom("<Q>(c,c)."));
atomSet.add(DlgpParser.parseAtom("<S>(z,z)."));
// /////////////////////////////////////////////////////////////////////
// create a rule set
RuleSet ruleSet = new LinkedListRuleSet();
// add a rule into this rule set
ruleSet.add(DlgpParser.parseRule("<R>(X,X) :- <P>(X,X), <Q>(X,X)."));
ruleSet.add(DlgpParser.parseRule("<S>(X, Y) :- <P>(X,X), <Q>(Y,Y)."));
// /////////////////////////////////////////////////////////////////////
// run saturation
StaticChase.executeChase(atomSet, ruleSet);
// /////////////////////////////////////////////////////////////////////
// execute query
ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y) :- <S>(X, Y), <P>(X,X), <Q>(Y,Y).");
CloseableIterator<Substitution> subReader = SmartHomomorphism.instance().execute(query, atomSet);
Assert.assertTrue(subReader.hasNext());
}
Aggregations