use of fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet in project graal by graphik-team.
the class LiteralsTest method stringType.
@Theory
public void stringType(AtomSet store) throws Exception {
Atom a = DlgpParser.parseAtom("<NAME>(a,\"john\").");
store.add(a);
ConjunctiveQuery q = new DefaultConjunctiveQuery(new LinkedListAtomSet(a), Collections.<Term>emptyList());
Assert.assertTrue(SmartHomomorphism.instance().execute(q, store).hasNext());
Atom b = store.iterator().next();
Assert.assertEquals(a, b);
}
use of fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet in project graal by graphik-team.
the class LiteralsTest method decimalType.
@Theory
public void decimalType(AtomSet store) throws Exception {
Atom a = DlgpParser.parseAtom("<AGE>(a,-5.1).");
store.add(a);
ConjunctiveQuery q = new DefaultConjunctiveQuery(new LinkedListAtomSet(a), Collections.<Term>emptyList());
Assert.assertTrue(SmartHomomorphism.instance().execute(q, store).hasNext());
Atom b = store.iterator().next();
Assert.assertEquals(a, b);
}
use of fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet 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.core.atomset.LinkedListAtomSet 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.core.atomset.LinkedListAtomSet in project graal by graphik-team.
the class PureQuery method removeAnswerPredicate.
public static void removeAnswerPredicate(ConjunctiveQuery query) {
Term[] ans = query.getAnswerVariables().toArray(new Term[query.getAnswerVariables().size()]);
CloseableIteratorWithoutException<Atom> ita = query.getAtomSet().iterator();
InMemoryAtomSet toRemove = new LinkedListAtomSet();
InMemoryAtomSet toAdd = new LinkedListAtomSet();
while (ita.hasNext()) {
Atom a = ita.next();
if (a.getPredicate().equals(ansPredicate)) {
Term ansTerm = ans[(Integer) ((Literal) a.getTerm(0)).getValue()];
if (!ansTerm.equals(a.getTerm(1))) {
toAdd.add(DefaultAtomFactory.instance().create(Predicate.EQUALITY, ansTerm, a.getTerm(1)));
}
toRemove.add(a);
}
}
query.getAtomSet().removeAll(toRemove);
query.getAtomSet().addAll(toAdd);
}
Aggregations