use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class ConjunctiveQueryTest method booleanQueryTest.
/**
* Test a boolean query
*/
@Theory
public void booleanQueryTest(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
try {
store.addAll(DlgpParser.parseAtomSet("<P>(a,b).<P>(b,c).<Q>(a,c).<Q>(d,c)."));
InMemoryAtomSet queryAtomSet = new LinkedListAtomSet();
queryAtomSet.add(DlgpParser.parseAtom("<Q>(a,c)."));
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(queryAtomSet);
CloseableIterator<Substitution> subReader;
Substitution sub;
subReader = h.execute(query, store);
Assert.assertTrue(subReader.hasNext());
sub = subReader.next();
Assert.assertEquals(0, sub.getTerms().size());
Assert.assertFalse(subReader.hasNext());
subReader.close();
} catch (Exception e) {
Assert.assertTrue(e.getMessage(), false);
}
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class ConjunctiveQueryTest method booleanQueryWithoutAnswerTest.
/**
* Test a boolean query
*/
@Theory
public void booleanQueryWithoutAnswerTest(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
try {
InMemoryAtomSet queryAtomSet = new LinkedListAtomSet();
queryAtomSet.add(DlgpParser.parseAtom("<Q>(a,c)."));
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(queryAtomSet);
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.InMemoryAtomSet in project graal by graphik-team.
the class StoreTest method bugConcurrentModificationException.
@Theory
public void bugConcurrentModificationException(InMemoryAtomSet store) throws IteratorException, RuleApplicationException, AtomSetException {
Assume.assumeTrue(store instanceof Store);
Rule r = DlgpParser.parseRule("<T>(Z,W), <P>(Y,W) :- <P>(X,Y).");
store.addAll(DlgpParser.parseAtomSet("<P>(a,a)."));
try {
DefaultRuleApplier<InMemoryAtomSet> applier = new DefaultRuleApplier<InMemoryAtomSet>();
applier.apply(r, store);
applier.apply(r, store);
} catch (Exception e) {
Assert.fail();
}
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class Utils method getSafeCopy.
public static Rule getSafeCopy(Rule rule) {
Substitution substitution = new TreeMapSubstitution();
for (Variable t : rule.getVariables()) {
substitution.put(t, varGen.getFreshSymbol());
}
InMemoryAtomSet body = rule.getBody();
InMemoryAtomSet head = rule.getHead();
InMemoryAtomSet safeBody = new LinkedListAtomSet();
InMemoryAtomSet safeHead = new LinkedListAtomSet();
substitution.apply(body, safeBody);
substitution.apply(head, safeHead);
return DefaultRuleFactory.instance().create(safeBody, safeHead);
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet 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;
}
Aggregations