Search in sources :

Example 56 with Variable

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

the class RestrictedChaseRuleApplier method apply.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean apply(Rule rule, T atomSet) throws RuleApplicationException {
    try {
        boolean res = false;
        ConjunctiveQueryWithNegatedParts query = new RuleWrapper2ConjunctiveQueryWithNegatedParts(rule);
        CloseableIterator<Substitution> results;
        results = SmartHomomorphism.instance().execute(query, atomSet);
        while (results.hasNext()) {
            res = true;
            Substitution proj = results.next();
            // replace variables by fresh symbol
            for (Variable t : rule.getExistentials()) {
                proj.put(t, atomSet.getFreshSymbolGenerator().getFreshSymbol());
            }
            CloseableIteratorWithoutException<Atom> it = proj.createImageOf(rule.getHead()).iterator();
            while (it.hasNext()) {
                atomSet.add(it.next());
            }
        }
        return res;
    } catch (HomomorphismException e) {
        throw new RuleApplicationException("", e);
    } catch (AtomSetException e) {
        throw new RuleApplicationException("", e);
    } catch (IteratorException e) {
        throw new RuleApplicationException("", e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) RuleWrapper2ConjunctiveQueryWithNegatedParts(fr.lirmm.graphik.graal.core.RuleWrapper2ConjunctiveQueryWithNegatedParts) RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) RuleWrapper2ConjunctiveQueryWithNegatedParts(fr.lirmm.graphik.graal.core.RuleWrapper2ConjunctiveQueryWithNegatedParts) ConjunctiveQueryWithNegatedParts(fr.lirmm.graphik.graal.api.core.ConjunctiveQueryWithNegatedParts) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 57 with Variable

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

the class BacktrackIteratorData method preprocessing.

private void preprocessing(Set<Variable> variablesToParameterize, Profiler profiler) throws HomomorphismException {
    profiler.start("preprocessingTime");
    // Compute order on query variables and atoms
    if (variablesToParameterize != null && !variablesToParameterize.isEmpty()) {
        assert this.scheduler instanceof PatternScheduler;
        PatternScheduler sched = (PatternScheduler) this.scheduler;
        this.varsOrder = sched.execute(this.query, variablesToParameterize, ans, this.data, this.compilation);
    } else {
        this.varsOrder = this.scheduler.execute(this.query, ans, this.data, this.compilation);
    }
    this.levelMax = varsOrder.length - 2;
    // PROFILING
    if (profiler.isProfilingEnabled()) {
        this.profilingVarOrder(this.varsOrder, profiler);
    }
    // Index Var structures by original variable object
    this.index = new TreeMap<Variable, Integer>();
    for (VarSharedData v : this.varsOrder) {
        if (v.value != null) {
            // 
            this.index.put(v.value, v.level);
        }
    }
    if (this.ans.isEmpty()) {
        this.varsOrder[this.levelMax + 1].previousLevel = -1;
    }
    computeAtomOrder(this.query, this.varsOrder, this.index);
    this.fc.init(this.varsOrder, this.index);
    this.bj.init(this.varsOrder);
    Set<Variable> allVarsFromH = query.getVariables();
    for (InMemoryAtomSet negPart : this.negParts) {
        Set<Variable> frontier = SetUtils.intersection(allVarsFromH, negPart.getVariables());
        this.varsOrder[maxLevel(frontier)].negatedPartsToCheck.add(BacktrackHomomorphismPattern.instance().prepareHomomorphism(new DefaultConjunctiveQuery(negPart, Collections.<Term>emptyList()), frontier, this.data, this.compilation));
    }
    profiler.stop("preprocessingTime");
}
Also used : DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Variable(fr.lirmm.graphik.graal.api.core.Variable) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) PatternScheduler(fr.lirmm.graphik.graal.homomorphism.scheduler.PatternScheduler)

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