Search in sources :

Example 1 with ConjunctiveQueryWithFixedVariables

use of fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables 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 2 with ConjunctiveQueryWithFixedVariables

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

the class RestrictedChaseHaltingCondition method apply.

@Override
public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException {
    InMemoryAtomSet newFacts = substitution.createImageOf(rule.getHead());
    ConjunctiveQuery query = new ConjunctiveQueryWithFixedVariables(newFacts, substitution.createImageOf(rule.getFrontier()));
    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);
    } catch (BacktrackException e) {
        throw new HomomorphismException("An errors occurs while iterating results", e);
    }
    // replace variables by fresh symbol
    for (Variable t : rule.getExistentials()) {
        substitution.put(t, data.getFreshSymbolGenerator().getFreshSymbol());
    }
    CloseableIteratorWithoutException<Atom> it = substitution.createImageOf(rule.getHead()).iterator();
    return it;
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Variable(fr.lirmm.graphik.graal.api.core.Variable) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) ConjunctiveQueryWithFixedVariables(fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables) Atom(fr.lirmm.graphik.graal.api.core.Atom) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException)

Example 3 with ConjunctiveQueryWithFixedVariables

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

the class RestrictedProductivityChecker method isValidDependency.

// /////////////////////////////////////////////////////////////////////////
// 
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean isValidDependency(Rule r1, Rule r2, Substitution s) {
    InMemoryAtomSet b1 = s.createImageOf(r1.getBody());
    InMemoryAtomSet h1 = s.createImageOf(r1.getHead());
    InMemoryAtomSet b2 = s.createImageOf(r2.getBody());
    InMemoryAtomSet h2 = s.createImageOf(r2.getHead());
    InMemoryAtomSet f = new LinkedListAtomSet();
    f.addAll(b1.iterator());
    f.addAll(h1.iterator());
    f.addAll(b2.iterator());
    Set<Variable> fixedVariables = h2.getVariables();
    fixedVariables.retainAll(b2.getVariables());
    try {
        ConjunctiveQueryWithFixedVariables query = new ConjunctiveQueryWithFixedVariables(h2, fixedVariables);
        return !PureHomomorphism.instance().exist(query.getAtomSet(), f);
    } catch (HomomorphismException e) {
        // TODO treat this exception
        e.printStackTrace();
        throw new Error("Untreated exception");
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) ConjunctiveQueryWithFixedVariables(fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables)

Aggregations

InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)3 Variable (fr.lirmm.graphik.graal.api.core.Variable)3 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)3 ConjunctiveQueryWithFixedVariables (fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables)3 Atom (fr.lirmm.graphik.graal.api.core.Atom)2 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)2 CloseableIteratorAdapter (fr.lirmm.graphik.util.stream.CloseableIteratorAdapter)2 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)2 Term (fr.lirmm.graphik.graal.api.core.Term)1 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)1 BacktrackException (fr.lirmm.graphik.graal.homomorphism.BacktrackException)1 TreeSet (java.util.TreeSet)1