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);
}
}
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;
}
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;
}
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));
}
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));
}
Aggregations