use of fr.lirmm.graphik.graal.api.core.Substitution 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.api.core.Substitution 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.api.core.Substitution in project graal by graphik-team.
the class AbstractNFC method select.
protected boolean select(Atom atom, Var v, AtomSet g, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws AtomSetException, IteratorException {
boolean contains = false;
Set<Var> postVarsFromThisAtom = new HashSet<Var>();
for (Pair<Atom, Substitution> rew : rc.getRewritingOf(atom)) {
Atom a = rew.getLeft();
Var[] postV = this.computePostVariablesPosition(a, v.shared.level, map, varData, postVarsFromThisAtom);
Atom im = BacktrackUtils.createImageOf(a, initialSubstitution, map, varData);
Profiler profiler = this.getProfiler();
if (profiler != null) {
profiler.incr("#Select", 1);
profiler.start("SelectTime");
}
int nbAns = 0;
CloseableIterator<? extends Atom> it = g.match(im);
while (it.hasNext()) {
++nbAns;
int i = -1;
for (Term t : it.next()) {
++i;
if (postV[i] != null) {
this.data[postV[i].shared.level].tmp.add(t);
}
}
contains = true;
}
if (profiler != null) {
profiler.stop("SelectTime");
profiler.incr("#SelectAns", nbAns);
}
}
boolean isThereAnEmptiedList = false;
if (contains) {
// set computed candidats for post variables
for (Var z : postVarsFromThisAtom) {
if (!isThereAnEmptiedList) {
AcceptableCandidats ac = this.data[z.shared.level].candidats[v.shared.level];
if (ac.init) {
ac.candidats.retainAll(this.data[z.shared.level].tmp);
isThereAnEmptiedList |= ac.candidats.isEmpty();
if (ac.candidats.isEmpty()) {
this.bj.addNeighborhoodToBackjumpSet(z.shared, v.shared);
}
} else {
ac.candidats.addAll(this.data[z.shared.level].tmp);
ac.init = true;
}
}
this.data[z.shared.level].tmp.clear();
}
} else {
Var z = postVarsFromThisAtom.iterator().next();
this.bj.addNeighborhoodToBackjumpSet(z.shared, v.shared);
}
return contains && !isThereAnEmptiedList;
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class AbstractNFC method check.
protected boolean check(Atom atom, VarSharedData currentVar, VarSharedData varToCompute, AtomSet g, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws AtomSetException {
Substitution s = BacktrackUtils.createSubstitution(varData);
s.put(initialSubstitution);
Atom im = s.createImageOf(atom);
this.data[varToCompute.level].tmp.clear();
Set<Term> candidats = this.data[varToCompute.level].candidats[currentVar.level].candidats;
// FIXMEÂ bug with p(X,Y,Z) -> q(X,Y) in the compilation
for (Pair<Atom, Substitution> rew : rc.getRewritingOf(im)) {
Atom a = rew.getLeft();
Iterator<Term> it = candidats.iterator();
while (it.hasNext()) {
Term t = it.next();
Atom fullInstantiatedAtom = Substitutions.createImageOf(a, varToCompute.value, t);
Profiler profiler = this.getProfiler();
if (profiler != null) {
profiler.incr("#check", 1);
profiler.start("checkTime");
}
if (g.contains(fullInstantiatedAtom)) {
this.data[varToCompute.level].tmp.add(t);
}
if (profiler != null) {
profiler.stop("checkTime");
}
}
}
candidats.retainAll(this.data[varToCompute.level].tmp);
this.data[varToCompute.level].tmp.clear();
if (candidats.isEmpty()) {
this.bj.addNeighborhoodToBackjumpSet(varToCompute, currentVar);
return false;
} else {
return true;
}
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class FrontierRestrictedChaseHaltingConditionTest method test.
@Test
public void test() throws IteratorException, HomomorphismFactoryException, HomomorphismException {
InMemoryAtomSet atomset = DefaultAtomSetFactory.instance().create(DlgpParser.parseAtom("p(a,b)."));
Rule rule = DlgpParser.parseRule("p(X,Z):-p(X,Y).");
Variable x = DefaultTermFactory.instance().createVariable("X");
Variable y = DefaultTermFactory.instance().createVariable("Y");
Constant a = DefaultTermFactory.instance().createConstant("a");
Constant b = DefaultTermFactory.instance().createConstant("b");
Substitution s = DefaultSubstitutionFactory.instance().createSubstitution();
s.put(x, a);
s.put(y, b);
FrontierRestrictedChaseHaltingCondition condition = new FrontierRestrictedChaseHaltingCondition();
CloseableIterator<Atom> toAdd = condition.apply(rule, s, atomset);
Assert.assertTrue(toAdd.hasNext());
Atom atom1 = toAdd.next();
atomset.add(atom1);
Assert.assertFalse(toAdd.hasNext());
toAdd.close();
s = DefaultSubstitutionFactory.instance().createSubstitution();
s.put(x, a);
s.put(y, atom1.getTerm(1));
toAdd = condition.apply(rule, s, atomset);
Assert.assertFalse(toAdd.hasNext());
toAdd.close();
}
Aggregations