InstanceCreator extends BaseFunction { @SuppressWarnings("unchecked") protected Instance<L> createInstance(TridentTuple tuple) { Instance<L> instance = null; if (this.withLabel) { L label = (L) tuple.get(0); double[] features = new double[tuple.size() - 1]; for (int i = 1; i < tuple.size(); i++) { features[i - 1] = tuple.getDouble(i); } instance = new Instance<L>(label, features); } else { double[] features = new double[tuple.size()]; for (int i = 0; i < tuple.size(); i++) { features[i] = tuple.getDouble(i); } instance = new Instance<L>(features); } return instance; } InstanceCreator();  InstanceCreator(boolean withLabel); @Override void execute(TridentTuple tuple, TridentCollector collector);  }
@Test public void testCreateInstanceWithLabel() { InstanceCreator<Boolean> instanceCreator = new InstanceCreator<Boolean>(); TridentTuple tuple = mock(TridentTuple.class); Boolean expectedLabel = true; double f1 = 0.53; double f2 = 0.65; when(tuple.size()).thenReturn(3); when(tuple.get(0)).thenReturn(expectedLabel); when(tuple.getDouble(1)).thenReturn(f1); when(tuple.getDouble(2)).thenReturn(f2); Instance<Boolean> actualInstance = instanceCreator.createInstance(tuple); Instance<Boolean> expectedInstance = new Instance<Boolean>(expectedLabel, new double[] { f1, f2 }); assertEquals(expectedInstance, actualInstance); }
@Test public void testCreateInstanceWithoutLabel() { InstanceCreator<Boolean> instanceCreator = new InstanceCreator<Boolean>(false); TridentTuple tuple = mock(TridentTuple.class); double f1 = 0.53; double f2 = 0.65; when(tuple.size()).thenReturn(2); when(tuple.getDouble(0)).thenReturn(f1); when(tuple.getDouble(1)).thenReturn(f2); Instance<Boolean> actualInstance = instanceCreator.createInstance(tuple); Instance<Boolean> expectedInstance = new Instance<Boolean>(new double[] { f1, f2 }); assertEquals(expectedInstance, actualInstance); }