use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class IDCompilation method homomorphism.
@Override
public LinkedList<Substitution> homomorphism(Atom father, Atom son) {
LinkedList<Substitution> res = new LinkedList<Substitution>();
Predicate predB = son.getPredicate();
Predicate predH = father.getPredicate();
List<IDCondition> conds = getConditions(predB, predH);
for (IDCondition cond : conds) {
if (cond.checkBody(son.getTerms())) {
Substitution homo = cond.homomorphism(father.getTerms(), son.getTerms());
if (homo != null) {
res.add(new TreeMapSubstitution(homo));
}
}
}
return res;
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class DefaultKnowledgeBaseQueryTest method testQuery.
/**
* Test method for
* {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#query(fr.lirmm.graphik.graal.api.core.Query)}.
* @throws AtomSetException
* @throws KnowledgeBaseException
* @throws ParseException
* @throws IteratorException
*/
@Test
public void testQuery() throws AtomSetException, ParseException, KnowledgeBaseException, IteratorException {
KnowledgeBase kb = new DefaultKnowledgeBase(new DlgpParser("p(X) :- q(X). q(X) :- r(X). r(X) :- s(X). s(a)."));
CloseableIterator<Substitution> res = kb.query(DlgpParser.parseQuery("? :- p(a)."));
Assert.assertTrue(res.hasNext());
res.close();
kb.close();
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class DefaultKnowledgeBaseQueryTest method testQueryWithTimeout0.
/**
* Test method for
* {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#query(fr.lirmm.graphik.graal.api.core.Query, long)}.
* @throws AtomSetException
* @throws KnowledgeBaseException
* @throws ParseException
* @throws IteratorException
* @throws TimeoutException
*/
@Test
public void testQueryWithTimeout0() throws AtomSetException, ParseException, KnowledgeBaseException, IteratorException, TimeoutException {
KnowledgeBase kb = new DefaultKnowledgeBase(new DlgpParser("p(X) :- q(X). q(X) :- r(X). r(X) :- s(X). s(a)."));
CloseableIterator<Substitution> res = kb.query(DlgpParser.parseQuery("? :- p(a)."), 0);
Assert.assertTrue(res.hasNext());
res.close();
kb.close();
}
use of fr.lirmm.graphik.graal.api.core.Substitution 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.api.core.Substitution 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();
}
Aggregations