use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery 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.api.core.ConjunctiveQuery in project graal by graphik-team.
the class RewritinCloseableIterator method compute.
// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private void compute() {
if (this.getProfiler() != null && this.getProfiler().isProfilingEnabled()) {
this.getProfiler().trace(this.pquery.getLabel());
}
IndexedByHeadPredicatesRuleSet indexedRuleSet = new IndexedByHeadPredicatesRuleSet(this.ruleset);
// rewriting
RewritingAlgorithm algo = new RewritingAlgorithm(this.operator);
operator.setProfiler(this.getProfiler());
algo.setProfiler(this.getProfiler());
Iterable<ConjunctiveQuery> queries = algo.execute(pquery, indexedRuleSet, compilation);
if (this.unfolding) {
queries = Utils.unfold(queries, this.compilation, this.getProfiler());
}
this.rewrites = new CloseableIteratorAdapter<ConjunctiveQuery>(queries.iterator());
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class AggregAllRulesOperator 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>();
List<QueryUnifier> unifiers;
for (Rule r : getUnifiableRules(q.getAtomSet().predicatesIterator(), ruleSet, compilation)) {
unifiers = getSinglePieceUnifiers(q, r, compilation);
for (QueryUnifier u : unifiers) {
rewriteSet.add(Utils.rewriteWithMark(q, u));
}
}
return rewriteSet;
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class ForwardCheckingTest method simpleFCTest1.
@Test
public void simpleFCTest1() throws HomomorphismException, IteratorException, ParseException, AtomSetException {
Profiler profiler = new CPUTimeProfiler();
InMemoryAtomSet data = new DefaultInMemoryGraphStore();
data.addAll(DlgpParser.parseAtomSet("p(a,b), q(b,c)."));
ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y,Z) :- p(X,Y), q(Y,Z).");
Homomorphism<ConjunctiveQuery, AtomSet> h = new BacktrackHomomorphism(new SimpleFC());
h.setProfiler(profiler);
CloseableIterator<Substitution> results = h.execute(query, data);
while (results.hasNext()) {
results.next();
}
results.close();
Assert.assertEquals(7, profiler.get("#calls"));
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class ForwardCheckingTest method NFC2Test2.
@Test
public void NFC2Test2() throws HomomorphismException, IteratorException, ParseException {
Profiler profiler = new CPUTimeProfiler();
Predicate[] predicates = { new Predicate("p", 2), new Predicate("q", 2), new Predicate("r", 2) };
InMemoryAtomSet data = new DefaultInMemoryGraphStore();
TestUtil.addNAtoms(data, 32, predicates, 5, new Random(0));
ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y,Z) :- p(X,Y), q(X,Z), r(Y,Z).");
Homomorphism<ConjunctiveQuery, AtomSet> h = new BacktrackHomomorphism(new NFC2());
h.setProfiler(profiler);
CloseableIterator<Substitution> results = h.execute(query, data);
while (results.hasNext()) {
results.next();
}
results.close();
}
Aggregations