FrontierRestrictedChaseHaltingCondition implements ChaseHaltingCondition { @Override public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException { 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("-"); 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)); } ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(rule.getHead()); if (SmartHomomorphism.instance().exist(query, data, substitution)) { return new CloseableIteratorAdapter<Atom>(Collections.<Atom> emptyList().iterator()); } return substitution.createImageOf(rule.getHead()).iterator(); }  @Override CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data);  }
@Test public void test() throws IteratorException, HomomorphismFactoryException, HomomorphismException { InMemoryAtomSet atomset = DefaultAtomSetFactory.instance().create(DlgpParser.parseAtom("p(a,b).")); 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,a); s.put(y,atom1.getTerm(1)); toAdd = condition.apply(rule, s, atomset); Assert.assertFalse(toAdd.hasNext()); toAdd.close(); }
@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)); }