use of fr.lirmm.graphik.graal.homomorphism.BacktrackException in project graal by graphik-team.
the class SimpleFC method getCandidatsIterator.
@Override
public CloseableIterator<Term> getCandidatsIterator(AtomSet g, Var var, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws BacktrackException {
HomomorphismIteratorChecker tmp;
try {
tmp = new HomomorphismIteratorChecker(var, g.termsIterator(), var.shared.preAtoms, g, initialSubstitution, map, varData, rc);
} catch (AtomSetException e) {
throw new BacktrackException(e);
}
tmp.setProfiler(this.getProfiler());
return tmp;
}
use of fr.lirmm.graphik.graal.homomorphism.BacktrackException 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;
}
use of fr.lirmm.graphik.graal.homomorphism.BacktrackException in project graal by graphik-team.
the class NoForwardChecking method getCandidatsIterator.
@Override
public CloseableIterator<Term> getCandidatsIterator(AtomSet g, Var var, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws BacktrackException {
HomomorphismIteratorChecker tmp;
try {
tmp = new HomomorphismIteratorChecker(var, g.termsIterator(), var.shared.preAtoms, g, initialSubstitution, map, varData, rc);
} catch (AtomSetException e) {
throw new BacktrackException(e);
}
tmp.setProfiler(this.getProfiler());
return tmp;
}
use of fr.lirmm.graphik.graal.homomorphism.BacktrackException in project graal by graphik-team.
the class DefaultBootstrapper method exec.
// /////////////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Term> exec(final VarSharedData v, Collection<Atom> preAtoms, Collection<Atom> postAtoms, final AtomSet data, RulesCompilation compilation) throws BacktrackException {
Iterator<Atom> it = postAtoms.iterator();
if (it.hasNext()) {
Atom a = it.next();
final Iterator<Pair<Atom, Substitution>> rewritingOf = compilation.getRewritingOf(a).iterator();
// TODO refactor the following code using converter Iterator or
// create a private class?
CloseableIterator<CloseableIterator<Term>> metaIt = new AbstractCloseableIterator<CloseableIterator<Term>>() {
CloseableIterator<Term> next = null;
@Override
public void close() {
if (next != null)
this.next.close();
}
@Override
public boolean hasNext() throws IteratorException {
try {
if (next == null && rewritingOf.hasNext()) {
Pair<Atom, Substitution> rew = rewritingOf.next();
Atom im = rew.getLeft();
Predicate predicate = im.getPredicate();
int pos = im.indexOf(rew.getRight().createImageOf(v.value));
next = data.termsByPredicatePosition(predicate, pos);
}
} catch (AtomSetException e) {
throw new IteratorException("An errors occurs while getting terms by predicate position", e);
}
return next != null;
}
@Override
public CloseableIterator<Term> next() throws IteratorException {
if (next == null)
this.hasNext();
CloseableIterator<Term> ret = next;
next = null;
return ret;
}
};
return new CloseableIteratorAggregator<Term>(metaIt);
} else {
try {
return data.termsIterator();
} catch (AtomSetException e) {
throw new BacktrackException(e);
}
}
}
use of fr.lirmm.graphik.graal.homomorphism.BacktrackException in project graal by graphik-team.
the class RestrictedChaseHaltingCondition method apply.
@Override
public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException {
InMemoryAtomSet newFacts = substitution.createImageOf(rule.getHead());
ConjunctiveQuery query = new ConjunctiveQueryWithFixedVariables(newFacts, substitution.createImageOf(rule.getFrontier()));
try {
if (SmartHomomorphism.instance().execute(query, data).hasNext()) {
return new CloseableIteratorAdapter<Atom>(Collections.<Atom>emptyList().iterator());
}
} catch (IteratorException e) {
throw new HomomorphismException("An errors occurs while iterating results", e);
} catch (BacktrackException e) {
throw new HomomorphismException("An errors occurs while iterating results", e);
}
// replace variables by fresh symbol
for (Variable t : rule.getExistentials()) {
substitution.put(t, data.getFreshSymbolGenerator().getFreshSymbol());
}
CloseableIteratorWithoutException<Atom> it = substitution.createImageOf(rule.getHead()).iterator();
return it;
}
Aggregations