Search in sources :

Example 1 with ConjunctiveQuery

use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.

the class BackjumpTest method test1.

/*
	 * X1 --(p15)--> X5 --(p56)--> X6 \ (p12)--> X2 --(p24)--> X4 \ (p23)--> X3
	 * 
	 * solutions: X1 X2 X3 X4 X5 X6 
	 *            b  b  c  d  a  a 
	 *            b  b  d  d  a  a 
	 *            b  d  a  a  a  a
	 */
@Test
public void test1() throws HomomorphismException, IteratorException, ParseException, AtomSetException {
    InMemoryAtomSet data = new DefaultInMemoryGraphStore();
    data.addAll(DlgpParser.parseAtomSet("p12(a,a), p12(b,b), p12(b,c), p12(b,d)."));
    data.addAll(DlgpParser.parseAtomSet("p23(a,a), p23(b,c), p23(b,d), p23(c,a), p23(d,a)."));
    data.addAll(DlgpParser.parseAtomSet("p24(a,a), p24(b,d), p24(d,a)."));
    data.addAll(DlgpParser.parseAtomSet("p15(b,a), p15(b,b)."));
    data.addAll(DlgpParser.parseAtomSet("p56(a,a)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X1,X2,X3,X4,X5,X6) :- p12(X1,X2), p23(X2,X3), p24(X2,X4), p15(X1,X5), p56(X5,X6).");
    BCC bcc = new BCC();
    Homomorphism<ConjunctiveQuery, AtomSet> h = new BacktrackHomomorphism(bcc.getBCCScheduler(), AllDomainBootstrapper.instance(), NoForwardChecking.instance(), bcc.getBCCBackJumping());
    h.setProfiler(new CPUTimeProfiler());
    CloseableIterator<Substitution> results = h.execute(query, data);
    int i = 0;
    while (results.hasNext()) {
        results.next();
        ++i;
    }
    Assert.assertEquals(3, i);
    Assert.assertEquals(49, h.getProfiler().get("#calls"));
}
Also used : BCC(fr.lirmm.graphik.graal.homomorphism.bbc.BCC) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) CPUTimeProfiler(fr.lirmm.graphik.util.profiler.CPUTimeProfiler) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Test(org.junit.Test)

Example 2 with ConjunctiveQuery

use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.

the class ForwardCheckingTest method NFC2Test.

@Test
public void NFC2Test() throws HomomorphismException, IteratorException, ParseException {
    Profiler profiler = new CPUTimeProfiler();
    Predicate[] predicates = { new Predicate("p2", 2), new Predicate("p3", 3), new Predicate("p4", 4) };
    InMemoryAtomSet data = new DefaultInMemoryGraphStore();
    TestUtil.addNAtoms(data, 13, predicates, 5, new Random(0));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X5,X6,X7,X8) :- p4(X5,X6,X7,X8), p4(X8,X7,X6,X5), p3(X7,X8,X9), p2(X7,X11).");
    Homomorphism<ConjunctiveQuery, AtomSet> h = new BacktrackHomomorphism(new NFC2());
    h.setProfiler(profiler);
    CloseableIterator<Substitution> results = h.execute(query, data);
    while (results.hasNext()) {
        results.next();
    }
    results.close();
    Assert.assertEquals(1, profiler.get("#calls"));
}
Also used : NFC2(fr.lirmm.graphik.graal.homomorphism.forward_checking.NFC2) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) CPUTimeProfiler(fr.lirmm.graphik.util.profiler.CPUTimeProfiler) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Profiler(fr.lirmm.graphik.util.profiler.Profiler) CPUTimeProfiler(fr.lirmm.graphik.util.profiler.CPUTimeProfiler) Random(java.util.Random) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Test(org.junit.Test)

Example 3 with ConjunctiveQuery

use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.

the class ForwardCheckingTest method FCTest2.

@Test
public void FCTest2() throws HomomorphismException, IteratorException, ParseException, AtomSetException {
    Profiler profiler = new CPUTimeProfiler();
    InMemoryAtomSet data = new DefaultInMemoryGraphStore();
    data.addAll(DlgpParser.parseAtomSet("p(a,b), p(a,c), q(a,a), q(a,b)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y,Z) :- p(X,Z), q(Y,Z).");
    Homomorphism<ConjunctiveQuery, AtomSet> h = new BacktrackHomomorphism(new NFC2());
    h.setProfiler(profiler);
    CloseableIterator<Substitution> results = h.execute(query, data);
    while (results.hasNext()) {
        results.next();
    }
    results.close();
    Assert.assertEquals(7, profiler.get("#calls"));
}
Also used : NFC2(fr.lirmm.graphik.graal.homomorphism.forward_checking.NFC2) Profiler(fr.lirmm.graphik.util.profiler.Profiler) CPUTimeProfiler(fr.lirmm.graphik.util.profiler.CPUTimeProfiler) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) CPUTimeProfiler(fr.lirmm.graphik.util.profiler.CPUTimeProfiler) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Test(org.junit.Test)

Example 4 with ConjunctiveQuery

use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.

the class ForwardCheckingTest method test1.

@Test
public void test1() throws HomomorphismException, IteratorException, ParseException, AtomSetException {
    InMemoryAtomSet data = new DefaultInMemoryGraphStore();
    data.addAll(DlgpParser.parseAtomSet("p(a,b)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y,Z) :- p(X,Y), p(X,Z).");
    Homomorphism<ConjunctiveQuery, AtomSet> h = new BacktrackHomomorphism(new NFC2());
    CloseableIterator<Substitution> results = h.execute(query, data);
    while (results.hasNext()) {
        results.next();
    }
    results.close();
}
Also used : NFC2(fr.lirmm.graphik.graal.homomorphism.forward_checking.NFC2) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Test(org.junit.Test)

Example 5 with ConjunctiveQuery

use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.

the class DlgpParserTest method parseQueryWithPrefix.

@Test
public void parseQueryWithPrefix() throws ParseException {
    ConjunctiveQuery q = DlgpParser.parseQuery("@prefix ex: <http://example.com/> ?(X) :- ex:p(a,X).");
    Assert.assertEquals(X, q.getAnswerVariables().get(0));
    Atom a = q.getAtomSet().iterator().next();
    Assert.assertEquals(A, a.getTerm(0));
    Assert.assertEquals(X, a.getTerm(1));
}
Also used : ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) Test(org.junit.Test)

Aggregations

ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)113 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)58 Theory (org.junit.experimental.theories.Theory)57 Atom (fr.lirmm.graphik.graal.api.core.Atom)34 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)29 Test (org.junit.Test)29 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)23 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)22 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)22 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)19 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)18 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)14 Term (fr.lirmm.graphik.graal.api.core.Term)14 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)14 LinkedList (java.util.LinkedList)13 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)12 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)11 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)10 TripleStore (fr.lirmm.graphik.graal.api.store.TripleStore)9 RulesCompilation (fr.lirmm.graphik.graal.api.core.RulesCompilation)7