Search in sources :

Example 1 with RuleSet

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

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

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

the class DefaultKnowledgeBase method getFESGraphOfRuleDependencies.

protected GraphOfRuleDependencies getFESGraphOfRuleDependencies() {
    if (this.fesGRD == null) {
        RuleSet fesRuleSet = this.getFESRuleSet();
        this.fesGRD = new DefaultGraphOfRuleDependencies(fesRuleSet);
    }
    return this.fesGRD;
}
Also used : AnalyserRuleSet(fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet) RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) DefaultGraphOfRuleDependencies(fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies)

Example 4 with RuleSet

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

the class DefaultKnowledgeBaseTest method testGetRuleNames.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#getRuleNames()}.
 * @throws ParseException
 */
@Test
public void testGetRuleNames() throws ParseException {
    Rule r1 = DlgpParser.parseRule("[R1] p(x) :- q(X).");
    Rule r2 = DlgpParser.parseRule("[R2] q(x) :- r(X).");
    AtomSet store = new DefaultInMemoryGraphStore();
    RuleSet ruleset = new LinkedListRuleSet();
    ruleset.add(r1);
    ruleset.add(r2);
    KnowledgeBase kb = new DefaultKnowledgeBase(store, ruleset);
    Assert.assertTrue(kb.getRuleNames().contains("R1"));
    Assert.assertTrue(kb.getRuleNames().contains("R2"));
    Assert.assertEquals(r1, kb.getRule("R1"));
    Assert.assertEquals(r2, kb.getRule("R2"));
    kb.close();
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) KnowledgeBase(fr.lirmm.graphik.graal.api.kb.KnowledgeBase) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Test(org.junit.Test)

Example 5 with RuleSet

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

the class DefaultKnowledgeBaseTest method testDefaultKnowledgeBaseAtomSetRuleSet.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#DefaultKnowledgeBase(fr.lirmm.graphik.graal.api.core.AtomSet, fr.lirmm.graphik.graal.api.core.RuleSet)}.
 * @throws AtomSetException
 * @throws ParseException
 */
@Test
public void testDefaultKnowledgeBaseAtomSetRuleSet() throws AtomSetException, ParseException {
    Atom aa = DlgpParser.parseAtom("q(a).");
    Atom ab = DlgpParser.parseAtom("q(b).");
    Atom ac = DlgpParser.parseAtom("q(c).");
    Rule r = DlgpParser.parseRule("[R1] p(x) :- q(X).");
    NegativeConstraint nc = DlgpParser.parseNegativeConstraint("[NC] ! :- q(X), p(X).");
    AtomSet store = new DefaultInMemoryGraphStore();
    store.add(aa);
    store.add(ab);
    store.add(ac);
    RuleSet ruleset = new LinkedListRuleSet();
    ruleset.add(r);
    ruleset.add(nc);
    KnowledgeBase kb = new DefaultKnowledgeBase(store, ruleset);
    Assert.assertTrue(kb.getOntology().contains(r));
    Assert.assertTrue(kb.getOntology().contains(nc));
    Assert.assertTrue(kb.getFacts().contains(aa));
    Assert.assertTrue(kb.getFacts().contains(ab));
    Assert.assertTrue(kb.getFacts().contains(ac));
    kb.close();
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) KnowledgeBase(fr.lirmm.graphik.graal.api.kb.KnowledgeBase) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Atom(fr.lirmm.graphik.graal.api.core.Atom) NegativeConstraint(fr.lirmm.graphik.graal.api.core.NegativeConstraint) Test(org.junit.Test)

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