Search in sources :

Example 16 with Variable

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

the class HomomorphismTest method test6.

@Test
public void test6() throws HomomorphismException, IteratorException, AtomSetException {
    InMemoryAtomSet data = new DefaultInMemoryGraphStore();
    data.addAll(DlgpParser.parseAtomSet("p(a,b), q(b), p(a,c), r(a,d), q(d), r(a,e)."));
    Variable y = DefaultTermFactory.instance().createVariable("Y");
    Variable z = DefaultTermFactory.instance().createVariable("Z");
    InMemoryAtomSet positivePart = new LinkedListAtomSet();
    positivePart.addAll(DlgpParser.parseAtomSet("p(X,Y),r(X,Z)."));
    CloseableIteratorWithoutException<Atom> it = positivePart.iterator();
    it.next().setTerm(1, y);
    it.next().setTerm(1, z);
    LinkedList<InMemoryAtomSet> parts = new LinkedList<InMemoryAtomSet>();
    InMemoryAtomSet negatedPart = new LinkedListAtomSet();
    negatedPart.addAll(DlgpParser.parseAtomSet("q(Y)."));
    negatedPart.iterator().next().setTerm(0, y);
    parts.add(negatedPart);
    negatedPart = new LinkedListAtomSet();
    negatedPart.addAll(DlgpParser.parseAtomSet("q(Z)."));
    negatedPart.iterator().next().setTerm(0, z);
    parts.add(negatedPart);
    DefaultConjunctiveQueryWithNegatedParts query = new DefaultConjunctiveQueryWithNegatedParts(positivePart, parts);
    BacktrackHomomorphismWithNegatedParts h = new BacktrackHomomorphismWithNegatedParts();
    CloseableIterator<Substitution> res = h.execute(query, data);
    Assert.assertTrue(res.hasNext());
    res.next();
    Assert.assertFalse(res.hasNext());
    res.close();
}
Also used : DefaultConjunctiveQueryWithNegatedParts(fr.lirmm.graphik.graal.core.DefaultConjunctiveQueryWithNegatedParts) Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 17 with Variable

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

the class RecursiveBacktrackHomomorphism method execute.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery query, AtomSet facts) throws HomomorphismException {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(query.toString());
    }
    if (profiler != null) {
        profiler.start("preprocessing time");
    }
    Pair<ConjunctiveQuery, Substitution> pair = EqualityUtils.processEquality(query);
    List<Variable> orderedVars = order(pair.getLeft().getAtomSet().getVariables());
    Collection<Atom>[] queryAtomRanked = getAtomRank(pair.getLeft().getAtomSet(), orderedVars);
    if (profiler != null) {
        profiler.stop("preprocessing time");
    }
    try {
        this.domain = facts.getTerms();
        if (profiler != null) {
            profiler.start("backtracking time");
        }
        CloseableIterator<Substitution> results;
        if (isHomomorphism(queryAtomRanked[0], facts, new HashMapSubstitution())) {
            results = new CloseableIteratorAdapter<Substitution>(homomorphism(pair.getLeft(), queryAtomRanked, facts, new HashMapSubstitution(), orderedVars, 1).iterator());
        } else {
            // return false
            results = new CloseableIteratorAdapter<Substitution>(Collections.<Substitution>emptyList().iterator());
        }
        if (profiler != null) {
            profiler.stop("backtracking time");
        }
        if (!pair.getRight().getTerms().isEmpty()) {
            results = new ConverterCloseableIterator<Substitution, Substitution>(results, new EqualityHandlerConverter(pair.getRight()));
        }
        return results;
    } catch (Exception e) {
        throw new HomomorphismException(e.getMessage(), e);
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) CloseableIterableWithoutException(fr.lirmm.graphik.util.stream.CloseableIterableWithoutException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) Collection(java.util.Collection) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) EqualityHandlerConverter(fr.lirmm.graphik.graal.homomorphism.utils.EqualityHandlerConverter)

Example 18 with Variable

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

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

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