Search in sources :

Example 1 with CellValueProducer

use of org.knime.core.data.convert.map.CellValueProducer 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());
}
Also used : CellValueProducer(org.knime.core.data.convert.map.CellValueProducer) LongValue(org.knime.core.data.LongValue) Arrays(java.util.Arrays) RowKey(org.knime.core.data.RowKey) DataTableSpec(org.knime.core.data.DataTableSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ProducerRegistry(org.knime.core.data.convert.map.ProducerRegistry) MappingFramework(org.knime.core.data.convert.map.MappingFramework) ProductionPath(org.knime.core.data.convert.map.ProductionPath) NodeSettings(org.knime.core.node.NodeSettings) LongCell(org.knime.core.data.def.LongCell) ArrayList(java.util.ArrayList) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) SerializeUtil(org.knime.core.data.convert.util.SerializeUtil) SimpleCellValueProducerFactory(org.knime.core.data.convert.map.SimpleCellValueProducerFactory) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Destination(org.knime.core.data.convert.map.Destination) Source(org.knime.core.data.convert.map.Source) StringValue(org.knime.core.data.StringValue) IntValue(org.knime.core.data.IntValue) CellValueConsumerFactory(org.knime.core.data.convert.map.CellValueConsumerFactory) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell) DataValue(org.knime.core.data.DataValue) Collection(java.util.Collection) CellValueConsumer(org.knime.core.data.convert.map.CellValueConsumer) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) DoubleCell(org.knime.core.data.def.DoubleCell) Collectors(java.util.stream.Collectors) ConsumptionPath(org.knime.core.data.convert.map.ConsumptionPath) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) DataRow(org.knime.core.data.DataRow) Objects(java.util.Objects) DataCellToJavaConverterRegistry(org.knime.core.data.convert.java.DataCellToJavaConverterRegistry) List(java.util.List) Stream(java.util.stream.Stream) Assert.assertFalse(org.junit.Assert.assertFalse) MissingCell(org.knime.core.data.MissingCell) Optional(java.util.Optional) SimpleCellValueConsumerFactory(org.knime.core.data.convert.map.SimpleCellValueConsumerFactory) StringCell(org.knime.core.data.def.StringCell) Config(org.knime.core.node.config.Config) DataType(org.knime.core.data.DataType) JavaToDataCellConverterRegistry(org.knime.core.data.convert.datacell.JavaToDataCellConverterRegistry) CellValueProducerFactory(org.knime.core.data.convert.map.CellValueProducerFactory) Assert.assertEquals(org.junit.Assert.assertEquals) DataTableSpec(org.knime.core.data.DataTableSpec) DataRow(org.knime.core.data.DataRow) SimpleCellValueProducerFactory(org.knime.core.data.convert.map.SimpleCellValueProducerFactory) LongValue(org.knime.core.data.LongValue) DataType(org.knime.core.data.DataType) StringValue(org.knime.core.data.StringValue) IntValue(org.knime.core.data.IntValue) ProductionPath(org.knime.core.data.convert.map.ProductionPath) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Assert.assertArrayEquals (org.junit.Assert.assertArrayEquals)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertNotEquals (org.junit.Assert.assertNotEquals)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Test (org.junit.Test)1 DataRow (org.knime.core.data.DataRow)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 DataType (org.knime.core.data.DataType)1 DataValue (org.knime.core.data.DataValue)1 IntValue (org.knime.core.data.IntValue)1 LongValue (org.knime.core.data.LongValue)1