use of fr.lirmm.graphik.graal.api.core.AtomSetException 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.api.core.AtomSetException 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.api.core.AtomSetException in project graal by graphik-team.
the class AbstractAtomSet method getConstants.
@Override
public Set<Constant> getConstants() throws AtomSetException {
Set<Constant> terms = new HashSet<Constant>();
CloseableIterator<Atom> atomIt = this.iterator();
try {
while (atomIt.hasNext()) {
Iterator<Term> termIt = atomIt.next().iterator();
while (termIt.hasNext()) {
Term t = termIt.next();
if (t.isConstant()) {
terms.add((Constant) t);
}
}
}
} catch (Exception e) {
throw new AtomSetException(e);
}
return terms;
}
use of fr.lirmm.graphik.graal.api.core.AtomSetException in project graal by graphik-team.
the class AtomicQueryHomomorphismWithNegatedParts method execute.
// /////////////////////////////////////////////////////////////////////////
// HOMOMORPHISM METHODS
// /////////////////////////////////////////////////////////////////////////
public CloseableIterator<Substitution> execute(ConjunctiveQueryWithNegatedParts query, AtomSet data, RulesCompilation compilation) throws HomomorphismException {
try {
Atom atom = query.getPositivePart().iterator().next();
List<Term> ans = query.getAnswerVariables();
List<CloseableIterator<Substitution>> iteratorsList = new LinkedList<CloseableIterator<Substitution>>();
for (Pair<Atom, Substitution> im : compilation.getRewritingOf(atom)) {
iteratorsList.add(new ConverterCloseableIterator<Atom, Substitution>(data.match(im.getLeft()), new Atom2SubstitutionConverter(im.getLeft(), ans, im.getRight())));
}
CloseableIterator<Substitution> subIt = new CloseableIteratorAggregator<Substitution>(new CloseableIteratorAdapter<CloseableIterator<Substitution>>(iteratorsList.iterator()));
// manage negative parts
Set<Variable> variables = query.getPositivePart().getVariables();
@SuppressWarnings("rawtypes") Filter[] filters = new Filter[query.getNegatedParts().size()];
int i = 0;
for (InMemoryAtomSet negPart : query.getNegatedParts()) {
Set<Variable> frontier = SetUtils.intersection(variables, negPart.getVariables());
filters[i++] = new NegFilter(negPart, frontier, data, compilation);
}
@SuppressWarnings("unchecked") Filter<Substitution> filter = new AndFilter<Substitution>(filters);
return new FilterIterator<Substitution, Substitution>(subIt, filter);
} catch (AtomSetException e) {
throw new HomomorphismException(e);
}
}
use of fr.lirmm.graphik.graal.api.core.AtomSetException in project graal by graphik-team.
the class AbstractRuleApplier method apply.
@Override
public boolean apply(Rule rule, T atomSet) throws RuleApplicationException {
boolean isChanged = false;
ConjunctiveQuery query = this.generateQuery(rule);
try {
CloseableIterator<Substitution> subIt = this.executeQuery(query, atomSet);
while (subIt.hasNext()) {
Substitution substitution = subIt.next();
CloseableIterator<Atom> it = this.getHaltingCondition().apply(rule, substitution, atomSet);
if (it.hasNext()) {
atomSet.addAll(it);
isChanged = true;
}
}
subIt.close();
} catch (HomomorphismFactoryException e) {
throw new RuleApplicationException("Error during rule application", e);
} catch (HomomorphismException e) {
throw new RuleApplicationException("Error during rule application", e);
} catch (AtomSetException e) {
throw new RuleApplicationException("Error during rule application", e);
} catch (IteratorException e) {
throw new RuleApplicationException("Error during rule application", e);
}
return isChanged;
}
Aggregations