use of fr.lirmm.graphik.graal.api.homomorphism.HomomorphismFactoryException in project graal by graphik-team.
the class RuleApplierIterator method hasNext.
@Override
public boolean hasNext() throws IteratorException {
if (!this.hasNextCallDone) {
this.hasNextCallDone = true;
if (this.localIt != null && !this.localIt.hasNext()) {
this.localIt.close();
this.localIt = null;
}
while ((this.localIt == null || !this.localIt.hasNext()) && this.substitutionIt.hasNext()) {
try {
localIt = haltingCondition.apply(rule, substitutionIt.next(), atomset);
} catch (HomomorphismFactoryException e) {
throw new IteratorException("Error during rule application", e);
} catch (HomomorphismException e) {
throw new IteratorException("Error during rule application", e);
}
}
}
return this.localIt != null && this.localIt.hasNext();
}
use of fr.lirmm.graphik.graal.api.homomorphism.HomomorphismFactoryException in project graal by graphik-team.
the class ChaseWithGRDAndUnfiers method next.
// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public void next() throws ChaseException {
Rule rule, unifiedRule;
Substitution unificator;
Queue<Triple<Rule, Substitution, InMemoryAtomSet>> newQueue = new LinkedList<Triple<Rule, Substitution, InMemoryAtomSet>>();
InMemoryAtomSet newAtomSet = new DefaultInMemoryGraphStore();
try {
while (!queue.isEmpty()) {
Triple<Rule, Substitution, InMemoryAtomSet> pair = queue.poll();
if (pair != null) {
unificator = pair.getMiddle();
InMemoryAtomSet part = pair.getRight();
rule = pair.getLeft();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("\nExecute rule: {} with unificator {}", rule, unificator);
}
unifiedRule = DefaultUnifierAlgorithm.getTargetVariablesSubstitution().createImageOf(rule);
unifiedRule = unificator.createImageOf(unifiedRule);
unifiedRule.getBody().removeAll(part);
unificator = targetToSource(unificator);
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(unifiedRule.getBody(), new LinkedList<Term>(unifiedRule.getFrontier()));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Rule to execute: {}", unifiedRule.toString());
LOGGER.debug(" -- Query: {}", query.toString());
}
// Get projections
List<Substitution> projections = Iterators.toList(SmartHomomorphism.instance().execute(query, atomSet));
try {
for (Substitution proj : projections) {
InMemoryAtomSet newFacts = proj.createImageOf(unifiedRule.getHead());
ConjunctiveQuery q = new DefaultConjunctiveQuery(newFacts);
if (!SmartHomomorphism.instance().execute(q, newAtomSet).hasNext()) {
// Existential variables instantiation added to proj
CloseableIterator<Atom> it = hc.apply(unifiedRule, proj, atomSet);
if (it.hasNext()) {
LinkedListAtomSet foundPart = new LinkedListAtomSet();
foundPart.addAll(it);
newAtomSet.addAll(foundPart);
// Makes the projection compatible with triggered rules unifiers
Substitution compatibleProj = targetToSource(proj);
for (Pair<Rule, Substitution> p : this.grd.getTriggeredRulesWithUnifiers(rule)) {
Rule triggeredRule = p.getKey();
Substitution u = p.getValue();
if (u != null) {
Substitution comp = unificator.compose(u);
Substitution aggreg = compatibleProj.aggregate(comp);
aggreg = forgetSource(aggreg);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("-- -- Dependency: {}", triggeredRule);
LOGGER.debug("-- -- Substitution:{} ", compatibleProj);
LOGGER.debug("-- -- Unificator: {}", u);
LOGGER.debug("-- -- Aggregation: {}\n", aggreg);
}
if (aggreg != null) {
newQueue.add(new ImmutableTriple<Rule, Substitution, InMemoryAtomSet>(triggeredRule, aggreg, foundPart));
}
}
}
}
}
}
} catch (HomomorphismFactoryException e) {
throw new RuleApplicationException("Error during rule application", e);
} catch (HomomorphismException e) {
throw new RuleApplicationException("Error during rule application", e);
} catch (IteratorException e) {
throw new RuleApplicationException("Error during rule application", e);
}
}
}
queue = newQueue;
atomSet.addAll(newAtomSet);
} catch (Exception e) {
e.printStackTrace();
throw new ChaseException("An error occur pending saturation step.", e);
}
}
use of fr.lirmm.graphik.graal.api.homomorphism.HomomorphismFactoryException 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;
}
use of fr.lirmm.graphik.graal.api.homomorphism.HomomorphismFactoryException in project graal by graphik-team.
the class AbstractRuleApplier method delegatedApply.
@Override
public CloseableIterator<Atom> delegatedApply(Rule rule, T atomSetOnWichQuerying, T atomSetOnWichCheck) throws RuleApplicationException {
ConjunctiveQuery query = this.generateQuery(rule);
CloseableIterator<Substitution> subIt;
try {
subIt = this.executeQuery(query, atomSetOnWichQuerying);
} catch (HomomorphismFactoryException e) {
throw new RuleApplicationException("Error during rule application", e);
} catch (HomomorphismException e) {
throw new RuleApplicationException("Error during rule application", e);
}
return new RuleApplierIterator(subIt, rule, atomSetOnWichCheck, haltingCondition);
}
Aggregations