use of org.knime.core.data.convert.map.SimpleCellValueProducerFactory in project knime-core by knime.
the class MappingFrameworkTest method producerTest.
/**
* @throws Exception
*/
@Test
public void producerTest() throws Exception {
/* One time setup */
final CellValueProducer<H2OSource, Integer, H2OParameters> lambda = (c, p) -> {
return (Integer) c.h2oFrame.get(p.rowIndex)[p.columnIndex];
};
final SimpleCellValueProducerFactory<H2OSource, String, Integer, H2OParameters> intProducerFromBIGINT = new SimpleCellValueProducerFactory<>("BIGINT", Integer.class, lambda);
final SimpleCellValueProducerFactory<H2OSource, String, Long, H2OParameters> longProducer = new SimpleCellValueProducerFactory<>("LONG", Long.class, (c, p) -> {
return (Long) c.h2oFrame.get(p.rowIndex)[p.columnIndex];
});
//
MappingFramework.forSourceType(H2OSource.class).unregisterAllProducers().register(//
intProducer).register(//
longProducer).register(//
stringProducer).register(//
intProducerFromBIGINT);
assertEquals(intProducer, MappingFramework.forSourceType(H2OSource.class).getFactories("INT", Integer.class).stream().findFirst().get());
final List<ProductionPath> paths = MappingFramework.forSourceType(H2OSource.class).getAvailableProductionPaths("INT");
assertEquals(3, paths.size());
final DataType[] outTypes = paths.stream().map(p -> p.m_converterFactory.getDestinationType()).toArray(n -> new DataType[n]);
assertEquals(1, Stream.of(outTypes).filter(s -> s.equals(IntCell.TYPE)).count());
assertEquals(1, Stream.of(outTypes).filter(s -> s.equals(LongCell.TYPE)).count());
assertEquals(1, Stream.of(outTypes).filter(s -> s.equals(DoubleCell.TYPE)).count());
final ProductionPath expectedPath = new ProductionPath(intProducer, JavaToDataCellConverterRegistry.getInstance().getConverterFactories(Integer.class, IntCell.TYPE).stream().findFirst().get());
assertTrue(paths.contains(expectedPath));
assertTrue(MappingFramework.forDestinationType(H2ODestination.class).getFactories(ConsumptionPath.class, "INT").isEmpty());
ProductionPath[] mapping = new ProductionPath[] { new ProductionPath(MappingFramework.forSourceType(H2OSource.class).getFactory("STR->java.lang.String").get(), JavaToDataCellConverterRegistry.getInstance().getConverterFactories(String.class, StringCell.TYPE).stream().findFirst().get()), new ProductionPath(MappingFramework.forSourceType(H2OSource.class).getFactory("INT->java.lang.Integer").get(), JavaToDataCellConverterRegistry.getInstance().getConverterFactories(Integer.class, IntCell.TYPE).stream().findFirst().get()), new ProductionPath(MappingFramework.forSourceType(H2OSource.class).getFactory("LONG->java.lang.Long").get(), JavaToDataCellConverterRegistry.getInstance().getConverterFactories(Long.class, LongCell.TYPE).stream().findFirst().get()), new ProductionPath(MappingFramework.forSourceType(H2OSource.class).getFactory("LONG->java.lang.Long").get(), JavaToDataCellConverterRegistry.getInstance().getConverterFactories(Long.class, LongCell.TYPE).stream().findFirst().get()) };
H2OParameters[] parameters = new H2OParameters[4];
for (int i = 0; i < parameters.length; ++i) {
parameters[i] = new H2OParameters();
parameters[i].columnIndex = i;
parameters[i].rowIndex = 0;
}
{
final H2OSource testSource = new H2OSource();
testSource.h2oFrame.add(new Object[] { "KNIME", new Integer(42), new Long(42L), null });
final DataRow row = MappingFramework.map(RowKey.createRowKey(0L), testSource, mapping, parameters, null);
assertEquals(StringCell.TYPE, row.getCell(0).getType());
assertEquals("KNIME", ((StringValue) row.getCell(0)).getStringValue());
assertEquals(IntCell.TYPE, row.getCell(1).getType());
assertEquals(42, ((IntValue) row.getCell(1)).getIntValue());
assertEquals(LongCell.TYPE, row.getCell(2).getType());
assertEquals(42L, ((LongValue) row.getCell(2)).getLongValue());
assertTrue(row.getCell(3).isMissing());
}
DataTableSpec spec = MappingFramework.createSpec(new String[] { "s", "i", "l", "missing" }, mapping);
assertEquals(StringCell.TYPE, spec.getColumnSpec(0).getType());
assertEquals(IntCell.TYPE, spec.getColumnSpec(1).getType());
assertEquals(LongCell.TYPE, spec.getColumnSpec(2).getType());
assertEquals(LongCell.TYPE, spec.getColumnSpec(2).getType());
}
Aggregations