use of fr.lirmm.graphik.util.stream.CloseableIterator in project graal by graphik-team.
the class AtomicQueryHomomorphism method execute.
@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery q, AtomSet a, RulesCompilation rc) throws HomomorphismException {
try {
List<CloseableIterator<Substitution>> iteratorsList = new LinkedList<CloseableIterator<Substitution>>();
Atom atom = q.getAtomSet().iterator().next();
for (Pair<Atom, Substitution> im : rc.getRewritingOf(atom)) {
iteratorsList.add(new ConverterCloseableIterator<Atom, Substitution>(a.match(im.getLeft()), new Atom2SubstitutionConverter(im.getLeft(), q.getAnswerVariables(), im.getRight())));
}
return new CloseableIteratorAggregator<Substitution>(new CloseableIteratorAdapter<CloseableIterator<Substitution>>(iteratorsList.iterator()));
} catch (AtomSetException e) {
throw new HomomorphismException(e);
}
}
use of fr.lirmm.graphik.util.stream.CloseableIterator in project graal by graphik-team.
the class DefaultKnowledgeBase method query.
@Override
public CloseableIterator<Substitution> query(Query query, long timeout) throws KnowledgeBaseException, TimeoutException {
long remainingTime = timeout;
if (this.isSaturated) {
try {
return SmartHomomorphism.instance().execute(query, this.store);
} catch (HomomorphismException e) {
throw new KnowledgeBaseException(e);
}
} else if (query instanceof ConjunctiveQuery) {
ConjunctiveQuery cq = (ConjunctiveQuery) query;
if (this.approach != Approach.REWRITING_ONLY && this.approach != Approach.SATURATION_ONLY) {
this.analyse();
}
if (this.approach == Approach.REWRITING_ONLY || this.approach == Approach.SATURATION_ONLY || this.analyse.isDecidable()) {
try {
long time = System.currentTimeMillis();
this.fesSaturate(remainingTime);
if (timeout > 0) {
remainingTime -= (System.currentTimeMillis() - time);
if (remainingTime <= 0) {
throw new TimeoutException(timeout);
}
}
this.compileRule();
RuleSet fusRuleSet = this.getFUSRuleSet();
PureRewriter pure = new PureRewriter(false);
CloseableIteratorWithoutException<ConjunctiveQuery> it = pure.execute(cq, fusRuleSet, this.ruleCompilation, remainingTime);
UnionOfConjunctiveQueries ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
CloseableIterator<Substitution> resultIt = null;
try {
resultIt = SmartHomomorphism.instance().execute(ucq, this.store, this.ruleCompilation);
} catch (HomomorphismException e) {
if (this.getApproach().equals(Approach.REWRITING_FIRST)) {
it = PureRewriter.unfold(ucq, this.ruleCompilation);
ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
} else {
this.semiSaturate();
}
resultIt = SmartHomomorphism.instance().execute(ucq, this.store);
}
return new FilterIterator<Substitution, Substitution>(resultIt, new UniqFilter<Substitution>());
} catch (ChaseException e) {
throw new KnowledgeBaseException(e);
} catch (HomomorphismException e) {
throw new KnowledgeBaseException(e);
}
} else {
throw new KnowledgeBaseException("No decidable combinaison found with the defined approach: " + this.getApproach());
}
} else {
throw new KnowledgeBaseException("No implementation found for this kind of query: " + query.getClass());
}
}
use of fr.lirmm.graphik.util.stream.CloseableIterator in project graal by graphik-team.
the class DefaultKnowledgeBase method query.
@Override
public CloseableIterator<Substitution> query(Query query) throws KnowledgeBaseException {
if (this.isSaturated) {
try {
return SmartHomomorphism.instance().execute(query, this.store);
} catch (HomomorphismException e) {
throw new KnowledgeBaseException(e);
}
} else if (query instanceof ConjunctiveQuery) {
ConjunctiveQuery cq = (ConjunctiveQuery) query;
this.analyse();
if (this.analyse.isDecidable() && (!this.getApproach().equals(Approach.REWRITING_ONLY) || this.getFESRuleSet().isEmpty()) && (!this.getApproach().equals(Approach.SATURATION_ONLY) || this.getFUSRuleSet().isEmpty())) {
try {
this.fesSaturate();
this.compileRule();
RuleSet fusRuleSet = this.getFUSRuleSet();
PureRewriter pure = new PureRewriter(false);
CloseableIteratorWithoutException<ConjunctiveQuery> it = pure.execute(cq, fusRuleSet, this.ruleCompilation);
UnionOfConjunctiveQueries ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
CloseableIterator<Substitution> resultIt = null;
try {
if (this.isSemiSaturated) {
resultIt = SmartHomomorphism.instance().execute(query, this.store);
} else {
resultIt = SmartHomomorphism.instance().execute(ucq, this.store, this.ruleCompilation);
}
} catch (HomomorphismException e) {
if (this.getApproach().equals(Approach.REWRITING_FIRST)) {
it = PureRewriter.unfold(ucq, this.ruleCompilation);
ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
} else {
this.semiSaturate();
}
resultIt = SmartHomomorphism.instance().execute(ucq, this.store);
}
return new FilterIterator<Substitution, Substitution>(resultIt, new UniqFilter<Substitution>());
} catch (ChaseException e) {
throw new KnowledgeBaseException(e);
} catch (HomomorphismException e) {
throw new KnowledgeBaseException(e);
}
} else {
throw new KnowledgeBaseException("No decidable combinaison found with the defined approach: " + this.getApproach());
}
} else {
throw new KnowledgeBaseException("No implementation found for this kind of query: " + query.getClass());
}
}
use of fr.lirmm.graphik.util.stream.CloseableIterator 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.util.stream.CloseableIterator 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);
}
}
}
Aggregations