Search in sources :

Example 1 with Variable

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

the class IDConditionImpl method homomorphism.

@Override
public Substitution homomorphism(List<Term> head, List<Term> to) {
    if (!checkBody(to)) {
        return null;
    }
    Pair<List<Term>, Substitution> ret = this.generateBody(head);
    if (ret == null) {
        return null;
    }
    Substitution s = ret.getRight();
    Substitution homo = DefaultSubstitutionFactory.instance().createSubstitution();
    List<Term> generatedBody = ret.getLeft();
    // check for a simple homomorphism from generated body into 'to'
    Iterator<Term> itFrom = generatedBody.iterator();
    Iterator<Term> itTo = to.iterator();
    while (itFrom.hasNext() && itTo.hasNext()) {
        Term termFrom = itFrom.next();
        Term termTo = itTo.next();
        if (termFrom.isConstant()) {
            if (!termFrom.equals(termTo)) {
                return null;
            }
        } else {
            if (!homo.put((Variable) termFrom, termTo)) {
                return null;
            }
        }
    }
    if (itFrom.hasNext() || itTo.hasNext()) {
        throw new Error("Wrong term number");
    }
    // homo
    for (Variable t : s.getTerms()) {
        homo.put(t, homo.createImageOf(s.createImageOf(t)));
    }
    return homo;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) MethodNotImplementedError(fr.lirmm.graphik.util.MethodNotImplementedError) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Term(fr.lirmm.graphik.graal.api.core.Term)

Example 2 with Variable

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

