Search in sources :

Example 1 with VarSharedData

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

the class GraphBaseBackJumping method previousLevel.

@Override
public int previousLevel(VarSharedData var, Var[] vars) {
    int ret = var.previousLevel;
    VarSharedData v = null, y = null;
    // get the higher neighbor of var
    if (!var.preVars.isEmpty()) {
        v = var.preVars.last();
    }
    // look for a higher var in the backjump set
    if (!this.data[var.level].backjumpSet.isEmpty()) {
        y = this.data[var.level].backjumpSet.last();
        if (v != null && v.compareTo(y) < 0) {
            v = y;
        }
    }
    // update backjump set of the var v
    if (v != null && !data[v.level].success) {
        this.data[v.level].backjumpSet.addAll(var.preVars);
        this.data[v.level].backjumpSet.addAll(this.data[var.level].backjumpSet);
        this.data[v.level].backjumpSet.remove(v);
        if (this.getProfiler().isProfilingEnabled()) {
            this.getProfiler().incr("#backjumps", 1);
            this.getProfiler().incr("#varsBackjumped", var.level - v.level);
        }
        ret = v.level;
    }
    this.data[var.level].backjumpSet.clear();
    return ret;
}
Also used : VarSharedData(fr.lirmm.graphik.graal.homomorphism.VarSharedData)

Example 2 with VarSharedData

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

the class GraphBaseBackJumping method append.

@Override
public StringBuilder append(StringBuilder sb, int level) {
    sb.append("\tBJSet{");
    for (VarSharedData v : this.data[level].backjumpSet) {
        sb.append(v.value);
        sb.append(", ");
    }
    return sb.append("}");
}
Also used : VarSharedData(fr.lirmm.graphik.graal.homomorphism.VarSharedData)

Example 3 with VarSharedData

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

the class BCCScheduler method execute.

@Override
public VarSharedData[] execute(InMemoryAtomSet query, Set<Variable> preAffectedVars, List<Term> ans, AtomSet data, RulesCompilation rc) {
    InMemoryAtomSet fixedQuery = (preAffectedVars.isEmpty()) ? query : computeFixedQuery(query, preAffectedVars);
    // Term index
    Set<Variable> variables = fixedQuery.getVariables();
    Map<Term, Integer> map = new HashMap<Term, Integer>();
    this.inverseMap = new Term[variables.size() + 1];
    {
        // init indexes
        int i = 0;
        for (Variable t : variables) {
            inverseMap[++i] = t;
            map.put(t, i);
        }
    }
    HyperGraph graph = constructHyperGraph(fixedQuery, variables.size(), this.inverseMap, map, ans);
    double[] proba;
    if (data instanceof Store) {
        proba = this.computeProba(fixedQuery, (Store) data, variables.size(), map, rc);
    } else {
        proba = new double[variables.size() + 1];
        Arrays.fill(proba, 1);
    }
    // bias proba of answer variables
    for (Term t : ans) {
        if (t.isVariable()) {
            int idx = map.get(t);
            proba[idx] *= ansVariableFactor;
        }
    }
    this.varComparator = new IntegerComparator(proba);
    TmpData d = biconnect(graph, this.varComparator);
    VarSharedData[] vars = new VarSharedData[variables.size() + 2];
    this.BCC.varData = new VarData[variables.size() + 2];
    vars[0] = new VarSharedData(0);
    this.BCC.varData[0] = new VarData();
    int lastAnswerVariable = -1;
    for (int i = 1; i < d.vars.length; ++i) {
        VarSharedData v = d.vars[i];
        vars[v.level] = v;
        this.BCC.varData[v.level] = d.ext[i];
        v.value = (Variable) this.inverseMap[i];
        v.nextLevel = v.level + 1;
        v.previousLevel = v.level - 1;
        if (this.withForbiddenCandidate && this.BCC.varData[v.level].isAccesseur) {
            this.BCC.varData[v.level].forbidden = new HashSet<Term>();
        }
        if (ans.contains(v.value)) {
            if (v.level > lastAnswerVariable)
                lastAnswerVariable = v.level;
        }
    }
    int level = variables.size() + 1;
    vars[level] = new VarSharedData(level);
    this.BCC.varData[level] = new VarData();
    // if an homomorphism is found, go to the last answer variable
    vars[level].previousLevel = lastAnswerVariable;
    // Profiling
    if (this.getProfiler().isProfilingEnabled()) {
        StringBuilder sb = new StringBuilder();
        for (VarSharedData v : vars) {
            sb.append(v.value);
            sb.append(" > ");
        }
        this.getProfiler().put("BCCOrder", sb.toString());
    }
    return vars;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) HashMap(java.util.HashMap) Store(fr.lirmm.graphik.graal.api.store.Store) Term(fr.lirmm.graphik.graal.api.core.Term) VarSharedData(fr.lirmm.graphik.graal.homomorphism.VarSharedData) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultHyperGraph(fr.lirmm.graphik.util.graph.DefaultHyperGraph) HyperGraph(fr.lirmm.graphik.util.graph.HyperGraph)

Example 4 with VarSharedData

use of fr.lirmm.graphik.graal.homomorphism.VarSharedData 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 5 with VarSharedData

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

the class FixedOrderScheduler method execute.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public VarSharedData[] execute(InMemoryAtomSet h, List<Term> ans, AtomSet data, RulesCompilation rc) throws HomomorphismException {
    Set<Variable> terms = h.getVariables();
    VarSharedData[] vars = new VarSharedData[terms.size() + 2];
    int level = 0;
    vars[level] = new VarSharedData(level);
    Set<Term> alreadyAffected = new TreeSet<Term>();
    for (Variable v : this.order) {
        if (!terms.contains(v)) {
            throw new HomomorphismException("Try to schedule a variable which is not in the query :" + v);
        }
        if (alreadyAffected.contains(v)) {
            throw new HomomorphismException("There is two occurences of the same variable in the specified order.");
        }
        ++level;
        vars[level] = new VarSharedData(level);
        vars[level].value = v;
        alreadyAffected.add(v);
    }
    terms.removeAll(alreadyAffected);
    if (!terms.isEmpty()) {
        throw new HomomorphismException("Some variables of the query are not scheduled :" + terms);
    }
    ++level;
    vars[level] = new VarSharedData(level);
    vars[level].previousLevel = level - 1;
    return vars;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) TreeSet(java.util.TreeSet) Term(fr.lirmm.graphik.graal.api.core.Term) VarSharedData(fr.lirmm.graphik.graal.homomorphism.VarSharedData)

Aggregations

VarSharedData (fr.lirmm.graphik.graal.homomorphism.VarSharedData)7 Variable (fr.lirmm.graphik.graal.api.core.Variable)5 Term (fr.lirmm.graphik.graal.api.core.Term)4 Atom (fr.lirmm.graphik.graal.api.core.Atom)2 TreeSet (java.util.TreeSet)2 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)1 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)1 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)1 Store (fr.lirmm.graphik.graal.api.store.Store)1 BacktrackException (fr.lirmm.graphik.graal.homomorphism.BacktrackException)1 Var (fr.lirmm.graphik.graal.homomorphism.Var)1 DefaultHyperGraph (fr.lirmm.graphik.util.graph.DefaultHyperGraph)1 HyperGraph (fr.lirmm.graphik.util.graph.HyperGraph)1 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)1 HashMap (java.util.HashMap)1