use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class ConjunctiveQuery2Test method wrongArityQuery.
@Theory
public void wrongArityQuery(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
Assume.assumeFalse(store instanceof TripleStore);
try {
store.add(DlgpParser.parseAtom("<P>(a,b)."));
ConjunctiveQuery query = DlgpParser.parseQuery("? :- <P>(X).");
CloseableIterator<Substitution> subReader;
subReader = h.execute(query, store);
Assert.assertFalse(subReader.hasNext());
subReader.close();
} catch (Exception e) {
Assert.assertTrue(e.getMessage(), false);
}
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class ConjunctiveQuery2Test method diffLiteralQueryTest.
@Theory
public void diffLiteralQueryTest(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
try {
store.add(DlgpParser.parseAtom("<P>(a,\"literal\")."));
ConjunctiveQuery query = DlgpParser.parseQuery("? :- <P>(a,\"otherLiteral\").");
CloseableIterator<Substitution> subReader;
subReader = h.execute(query, store);
Assert.assertFalse("Error on " + store.getClass(), subReader.hasNext());
subReader.close();
} catch (Exception e) {
Assert.assertTrue(e.getMessage(), false);
}
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class ConjunctiveQuery2Test method NFC2WithLimit8Test.
@Theory
public void NFC2WithLimit8Test(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
try {
store.addAll(DlgpParser.parseAtomSet("<Q>(k,a),<Q>(k,k),<P>(k,a),<P>(k,b),<P>(k,c),<P>(k,d),<P>(k,e),<P>(k,f),<P>(k,g),<P>(k,h),<P>(k,i)."));
ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y,Z) :- <P>(X,Z),<Q>(Y,Z).");
CloseableIterator<Substitution> subReader = h.execute(query, store);
Assert.assertTrue(subReader.hasNext());
subReader.next();
Assert.assertFalse(subReader.hasNext());
subReader.close();
} catch (Exception e) {
Assert.assertTrue(e.getMessage(), false);
}
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class Utils method developpRewriting.
// /////////////////////////////////////////////////////////////////////////
// PRIVATE FUNCTIONS
// /////////////////////////////////////////////////////////////////////////
/**
* Add in the given rewriting set the rewrites that can be entailed from the
* predicate order ex: the rewrite A(x) can be entailed from the rewrite
* B(x) and the predicate order A > B
*
* @return a Collection of unfolded rewritings.
*/
private static Collection<ConjunctiveQuery> developpRewriting(Iterable<ConjunctiveQuery> rewritingSet, RulesCompilation compilation) {
Collection<ConjunctiveQuery> unfoldingRewritingSet = new LinkedList<ConjunctiveQuery>();
LinkedList<Pair<InMemoryAtomSet, Substitution>> newQueriesBefore = new LinkedList<Pair<InMemoryAtomSet, Substitution>>();
LinkedList<Pair<InMemoryAtomSet, Substitution>> newQueriesAfter = new LinkedList<Pair<InMemoryAtomSet, Substitution>>();
LinkedList<Pair<InMemoryAtomSet, Substitution>> newQueriesTmp;
Iterable<Pair<Atom, Substitution>> atomsRewritings;
InMemoryAtomSet copy;
for (ConjunctiveQuery originalQuery : rewritingSet) {
if (Thread.currentThread().isInterrupted()) {
break;
}
// q = query.getIrredondant(compilation);
// for all atom of the query we will build a list of all the
// rewriting
newQueriesBefore.clear();
newQueriesBefore.add(new ImmutablePair<InMemoryAtomSet, Substitution>(new LinkedListAtomSet(), DefaultSubstitutionFactory.instance().createSubstitution()));
// we will build all the possible fact from the rewriting of the
// atoms
CloseableIteratorWithoutException<Atom> it = originalQuery.iterator();
while (!Thread.currentThread().isInterrupted() && it.hasNext()) {
Atom a = it.next();
atomsRewritings = compilation.getRewritingOf(a);
for (Pair<InMemoryAtomSet, Substitution> before : newQueriesBefore) {
// query and add the atom
for (Pair<Atom, Substitution> rew : atomsRewritings) {
//
copy = new LinkedListAtomSet(before.getLeft());
copy.add(rew.getLeft());
Substitution newSub = Substitutions.aggregate(before.getRight(), rew.getRight());
if (newSub != null) {
newQueriesAfter.add(new ImmutablePair<InMemoryAtomSet, Substitution>(copy, newSub));
}
}
}
// switch list
newQueriesTmp = newQueriesBefore;
newQueriesBefore = newQueriesAfter;
newQueriesAfter = newQueriesTmp;
newQueriesAfter.clear();
}
for (Pair<InMemoryAtomSet, Substitution> before : newQueriesBefore) {
if (Thread.currentThread().isInterrupted()) {
break;
}
Substitution s = before.getRight();
InMemoryAtomSet atomset = before.getLeft();
atomset = s.createImageOf(atomset);
List<Term> ans = s.createImageOf(originalQuery.getAnswerVariables());
unfoldingRewritingSet.add(DefaultConjunctiveQueryFactory.instance().create(atomset, ans));
}
}
return unfoldingRewritingSet;
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class RewritingAlgorithm method execute.
// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
/**
* Compute and returns all the most general rewrites of the object's query
*
* @return a list of the most general rewrite computed for the specified query and
* the specified compilation
*
* @author Mélanie KÖNIG
*/
public Collection<ConjunctiveQuery> execute(ConjunctiveQuery query, IndexedByHeadPredicatesRuleSet ruleSet, RulesCompilation compilation) {
if (this.verbose) {
this.profiler.trace(query.toString());
this.profiler.put("CONFIG", operator.getClass().getSimpleName());
}
LinkedList<ConjunctiveQuery> finalRewritingSet = new LinkedList<ConjunctiveQuery>();
Queue<ConjunctiveQuery> rewriteSetToExplore = new LinkedList<ConjunctiveQuery>();
Collection<ConjunctiveQuery> currentRewriteSet;
int exploredRewrites = 0;
int generatedRewrites = 0;
if (this.verbose) {
this.profiler.clear("Rewriting time");
this.profiler.start("Rewriting time");
}
// remove some basic redundancy
PureQuery pquery = new PureQuery(compilation.getIrredondant(query.getAtomSet()), query.getAnswerVariables());
pquery.addAnswerPredicate();
rewriteSetToExplore.add(pquery);
finalRewritingSet.add(pquery);
ConjunctiveQuery q;
while (!Thread.currentThread().isInterrupted() && !rewriteSetToExplore.isEmpty()) {
/* take the first query to rewrite */
q = rewriteSetToExplore.poll();
// stats
++exploredRewrites;
/* compute all the rewrite from it */
currentRewriteSet = this.operator.getRewritesFrom(q, ruleSet, compilation);
// stats
generatedRewrites += currentRewriteSet.size();
/* keep only the most general among query just computed */
Utils.computeCover(currentRewriteSet, compilation);
/*
* keep only the query just computed that are more general than
* query already compute
*/
selectMostGeneralFromRelativeTo(currentRewriteSet, finalRewritingSet, compilation);
// keep to explore only most general query
selectMostGeneralFromRelativeTo(rewriteSetToExplore, currentRewriteSet, compilation);
// add to explore the query just computed that we keep
rewriteSetToExplore.addAll(currentRewriteSet);
/*
* keep in final rewrite set only query more general than query just
* computed
*/
selectMostGeneralFromRelativeTo(finalRewritingSet, currentRewriteSet, compilation);
// add in final rewrite set the query just compute that we keep
finalRewritingSet.addAll(currentRewriteSet);
}
/* clean the rewrites to return */
Utils.computeCover(finalRewritingSet);
if (this.verbose) {
this.profiler.stop("Rewriting time");
this.profiler.put("Generated rewritings", generatedRewrites);
this.profiler.put("Explored rewritings", exploredRewrites);
this.profiler.put("Pivotal rewritings", finalRewritingSet.size());
}
return finalRewritingSet;
}
Aggregations