Search in sources :

Example 31 with LinkedListAtomSet

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);
}
Also used : DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Atom(fr.lirmm.graphik.graal.api.core.Atom) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Example 32 with LinkedListAtomSet

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);
}
Also used : DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Atom(fr.lirmm.graphik.graal.api.core.Atom) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Example 33 with LinkedListAtomSet

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);
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution)

Example 34 with LinkedListAtomSet

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;
}
Also used : LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Example 35 with LinkedListAtomSet

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);
}
Also used : Literal(fr.lirmm.graphik.graal.api.core.Literal) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Aggregations

LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)52 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)35 Atom (fr.lirmm.graphik.graal.api.core.Atom)28 LinkedList (java.util.LinkedList)19 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)18 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)17 Term (fr.lirmm.graphik.graal.api.core.Term)17 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)11 Theory (org.junit.experimental.theories.Theory)11 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)9 Test (org.junit.Test)9 Variable (fr.lirmm.graphik.graal.api.core.Variable)8 DefaultConjunctiveQueryWithNegatedParts (fr.lirmm.graphik.graal.core.DefaultConjunctiveQueryWithNegatedParts)8 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)7 Rule (fr.lirmm.graphik.graal.api.core.Rule)6 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)5 SubstitutionIterator2AtomIterator (fr.lirmm.graphik.graal.core.stream.SubstitutionIterator2AtomIterator)5 SqlHomomorphism (fr.lirmm.graphik.graal.store.rdbms.homomorphism.SqlHomomorphism)5 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)4 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)4