Search in sources :

Example 16 with Term

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

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

the class FixedOrderScheduler method execute.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public VarSharedData[] execute(InMemoryAtomSet h, List<Term> ans, AtomSet data, RulesCompilation rc) throws HomomorphismException {
    Set<Variable> terms = h.getVariables();
    VarSharedData[] vars = new VarSharedData[terms.size() + 2];
    int level = 0;
    vars[level] = new VarSharedData(level);
    Set<Term> alreadyAffected = new TreeSet<Term>();
    for (Variable v : this.order) {
        if (!terms.contains(v)) {
            throw new HomomorphismException("Try to schedule a variable which is not in the query :" + v);
        }
        if (alreadyAffected.contains(v)) {
            throw new HomomorphismException("There is two occurences of the same variable in the specified order.");
        }
        ++level;
        vars[level] = new VarSharedData(level);
        vars[level].value = v;
        alreadyAffected.add(v);
    }
    terms.removeAll(alreadyAffected);
    if (!terms.isEmpty()) {
        throw new HomomorphismException("Some variables of the query are not scheduled :" + terms);
    }
    ++level;
    vars[level] = new VarSharedData(level);
    vars[level].previousLevel = level - 1;
    return vars;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) TreeSet(java.util.TreeSet) Term(fr.lirmm.graphik.graal.api.core.Term) VarSharedData(fr.lirmm.graphik.graal.homomorphism.VarSharedData)

Example 18 with Term

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

the class DlgpParserTest method parseQueryWithIntegerLiteralInAns.

@Test
public void parseQueryWithIntegerLiteralInAns() throws ParseException {
    ConjunctiveQuery q = DlgpParser.parseQuery("?(1,X) :- p(a,X).");
    List<Term> ans = q.getAnswerVariables();
    Assert.assertEquals(L1, ans.get(0));
    Assert.assertEquals(X, ans.get(1));
    Atom a = q.getAtomSet().iterator().next();
    Assert.assertEquals(A, a.getTerm(0));
    Assert.assertEquals(X, a.getTerm(1));
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) Test(org.junit.Test)

Example 19 with Term

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

the class DlgpParserTest method parseQueryWithConstantInAns.

@Test
public void parseQueryWithConstantInAns() throws ParseException {
    ConjunctiveQuery q = DlgpParser.parseQuery("?(b,X) :- p(a,X).");
    List<Term> ans = q.getAnswerVariables();
    Assert.assertEquals(B, ans.get(0));
    Assert.assertEquals(X, ans.get(1));
    Atom a = q.getAtomSet().iterator().next();
    Assert.assertEquals(A, a.getTerm(0));
    Assert.assertEquals(X, a.getTerm(1));
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) Test(org.junit.Test)

Example 20 with Term

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

the class DlgpParserTest method parseQueryWithBooleanLiteralInAns.

@Test
public void parseQueryWithBooleanLiteralInAns() throws ParseException {
    ConjunctiveQuery q = DlgpParser.parseQuery("?(true,X) :- p(a,X).");
    List<Term> ans = q.getAnswerVariables();
    Assert.assertEquals(LTRUE, ans.get(0));
    Assert.assertEquals(X, ans.get(1));
    Atom a = q.getAtomSet().iterator().next();
    Assert.assertEquals(A, a.getTerm(0));
    Assert.assertEquals(X, a.getTerm(1));
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) Test(org.junit.Test)

Aggregations

Term (fr.lirmm.graphik.graal.api.core.Term)173 Atom (fr.lirmm.graphik.graal.api.core.Atom)86 LinkedList (java.util.LinkedList)41 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)36 Test (org.junit.Test)35 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)34 Variable (fr.lirmm.graphik.graal.api.core.Variable)32 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)27 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)26 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)23 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)18 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)15 Rule (fr.lirmm.graphik.graal.api.core.Rule)15 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)14 TreeSet (java.util.TreeSet)12 Constant (fr.lirmm.graphik.graal.api.core.Constant)9 DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)9 TreeMap (java.util.TreeMap)8