Search in sources :

Example 96 with Substitution

use of fr.lirmm.graphik.graal.api.core.Substitution 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 97 with Substitution

use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.

the class ConjunctiveQueryWithFixedVariables method computeFixedQuery.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private static InMemoryAtomSet computeFixedQuery(InMemoryAtomSet atomset, Iterable<? extends Term> fixedTerms) {
    // create a Substitution for fixed query
    InMemoryAtomSet fixedQuery = DefaultAtomSetFactory.instance().create();
    Substitution fixSub = DefaultSubstitutionFactory.instance().createSubstitution();
    for (Term t : fixedTerms) {
        if (t.isVariable()) {
            fixSub.put((Variable) t, DefaultTermFactory.instance().createConstant(t.getLabel()));
        }
    }
    // apply substitution
    CloseableIteratorWithoutException<Atom> it = atomset.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        fixedQuery.add(fixSub.createImageOf(a));
    }
    return fixedQuery;
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 98 with Substitution

use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.

the class ProbaUtils method computeProba.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
/**
 * Compute the probability to have one specific instance of the specified
 * atom over data
 *
 * @param atom
 * @param data
 * @param rc
 * @return the probability to have the specified atom over specified data.
 */
public static double computeProba(Atom atom, Store data, RulesCompilation rc) {
    int count = 0;
    for (Pair<Atom, Substitution> im : rc.getRewritingOf(atom)) {
        count += data.size(im.getLeft().getPredicate());
    }
    double probaA;
    if (count == 0) {
        probaA = 0.0;
    } else {
        int nbCst = 0;
        int nbVar = 0;
        for (Term t : atom) {
            if (t.isConstant()) {
                ++nbCst;
            } else {
                ++nbVar;
            }
        }
        int domSize = data.getDomainSize();
        probaA = (count / (Math.pow(domSize, nbCst)) / Math.pow(data.getDomainSize(), nbVar));
    }
    return probaA;
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 99 with Substitution

use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.

the class ForwardCheckingTest method simpleFCTest1.

@Test
public void simpleFCTest1() throws HomomorphismException, IteratorException, ParseException, AtomSetException {
    Profiler profiler = new CPUTimeProfiler();
    InMemoryAtomSet data = new DefaultInMemoryGraphStore();
    data.addAll(DlgpParser.parseAtomSet("p(a,b), q(b,c)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y,Z) :- p(X,Y), q(Y,Z).");
    Homomorphism<ConjunctiveQuery, AtomSet> h = new BacktrackHomomorphism(new SimpleFC());
    h.setProfiler(profiler);
    CloseableIterator<Substitution> results = h.execute(query, data);
    while (results.hasNext()) {
        results.next();
    }
    results.close();
    Assert.assertEquals(7, profiler.get("#calls"));
}
Also used : SimpleFC(fr.lirmm.graphik.graal.homomorphism.forward_checking.SimpleFC) Profiler(fr.lirmm.graphik.util.profiler.Profiler) CPUTimeProfiler(fr.lirmm.graphik.util.profiler.CPUTimeProfiler) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) CPUTimeProfiler(fr.lirmm.graphik.util.profiler.CPUTimeProfiler) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Test(org.junit.Test)

Example 100 with Substitution

use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.

the class ForwardCheckingTest method NFC2Test2.

@Test
public void NFC2Test2() throws HomomorphismException, IteratorException, ParseException {
    Profiler profiler = new CPUTimeProfiler();
    Predicate[] predicates = { new Predicate("p", 2), new Predicate("q", 2), new Predicate("r", 2) };
    InMemoryAtomSet data = new DefaultInMemoryGraphStore();
    TestUtil.addNAtoms(data, 32, predicates, 5, new Random(0));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y,Z) :- p(X,Y), q(X,Z), r(Y,Z).");
    Homomorphism<ConjunctiveQuery, AtomSet> h = new BacktrackHomomorphism(new NFC2());
    h.setProfiler(profiler);
    CloseableIterator<Substitution> results = h.execute(query, data);
    while (results.hasNext()) {
        results.next();
    }
    results.close();
}
Also used : NFC2(fr.lirmm.graphik.graal.homomorphism.forward_checking.NFC2) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) CPUTimeProfiler(fr.lirmm.graphik.util.profiler.CPUTimeProfiler) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Profiler(fr.lirmm.graphik.util.profiler.Profiler) CPUTimeProfiler(fr.lirmm.graphik.util.profiler.CPUTimeProfiler) Random(java.util.Random) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Test(org.junit.Test)

Aggregations

Substitution (fr.lirmm.graphik.graal.api.core.Substitution)158 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)58 Test (org.junit.Test)55 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)46 Theory (org.junit.experimental.theories.Theory)44 Atom (fr.lirmm.graphik.graal.api.core.Atom)41 Term (fr.lirmm.graphik.graal.api.core.Term)36 LinkedList (java.util.LinkedList)27 Variable (fr.lirmm.graphik.graal.api.core.Variable)25 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)24 Rule (fr.lirmm.graphik.graal.api.core.Rule)23 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)19 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)18 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)16 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)15 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)14 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)14 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)13 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)13 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)13