the class DefaultRule method computeFrontierAndExistentials.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private void computeFrontierAndExistentials() {
    this.frontier = new TreeSet<Variable>();
    this.existentials = new TreeSet<Variable>();
    Collection<Variable> body = this.getBody().getVariables();
    for (Variable termHead : this.getHead().getVariables()) {
        boolean isExistential = true;
        for (Variable termBody : body) {
            if (termBody.equals(termHead)) {
                this.frontier.add(termHead);
                isExistential = false;
            }
        }
        if (isExistential) {
            this.existentials.add(termHead);
        }
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable)

Example 3 with Variable

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

the class BacktrackIteratorData method computeAtomOrder.

/**
 * The index 0 contains the fully instantiated atoms.
 *
 * @param atomset
 * @param vars
 */
private static void computeAtomOrder(CloseableIterableWithoutException<Atom> atomset, VarSharedData[] vars, Map<Variable, Integer> index) {
    int tmp, rank;
    // initialisation preAtoms and postAtoms Collections
    for (int i = 0; i < vars.length; ++i) {
        vars[i].preAtoms = new HashSet<Atom>();
        vars[i].postAtoms = new HashSet<Atom>();
        vars[i].postVars = new HashSet<VarSharedData>();
        vars[i].preVars = new TreeSet<VarSharedData>();
    }
    // 
    CloseableIteratorWithoutException<Atom> it = atomset.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        rank = 0;
        for (Variable t : a.getVariables()) {
            Integer idx = index.get(t);
            if (idx != null) {
                tmp = vars[idx].level;
                vars[tmp].postAtoms.add(a);
                if (rank < tmp)
                    rank = tmp;
            }
        }
        vars[rank].postAtoms.remove(a);
        vars[rank].preAtoms.add(a);
    }
    for (int i = 0; i < vars.length; ++i) {
        for (Atom a : vars[i].postAtoms) {
            for (Variable t : a.getVariables()) {
                Integer idx = index.get(t);
                if (idx != null) {
                    if (vars[idx].level > i) {
                        vars[i].postVars.add(vars[idx]);
                        vars[idx].preVars.add(vars[i]);
                    }
                }
            }
        }
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 4 with Variable

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

the class RecursiveBacktrackHomomorphism method homomorphism.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private Collection<Substitution> homomorphism(ConjunctiveQuery query, Collection<Atom>[] queryAtomRanked, AtomSet facts, Substitution substitution, List<Variable> orderedVars, int rank) throws Exception {
    Collection<Substitution> substitutionList = new LinkedList<Substitution>();
    if (orderedVars.size() == 0) {
        Substitution filteredSub = new HashMapSubstitution();
        for (Term var : query.getAnswerVariables()) {
            if (var.isVariable()) {
                filteredSub.put((Variable) var, substitution.createImageOf(var));
            }
        }
        substitutionList.add(filteredSub);
    } else {
        Term var = orderedVars.remove(0);
        if (var.isVariable()) {
            for (Term substitut : domain) {
                Substitution tmpSubstitution = new HashMapSubstitution(substitution);
                tmpSubstitution.put((Variable) var, substitut);
                // Test partial homomorphism
                if (isHomomorphism(queryAtomRanked[rank], facts, tmpSubstitution))
                    substitutionList.addAll(homomorphism(query, queryAtomRanked, facts, tmpSubstitution, new LinkedList<Variable>(orderedVars), rank + 1));
            }
        }
    }
    return substitutionList;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) Term(fr.lirmm.graphik.graal.api.core.Term) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) LinkedList(java.util.LinkedList)

Example 5 with Variable

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

the class RecursiveBacktrackHomomorphism method execute.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery query, AtomSet facts) throws HomomorphismException {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(query.toString());
    }
    if (profiler != null) {
        profiler.start("preprocessing time");
    }
    Pair<ConjunctiveQuery, Substitution> pair = EqualityUtils.processEquality(query);
    List<Variable> orderedVars = order(pair.getLeft().getAtomSet().getVariables());
    Collection<Atom>[] queryAtomRanked = getAtomRank(pair.getLeft().getAtomSet(), orderedVars);
    if (profiler != null) {
        profiler.stop("preprocessing time");
    }
    try {
        this.domain = facts.getTerms();
        if (profiler != null) {
            profiler.start("backtracking time");
        }
        CloseableIterator<Substitution> results;
        if (isHomomorphism(queryAtomRanked[0], facts, new HashMapSubstitution())) {
            results = new CloseableIteratorAdapter<Substitution>(homomorphism(pair.getLeft(), queryAtomRanked, facts, new HashMapSubstitution(), orderedVars, 1).iterator());
        } else {
            // return false
            results = new CloseableIteratorAdapter<Substitution>(Collections.<Substitution>emptyList().iterator());
        }
        if (profiler != null) {
            profiler.stop("backtracking time");
        }
        if (!pair.getRight().getTerms().isEmpty()) {
            results = new ConverterCloseableIterator<Substitution, Substitution>(results, new EqualityHandlerConverter(pair.getRight()));
        }
        return results;
    } catch (Exception e) {
        throw new HomomorphismException(e.getMessage(), e);
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) CloseableIterableWithoutException(fr.lirmm.graphik.util.stream.CloseableIterableWithoutException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) Collection(java.util.Collection) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) EqualityHandlerConverter(fr.lirmm.graphik.graal.homomorphism.utils.EqualityHandlerConverter)

Aggregations

Variable (fr.lirmm.graphik.graal.api.core.Variable)57 Atom (fr.lirmm.graphik.graal.api.core.Atom)33 Term (fr.lirmm.graphik.graal.api.core.Term)32 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)25 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)19 LinkedList (java.util.LinkedList)15 Test (org.junit.Test)15 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)8 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)8 Constant (fr.lirmm.graphik.graal.api.core.Constant)7 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)6 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)6 Rule (fr.lirmm.graphik.graal.api.core.Rule)6 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)6 ConversionException (fr.lirmm.graphik.util.stream.converter.ConversionException)6 VarSharedData (fr.lirmm.graphik.graal.homomorphism.VarSharedData)5 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)4 DefaultConjunctiveQueryWithNegatedParts (fr.lirmm.graphik.graal.core.DefaultConjunctiveQueryWithNegatedParts)4 HashMapSubstitution (fr.lirmm.graphik.graal.core.HashMapSubstitution)4 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)4