Search in sources :

Example 46 with Term

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

the class TermTest method variableConstructorTest.

@SuppressWarnings("deprecation")
@Test
public void variableConstructorTest() {
    String label = "label";
    Term term = DefaultTermFactory.instance().createVariable(label);
    Assert.assertTrue(Term.Type.VARIABLE.equals(term.getType()));
    Assert.assertFalse(term.isConstant());
    Assert.assertFalse(term.isLiteral());
    Assert.assertTrue(term.isVariable());
    Assert.assertTrue(term.getIdentifier().toString().equals(label));
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term) Test(org.junit.Test)

Example 47 with Term

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

the class TermPartitionUtils method getAssociatedSubstitution.

/**
 * Compute the substitution associated with the current partition this
 * method computes a substitution by choosing one representative term by
 * class. Choosing first constant then answer variable (in context) return
 * null if the partition contain two constants in the same class
 */
public static Substitution getAssociatedSubstitution(Partition<Term> partition, ConjunctiveQuery context) {
    Substitution substitution = DefaultSubstitutionFactory.instance().createSubstitution();
    // partition
    for (Collection<Term> set : partition) {
        Iterator<Term> i = set.iterator();
        Term representative = i.next();
        while (i.hasNext()) {
            Term t = i.next();
            // t and the current representative are different
            if (representative.equals(t)) {
                i.remove();
            } else {
                if (t.isConstant()) {
                    // representative is a different constant
                    if (representative.isConstant()) {
                        return null;
                    } else {
                        representative = t;
                    }
                } else if (representative.isVariable()) {
                    // t is a variable from the answer
                    if (context != null && !context.getAnswerVariables().contains(representative) && context.getAtomSet().getTerms().contains(t)) {
                        representative = t;
                    }
                }
            }
        }
        // representative of the equivalence set
        for (Term t : set) {
            if (!t.equals(representative)) {
                if (t.isVariable()) {
                    substitution.put((Variable) t, representative);
                }
            }
        }
    }
    return substitution;
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Term(fr.lirmm.graphik.graal.api.core.Term)

Example 48 with Term

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

the class RecursiveBacktrackHomomorphism method homomorphism.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private Collection<Substitution> homomorphism(ConjunctiveQuery query, Collection<Atom>[] queryAtomRanked, AtomSet facts, Substitution substitution, List<Variable> orderedVars, int rank) throws Exception {
    Collection<Substitution> substitutionList = new LinkedList<Substitution>();
    if (orderedVars.size() == 0) {
        Substitution filteredSub = new HashMapSubstitution();
        for (Term var : query.getAnswerVariables()) {
            if (var.isVariable()) {
                filteredSub.put((Variable) var, substitution.createImageOf(var));
            }
        }
        substitutionList.add(filteredSub);
    } else {
        Term var = orderedVars.remove(0);
        if (var.isVariable()) {
            for (Term substitut : domain) {
                Substitution tmpSubstitution = new HashMapSubstitution(substitution);
                tmpSubstitution.put((Variable) var, substitut);
                // Test partial homomorphism
                if (isHomomorphism(queryAtomRanked[rank], facts, tmpSubstitution))
                    substitutionList.addAll(homomorphism(query, queryAtomRanked, facts, tmpSubstitution, new LinkedList<Variable>(orderedVars), rank + 1));
            }
        }
    }
    return substitutionList;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) Term(fr.lirmm.graphik.graal.api.core.Term) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) LinkedList(java.util.LinkedList)

Example 49 with Term

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

the class RecursiveBacktrackHomomorphism method existHomomorphism.

private static boolean existHomomorphism(AtomSet atomSet1, Collection<Atom>[] queryAtomRanked, AtomSet atomSet2, Substitution substitution, List<Variable> orderedVars, int rank) throws Exception {
    if (orderedVars.size() == 0) {
        return true;
    } else {
        Term var;
        Set<Term> domaine = atomSet2.getTerms();
        var = orderedVars.remove(0);
        if (var.isVariable()) {
            for (Term substitut : domaine) {
                Substitution tmpSubstitution = new HashMapSubstitution(substitution);
                tmpSubstitution.put((Variable) var, substitut);
                // Test partial homomorphism
                if (isHomomorphism(queryAtomRanked[rank], atomSet2, tmpSubstitution))
                    if (existHomomorphism(atomSet1, queryAtomRanked, atomSet2, tmpSubstitution, new LinkedList<Variable>(orderedVars), rank + 1)) {
                        return true;
                    }
            }
        }
    }
    return false;
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) Term(fr.lirmm.graphik.graal.api.core.Term) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) LinkedList(java.util.LinkedList)

Example 50 with Term

use of fr.lirmm.graphik.graal.api.core.Term 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

Term (fr.lirmm.graphik.graal.api.core.Term)173 Atom (fr.lirmm.graphik.graal.api.core.Atom)86 LinkedList (java.util.LinkedList)41 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)36 Test (org.junit.Test)35 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)34 Variable (fr.lirmm.graphik.graal.api.core.Variable)32 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)27 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)26 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)23 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)18 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)15 Rule (fr.lirmm.graphik.graal.api.core.Rule)15 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)14 TreeSet (java.util.TreeSet)12 Constant (fr.lirmm.graphik.graal.api.core.Constant)9 DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)9 TreeMap (java.util.TreeMap)8