Search in sources :

Example 6 with LinkedListRuleSet

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

the class BackwardChainingTest method issue34_2.

@Theory
public void issue34_2(RulesCompilation compilation, RewritingOperator operator) throws IteratorException {
    try {
        RuleSet rules = new LinkedListRuleSet();
        rules.add(DlgpParser.parseRule("q(X,Y,X) :- p(X,Y)."));
        rules.add(DlgpParser.parseRule("r(Z,T) :- q(X,Y,Z)."));
        ConjunctiveQuery query = DlgpParser.parseQuery("? :- r(a,X),p(a,b).");
        compilation.compile(rules.iterator());
        PureRewriter bc = new PureRewriter(operator, true);
        CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
        int i = Iterators.count(it);
        Assert.assertEquals(1, i);
    } catch (Throwable t) {
        Assert.assertFalse("There is an error.", true);
    }
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Example 7 with LinkedListRuleSet

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

the class BackwardChainingTest method constantInRulesIssue62.

@Theory
public void constantInRulesIssue62(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X,a) :- q(X)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y) :- p(X,Y).");
    compilation.compile(rules.iterator());
    PureRewriter bc = new PureRewriter(operator, true);
    CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
    boolean found = false;
    int i = 0;
    while (it.hasNext()) {
        ConjunctiveQuery next = it.next();
        CloseableIteratorWithoutException<Atom> atomIt = next.getAtomSet().iterator();
        while (atomIt.hasNext()) {
            if (atomIt.next().getPredicate().equals(Predicate.EQUALITY)) {
                found = true;
            }
        }
        ++i;
    }
    Assert.assertTrue(found);
    Assert.assertEquals(2, i);
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) Theory(org.junit.experimental.theories.Theory)

Example 8 with LinkedListRuleSet

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

the class BackwardChainingTest method issue34_1.

@Theory
public void issue34_1(RulesCompilation compilation, RewritingOperator operator) throws IteratorException {
    try {
        RuleSet rules = new LinkedListRuleSet();
        rules.add(DlgpParser.parseRule("p(X,Y) :- q(Y,X)."));
        rules.add(DlgpParser.parseRule("r(X,Z) :- p(X,Y)."));
        rules.add(DlgpParser.parseRule("r(X,Z) :- q(X,Y)."));
        ConjunctiveQuery query = DlgpParser.parseQuery("? :- r(a,Y).");
        compilation.compile(rules.iterator());
        PureRewriter bc = new PureRewriter(operator, true);
        CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
        int i = Iterators.count(it);
        Assert.assertEquals(4, i);
    } catch (Throwable t) {
        Assert.assertFalse("There is an error.", true);
    }
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Example 9 with LinkedListRuleSet

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

the class PureRewriter method execute.

// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIteratorWithoutException<ConjunctiveQuery> execute(ConjunctiveQuery query, Iterable<Rule> rules) {
    RuleSet newRulSet = new LinkedListRuleSet(rules);
    RulesCompilation compilation = new IDCompilation();
    compilation.compile(newRulSet.iterator());
    RewritinCloseableIterator it = new RewritinCloseableIterator(true, query, newRulSet, compilation, this.operator);
    it.setProfiler(this.getProfiler());
    return it;
}
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) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RulesCompilation(fr.lirmm.graphik.graal.api.core.RulesCompilation)

Example 10 with LinkedListRuleSet

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

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