Search in sources :

Example 11 with Term

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

the class DlgpParserTest method parseAtomSetWithPrefix.

@Test
public void parseAtomSetWithPrefix() throws ParseException, IteratorException {
    CloseableIterator<Atom> it = DlgpParser.parseAtomSet("@prefix ex: <http://example.com/> ex:p(a). ex:p(b), ex:p(1).");
    int cpt = 0;
    while (it.hasNext()) {
        ++cpt;
        Atom a = it.next();
        Term t = a.getTerm(0);
        Assert.assertTrue(t.equals(B) || t.equals(A) || t.equals(L1));
    }
    Assert.assertEquals(3, cpt);
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom) NegativeConstraint(fr.lirmm.graphik.graal.api.core.NegativeConstraint) Test(org.junit.Test)

Example 12 with Term

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

the class AbstractNFC method init.

@Override
public void init(VarSharedData[] vars, Map<Variable, Integer> map) {
    this.data = new VarData[vars.length];
    for (int i = 0; i < vars.length; ++i) {
        this.data[vars[i].level] = new VarData();
        this.data[vars[i].level].candidats = new AcceptableCandidats[vars.length];
        this.data[vars[i].level].tmp = new HashSet<Term>();
        this.data[vars[i].level].toCheckAfterAssignment = new LinkedList<Atom>();
        for (Atom a : vars[i].preAtoms) {
            int cpt = 0;
            boolean toAdd = true;
            for (Variable t : a.getVariables()) {
                if (map.containsKey(t)) {
                    if (t.equals(vars[i].value))
                        ++cpt;
                    else
                        toAdd = false;
                }
            }
            if (toAdd || cpt > 1) {
                this.data[vars[i].level].toCheckAfterAssignment.add(a);
            }
        }
        AcceptableCandidats previous = new AcceptableCandidats();
        for (VarSharedData z : vars[i].preVars) {
            AcceptableCandidats ac = new AcceptableCandidats();
            ac.candidats = new TreeSet<Term>();
            ac.previous = previous;
            previous = ac;
            this.data[vars[i].level].candidats[z.level] = ac;
        }
        this.data[vars[i].level].last = previous;
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Term(fr.lirmm.graphik.graal.api.core.Term) VarSharedData(fr.lirmm.graphik.graal.homomorphism.VarSharedData) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 13 with Term

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

the class AbstractNFC method check.

protected boolean check(Atom atom, VarSharedData currentVar, VarSharedData varToCompute, AtomSet g, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws AtomSetException {
    Substitution s = BacktrackUtils.createSubstitution(varData);
    s.put(initialSubstitution);
    Atom im = s.createImageOf(atom);
    this.data[varToCompute.level].tmp.clear();
    Set<Term> candidats = this.data[varToCompute.level].candidats[currentVar.level].candidats;
    // FIXME bug with p(X,Y,Z) -> q(X,Y) in the compilation
    for (Pair<Atom, Substitution> rew : rc.getRewritingOf(im)) {
        Atom a = rew.getLeft();
        Iterator<Term> it = candidats.iterator();
        while (it.hasNext()) {
            Term t = it.next();
            Atom fullInstantiatedAtom = Substitutions.createImageOf(a, varToCompute.value, t);
            Profiler profiler = this.getProfiler();
            if (profiler != null) {
                profiler.incr("#check", 1);
                profiler.start("checkTime");
            }
            if (g.contains(fullInstantiatedAtom)) {
                this.data[varToCompute.level].tmp.add(t);
            }
            if (profiler != null) {
                profiler.stop("checkTime");
            }
        }
    }
    candidats.retainAll(this.data[varToCompute.level].tmp);
    this.data[varToCompute.level].tmp.clear();
    if (candidats.isEmpty()) {
        this.bj.addNeighborhoodToBackjumpSet(varToCompute, currentVar);
        return false;
    } else {
        return true;
    }
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Profiler(fr.lirmm.graphik.util.profiler.Profiler) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 14 with Term

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

the class AbstractNFC method select.

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;
    Set<Var> postVarsFromThisAtom = new HashSet<Var>();
    for (Pair<Atom, Substitution> rew : rc.getRewritingOf(atom)) {
        Atom a = rew.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 nbAns = 0;
        CloseableIterator<? extends Atom> it = g.match(im);
        while (it.hasNext()) {
            ++nbAns;
            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", nbAns);
        }
    }
    boolean isThereAnEmptiedList = false;
    if (contains) {
        // set computed candidats for post variables
        for (Var z : postVarsFromThisAtom) {
            if (!isThereAnEmptiedList) {
                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) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Profiler(fr.lirmm.graphik.util.profiler.Profiler) HashSet(java.util.HashSet)

Example 15 with Term

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

the class AbstractNFC method computePostVariablesPosition.

/**
 * Return an array containing the corresponding instance of Var class for
 * each position of a variable in the specified atom with a higher level
 * than the specified level. Constant, literal and lower or equals level
 * variable postions contain null value.
 *
 * @param atom
 * @param level
 * @param map
 *            Correspondence between Variable instance and Var instance.
 * @param postVars
 *            output parameter that is a Set in which must be added higher
 *            level variables from this atom.
 * @return an array containing the coresseponding instance of Var class for
 * each position of a variable in the specified atom with a higher level
 * than the specified level.
 */
protected Var[] computePostVariablesPosition(Atom atom, int level, Map<Variable, Integer> map, Var[] varData, Set<Var> postVars) {
    Var[] postV = new Var[atom.getPredicate().getArity()];
    int i = -1;
    for (Term t : atom) {
        ++i;
        Integer idx = map.get(t);
        if (idx != null) {
            Var z = varData[idx];
            if (z.shared.level > level) {
                postV[i] = z;
                postVars.add(z);
            }
        }
    }
    return postV;
}
Also used : Var(fr.lirmm.graphik.graal.homomorphism.Var) Term(fr.lirmm.graphik.graal.api.core.Term)

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