use of fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables in project graal by graphik-team.
the class FrontierRestrictedChaseHaltingCondition method apply.
@Override
public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException {
Set<Term> fixedVars = substitution.getValues();
if (ruleIndex.get(rule) == null) {
ruleIndex.put(rule, _currentRuleIndex++);
}
final int index = ruleIndex.get(rule).intValue();
StringBuilder frontierSb = new StringBuilder();
SortedSet<Variable> frontierSet = new TreeSet<Variable>(rule.getFrontier());
for (Term t : frontierSet) {
frontierSb.append("_");
frontierSb.append(t.getLabel());
frontierSb.append(substitution.createImageOf(t).getLabel());
}
String frontier = frontierSb.toString();
for (Variable t : rule.getExistentials()) {
substitution.put(t, DefaultTermFactory.instance().createConstant("f_" + index + "_" + t.getIdentifier() + frontier));
}
InMemoryAtomSet newFacts = substitution.createImageOf(rule.getHead());
ConjunctiveQuery query = new ConjunctiveQueryWithFixedVariables(newFacts, fixedVars);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Fixed Query:" + query);
}
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);
}
return newFacts.iterator();
}
use of fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables 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;
}
use of fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables in project graal by graphik-team.
the class RestrictedProductivityChecker method isValidDependency.
// /////////////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean isValidDependency(Rule r1, Rule r2, Substitution s) {
InMemoryAtomSet b1 = s.createImageOf(r1.getBody());
InMemoryAtomSet h1 = s.createImageOf(r1.getHead());
InMemoryAtomSet b2 = s.createImageOf(r2.getBody());
InMemoryAtomSet h2 = s.createImageOf(r2.getHead());
InMemoryAtomSet f = new LinkedListAtomSet();
f.addAll(b1.iterator());
f.addAll(h1.iterator());
f.addAll(b2.iterator());
Set<Variable> fixedVariables = h2.getVariables();
fixedVariables.retainAll(b2.getVariables());
try {
ConjunctiveQueryWithFixedVariables query = new ConjunctiveQueryWithFixedVariables(h2, fixedVariables);
return !PureHomomorphism.instance().exist(query.getAtomSet(), f);
} catch (HomomorphismException e) {
// TODO treat this exception
e.printStackTrace();
throw new Error("Untreated exception");
}
}
Aggregations