@Override public void activate(final Token token) { resetDependencySet(token); final VariableBinding binding = new VariableBinding(_strategy.getABox()); for (final Entry<AtomVariable, NodeProvider> entry : _args.entrySet()) { final AtomObject arg = entry.getKey(); final Node node = getNode(_args.get(arg), token); if (arg instanceof AtomIVariable) binding.set((AtomIVariable) arg, (Individual) node); else binding.set((AtomDVariable) arg, (Literal) node); } _logger.fine(() -> "Produce binding " + _rule + " -> " + binding); _strategy.addPartialBinding(new PartialBinding(_rule, binding, _ds)); }
@Override public void rebind(final VariableBinding newBinding) { if (newBinding.containsKey(_var)) _individualIterator = Collections.singleton(newBinding.get(_var)).iterator(); else _individualIterator = new AllNamedIndividualsIterator(_abox); }
@Override public void setCurrentBinding(final VariableBinding currentBinding) { for (final Map.Entry<AtomDVariable, Literal> entry : _partial.dataEntrySet()) currentBinding.set(entry.getKey(), entry.getValue()); }
@Override public void setCurrentBinding(final VariableBinding currentBinding) { if (VariableUtils.isVariable(_head)) { final ATermAppl resultTerm = ATermUtils.makePlainLiteral(_match); final Literal resultLit = currentBinding.getABox().addLiteral(resultTerm); currentBinding.set(_head, resultLit); } }
arguments[i] = newBinding.get(_atom.getAllArguments().get(i)); if (_function.apply(newBinding.getABox(), arguments)) final VariableBinding newPartial = new VariableBinding(newBinding.getABox()); for (int i = 0; i < arguments.length; i++) final Literal current = newBinding.get(arg); if (newBinding.get(arg) != null) throw new InternalReasonerException("General Function implementation overwrote one of its arguments!"); BuiltInRegistry._logger.info("Function results in multiple simultaneous values for variable"); newBinding.set(arg, result);
/** * 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; }
@Override public void setCurrentBinding(final VariableBinding currentBinding) { currentBinding.set(_head, _value); }
@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); }
@Override public Map<AtomVariable, ATermAppl> next() { final Map<AtomVariable, ATermAppl> result = new HashMap<>(); final VariableBinding binding = _iter.next(); for (final Map.Entry<? extends AtomVariable, ? extends Node> entry : binding.entrySet()) result.put(entry.getKey(), entry.getValue().getTerm()); return result; }
arguments[i] = newBinding.get(_atom.getAllArguments().get(i)); if (_function.apply(newBinding.getABox(), arguments)) final VariableBinding newPartial = new VariableBinding(newBinding.getABox()); for (int i = 0; i < arguments.length; i++) final Literal current = newBinding.get(arg); if (newBinding.get(arg) != null) throw new InternalReasonerException("General Function implementation overwrote one of its arguments!"); BuiltInRegistry._logger.info("Function results in multiple simultaneous values for variable"); newBinding.set(arg, result);
/** * 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; }
@Override public void setCurrentBinding(final VariableBinding currentBinding) { currentBinding.set(_var, _currentIndividual); }
@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); }
@Override public void setCurrentBinding(final VariableBinding currentBinding) { for (final Map.Entry<AtomDVariable, Literal> entry : _partial.dataEntrySet()) currentBinding.set(entry.getKey(), entry.getValue()); }
@Override public void setCurrentBinding(final VariableBinding currentBinding) { if (VariableUtils.isVariable(_head)) { final ATermAppl resultTerm = ATermUtils.makePlainLiteral(_match); final Literal resultLit = currentBinding.getABox().addLiteral(resultTerm); currentBinding.set(_head, resultLit); } }
@Override public void activate(final Token token) { resetDependencySet(token); final VariableBinding binding = new VariableBinding(_strategy.getABox()); for (final Entry<AtomVariable, NodeProvider> entry : _args.entrySet()) { final AtomObject arg = entry.getKey(); final Node node = getNode(_args.get(arg), token); if (arg instanceof AtomIVariable) binding.set((AtomIVariable) arg, (Individual) node); else binding.set((AtomDVariable) arg, (Literal) node); } _logger.fine(() -> "Produce binding " + _rule + " -> " + binding); _strategy.addPartialBinding(new PartialBinding(_rule, binding, _ds)); }