Search in sources :

Example 1 with ConsumptionPath

use of org.knime.core.data.convert.map.ConsumptionPath in project knime-core by knime.

the class MappingFrameworkTest method consumptionPaths.

/**
 * Tests {@link ConsumptionPath#equals} and {@link ConsumptionPath#toString()}
 */
@Test
public void consumptionPaths() {
    final DataCellToJavaConverterFactory<? extends DataValue, Integer> intFactory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(IntCell.TYPE, Integer.class).stream().findFirst().get();
    final DataCellToJavaConverterFactory<? extends DataValue, String> stringFactory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(StringCell.TYPE, String.class).stream().findFirst().get();
    final ConsumptionPath pathA = new ConsumptionPath(intFactory, intConsumer);
    final ConsumptionPath pathB = new ConsumptionPath(intFactory, intConsumer);
    final ConsumptionPath pathC = new ConsumptionPath(stringFactory, intConsumer);
    final ConsumptionPath pathD = new ConsumptionPath(null, null);
    final ConsumptionPath pathE = new ConsumptionPath(null, intConsumer);
    assertTrue("ConversionPath should equal itself", pathA.equals(pathA));
    assertTrue(pathA.equals(pathB));
    assertTrue(pathB.equals(pathA));
    assertTrue(pathD.equals(pathD));
    assertFalse(pathA.equals(pathC));
    assertFalse(pathA.equals(new Integer(42)));
    assertFalse(pathA.equals(pathD));
    assertFalse(pathE.equals(pathD));
    assertFalse(pathD.equals(pathE));
    assertFalse(pathE.equals(pathA));
    assertFalse(pathA.equals(null));
    assertEquals("IntValue --(\"Integer\")-> Integer ---> INT", pathA.toString());
    assertEquals(pathA.hashCode(), pathA.hashCode());
    assertEquals(pathA.hashCode(), pathB.hashCode());
    assertNotEquals(pathA.hashCode(), pathC.hashCode());
    assertEquals(pathA.getConsumerFactory(), pathA.m_consumerFactory);
    assertEquals(pathA.getConverterFactory(), pathA.m_converterFactory);
}
Also used : ConsumptionPath(org.knime.core.data.convert.map.ConsumptionPath) Test(org.junit.Test)

Example 2 with ConsumptionPath

use of org.knime.core.data.convert.map.ConsumptionPath in project knime-core by knime.

the class MappingFrameworkTest method consumerTest.

/**
 * @throws Exception
 */
@Test
public void consumerTest() throws Exception {
    /* One time setup */
    final CellValueConsumer<H2ODestination, Object, H2OParameters> generalConsumer = (c, v, p) -> {
        c.h2oFrame.get(p.rowIndex)[p.columnIndex] = v;
    };
    // TODO extension point
    // 
    MappingFramework.forDestinationType(H2ODestination.class).unregisterAllConsumers().register(// 
    intConsumer).register(// 
    new SimpleCellValueConsumerFactory<>(Long.class, "LONG", generalConsumer)).register(// 
    new SimpleCellValueConsumerFactory<>(Float.class, "FLOAT", generalConsumer)).register(// 
    new SimpleCellValueConsumerFactory<>(Double.class, "DOUBLE", generalConsumer)).register(// 
    stringConsumer).register(// Will display a CODING error, but should not succeed
    stringConsumer);
    assertEquals(intConsumer, MappingFramework.forDestinationType(H2ODestination.class).getFactories(Integer.class, "INT").stream().findFirst().get());
    final List<ConsumptionPath> paths = MappingFramework.forDestinationType(H2ODestination.class).getAvailableConsumptionPaths(IntCell.TYPE);
    assertEquals(4, paths.size());
    final String[] inTypes = paths.stream().map(p -> p.m_consumerFactory.getDestinationType()).toArray(n -> new String[n]);
    assertEquals(1, Stream.of(inTypes).filter(s -> s.equals("INT")).count());
    assertEquals(1, Stream.of(inTypes).filter(s -> s.equals("STR")).count());
    assertEquals(1, Stream.of(inTypes).filter(s -> s.equals("LONG")).count());
    assertEquals(1, Stream.of(inTypes).filter(s -> s.equals("DOUBLE")).count());
    assertEquals(intConsumer.getDestinationType(), intConsumer.getName());
    // TODO later: deprecation mechanism?
    // TODO later: priority mechanism (=deprecation?).
    final ConsumptionPath expectedPath = new ConsumptionPath(DataCellToJavaConverterRegistry.getInstance().getConverterFactories(IntCell.TYPE, Integer.class).stream().findFirst().get(), intConsumer);
    assertTrue(paths.contains(expectedPath));
    assertTrue(MappingFramework.forDestinationType(H2ODestination.class).getFactories(ConsumptionPath.class, "INT").isEmpty());
    // TODO
    // Assumption: RESULTSET is stateful...
    // expectedPath.map(Col, RESULTSET);
    // expectedPath.map(Col, RESULTSET);
    /* Some example input */
    final DefaultRow row = new DefaultRow(RowKey.createRowKey(0L), new StringCell("KNIME"), new IntCell(42), new LongCell(42L), new MissingCell("missing"));
    final H2ODestination testSink = new H2ODestination();
    testSink.h2oFrame.add(new Object[4]);
    final ConsumptionPath[] mapping = new ConsumptionPath[] { new ConsumptionPath(DataCellToJavaConverterRegistry.getInstance().getConverterFactories(StringCell.TYPE, String.class).stream().findFirst().get(), MappingFramework.forDestinationType(H2ODestination.class).getFactory("java.lang.String->STR").get()), new ConsumptionPath(DataCellToJavaConverterRegistry.getInstance().getConverterFactories(IntCell.TYPE, Integer.class).stream().findFirst().get(), MappingFramework.forDestinationType(H2ODestination.class).getFactory("java.lang.Integer->INT").get()), new ConsumptionPath(DataCellToJavaConverterRegistry.getInstance().getConverterFactories(LongCell.TYPE, Long.class).stream().findFirst().get(), MappingFramework.forDestinationType(H2ODestination.class).getFactory("java.lang.Long->LONG").get()), new ConsumptionPath(DataCellToJavaConverterRegistry.getInstance().getConverterFactories(LongCell.TYPE, Long.class).stream().findFirst().get(), MappingFramework.forDestinationType(H2ODestination.class).getFactory("java.lang.Long->LONG").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;
    }
    MappingFramework.map(row, testSink, mapping, parameters);
    assertEquals(1, testSink.h2oFrame.size());
    assertEquals(4, testSink.h2oFrame.get(0).length);
    assertArrayEquals(new Object[] { "KNIME", new Integer(42), new Long(42L), null }, testSink.h2oFrame.get(0));
}
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) ConsumptionPath(org.knime.core.data.convert.map.ConsumptionPath) IntCell(org.knime.core.data.def.IntCell) LongCell(org.knime.core.data.def.LongCell) StringCell(org.knime.core.data.def.StringCell) MissingCell(org.knime.core.data.MissingCell) SimpleCellValueConsumerFactory(org.knime.core.data.convert.map.SimpleCellValueConsumerFactory) DefaultRow(org.knime.core.data.def.DefaultRow) Test(org.junit.Test)

