Search in sources :

Example 11 with RuleSet

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

the class PureHomomorphismTest method issue34.

@Theory
public void issue34(ExistentialHomomorphismWithCompilation<InMemoryAtomSet, AtomSet> h) throws HomomorphismException, IteratorException, ParseException {
    InMemoryAtomSet query1 = new LinkedListAtomSet();
    query1.add(DlgpParser.parseAtom("p(a,Y)."));
    InMemoryAtomSet facts = new LinkedListAtomSet();
    facts.add(DlgpParser.parseAtom("q(a,b)."));
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X,Y) :- q(Y,X)."));
    RulesCompilation comp = new IDCompilation();
    comp.compile(rules.iterator());
    Assert.assertFalse(h.exist(query1, facts, comp));
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) IDCompilation(fr.lirmm.graphik.graal.core.compilation.IDCompilation) 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) Theory(org.junit.experimental.theories.Theory)

Example 12 with RuleSet

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

the class RdbmsStoreTest method SQLRuleApplierTest.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Theory
public void SQLRuleApplierTest(RdbmsStore store) throws AtomSetException, IteratorException, ParseException {
    RuleSet ruleSet = new LinkedListRuleSet();
    ruleSet.add(DlgpParser.parseRule("<Q>(X,Y) :- <P>(X)."));
    ruleSet.add(DlgpParser.parseRule("<R>(Y) :- <Q>(X,Y)."));
    Atom a = DlgpParser.parseAtom("<P>(a).");
    store.add(a);
    Chase chase = new BasicChase<RdbmsStore>(ruleSet, store, new SQLRuleApplier(SqlHomomorphism.instance()));
    try {
        chase.execute();
    } catch (ChaseException e) {
        Assert.fail(e.getMessage());
    }
    CloseableIterator<Atom> it = store.iterator();
    int count = Iterators.count(it);
    Assert.assertEquals(3, count);
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) BasicChase(fr.lirmm.graphik.graal.forward_chaining.BasicChase) SQLRuleApplier(fr.lirmm.graphik.graal.store.rdbms.rule_applier.SQLRuleApplier) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) Atom(fr.lirmm.graphik.graal.api.core.Atom) Chase(fr.lirmm.graphik.graal.api.forward_chaining.Chase) BasicChase(fr.lirmm.graphik.graal.forward_chaining.BasicChase) Theory(org.junit.experimental.theories.Theory)

Example 13 with RuleSet

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

the class ChaseTest method test2.

// @Theory
// public void coreChaseTest(AtomSet atomSet) throws AtomSetException,
// HomomorphismFactoryException,
// HomomorphismException, ChaseException {
// atomSet.addAll(DlgpParser.parseAtomSet("e(X,Y), e(Y,X)."));
// 
// LinkedList<Rule> ruleSet = new LinkedList<Rule>();
// ruleSet.add(DlgpParser.parseRule("e(Z,Z) :- e(X,Y), e(Y,X)."));
// ruleSet.add(DlgpParser.parseRule("e(X,Z), e(Z,Y) :- e(X,Y)."));
// 
// Chase chase = new DefaultChase(ruleSet, atomSet,
// RecursiveBacktrackHomomorphism.instance(),
// new CoreChaseStopCondition());
// chase.execute();
// 
// int size = 0;
// for (Iterator<Atom> it = atomSet.iterator(); it.hasNext(); it.next()) {
// if (++size > 3)
// Assert.assertFalse(true);
// }
// 
// Assert.assertTrue(true);
// }
// 
// @Theory
// public void coreChaseTest2(AtomSet atomSet) throws AtomSetException,
// HomomorphismFactoryException,
// HomomorphismException, ChaseException {
// atomSet.addAll(DlgpParser.parseAtomSet("e(X,Y), e(Y,Z)."));
// 
// LinkedList<Rule> ruleSet = new LinkedList<Rule>();
// ruleSet.add(DlgpParser.parseRule("e(X,Z) :- e(X,Y), e(Y,Z)."));
// 
// Chase chase = new DefaultChase(ruleSet, atomSet,
// RecursiveBacktrackHomomorphism.instance(),
// new CoreChaseStopCondition());
// chase.execute();
// 
// ConjunctiveQuery query = DlgpParser.parseQuery("? :-
// e(X,Y),e(Y,Z),e(X,Z).");
// Assert.assertTrue(StaticHomomorphism.instance().execute(query,
// atomSet).hasNext());
// }
@Theory
public void test2(InMemoryAtomSet atomSet) throws ChaseException, HomomorphismFactoryException, HomomorphismException, IteratorException, ParseException {
    // add assertions into this atom set
    atomSet.add(DlgpParser.parseAtom("<P>(a,a)."));
    atomSet.add(DlgpParser.parseAtom("<P>(c,c)."));
    atomSet.add(DlgpParser.parseAtom("<Q>(b,b)."));
    atomSet.add(DlgpParser.parseAtom("<Q>(c,c)."));
    atomSet.add(DlgpParser.parseAtom("<S>(z,z)."));
    // /////////////////////////////////////////////////////////////////////
    // create a rule set
    RuleSet ruleSet = new LinkedListRuleSet();
    // add a rule into this rule set
    ruleSet.add(DlgpParser.parseRule("<R>(X,X) :- <P>(X,X), <Q>(X,X)."));
    ruleSet.add(DlgpParser.parseRule("<S>(X, Y) :- <P>(X,X), <Q>(Y,Y)."));
    // /////////////////////////////////////////////////////////////////////
    // run saturation
    StaticChase.executeChase(atomSet, ruleSet);
    // /////////////////////////////////////////////////////////////////////
    // execute query
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y) :- <S>(X, Y), <P>(X,X), <Q>(Y,Y).");
    CloseableIterator<Substitution> subReader = SmartHomomorphism.instance().execute(query, atomSet);
    Assert.assertTrue(subReader.hasNext());
}
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) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Example 14 with RuleSet

use of fr.lirmm.graphik.graal.api.core.RuleSet 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 15 with RuleSet

use of fr.lirmm.graphik.graal.api.core.RuleSet 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)

Aggregations

RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)39 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)37 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)22 Theory (org.junit.experimental.theories.Theory)22 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)14 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)14 RulesCompilation (fr.lirmm.graphik.graal.api.core.RulesCompilation)13 Atom (fr.lirmm.graphik.graal.api.core.Atom)11 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)11 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)10 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)9 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)9 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)7 AnalyserRuleSet (fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet)7 Test (org.junit.Test)7 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)6 HomomorphismWithCompilation (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation)6 Rule (fr.lirmm.graphik.graal.api.core.Rule)5 ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)5 TripleStore (fr.lirmm.graphik.graal.api.store.TripleStore)5