Search in sources :

Example 1 with HyperGraph

use of fr.lirmm.graphik.util.graph.HyperGraph in project graal by graphik-team.

the class BCCScheduler method constructHyperGraph.

/**
 * The HyperGraph of variables of h. There is an hyper edge between a set of
 * variables if they appear in a same atom.
 *
 * @param h
 * @return the HyperGraph of variables of h.
 */
protected static HyperGraph constructHyperGraph(InMemoryAtomSet h, int nbVariables, Term[] inverseMap, Map<Term, Integer> map, Iterable<Term> ans) {
    HyperGraph graph = new DefaultHyperGraph(nbVariables + 1);
    CloseableIteratorWithoutException<Atom> it = h.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        DefaultHyperEdge edge = new DefaultHyperEdge();
        int arity = 0;
        for (Variable t : a.getVariables()) {
            ++arity;
            edge.addVertice(map.get(t));
        }
        if (arity >= 2) {
            graph.add(edge);
        }
    }
    return graph;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) DefaultHyperGraph(fr.lirmm.graphik.util.graph.DefaultHyperGraph) DefaultHyperEdge(fr.lirmm.graphik.util.graph.DefaultHyperEdge) DefaultHyperGraph(fr.lirmm.graphik.util.graph.DefaultHyperGraph) HyperGraph(fr.lirmm.graphik.util.graph.HyperGraph) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 2 with HyperGraph

use of fr.lirmm.graphik.util.graph.HyperGraph 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)

Aggregations

Variable (fr.lirmm.graphik.graal.api.core.Variable)2 DefaultHyperGraph (fr.lirmm.graphik.util.graph.DefaultHyperGraph)2 HyperGraph (fr.lirmm.graphik.util.graph.HyperGraph)2 Atom (fr.lirmm.graphik.graal.api.core.Atom)1 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)1 Term (fr.lirmm.graphik.graal.api.core.Term)1 Store (fr.lirmm.graphik.graal.api.store.Store)1 VarSharedData (fr.lirmm.graphik.graal.homomorphism.VarSharedData)1 DefaultHyperEdge (fr.lirmm.graphik.util.graph.DefaultHyperEdge)1 HashMap (java.util.HashMap)1