use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet 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();
}
use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.
the class ConjunctiveQueryWithCompilation method test2.
@Theory
public void test2(Homomorphism<ConjunctiveQuery, AtomSet> hh, RulesCompilationFactory factory, AtomSet store) throws Exception {
Assume.assumeTrue(hh instanceof HomomorphismWithCompilation);
HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = (HomomorphismWithCompilation<ConjunctiveQuery, AtomSet>) hh;
store.add(DlgpParser.parseAtom("<P>(a,b)."));
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("<Q>(X,Y) :- <P>(Y,X)."));
RulesCompilation comp = factory.create();
comp.compile(rules.iterator());
StaticChase.executeChase(store, rules);
CloseableIterator<Substitution> results = h.execute(DlgpParser.parseQuery("?(X,Y) :- <Q>(X,Y)."), store, comp);
Assert.assertTrue(results.hasNext());
Substitution next = results.next();
Assert.assertEquals(DefaultTermFactory.instance().createConstant("a"), next.createImageOf(DefaultTermFactory.instance().createVariable("Y")));
Assert.assertEquals(DefaultTermFactory.instance().createConstant("b"), next.createImageOf(DefaultTermFactory.instance().createVariable("X")));
Assert.assertFalse(results.hasNext());
results.close();
}
use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.
the class ConjunctiveQueryWithCompilation method issue34.
@Theory
public void issue34(Homomorphism<ConjunctiveQuery, AtomSet> hh, RulesCompilationFactory factory, AtomSet store) throws Exception {
Assume.assumeTrue(hh instanceof HomomorphismWithCompilation);
HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = (HomomorphismWithCompilation<ConjunctiveQuery, AtomSet>) hh;
store.add(DlgpParser.parseAtom("<Q>(a,b)."));
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("<P>(X,Y) :- <Q>(Y,X)."));
RulesCompilation comp = factory.create();
comp.compile(rules.iterator());
StaticChase.executeChase(store, rules);
InMemoryAtomSet query1 = new LinkedListAtomSet();
query1.add(DlgpParser.parseAtom("<P>(a,Y)."));
CloseableIterator<Substitution> results = h.execute(new DefaultConjunctiveQuery(query1), store, comp);
Assert.assertFalse(results.hasNext());
results.close();
}
use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.
the class ConjunctiveQueryWithCompilation method issue35.
@Theory
public void issue35(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.addAll(DlgpParser.parseAtomSet("<P>(a,a), <R>(a,b,b)."));
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("<Q>(X,Y,X) :- <P>(X,Y)."));
ConjunctiveQuery query = DlgpParser.parseQuery("? :- <Q>(X,Y,Y), <R>(X,Y,Z).");
RulesCompilation comp = factory.create();
comp.compile(rules.iterator());
StaticChase.executeChase(store, rules);
CloseableIterator<Substitution> results = h.execute(new DefaultConjunctiveQuery(query), store, comp);
Assert.assertFalse(results.hasNext());
results.close();
}
use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet 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;
}
Aggregations