Search in sources :

Example 96 with Atom

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

the class BacktrackUtils method isHomomorphism.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
/**
 * @param atomsFrom
 * @param atomsTo
 * @param index
 * @param rc
 * @return true if there is a homomorphism, false otherwise.
 * @throws AtomSetException
 */
public static boolean isHomomorphism(Iterable<Atom> atomsFrom, AtomSet atomsTo, Substitution initialSubstitution, Map<Variable, Integer> index, Var[] varData, RulesCompilation rc) throws AtomSetException {
    for (Atom atom : atomsFrom) {
        Atom image = BacktrackUtils.createImageOf(atom, initialSubstitution, index, varData);
        boolean contains = false;
        for (Pair<Atom, Substitution> p : rc.getRewritingOf(image)) {
            if (atomsTo.contains(p.getLeft())) {
                contains = true;
                break;
            }
        }
        if (!contains)
            return false;
    }
    return true;
}
Also used : TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 97 with Atom

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

the class EqualityUtils method processEquality.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
/**
 * This method produces a conjunctive query based on the specified one where
 * equality atoms are removed and affected variables are replaced or merged.
 * It returns the new query and a substitution which allow to rebuild
 * answers to the original query based on answers to the returned one by
 * composition of each answer with the returned substitution.
 *
 * @param q
 *            a conjunctive query
 * @return a pair composed of the computed conjunctive query and the
 *         substitution which allow to rebuild answers.
 */
public static Pair<ConjunctiveQuery, Substitution> processEquality(ConjunctiveQuery q) {
    LinkedList<Atom> toRemove = new LinkedList<Atom>();
    Substitution s = DefaultSubstitutionFactory.instance().createSubstitution();
    CloseableIteratorWithoutException<Atom> it = q.getAtomSet().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        if (Predicate.EQUALITY.equals(a.getPredicate())) {
            if (a.getTerm(0).isVariable()) {
                if (!updateSubstitution(s, (Variable) a.getTerm(0), a.getTerm(1))) {
                    return generateBottomResult();
                }
                toRemove.add(a);
            } else if (a.getTerm(1).isVariable()) {
                if (!updateSubstitution(s, (Variable) a.getTerm(1), a.getTerm(0))) {
                    return generateBottomResult();
                }
                toRemove.add(a);
            } else {
                return generateBottomResult();
            }
        }
    }
    return new ImmutablePair<ConjunctiveQuery, Substitution>(generateQuery(q, s, toRemove), s);
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList)

Example 98 with Atom

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

the class DlgpListener method createAtomSet.

@Override
protected void createAtomSet(InMemoryAtomSet atomset) {
    FreshVarSubstitution s = new FreshVarSubstitution(DlgpParser.freeVarGen);
    CloseableIteratorWithoutException<Atom> it = atomset.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        this.set.write(s.createImageOf(a));
    }
}
Also used : FreshVarSubstitution(fr.lirmm.graphik.graal.core.FreshVarSubstitution) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 99 with Atom

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

use of fr.lirmm.graphik.graal.api.core.Atom 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)

Aggregations

Atom (fr.lirmm.graphik.graal.api.core.Atom)269 Test (org.junit.Test)97 Term (fr.lirmm.graphik.graal.api.core.Term)86 Rule (fr.lirmm.graphik.graal.api.core.Rule)64 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)41 OWL2Parser (fr.lirmm.graphik.graal.io.owl.OWL2Parser)41 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)40 LinkedList (java.util.LinkedList)40 DefaultNegativeConstraint (fr.lirmm.graphik.graal.core.DefaultNegativeConstraint)36 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)35 Theory (org.junit.experimental.theories.Theory)35 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)34 Variable (fr.lirmm.graphik.graal.api.core.Variable)33 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)31 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)29 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)28 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)27 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)14 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)14 OWL2ParserException (fr.lirmm.graphik.graal.io.owl.OWL2ParserException)13