public Collection<? extends AtomVariable> getBindableVars(final Collection<AtomVariable> bound) { return _helper.getBindableVars(bound); }
public Collection<? extends AtomVariable> getBindableVars(final Collection<AtomVariable> bound) { return _helper.getBindableVars(bound); }
/** * 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)); } }
/** * 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)); } }
bound.addAll(helper.getBindableVars(bound)); bound.addAll(unsat.getBindableVars(bound)); unsatIter.remove();
bound.addAll(helper.getBindableVars(bound)); bound.addAll(unsat.getBindableVars(bound)); unsatIter.remove();
selectiveVariables.addAll(helper.getBindableVars(emptyCollection));
selectiveVariables.addAll(helper.getBindableVars(emptyCollection));
@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 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 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); }