Search in sources :

Example 1 with ProductionPath

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

the class MappingFrameworkTest method productionPaths.

/**
 * Tests {@link ProductionPath#equals} and {@link ProductionPath#toString()}
 */
@Test
public void productionPaths() {
    final JavaToDataCellConverterFactory<Integer> intFactory = JavaToDataCellConverterRegistry.getInstance().getConverterFactories(Integer.class, IntCell.TYPE).stream().findFirst().get();
    final JavaToDataCellConverterFactory<String> stringFactory = JavaToDataCellConverterRegistry.getInstance().getConverterFactories(String.class, StringCell.TYPE).stream().findFirst().get();
    final ProductionPath pathA = new ProductionPath(intProducer, intFactory);
    final ProductionPath pathB = new ProductionPath(intProducer, intFactory);
    final ProductionPath pathC = new ProductionPath(intProducer, stringFactory);
    final ProductionPath pathD = new ProductionPath(null, null);
    final ProductionPath pathE = new ProductionPath(intProducer, null);
    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("INT ---> Integer --(\"Integer\")-> Number (integer)", pathA.toString());
    assertEquals(pathA.hashCode(), pathA.hashCode());
    assertEquals(pathA.hashCode(), pathB.hashCode());
    assertNotEquals(pathA.hashCode(), pathC.hashCode());
    assertEquals(pathA.getProducerFactory(), pathA.m_producerFactory);
    assertEquals(pathA.getConverterFactory(), pathA.m_converterFactory);
}
Also used : ProductionPath(org.knime.core.data.convert.map.ProductionPath) Test(org.junit.Test)

Example 2 with ProductionPath

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

the class MappingFrameworkTest method productionPathSerializeTest.

/**
 * Test serialization of production path
 *
 * @throws InvalidSettingsException
 */
@Test
public void productionPathSerializeTest() throws InvalidSettingsException {
    MappingFramework.forSourceType(H2OSource.class).register(stringProducer);
    final ProductionPath path = new ProductionPath(MappingFramework.forSourceType(H2OSource.class).getFactory("STR->java.lang.String").get(), JavaToDataCellConverterRegistry.getInstance().getConverterFactories(String.class, StringCell.TYPE).stream().findFirst().get());
    final Config config = new NodeSettings("Test");
    SerializeUtil.storeProductionPath(path, config, "the_path");
    final Optional<ProductionPath> loadedPath = SerializeUtil.loadProductionPath(config, MappingFramework.forSourceType(H2OSource.class), "the_path");
    assertTrue(loadedPath.isPresent());
    assertEquals(path, loadedPath.get());
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) Config(org.knime.core.node.config.Config) ProductionPath(org.knime.core.data.convert.map.ProductionPath) Test(org.junit.Test)

Example 3 with ProductionPath

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

the class SerializeUtil method loadProductionPath.

/**
 * Load a {@link ProductionPath} from given config.
 *
 * @param config Config to load from
 * @param registry Registry to load producer with
 * @param key setting key
 * @return an optional {@link ProductionPath}, present if the converter factory identifier was found in the
 *         {@link JavaToDataCellConverterRegistry} and producer factory identifier was found in the registry.
 * @throws InvalidSettingsException
 * @since 3.6
 */
public static <ExternalType, SourceType extends Source<ExternalType>> Optional<ProductionPath> loadProductionPath(final ConfigBaseRO config, final ProducerRegistry<ExternalType, SourceType> registry, final String key) throws InvalidSettingsException {
    final Optional<JavaToDataCellConverterFactory<?>> converter = loadJavaToDataCellConverterFactory(config, key + "_converter");
    if (!converter.isPresent()) {
        return Optional.empty();
    }
    final Optional<CellValueProducerFactory<SourceType, ExternalType, ?, ?>> producer = loadConverterFactory(config, registry, key + "_producer");
    if (!producer.isPresent()) {
        return Optional.empty();
    }
    return Optional.of(new ProductionPath(producer.get(), converter.get()));
}
Also used : CellValueProducerFactory(org.knime.core.data.convert.map.CellValueProducerFactory) ProductionPath(org.knime.core.data.convert.map.ProductionPath) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Example 4 with ProductionPath

use of org.knime.core.data.convert.map.ProductionPath 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)

Example 5 with ProductionPath

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

the class MappingFrameworkTest method availableProductionPathsTest.

/**
 * Test getting all available production paths from {@link ProducerRegistry}
 */
@Test
public void availableProductionPathsTest() {
    final ProducerRegistry<String, H2OSource> reg = MappingFramework.forSourceType(H2OSource.class);
    final JavaToDataCellConverterRegistry convReg = JavaToDataCellConverterRegistry.getInstance();
    assertTrue("Expected to be empty but is: " + reg.getAvailableProductionPaths().stream().map(pp -> Objects.toString(pp, "<null>")).collect(Collectors.joining("\n", "\"", "\"")), reg.getAvailableProductionPaths().isEmpty());
    reg.register(intProducer);
    final List<ProductionPath> paths = reg.getAvailableProductionPaths();
    assertCollectionEquals(new Object[] { new ProductionPath(intProducer, convReg.getConverterFactories(Integer.class, IntCell.TYPE).stream().findFirst().get()), new ProductionPath(intProducer, convReg.getConverterFactories(Integer.class, LongCell.TYPE).stream().findFirst().get()), new ProductionPath(intProducer, convReg.getConverterFactories(Integer.class, DoubleCell.TYPE).stream().findFirst().get()) }, paths);
}
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) JavaToDataCellConverterRegistry(org.knime.core.data.convert.datacell.JavaToDataCellConverterRegistry) ProductionPath(org.knime.core.data.convert.map.ProductionPath) Test(org.junit.Test)

Aggregations

ProductionPath (org.knime.core.data.convert.map.ProductionPath)5 Test (org.junit.Test)4 JavaToDataCellConverterFactory (org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)3 CellValueProducerFactory (org.knime.core.data.convert.map.CellValueProducerFactory)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 List (java.util.List)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 Assert.assertArrayEquals (org.junit.Assert.assertArrayEquals)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertFalse (org.junit.Assert.assertFalse)2 Assert.assertNotEquals (org.junit.Assert.assertNotEquals)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 DataRow (org.knime.core.data.DataRow)2 DataTableSpec (org.knime.core.data.DataTableSpec)2 DataType (org.knime.core.data.DataType)2