Search in sources :

Example 31 with LinkedListRuleSet

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

the class BackwardChainingTest method issue22.

/**
 * Given p(X,Y) :- q(X,Y). q(X,Y) :- a(X), p(X,Y). Then rewrite(?(X) :-
 * q(X,Y), p(Y,Z).) Return ?(X) :- q(X,Y), p(Y,Z). ?(X) :- a(X), p(X,Y),
 * p(Y,Z). ?(X) :- q(X,Y), q(Y,Z). ?(X) :- a(X), p(X,Y), q(Y,Z).
 *
 * @param compilation
 * @param operator
 * @throws IteratorException
 * @throws ParseException
 */
@Theory
public void issue22(RulesCompilation compilation, RewritingOperator operator) throws IteratorException, ParseException {
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("p(X,Y) :- q(X,Y)."));
    rules.add(DlgpParser.parseRule("q(X,Y) :- a(X), p(X,Y)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- q(X,Y), p(Y,Z).");
    compilation.compile(rules.iterator());
    PureRewriter bc = new PureRewriter(operator, true);
    CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
    int i = 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 32 with LinkedListRuleSet

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

the class IDCompilation method getSaturation.

// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public Iterable<Rule> getSaturation() {
    LinkedListRuleSet saturation = new LinkedListRuleSet();
    Map<Predicate, TreeMap<List<Integer>, InMemoryAtomSet>> newMap = new TreeMap<Predicate, TreeMap<List<Integer>, InMemoryAtomSet>>();
    // p -> q
    Predicate p, q;
    for (Map.Entry<Predicate, TreeMap<Predicate, LinkedList<IDCondition>>> e : this.conditions.entrySet()) {
        q = e.getKey();
        for (Map.Entry<Predicate, LinkedList<IDCondition>> map : e.getValue().entrySet()) {
            p = map.getKey();
            TreeMap<List<Integer>, InMemoryAtomSet> head = newMap.get(p);
            if (head == null) {
                head = new TreeMap<List<Integer>, InMemoryAtomSet>(new ListComparator<Integer>());
                newMap.put(p, head);
            }
            for (IDCondition conditionPQ : map.getValue()) {
                InMemoryAtomSet atomSet = head.get(conditionPQ.getBody());
                if (atomSet == null) {
                    atomSet = new LinkedListAtomSet();
                    head.put(conditionPQ.getBody(), atomSet);
                }
                atomSet.add(new DefaultAtom(q, conditionPQ.generateHead()));
            }
        }
    }
    for (Map.Entry<Predicate, TreeMap<List<Integer>, InMemoryAtomSet>> e1 : newMap.entrySet()) {
        p = e1.getKey();
        for (Map.Entry<List<Integer>, InMemoryAtomSet> e2 : e1.getValue().entrySet()) {
            List<Term> terms = new LinkedList<Term>();
            for (Integer i : e2.getKey()) {
                terms.add(DefaultTermFactory.instance().createVariable("X" + i));
            }
            InMemoryAtomSet body = new LinkedListAtomSet();
            body.add(new DefaultAtom(p, terms));
            saturation.add(DefaultRuleFactory.instance().create(body, e2.getValue()));
        }
    }
    return saturation;
}
Also used : DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) ListComparator(fr.lirmm.graphik.util.collections.ListComparator) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Term(fr.lirmm.graphik.graal.api.core.Term) TreeMap(java.util.TreeMap) LinkedList(java.util.LinkedList) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 33 with LinkedListRuleSet

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

the class RuleSetTest method test.

@Test
public void test() {
    RuleSet rs = new LinkedListRuleSet();
    rs.add(rule1);
    rs.add(rule2);
    Iterator<Rule> it = rs.iterator();
    Assert.assertTrue(it.hasNext());
    it.next();
    Assert.assertTrue(it.hasNext());
}
Also used : LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) Test(org.junit.Test)

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