Search in sources :

Example 1 with LinkedListRuleSet

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

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

the class HomomorphismTest method test6Compilation.

@Test
public void test6Compilation() throws HomomorphismException, IteratorException, AtomSetException {
    InMemoryAtomSet data = new DefaultInMemoryGraphStore();
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("r(X,Y) :- s(X,Y)."));
    RulesCompilation comp = new IDCompilation();
    comp.compile(rules.iterator());
    data.addAll(DlgpParser.parseAtomSet("p(a,b), q(b), p(a,c), s(a,d), q(d), s(a,e)."));
    Variable y = DefaultTermFactory.instance().createVariable("Y");
    Variable z = DefaultTermFactory.instance().createVariable("Z");
    InMemoryAtomSet positivePart = new LinkedListAtomSet();
    positivePart.addAll(DlgpParser.parseAtomSet("p(X,Y),r(X,Z)."));
    CloseableIteratorWithoutException<Atom> it = positivePart.iterator();
    it.next().setTerm(1, y);
    it.next().setTerm(1, z);
    LinkedList<InMemoryAtomSet> parts = new LinkedList<InMemoryAtomSet>();
    InMemoryAtomSet negatedPart = new LinkedListAtomSet();
    negatedPart.addAll(DlgpParser.parseAtomSet("q(Y)."));
    negatedPart.iterator().next().setTerm(0, y);
    parts.add(negatedPart);
    negatedPart = new LinkedListAtomSet();
    negatedPart.addAll(DlgpParser.parseAtomSet("q(Z)."));
    negatedPart.iterator().next().setTerm(0, z);
    parts.add(negatedPart);
    DefaultConjunctiveQueryWithNegatedParts query = new DefaultConjunctiveQueryWithNegatedParts(positivePart, parts);
    BacktrackHomomorphismWithNegatedParts h = new BacktrackHomomorphismWithNegatedParts();
    CloseableIterator<Substitution> res = h.execute(query, data, comp);
    Assert.assertTrue(res.hasNext());
    res.next();
    Assert.assertFalse(res.hasNext());
    res.close();
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Variable(fr.lirmm.graphik.graal.api.core.Variable) IDCompilation(fr.lirmm.graphik.graal.core.compilation.IDCompilation) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList) DefaultConjunctiveQueryWithNegatedParts(fr.lirmm.graphik.graal.core.DefaultConjunctiveQueryWithNegatedParts) 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) RulesCompilation(fr.lirmm.graphik.graal.api.core.RulesCompilation) Test(org.junit.Test)

Example 3 with LinkedListRuleSet

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

the class BackwardChainingTest method Test1.

/**
 * Test 1
 *
 * @throws IteratorException
 * @throws ParseException
 */
@Theory
public void Test1(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("q(X1,X2), ppp(X2) :- r(X1)."));
    rules.add(DlgpParser.parseRule("pp(X) :- ppp(X)."));
    rules.add(DlgpParser.parseRule("p(X) :- pp(X)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- q(X, Y), p(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);
}
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 4 with LinkedListRuleSet

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

the class BackwardChainingTest method getBody2.

/**
 * c(X) :- b(X,X).
 *
 * getBody([X]) => b(X, X).
 *
 * @throws ParseException
 */
@Theory
public void getBody2(RulesCompilation compilation, RewritingOperator operator) throws ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X) :- q(X,X)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- p(X).");
    compilation.compile(rules.iterator());
    PureRewriter bc = new PureRewriter(operator, true);
    CloseableIteratorWithoutException<? extends ConjunctiveQuery> rewIt = bc.execute(query, rules, compilation);
    boolean isFound = false;
    while (rewIt.hasNext()) {
        ConjunctiveQuery rew = rewIt.next();
        CloseableIteratorWithoutException<Atom> it = rew.getAtomSet().iterator();
        if (it.hasNext()) {
            Atom a = it.next();
            if (a.getPredicate().equals(new Predicate("q", 2))) {
                isFound = true;
                List<Term> terms = a.getTerms();
                Assert.assertEquals(terms.get(0), terms.get(1));
            }
        }
    }
    Assert.assertTrue("Rewrite not found", isFound);
}
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) Term(fr.lirmm.graphik.graal.api.core.Term) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Theory(org.junit.experimental.theories.Theory)

Example 5 with LinkedListRuleSet

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

the class BackwardChainingTest method getUnification.

/**
 * c(X) :- p(X,Y,X). p(X,Y,X) :- a(X).
 *
 * ?(X) :- c(X).
 *
 * @throws IteratorException
 * @throws ParseException
 */
@Theory
public void getUnification(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X) :- q(X,Y,X)."));
    rules.add(DlgpParser.parseRule("q(X,Y,X) :- s(X)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- p(X).");
    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(3, 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) 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