use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery 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);
}
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class RewritinCloseableIterator method next.
@Override
public ConjunctiveQuery next() {
if (this.rewrites == null) {
this.compute();
}
ConjunctiveQuery query = this.rewrites.next();
PureQuery.removeAnswerPredicate(query);
return query;
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class Utils method rewrite.
/**
* Rewrite the fact q according to the unifier u.
*
* @param q
* the fact to rewrite
* @param u
* the unifier between q and r
* @return the rewrite of q according to the unifier u.
*/
public static ConjunctiveQuery rewrite(ConjunctiveQuery q, QueryUnifier u) {
InMemoryAtomSet ajout = u.getImageOf(u.getRule().getBody());
InMemoryAtomSet restant = u.getImageOf(AtomSetUtils.minus(q.getAtomSet(), u.getPiece()));
ConjunctiveQuery rew = null;
if (ajout != null && restant != null) {
// FIXME
InMemoryAtomSet res = AtomSetUtils.union(ajout, restant);
List<Term> ansVar = new LinkedList<Term>();
ansVar.addAll(q.getAnswerVariables());
rew = DefaultConjunctiveQueryFactory.instance().create(res, ansVar);
}
return rew;
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class Utils method computeCover.
/**
* Remove the fact that are not the most general (taking account of compiled
* rules) in the given facts
*
* @param comp
*/
public static void computeCover(Iterable<ConjunctiveQuery> set, RulesCompilation comp) {
Iterator<ConjunctiveQuery> beg = set.iterator();
Iterator<ConjunctiveQuery> end;
InMemoryAtomSet q;
InMemoryAtomSet o;
boolean finished;
while (beg.hasNext()) {
q = beg.next().getAtomSet();
finished = false;
end = set.iterator();
while (!finished && end.hasNext()) {
o = end.next().getAtomSet();
if (o != q && isMoreGeneralThan(o, q, comp)) {
finished = true;
beg.remove();
}
}
}
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class AggregSingleRuleOperator method getRewritesFrom.
// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
/**
* Returns the rewrites compute from the given fact and the rule set of the
* receiving object.
*
* @param q
* A fact
* @return the ArrayList that contains the rewrites compute from the given
* fact and the rule set of the receiving object.
*/
@Override
public Collection<ConjunctiveQuery> getRewritesFrom(ConjunctiveQuery q, IndexedByHeadPredicatesRuleSet ruleSet, RulesCompilation compilation) {
LinkedList<ConjunctiveQuery> rewriteSet = new LinkedList<ConjunctiveQuery>();
Collection<QueryUnifier> unifiers = new LinkedList<QueryUnifier>();
for (Rule r : getUnifiableRules(q.getAtomSet().predicatesIterator(), ruleSet, compilation)) {
unifiers.addAll(getSRUnifier(q, r, compilation));
}
/**
* compute the rewrite from the unifier *
*/
ConjunctiveQuery a;
for (QueryUnifier u : unifiers) {
a = Utils.rewrite(q, u);
if (a != null) {
rewriteSet.add(a);
}
}
return rewriteSet;
}
Aggregations