use of fr.lirmm.graphik.graal.homomorphism.Var 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.homomorphism.Var in project graal by graphik-team.
the class AbstractNFC method computePostVariablesPosition.
/**
* Return an array containing the corresponding instance of Var class for
* each position of a variable in the specified atom with a higher level
* than the specified level. Constant, literal and lower or equals level
* variable postions contain null value.
*
* @param atom
* @param level
* @param map
* Correspondence between Variable instance and Var instance.
* @param postVars
* output parameter that is a Set in which must be added higher
* level variables from this atom.
* @return an array containing the coresseponding instance of Var class for
* each position of a variable in the specified atom with a higher level
* than the specified level.
*/
protected Var[] computePostVariablesPosition(Atom atom, int level, Map<Variable, Integer> map, Var[] varData, Set<Var> postVars) {
Var[] postV = new Var[atom.getPredicate().getArity()];
int i = -1;
for (Term t : atom) {
++i;
Integer idx = map.get(t);
if (idx != null) {
Var z = varData[idx];
if (z.shared.level > level) {
postV[i] = z;
postVars.add(z);
}
}
}
return postV;
}
use of fr.lirmm.graphik.graal.homomorphism.Var in project graal by graphik-team.
the class NFC2WithLimit method select.
// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
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;
int nbAns = 0;
Iterator<Pair<Atom, Substitution>> rewIt = rc.getRewritingOf(atom).iterator();
Set<Var> postVarsFromThisAtom = new HashSet<Var>();
while (rewIt.hasNext() && nbAns < LIMIT) {
Atom a = rewIt.next().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 cpt = 0;
CloseableIterator<? extends Atom> it = g.match(im);
while (it.hasNext() && nbAns < LIMIT) {
++nbAns;
++cpt;
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", cpt);
}
}
boolean isThereAnEmptiedList = false;
if (contains) {
// set computed candidats for post variables
for (Var z : postVarsFromThisAtom) {
if (!isThereAnEmptiedList) {
if (nbAns >= LIMIT) {
this.dataWithLimit[z.shared.level].atomsToCheck.add(atom);
} else {
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.homomorphism.Var in project graal by graphik-team.
the class NFC2 method checkForward.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean checkForward(Var v, AtomSet g, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws BacktrackException {
// clear all computed candidats for post variables
for (VarSharedData z : v.shared.postVars) {
this.clear(v.shared, z);
}
Var varToAssign = null;
for (Atom atom : v.shared.postAtoms) {
boolean runCheck = true;
if (checkMode) {
int i = 0;
for (Variable t : atom.getVariables()) {
Integer idx = map.get(t);
if (idx != null) {
Var z = varData[idx];
if (z.shared.level > v.shared.level) {
++i;
varToAssign = z;
if (i > 1 || !this.data[z.shared.level].candidats[v.shared.level].init) {
runCheck = false;
break;
}
}
}
}
}
if (checkMode && runCheck) {
try {
if (!check(atom, v.shared, varToAssign.shared, g, initialSubstitution, map, varData, rc)) {
return false;
}
} catch (AtomSetException e) {
throw new BacktrackException("An error occurs while checking current candidate");
}
} else {
try {
if (!select(atom, v, g, initialSubstitution, map, varData, rc)) {
return false;
}
} catch (IteratorException e) {
throw new BacktrackException("An error occurs while selecting candidates for next steps ");
} catch (AtomSetException e) {
throw new BacktrackException("An error occurs while selecting candidates for next steps ");
}
}
}
return true;
}
Aggregations