Search in sources :

Example 1 with HashMapSubstitution

use of fr.lirmm.graphik.graal.core.HashMapSubstitution 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 2 with HashMapSubstitution

use of fr.lirmm.graphik.graal.core.HashMapSubstitution 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 3 with HashMapSubstitution

use of fr.lirmm.graphik.graal.core.HashMapSubstitution 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 4 with HashMapSubstitution

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

the class GRDTest method test2.

@Test
public void test2() throws ParseException {
    Rule r1 = DlgpParser.parseRule("wf(X0,Y0), o(Y0) :- e(X0).");
    Rule r2 = DlgpParser.parseRule("wf(Y1,X1) :- o(Y1).");
    Substitution s = new HashMapSubstitution();
    s.put(DefaultTermFactory.instance().createVariable("Y1"), DefaultTermFactory.instance().createVariable("Y0"));
    RestrictedProductivityChecker filter = RestrictedProductivityChecker.instance();
    Assert.assertTrue(filter.isValidDependency(r1, r2, s));
}
Also used : HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) RestrictedProductivityChecker(fr.lirmm.graphik.graal.core.unifier.checker.RestrictedProductivityChecker) Rule(fr.lirmm.graphik.graal.api.core.Rule) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) Test(org.junit.Test)

Example 5 with HashMapSubstitution

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

the class GRDTest method AtomErasingFilterTest.

@Test
public void AtomErasingFilterTest() {
    Rule r1 = DefaultRuleFactory.instance().create(DefaultAtomSetFactory.instance().create(TestUtils.pXZ), DefaultAtomSetFactory.instance().create(TestUtils.pXY, TestUtils.pYZ));
    Rule r2 = DefaultRuleFactory.instance().create(TestUtils.pUU, TestUtils.sU);
    Substitution s = new HashMapSubstitution();
    s.put(DefaultTermFactory.instance().createVariable("X"), DefaultTermFactory.instance().createVariable("U"));
    s.put(DefaultTermFactory.instance().createVariable("Y"), DefaultTermFactory.instance().createVariable("U"));
    s.put(DefaultTermFactory.instance().createVariable("Z"), DefaultTermFactory.instance().createVariable("U"));
    AtomErasingChecker filter = AtomErasingChecker.instance();
    Assert.assertFalse(filter.isValidDependency(r1, r2, s));
}
Also used : AtomErasingChecker(fr.lirmm.graphik.graal.core.unifier.checker.AtomErasingChecker) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Rule(fr.lirmm.graphik.graal.api.core.Rule) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) Test(org.junit.Test)

Aggregations

HashMapSubstitution (fr.lirmm.graphik.graal.core.HashMapSubstitution)9 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)8 Rule (fr.lirmm.graphik.graal.api.core.Rule)4 Variable (fr.lirmm.graphik.graal.api.core.Variable)4 Test (org.junit.Test)4 Term (fr.lirmm.graphik.graal.api.core.Term)3 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)2 RestrictedProductivityChecker (fr.lirmm.graphik.graal.core.unifier.checker.RestrictedProductivityChecker)2 CloseableIterableWithoutException (fr.lirmm.graphik.util.stream.CloseableIterableWithoutException)2 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)2 Collection (java.util.Collection)2 LinkedList (java.util.LinkedList)2 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)1 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)1 AtomErasingChecker (fr.lirmm.graphik.graal.core.unifier.checker.AtomErasingChecker)1 EqualityHandlerConverter (fr.lirmm.graphik.graal.homomorphism.utils.EqualityHandlerConverter)1