/** * 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; }
/** * 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; }
public void addUnsafeRule(final Rule rule, final Set<ATermAppl> explain) { _unsafeRules.add(new PartialBinding(rule, new VariableBinding(_abox), new DependencySet(explain))); }
public void addUnsafeRule(final Rule rule, final Set<ATermAppl> explain) { _unsafeRules.add(new PartialBinding(rule, new VariableBinding(_abox), new DependencySet(explain))); }
/** * Set the incoming binding for this helper. This fixes any variables that are already bound by a preceding Binding Helper. * * @param newBinding Binding map. Copied on input. */ @Override public void rebind(final VariableBinding newBinding) { _binding = new VariableBinding(newBinding); if (getSubject() != null) _subjectIterator = Collections.singleton(getSubject()).iterator(); else _subjectIterator = new AllNamedIndividualsIterator(_abox); }
/** * Set the incoming binding for this helper. This fixes any variables that are already bound by a preceding Binding Helper. * * @param newBinding Binding map. Copied on input. */ @Override public void rebind(final VariableBinding newBinding) { _binding = new VariableBinding(newBinding); if (getSubject() != null) _subjectIterator = Collections.singleton(getSubject()).iterator(); else _subjectIterator = new AllNamedIndividualsIterator(_abox); }
@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 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)); }
@Test public void testObjectVariableBindingGenerator() { _kb.addIndividual(mary); _kb.addIndividual(robert); _kb.addIndividual(victor); final BindingHelper genHelper = new ObjectVariableBindingHelper(_kb.getABox(), _x); final BindingGenerator gen = new BindingGeneratorImpl(new VariableBinding(_kb.getABox()), Collections.singletonList(genHelper)); assertIteratorValues(new BindingToMapIterator(gen.iterator()), new Object[] { Collections.singletonMap(_x, mary), Collections.singletonMap(_x, robert), Collections.singletonMap(_x, victor), }); }
@Test public void testDatavalueBindingGeneratorObjects() { final DatavaluedPropertyAtom pattern = new DatavaluedPropertyAtom(dp2, new AtomIConstant(mary), _z); final BindingHelper genHelper = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern); final BindingGenerator gen = new BindingGeneratorImpl(new VariableBinding(_kb.getABox()), Collections.singletonList(genHelper)); assertIteratorValues(new BindingToMapIterator(gen.iterator()), new Object[] { Collections.singletonMap(_z, data1), Collections.singletonMap(_z, data2), }); }
@Test public void testDatavalueBindingGeneratorSubjects() { final DatavaluedPropertyAtom pattern = new DatavaluedPropertyAtom(dp2, _x, new AtomDConstant(data2)); final BindingHelper genHelper = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern); final BindingGenerator gen = new BindingGeneratorImpl(new VariableBinding(_kb.getABox()), Collections.singletonList(genHelper)); assertIteratorValues(new BindingToMapIterator(gen.iterator()), new Object[] { Collections.singletonMap(_x, mary), Collections.singletonMap(_x, robert), }); }
@Test public void testCombinatorialBindingGeneration() { final BindingHelper genHelper1 = new ObjectVariableBindingHelper(_kb.getABox(), _x); final BindingHelper genHelper2 = new ObjectVariableBindingHelper(_kb.getABox(), _y); final Individual[] individualsUsed = { _kb.getABox().getIndividual(mary), _kb.getABox().getIndividual(robert), _kb.getABox().getIndividual(victor), }; final List<BindingHelper> genSet = new ArrayList<>(); final BindingGenerator emptyGen = new BindingGeneratorImpl(new VariableBinding(_kb.getABox()), genSet); assertFalse(emptyGen.iterator().hasNext()); genSet.add(genHelper1); genSet.add(genHelper2); final BindingGenerator gen = new BindingGeneratorImpl(new VariableBinding(_kb.getABox()), genSet); final List<VariableBinding> expected = new LinkedList<>(); for (final Individual xNode : individualsUsed) for (final Individual yNode : individualsUsed) { final VariableBinding bindings = new VariableBinding(_kb.getABox()); bindings.set(_x, xNode); bindings.set(_y, yNode); expected.add(bindings); } assertIteratorValues(gen.iterator(), expected.iterator()); }
@Test public void testDataRangeBindingHelper() { final DatavaluedPropertyAtom pattern = new DatavaluedPropertyAtom(dp2, _x, _z); final DataRangeAtom atom = new DataRangeAtom(ATermUtils.makeTermAppl(Namespaces.XSD + "integer"), _z); final BindingHelper patternHelper = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern); final BindingHelper rangeHelper = new DataRangeBindingHelper(_kb.getABox(), atom); final BindingGenerator gen = new BindingGeneratorImpl(new VariableBinding(_kb.getABox()), Arrays.asList(new BindingHelper[] { patternHelper, rangeHelper, })); final VariableBinding expectedBinding = new VariableBinding(_kb.getABox()); expectedBinding.set(_x, _kb.getABox().getIndividual(victor)); expectedBinding.set(_z, _kb.getABox().getLiteral(data4)); final List<VariableBinding> expected = new LinkedList<>(); expected.add(expectedBinding); assertIteratorValues(gen.iterator(), expected.iterator()); }
@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 testDatavalueBindingGeneratorChainedSubject() { final DatavaluedPropertyAtom pattern1 = new DatavaluedPropertyAtom(dp2, _x, new AtomDConstant(data2)); final DatavaluedPropertyAtom pattern2 = new DatavaluedPropertyAtom(dp2, _y, new AtomDConstant(data2)); final BindingHelper genHelper1 = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern1); final BindingHelper genHelper2 = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern2); final BindingGenerator gen = new BindingGeneratorImpl(new VariableBinding(_kb.getABox()), Arrays.asList(new BindingHelper[] { genHelper1, genHelper2 })); final List<VariableBinding> expected = new LinkedList<>(); VariableBinding binding; final ATermAppl[] names = new ATermAppl[] { mary, robert }; for (final ATermAppl xName : names) for (final ATermAppl yName : names) { final Individual xNode = _kb.getABox().getIndividual(xName); final Individual yNode = _kb.getABox().getIndividual(yName); binding = new VariableBinding(_kb.getABox()); binding.set(_x, xNode); binding.set(_y, yNode); expected.add(binding); } assertIteratorValues(gen.iterator(), expected.iterator()); }
@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 testDatavalueBindingGeneratorChained() { final DatavaluedPropertyAtom pattern1 = new DatavaluedPropertyAtom(dp2, _x, _z); final DatavaluedPropertyAtom pattern2 = new DatavaluedPropertyAtom(dp2, _y, _z); final BindingHelper genHelper1 = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern1); final BindingHelper genHelper2 = new DatavaluePropertyBindingHelper(_kb.getABox(), pattern2); final BindingGenerator gen = new BindingGeneratorImpl(new VariableBinding(_kb.getABox()), Arrays.asList(new BindingHelper[] { genHelper1, genHelper2 })); final List<VariableBinding> expected = new LinkedList<>(); VariableBinding binding; final ATermAppl[] names = new ATermAppl[] { mary, robert, victor }; final ATermAppl[] values = new ATermAppl[] { data1, data2, data3, data4 }; for (final ATermAppl xName : names) for (final ATermAppl yName : names) for (final ATermAppl zValue : values) { final Individual xNode = _kb.getABox().getIndividual(xName); final Individual yNode = _kb.getABox().getIndividual(yName); final Literal zNode = _kb.getABox().addLiteral(zValue); if (_kb.hasPropertyValue(xName, dp2, zValue) && _kb.hasPropertyValue(yName, dp2, zValue)) { binding = new VariableBinding(_kb.getABox()); binding.set(_x, xNode); binding.set(_y, yNode); binding.set(_z, zNode); expected.add(binding); } } assertIteratorValues(gen.iterator(), expected.iterator()); }
@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()); }
@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()); }
@Test public void testIsAtomTrue() { _kb.addObjectProperty(p); _kb.addIndividual(mary); _kb.addIndividual(robert); _kb.addPropertyValue(p, mary, robert); final VariableBinding binding = new VariableBinding(_kb.getABox()); binding.set(_x, mary); binding.set(_y, robert); final RuleAtom atom = new IndividualPropertyAtom(p, _x, _y); final TrivialSatisfactionHelpers tester = new TrivialSatisfactionHelpers(_kb.getABox()); assertTrue(tester.isAtomTrue(atom, binding) != null); }