Search in sources :

Example 6 with Profiler

use of fr.lirmm.graphik.util.profiler.Profiler in project graal by graphik-team.

the class NFC2WithLimit method select.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
protected boolean select(Atom atom, Var v, AtomSet g, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws AtomSetException, IteratorException {
    boolean contains = false;
    int nbAns = 0;
    Iterator<Pair<Atom, Substitution>> rewIt = rc.getRewritingOf(atom).iterator();
    Set<Var> postVarsFromThisAtom = new HashSet<Var>();
    while (rewIt.hasNext() && nbAns < LIMIT) {
        Atom a = rewIt.next().getLeft();
        Var[] postV = this.computePostVariablesPosition(a, v.shared.level, map, varData, postVarsFromThisAtom);
        Atom im = BacktrackUtils.createImageOf(a, initialSubstitution, map, varData);
        Profiler profiler = this.getProfiler();
        if (profiler != null) {
            profiler.incr("#Select", 1);
            profiler.start("SelectTime");
        }
        int cpt = 0;
        CloseableIterator<? extends Atom> it = g.match(im);
        while (it.hasNext() && nbAns < LIMIT) {
            ++nbAns;
            ++cpt;
            int i = -1;
            for (Term t : it.next()) {
                ++i;
                if (postV[i] != null) {
                    this.data[postV[i].shared.level].tmp.add(t);
                }
            }
            contains = true;
        }
        if (profiler != null) {
            profiler.stop("SelectTime");
            profiler.incr("#SelectAns", cpt);
        }
    }
    boolean isThereAnEmptiedList = false;
    if (contains) {
        // set computed candidats for post variables
        for (Var z : postVarsFromThisAtom) {
            if (!isThereAnEmptiedList) {
                if (nbAns >= LIMIT) {
                    this.dataWithLimit[z.shared.level].atomsToCheck.add(atom);
                } else {
                    AcceptableCandidats ac = this.data[z.shared.level].candidats[v.shared.level];
                    if (ac.init) {
                        ac.candidats.retainAll(this.data[z.shared.level].tmp);
                        isThereAnEmptiedList |= ac.candidats.isEmpty();
                        if (ac.candidats.isEmpty()) {
                            this.bj.addNeighborhoodToBackjumpSet(z.shared, v.shared);
                        }
                    } else {
                        ac.candidats.addAll(this.data[z.shared.level].tmp);
                        ac.init = true;
                    }
                }
            }
            this.data[z.shared.level].tmp.clear();
        }
    } else {
        Var z = postVarsFromThisAtom.iterator().next();
        this.bj.addNeighborhoodToBackjumpSet(z.shared, v.shared);
    }
    return contains && !isThereAnEmptiedList;
}
Also used : Var(fr.lirmm.graphik.graal.homomorphism.Var) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom) Profiler(fr.lirmm.graphik.util.profiler.Profiler) Pair(org.apache.commons.lang3.tuple.Pair) HashSet(java.util.HashSet)

Example 7 with Profiler

use of fr.lirmm.graphik.util.profiler.Profiler 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 8 with Profiler

use of fr.lirmm.graphik.util.profiler.Profiler 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)

Example 9 with Profiler

use of fr.lirmm.graphik.util.profiler.Profiler in project graal by graphik-team.

the class SimpleFC method checkForward.

@Override
public boolean checkForward(Var v, AtomSet g, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws BacktrackException {
    Profiler profiler = this.getProfiler();
    for (Atom atom : v.shared.postAtoms) {
        boolean contains = false;
        Atom im = BacktrackUtils.createImageOf(atom, initialSubstitution, map, varData);
        if (profiler != null) {
            profiler.incr("#selectOne", 1);
            profiler.start("selectOneTime");
        }
        for (Pair<Atom, Substitution> rew : rc.getRewritingOf(im)) {
            Atom a = rew.getLeft();
            CloseableIterator<Atom> matchIt = null;
            try {
                matchIt = g.match(a);
                if (matchIt.hasNext()) {
                    contains = true;
                    break;
                }
            } catch (IteratorException e) {
                throw new BacktrackException(e);
            } catch (AtomSetException e) {
                throw new BacktrackException(e);
            } finally {
                if (matchIt != null) {
                    matchIt.close();
                }
            }
        }
        if (profiler != null) {
            profiler.stop("selectOneTime");
        }
        if (!contains) {
            return false;
        }
    }
    return true;
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Profiler(fr.lirmm.graphik.util.profiler.Profiler) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Atom(fr.lirmm.graphik.graal.api.core.Atom) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException)

Example 10 with Profiler

use of fr.lirmm.graphik.util.profiler.Profiler in project graal by graphik-team.

the class PureHomomorphismImpl method exist.

// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
/**
 * @return true if there exist a homomorphism.
 * @throws HomomorphismException
 */
public boolean exist() throws HomomorphismException {
    // check if the query is empty
    if (source == null || !source.iterator().hasNext()) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Empty query");
        }
        return true;
    }
    // /////////////////////////////////////////////////////////////////////
    // Initialisation
    Profiler profiler = this.getProfiler();
    profiler.start("preprocessing time");
    boolean res = this.initialiseHomomorphism();
    profiler.stop("preprocessing time");
    if (res) {
        profiler.start("backtracking time");
        res = this.backtrack();
        profiler.stop("backtracking time");
    }
    return res;
}
Also used : Profiler(fr.lirmm.graphik.util.profiler.Profiler)

Aggregations

Profiler (fr.lirmm.graphik.util.profiler.Profiler)10 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)7 Atom (fr.lirmm.graphik.graal.api.core.Atom)4 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)4 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)4 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)4 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)4 CPUTimeProfiler (fr.lirmm.graphik.util.profiler.CPUTimeProfiler)4 Test (org.junit.Test)4 Term (fr.lirmm.graphik.graal.api.core.Term)3 NFC2 (fr.lirmm.graphik.graal.homomorphism.forward_checking.NFC2)3 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)2 Var (fr.lirmm.graphik.graal.homomorphism.Var)2 HashSet (java.util.HashSet)2 Random (java.util.Random)2 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)1 BacktrackException (fr.lirmm.graphik.graal.homomorphism.BacktrackException)1 SimpleFC (fr.lirmm.graphik.graal.homomorphism.forward_checking.SimpleFC)1 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)1 Pair (org.apache.commons.lang3.tuple.Pair)1