public Collection<? extends AtomVariable> getBindableVars(final Collection<AtomVariable> bound) { return _helper.getBindableVars(bound); }
int position = _helperChain.length - 1; while (position >= 0) if (_helperChain[position].selectNextBinding()) _helperChain[position].setCurrentBinding(newBinding); if (position < _helperChain.length - 1) _helperChain[position + 1].rebind(newBinding); position++;
public Collection<? extends AtomVariable> getPrerequisitesVars(final Collection<AtomVariable> bound) { return _helper.getPrerequisiteVars(bound); }
@Test public void testTokenizeBindingEmpty() { final KnowledgeBaseImpl kb = new KnowledgeBaseImpl(); final AtomDConstant data = new AtomDConstant(literal("hi;bye;foo;bar")); final AtomDVariable x = new AtomDVariable("x"); final AtomDConstant comma = new AtomDConstant(literal(",")); final Collection<AtomVariable> emptyCollection = Collections.emptySet(); final Collection<AtomVariable> xSingleton = Collections.singleton((AtomVariable) x); final BuiltInAtom oneVarAtom = new BuiltInAtom(Namespaces.SWRLB + "tokenize", x, data, comma); final BindingHelper sharedVarHelper = StringOperators.tokenize.createHelper(oneVarAtom); assertTrue(sharedVarHelper.getBindableVars(emptyCollection).equals(xSingleton)); final VariableBinding emptyBinding = new VariableBinding(kb.getABox()); sharedVarHelper.rebind(emptyBinding); final VariableBinding fillBinding = new VariableBinding(kb.getABox()); final List<String> expected = Collections.singletonList(ATermUtils.getLiteralValue(data.getValue())); final List<String> tokens = new ArrayList<>(); while (sharedVarHelper.selectNextBinding()) { sharedVarHelper.setCurrentBinding(fillBinding); final String token = ATermUtils.getLiteralValue(fillBinding.get(x).getTerm()); tokens.add(token); } assertEquals("String tokenizer returned unexpected sequence of tokens", expected, tokens); }
@Test public void testTokenizeSharedFailure() { final KnowledgeBaseImpl kb = new KnowledgeBaseImpl(); final AtomDConstant data = new AtomDConstant(literal("hi;bye;foo;bar")); final AtomDVariable x = new AtomDVariable("x"); final AtomDConstant semicolan = new AtomDConstant(literal(";")); final Collection<AtomVariable> emptyCollection = Collections.emptySet(); final Collection<AtomVariable> xSingleton = Collections.singleton((AtomVariable) x); final BuiltInAtom sharedVarAtom = new BuiltInAtom(Namespaces.SWRLB + "tokenize", x, x, semicolan); final BindingHelper sharedVarHelper = StringOperators.tokenize.createHelper(sharedVarAtom); assertTrue(sharedVarHelper.getBindableVars(emptyCollection).isEmpty()); assertTrue(sharedVarHelper.getBindableVars(xSingleton).isEmpty()); final VariableBinding xdataBinding = new VariableBinding(kb.getABox()); xdataBinding.set(x, data.getValue()); sharedVarHelper.rebind(xdataBinding); assertFalse(sharedVarHelper.selectNextBinding()); }
/** * Reorder the binding helpers so that completely bound binding helpers are push as far up the list as they can be */ private static void optimize(final List<BindingHelper> helpers) { final Set<AtomVariable> bound = new HashSet<>(); for (int i = 0; i < helpers.size(); i++) { // Move any helpers which are completely bound and satisfied // to this point int j = 0; while (i + j < helpers.size()) { final BindingHelper helper = helpers.get(i + j); if (bound.containsAll(helper.getBindableVars(bound)) && bound.containsAll(helper.getPrerequisiteVars(bound))) { helpers.remove(i + j); helpers.add(i, helper); i++; // Bump - since it is bound, we won't need to keep track of its bindable vars. } else j++; // Keep searching } // May have moved off the list. if (i < helpers.size()) bound.addAll(helpers.get(i).getBindableVars(bound)); } }
public BindingIterator() { _helperChain = new BindingHelper[_helpers.size()]; _helperChain = _helpers.toArray(_helperChain); if (_helperChain.length > 0) _helperChain[0].rebind(_initialBinding); }
/** * Return the _current _binding up through and including the <code>max</code> element of the pattern chain. * * @param max * @return */ private VariableBinding getBinding(final int max) { final VariableBinding newBinding = new VariableBinding(_initialBinding); for (int i = 0; i <= max; i++) _helperChain[i].setCurrentBinding(newBinding); return newBinding; }
@Test public void testTokenizeBinding() { final KnowledgeBaseImpl kb = new KnowledgeBaseImpl(); final AtomDConstant data = new AtomDConstant(literal("hi;bye;foo;bar")); final AtomDVariable x = new AtomDVariable("x"); final AtomDConstant semicolan = new AtomDConstant(literal(";")); final Collection<AtomVariable> emptyCollection = Collections.emptySet(); final Collection<AtomVariable> xSingleton = Collections.singleton((AtomVariable) x); final BuiltInAtom oneVarAtom = new BuiltInAtom(Namespaces.SWRLB + "tokenize", x, data, semicolan); final BindingHelper sharedVarHelper = StringOperators.tokenize.createHelper(oneVarAtom); assertTrue(sharedVarHelper.getBindableVars(emptyCollection).equals(xSingleton)); final VariableBinding emptyBinding = new VariableBinding(kb.getABox()); sharedVarHelper.rebind(emptyBinding); final VariableBinding fillBinding = new VariableBinding(kb.getABox()); final List<String> expected = Arrays.asList(new String[] { "hi", "bye", "foo", "bar" }); final List<String> tokens = new ArrayList<>(); while (sharedVarHelper.selectNextBinding()) { sharedVarHelper.setCurrentBinding(fillBinding); final String token = ATermUtils.getLiteralValue(fillBinding.get(x).getTerm()); tokens.add(token); } assertEquals("String tokenizer returned unexpected sequence of tokens", expected, tokens); }
@Test public void testTokenizeSharedSuccess() { final KnowledgeBaseImpl kb = new KnowledgeBaseImpl(); final AtomDConstant data = new AtomDConstant(literal("hi;bye;foo;bar")); final AtomDVariable x = new AtomDVariable("x"); final AtomDConstant comma = new AtomDConstant(literal(",")); final Collection<AtomVariable> emptyCollection = Collections.emptySet(); final Collection<AtomVariable> xSingleton = Collections.singleton((AtomVariable) x); final BuiltInAtom sharedVarAtom = new BuiltInAtom(Namespaces.SWRLB + "tokenize", x, x, comma); final BindingHelper sharedVarHelper = StringOperators.tokenize.createHelper(sharedVarAtom); assertTrue(sharedVarHelper.getBindableVars(emptyCollection).isEmpty()); assertTrue(sharedVarHelper.getBindableVars(xSingleton).isEmpty()); final VariableBinding xdataBinding = new VariableBinding(kb.getABox()); xdataBinding.set(x, data.getValue()); sharedVarHelper.rebind(xdataBinding); assertTrue(sharedVarHelper.selectNextBinding()); assertFalse(sharedVarHelper.selectNextBinding()); }
if (bound.containsAll(helper.getPrerequisiteVars(bound))) bound.addAll(helper.getBindableVars(bound)); if (bound.containsAll(unsat.getPrerequisiteVars(bound))) bound.addAll(unsat.getBindableVars(bound)); unsatIter.remove();
public BindingIterator() { _helperChain = new BindingHelper[_helpers.size()]; _helperChain = _helpers.toArray(_helperChain); if (_helperChain.length > 0) _helperChain[0].rebind(_initialBinding); }
/** * Return the _current _binding up through and including the <code>max</code> element of the pattern chain. * * @param max * @return */ private VariableBinding getBinding(final int max) { final VariableBinding newBinding = new VariableBinding(_initialBinding); for (int i = 0; i <= max; i++) _helperChain[i].setCurrentBinding(newBinding); return newBinding; }
int position = _helperChain.length - 1; while (position >= 0) if (_helperChain[position].selectNextBinding()) _helperChain[position].setCurrentBinding(newBinding); if (position < _helperChain.length - 1) _helperChain[position + 1].rebind(newBinding); position++;
/** * Reorder the binding helpers so that completely bound binding helpers are push as far up the list as they can be */ private static void optimize(final List<BindingHelper> helpers) { final Set<AtomVariable> bound = new HashSet<>(); for (int i = 0; i < helpers.size(); i++) { // Move any helpers which are completely bound and satisfied // to this point int j = 0; while (i + j < helpers.size()) { final BindingHelper helper = helpers.get(i + j); if (bound.containsAll(helper.getBindableVars(bound)) && bound.containsAll(helper.getPrerequisiteVars(bound))) { helpers.remove(i + j); helpers.add(i, helper); i++; // Bump - since it is bound, we won't need to keep track of its bindable vars. } else j++; // Keep searching } // May have moved off the list. if (i < helpers.size()) bound.addAll(helpers.get(i).getBindableVars(bound)); } }
public Collection<? extends AtomVariable> getBindableVars(final Collection<AtomVariable> bound) { return _helper.getBindableVars(bound); }
public Collection<? extends AtomVariable> getPrerequisitesVars(final Collection<AtomVariable> bound) { return _helper.getPrerequisiteVars(bound); }
if (bound.containsAll(helper.getPrerequisiteVars(bound))) bound.addAll(helper.getBindableVars(bound)); if (bound.containsAll(unsat.getPrerequisiteVars(bound))) bound.addAll(unsat.getBindableVars(bound)); unsatIter.remove();
selectiveVariables.addAll(helper.getBindableVars(emptyCollection));
selectiveVariables.addAll(helper.getBindableVars(emptyCollection));