Example 3 with ConsumptionPath

use of org.knime.core.data.convert.map.ConsumptionPath in project knime-core by knime.

the class SerializeUtil method loadConsumptionPath.

/**
 * Load a {@link ConsumptionPath} from given config.
 *
 * @param config Config to load from
 * @param registry Registry to load consumer with
 * @param key setting key
 * @return an optional {@link ConsumptionPath}, present if the converter factory identifier was found in the
 *         {@link DataCellToJavaConverterFactory} and consumer factory identifier was found in the registry.
 * @throws InvalidSettingsException
 * @since 3.6
 */
public static <ExternalType, DestType extends Destination<ExternalType>> Optional<ConsumptionPath> loadConsumptionPath(final ConfigBaseRO config, final ConsumerRegistry<ExternalType, DestType> registry, final String key) throws InvalidSettingsException {
    final Optional<DataCellToJavaConverterFactory<?, ?>> converter = loadDataCellToJavaConverterFactory(config, key + "_converter");
    if (!converter.isPresent()) {
        return Optional.empty();
    }
    final Optional<CellValueConsumerFactory<DestType, ?, ExternalType, ?>> consumer = loadConverterFactory(config, registry, key + "_consumer");
    if (!consumer.isPresent()) {
        return Optional.empty();
    }
    return Optional.of(new ConsumptionPath(converter.get(), consumer.get()));
}
Also used : ConsumptionPath(org.knime.core.data.convert.map.ConsumptionPath) CellValueConsumerFactory(org.knime.core.data.convert.map.CellValueConsumerFactory) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory)

Example 4 with ConsumptionPath

use of org.knime.core.data.convert.map.ConsumptionPath in project knime-core by knime.

the class MappingFrameworkTest method consumptionPathSerializeTest.

/**
 * Test serialization of consumption path
 *
 * @throws InvalidSettingsException
 */
@Test
public void consumptionPathSerializeTest() throws InvalidSettingsException {
    MappingFramework.forDestinationType(H2ODestination.class).register(stringConsumer);
    final ConsumptionPath path = new ConsumptionPath(DataCellToJavaConverterRegistry.getInstance().getConverterFactories(StringCell.TYPE, String.class).stream().findFirst().get(), MappingFramework.forDestinationType(H2ODestination.class).getFactory("java.lang.String->STR").get());
    final Config config = new NodeSettings("Test");
    SerializeUtil.storeConsumptionPath(path, config, "the_path");
    final Optional<ConsumptionPath> loadedPath = SerializeUtil.loadConsumptionPath(config, MappingFramework.forDestinationType(H2ODestination.class), "the_path");
    assertTrue(loadedPath.isPresent());
    assertEquals(path, loadedPath.get());
}
Also used : ConsumptionPath(org.knime.core.data.convert.map.ConsumptionPath) NodeSettings(org.knime.core.node.NodeSettings) Config(org.knime.core.node.config.Config) Test(org.junit.Test)

Aggregations

ConsumptionPath (org.knime.core.data.convert.map.ConsumptionPath)4 Test (org.junit.Test)3 DataCellToJavaConverterFactory (org.knime.core.data.convert.java.DataCellToJavaConverterFactory)2 CellValueConsumerFactory (org.knime.core.data.convert.map.CellValueConsumerFactory)2 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 DataRow (org.knime.core.data.DataRow)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 DataType (org.knime.core.data.DataType)1