Search in sources :

Example 11 with LinkedListRuleSet

use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.

the class ConjunctiveQueryWithCompilation method backtrackHomomorphismBootstrapperTest2.

@Theory
public void backtrackHomomorphismBootstrapperTest2(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>(b,a)."));
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("<Q>(X,Y) :- <P>(X,Y)."));
    RulesCompilation comp = factory.create();
    comp.compile(rules.iterator());
    StaticChase.executeChase(store, rules);
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- <Q>(X,a).");
    CloseableIterator<Substitution> subReader;
    Substitution sub;
    subReader = h.execute(query, store, comp);
    Assert.assertTrue(subReader.hasNext());
    sub = subReader.next();
    Assert.assertEquals(DefaultTermFactory.instance().createConstant("b"), sub.createImageOf(DefaultTermFactory.instance().createVariable("X")));
    Assert.assertFalse(subReader.hasNext());
    subReader.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) TripleStore(fr.lirmm.graphik.graal.api.store.TripleStore) 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 12 with LinkedListRuleSet

use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.

the class ConjunctiveQueryWithCompilation method backtrackHomomorphismBootstrapperTest1.

@Theory
public void backtrackHomomorphismBootstrapperTest1(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>(b,a)."));
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("<Q>(X,Y) :- <P>(X,Y)."));
    RulesCompilation comp = factory.create();
    comp.compile(rules.iterator());
    StaticChase.executeChase(store, rules);
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- <Q>(b,X).");
    CloseableIterator<Substitution> subReader;
    Substitution sub;
    subReader = h.execute(query, store, comp);
    Assert.assertTrue(subReader.hasNext());
    sub = subReader.next();
    Assert.assertEquals(DefaultTermFactory.instance().createConstant("a"), sub.createImageOf(DefaultTermFactory.instance().createVariable("X")));
    Assert.assertFalse(subReader.hasNext());
    subReader.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) TripleStore(fr.lirmm.graphik.graal.api.store.TripleStore) 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 13 with LinkedListRuleSet

use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.

the class ConjunctiveQueryWithCompilation method issueWithAtom2SubstitutionConverter.

@Theory
public void issueWithAtom2SubstitutionConverter(RulesCompilationFactory factory, AtomSet store) throws Exception {
    Assume.assumeFalse(store instanceof TripleStore);
    HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = AtomicQueryHomomorphism.instance();
    store.addAll(DlgpParser.parseAtomSet("<P>(a,a)."));
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("<Q>(X,Y,X) :- <P>(X,Y)."));
    RulesCompilation comp = factory.create();
    comp.compile(rules.iterator());
    StaticChase.executeChase(store, rules);
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- <Q>(X,Y,Y).");
    CloseableIterator<Substitution> results = h.execute(new DefaultConjunctiveQuery(query), store, comp);
    Assert.assertTrue(results.hasNext());
    results.close();
    query = DlgpParser.parseQuery("?(Y) :- <Q>(X,Y,Y).");
    results = h.execute(new DefaultConjunctiveQuery(query), store, comp);
    Assert.assertTrue(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) TripleStore(fr.lirmm.graphik.graal.api.store.TripleStore) 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) Theory(org.junit.experimental.theories.Theory)

Example 14 with LinkedListRuleSet

use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.

the class ConjunctiveQueryWithCompilation method test1.

// /////////////////////////////////////////////////////////////////////////
// TEST CASES
// /////////////////////////////////////////////////////////////////////////
@Theory
public void test1(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.add(DlgpParser.parseAtom("<P>(a)."));
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("<Q>(X) :- <P>(X)."));
    RulesCompilation comp = factory.create();
    comp.compile(rules.iterator());
    StaticChase.executeChase(store, rules);
    CloseableIterator<Substitution> results = h.execute(DlgpParser.parseQuery("?(X) :- <Q>(X)."), store, comp);
    Assert.assertTrue(results.hasNext());
    Substitution next = results.next();
    Assert.assertEquals(DefaultTermFactory.instance().createConstant("a"), 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) TripleStore(fr.lirmm.graphik.graal.api.store.TripleStore) 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 15 with LinkedListRuleSet

use of fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet in project graal by graphik-team.

the class MFAProperty method translateToMFA.

public static RuleSet translateToMFA(Iterable<Rule> rules) {
    RuleSet R = new LinkedListRuleSet();
    for (Rule r : rules) {
        for (Rule r2 : translateRuleToMFA(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;
}
Also used : DefaultRule(fr.lirmm.graphik.graal.core.DefaultRule) AnalyserRuleSet(fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet) RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultRule(fr.lirmm.graphik.graal.core.DefaultRule) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Aggregations

LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)33 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)32 Theory (org.junit.experimental.theories.Theory)22 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)20 RulesCompilation (fr.lirmm.graphik.graal.api.core.RulesCompilation)13 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)12 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)12 Atom (fr.lirmm.graphik.graal.api.core.Atom)11 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)11 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)10 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)9 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)7 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)7 Test (org.junit.Test)7 HomomorphismWithCompilation (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation)6 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)6 Rule (fr.lirmm.graphik.graal.api.core.Rule)5 TripleStore (fr.lirmm.graphik.graal.api.store.TripleStore)5 Term (fr.lirmm.graphik.graal.api.core.Term)3 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)3