Search in sources :

Example 66 with Substitution

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;
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution) LinkedList(java.util.LinkedList) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution)

Example 67 with Substitution

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();
}
Also used : KnowledgeBase(fr.lirmm.graphik.graal.api.kb.KnowledgeBase) DlgpParser(fr.lirmm.graphik.graal.io.dlp.DlgpParser) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Test(org.junit.Test)

Example 68 with Substitution

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();
}
Also used : KnowledgeBase(fr.lirmm.graphik.graal.api.kb.KnowledgeBase) DlgpParser(fr.lirmm.graphik.graal.io.dlp.DlgpParser) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Test(org.junit.Test)

Example 69 with Substitution

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();
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RulesCompilation(fr.lirmm.graphik.graal.api.core.RulesCompilation) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) HomomorphismWithCompilation(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation) Theory(org.junit.experimental.theories.Theory)

Example 70 with Substitution

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();
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RulesCompilation(fr.lirmm.graphik.graal.api.core.RulesCompilation) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) HomomorphismWithCompilation(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation) Theory(org.junit.experimental.theories.Theory)

Aggregations

Substitution (fr.lirmm.graphik.graal.api.core.Substitution)158 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)58 Test (org.junit.Test)55 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)46 Theory (org.junit.experimental.theories.Theory)44 Atom (fr.lirmm.graphik.graal.api.core.Atom)41 Term (fr.lirmm.graphik.graal.api.core.Term)36 LinkedList (java.util.LinkedList)27 Variable (fr.lirmm.graphik.graal.api.core.Variable)25 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)24 Rule (fr.lirmm.graphik.graal.api.core.Rule)23 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)19 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)18 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)16 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)15 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)14 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)14 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)13 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)13 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)13