Search in sources :

Example 6 with HomomorphismException

use of fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException in project graal by graphik-team.

the class FrontierRestrictedChaseHaltingCondition method apply.

@Override
public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException {
    Set<Term> fixedVars = substitution.getValues();
    if (ruleIndex.get(rule) == null) {
        ruleIndex.put(rule, _currentRuleIndex++);
    }
    final int index = ruleIndex.get(rule).intValue();
    StringBuilder frontierSb = new StringBuilder();
    SortedSet<Variable> frontierSet = new TreeSet<Variable>(rule.getFrontier());
    for (Term t : frontierSet) {
        frontierSb.append("_");
        frontierSb.append(t.getLabel());
        frontierSb.append(substitution.createImageOf(t).getLabel());
    }
    String frontier = frontierSb.toString();
    for (Variable t : rule.getExistentials()) {
        substitution.put(t, DefaultTermFactory.instance().createConstant("f_" + index + "_" + t.getIdentifier() + frontier));
    }
    InMemoryAtomSet newFacts = substitution.createImageOf(rule.getHead());
    ConjunctiveQuery query = new ConjunctiveQueryWithFixedVariables(newFacts, fixedVars);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Fixed Query:" + query);
    }
    try {
        if (SmartHomomorphism.instance().execute(query, data).hasNext()) {
            return new CloseableIteratorAdapter<Atom>(Collections.<Atom>emptyList().iterator());
        }
    } catch (IteratorException e) {
        throw new HomomorphismException("An errors occurs while iterating results", e);
    }
    return newFacts.iterator();
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) ConjunctiveQueryWithFixedVariables(fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables) Atom(fr.lirmm.graphik.graal.api.core.Atom) TreeSet(java.util.TreeSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

Example 7 with HomomorphismException

use of fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException in project graal by graphik-team.

the class BacktrackIterator method existNegParts.

private boolean existNegParts() throws BacktrackException {
    Substitution s = currentSubstitution(this.vars);
    s.put(initialSubstitution);
    for (PreparedExistentialHomomorphism negPart : this.currentVar().shared.negatedPartsToCheck) {
        try {
            if (negPart.exist(s)) {
                this.data.bj.success();
                return true;
            }
        } catch (HomomorphismException e) {
            throw new BacktrackException("Error while checking anegated part: ", e);
        }
    }
    return false;
}
Also used : HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) PreparedExistentialHomomorphism(fr.lirmm.graphik.graal.api.homomorphism.PreparedExistentialHomomorphism)

Example 8 with HomomorphismException

use of fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException in project graal by graphik-team.

the class DefaultUCQHomomorphism method exist.

@Override
public boolean exist(UnionOfConjunctiveQueries q, AtomSet a) throws HomomorphismException {
    try {
        CloseableIterator<Substitution> execute = this.execute(q, a);
        boolean res = execute.hasNext();
        execute.close();
        return res;
    } catch (IteratorException e) {
        throw new HomomorphismException(e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution)

Example 9 with HomomorphismException

use of fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException in project graal by graphik-team.

the class FullyInstantiatedQueryHomomorphism method execute.

@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery q, AtomSet a) throws HomomorphismException {
    try {
        CloseableIteratorWithoutException<Atom> it = q.getAtomSet().iterator();
        boolean contains = true;
        while (contains && it.hasNext()) {
            contains = a.contains(it.next());
        }
        if (contains) {
            return Iterators.singletonIterator(Substitutions.emptySubstitution());
        } else {
            return Iterators.emptyIterator();
        }
    } catch (AtomSetException e) {
        throw new HomomorphismException(e);
    }
}
Also used : HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 10 with HomomorphismException

use of fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException in project graal by graphik-team.

the class FullyInstantiatedQueryHomomorphism method execute.

@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery q, AtomSet a, RulesCompilation rc) throws HomomorphismException {
    try {
        CloseableIteratorWithoutException<Atom> it = q.getAtomSet().iterator();
        boolean contains = true;
        while (contains && it.hasNext()) {
            Atom atom = it.next();
            boolean containsAtom = false;
            for (Pair<Atom, Substitution> im : rc.getRewritingOf(atom)) {
                containsAtom = a.contains(im.getLeft());
                if (containsAtom) {
                    break;
                }
            }
            contains = containsAtom;
        }
        if (contains) {
            return Iterators.singletonIterator(Substitutions.emptySubstitution());
        } else {
            return Iterators.emptyIterator();
        }
    } catch (AtomSetException e) {
        throw new HomomorphismException(e);
    }
}
Also used : HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Aggregations

HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)35 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)17 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)15 Atom (fr.lirmm.graphik.graal.api.core.Atom)14 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)13 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)11 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)8 Variable (fr.lirmm.graphik.graal.api.core.Variable)8 Term (fr.lirmm.graphik.graal.api.core.Term)7 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)7 LinkedList (java.util.LinkedList)6 ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)5 RuleApplicationException (fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException)5 SubstitutionIterator2AtomIterator (fr.lirmm.graphik.graal.core.stream.SubstitutionIterator2AtomIterator)5 SqlHomomorphism (fr.lirmm.graphik.graal.store.rdbms.homomorphism.SqlHomomorphism)5 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)4 HomomorphismFactoryException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismFactoryException)4 HashMapSubstitution (fr.lirmm.graphik.graal.core.HashMapSubstitution)4 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)4 AnalyserRuleSet (fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet)4