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);
}
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;
}
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());
}
Aggregations