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