use of fr.lirmm.graphik.graal.api.core.Variable 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.Variable in project graal by graphik-team.
the class FrontierRestrictedChaseHaltingConditionTest method test2.
@Test
public void test2() throws IteratorException, HomomorphismFactoryException, HomomorphismException {
InMemoryAtomSet atomset = DefaultAtomSetFactory.instance().create(DlgpParser.parseAtomSet("p(a,b), p(b,a)."));
Rule rule = DlgpParser.parseRule("p(X,Z):-p(X,Y).");
Variable x = DefaultTermFactory.instance().createVariable("X");
Variable y = DefaultTermFactory.instance().createVariable("Y");
Constant a = DefaultTermFactory.instance().createConstant("a");
Constant b = DefaultTermFactory.instance().createConstant("b");
Substitution s = DefaultSubstitutionFactory.instance().createSubstitution();
s.put(x, a);
s.put(y, b);
FrontierRestrictedChaseHaltingCondition condition = new FrontierRestrictedChaseHaltingCondition();
CloseableIterator<Atom> toAdd = condition.apply(rule, s, atomset);
Assert.assertTrue(toAdd.hasNext());
Atom atom1 = toAdd.next();
atomset.add(atom1);
Assert.assertFalse(toAdd.hasNext());
toAdd.close();
s = DefaultSubstitutionFactory.instance().createSubstitution();
s.put(x, b);
s.put(y, a);
toAdd = condition.apply(rule, s, atomset);
Assert.assertTrue(toAdd.hasNext());
Atom atom2 = toAdd.next();
atomset.add(atom2);
Assert.assertFalse(toAdd.hasNext());
toAdd.close();
Assert.assertNotEquals(atom1.getTerm(1), atom2.getTerm(1));
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class BacktrackIterator method solutionFound.
private Substitution solutionFound(List<Term> ans) {
Substitution s = new HashMapSubstitution();
for (Term t : ans) {
if (t instanceof Variable) {
Integer idx = this.data.index.get((Variable) t);
Var v = this.vars[idx];
s.put(v.shared.value, v.image);
}
}
return s;
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class RecursiveBacktrackHomomorphism method exist.
@Override
public boolean exist(ConjunctiveQuery query, AtomSet data) throws HomomorphismException {
try {
InMemoryAtomSet atomSet1 = query.getAtomSet();
List<Variable> orderedVars = order(atomSet1.getVariables());
Collection<Atom>[] queryAtomRanked = getAtomRank(atomSet1, orderedVars);
if (isHomomorphism(queryAtomRanked[0], data, new HashMapSubstitution())) {
return existHomomorphism(atomSet1, queryAtomRanked, data, new HashMapSubstitution(), orderedVars, 1);
} else {
return false;
}
} catch (Exception e) {
throw new HomomorphismException(e.getMessage(), e);
}
}
use of fr.lirmm.graphik.graal.api.core.Variable 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