Search in sources :

Example 76 with ConjunctiveQuery

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

Example 77 with ConjunctiveQuery

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

Example 78 with ConjunctiveQuery

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

Example 79 with ConjunctiveQuery

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;
}
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 80 with ConjunctiveQuery

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;
}
Also used : ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) LinkedList(java.util.LinkedList)

Aggregations

ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)113 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)58 Theory (org.junit.experimental.theories.Theory)57 Atom (fr.lirmm.graphik.graal.api.core.Atom)34 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)29 Test (org.junit.Test)29 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)23 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)22 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)22 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)19 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)18 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)14 Term (fr.lirmm.graphik.graal.api.core.Term)14 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)14 LinkedList (java.util.LinkedList)13 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)12 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)11 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)10 TripleStore (fr.lirmm.graphik.graal.api.store.TripleStore)9 RulesCompilation (fr.lirmm.graphik.graal.api.core.RulesCompilation)7