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);
}
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());
}
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()));
}
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());
}
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);
}
Aggregations