use of fr.lirmm.graphik.graal.api.core.RuleSet 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.api.core.RuleSet 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.api.core.RuleSet in project graal by graphik-team.
the class DefaultKnowledgeBase method getFESGraphOfRuleDependencies.
protected GraphOfRuleDependencies getFESGraphOfRuleDependencies() {
if (this.fesGRD == null) {
RuleSet fesRuleSet = this.getFESRuleSet();
this.fesGRD = new DefaultGraphOfRuleDependencies(fesRuleSet);
}
return this.fesGRD;
}
use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.
the class DefaultKnowledgeBaseTest method testGetRuleNames.
/**
* Test method for
* {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#getRuleNames()}.
* @throws ParseException
*/
@Test
public void testGetRuleNames() throws ParseException {
Rule r1 = DlgpParser.parseRule("[R1] p(x) :- q(X).");
Rule r2 = DlgpParser.parseRule("[R2] q(x) :- r(X).");
AtomSet store = new DefaultInMemoryGraphStore();
RuleSet ruleset = new LinkedListRuleSet();
ruleset.add(r1);
ruleset.add(r2);
KnowledgeBase kb = new DefaultKnowledgeBase(store, ruleset);
Assert.assertTrue(kb.getRuleNames().contains("R1"));
Assert.assertTrue(kb.getRuleNames().contains("R2"));
Assert.assertEquals(r1, kb.getRule("R1"));
Assert.assertEquals(r2, kb.getRule("R2"));
kb.close();
}
use of fr.lirmm.graphik.graal.api.core.RuleSet in project graal by graphik-team.
the class DefaultKnowledgeBaseTest method testDefaultKnowledgeBaseAtomSetRuleSet.
/**
* Test method for
* {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#DefaultKnowledgeBase(fr.lirmm.graphik.graal.api.core.AtomSet, fr.lirmm.graphik.graal.api.core.RuleSet)}.
* @throws AtomSetException
* @throws ParseException
*/
@Test
public void testDefaultKnowledgeBaseAtomSetRuleSet() throws AtomSetException, ParseException {
Atom aa = DlgpParser.parseAtom("q(a).");
Atom ab = DlgpParser.parseAtom("q(b).");
Atom ac = DlgpParser.parseAtom("q(c).");
Rule r = DlgpParser.parseRule("[R1] p(x) :- q(X).");
NegativeConstraint nc = DlgpParser.parseNegativeConstraint("[NC] ! :- q(X), p(X).");
AtomSet store = new DefaultInMemoryGraphStore();
store.add(aa);
store.add(ab);
store.add(ac);
RuleSet ruleset = new LinkedListRuleSet();
ruleset.add(r);
ruleset.add(nc);
KnowledgeBase kb = new DefaultKnowledgeBase(store, ruleset);
Assert.assertTrue(kb.getOntology().contains(r));
Assert.assertTrue(kb.getOntology().contains(nc));
Assert.assertTrue(kb.getFacts().contains(aa));
Assert.assertTrue(kb.getFacts().contains(ab));
Assert.assertTrue(kb.getFacts().contains(ac));
kb.close();
}
Aggregations