Search in sources :

Example 61 with DataType

use of org.knime.core.data.DataType in project knime-core by knime.

the class JavaToDataCellConverterRegistry method register.

/**
 * Register a DataCellToJavaConverterFactory.
 *
 * @param factory The factory to register
 */
public synchronized void register(final JavaToDataCellConverterFactory<?> factory) {
    if (factory == null) {
        throw new IllegalArgumentException("factory must not be null");
    }
    final ConversionKey key = new ConversionKey(factory);
    ArrayList<JavaToDataCellConverterFactory<?>> list = m_converterFactories.get(key);
    if (list == null) {
        list = new ArrayList<>();
        m_converterFactories.put(key, list);
    }
    list.add(factory);
    final DataType destType = factory.getDestinationType();
    Set<JavaToDataCellConverterFactory<?>> byDestinationType = m_byDestinationType.get(destType);
    if (byDestinationType == null) {
        byDestinationType = new LinkedHashSet<>();
        m_byDestinationType.put(destType, byDestinationType);
    }
    byDestinationType.add(factory);
    final Class<?> sourceType = factory.getSourceType();
    Set<JavaToDataCellConverterFactory<?>> bySourceType = m_bySourceType.get(sourceType);
    if (bySourceType == null) {
        bySourceType = new LinkedHashSet<>();
        m_bySourceType.put(sourceType, bySourceType);
        bySourceType.add(factory);
    } else {
        bySourceType.add(factory);
    }
    final JavaToDataCellConverterFactory<?> previous = m_byIdentifier.put(factory.getIdentifier(), factory);
    if (previous != null) {
        LOGGER.coding("JavaToDataCellConverterFactory identifier is not unique (" + factory.getIdentifier() + ")");
    }
}
Also used : ConversionKey(org.knime.core.data.convert.ConversionKey) DataType(org.knime.core.data.DataType)

Example 62 with DataType

use of org.knime.core.data.DataType in project knime-core by knime.

the class JavaToDataCellConverterRegistry method parseAnnotations.

private void parseAnnotations() {
    final Collection<DataType> availableDataTypes = DataTypeRegistry.getInstance().availableDataTypes();
    for (final DataType dataType : availableDataTypes) {
        final Optional<DataCellFactory> cellFactory = dataType.getCellFactory(null);
        if (cellFactory.isPresent()) {
            final Class<? extends DataCellFactory> cellFactoryClass = cellFactory.get().getClass();
            for (final Pair<Method, DataCellFactoryMethod> pair : ClassUtil.getMethodsWithAnnotation(cellFactoryClass, DataCellFactoryMethod.class)) {
                final Method method = pair.getFirst();
                try {
                    final JavaToDataCellConverterFactory<?> factory = new FactoryMethodToDataCellConverterFactory<>(cellFactoryClass, method, ClassUtil.ensureObjectType(method.getParameterTypes()[0]), dataType, pair.getSecond().name());
                    // Check name of factory
                    if (!validateFactoryName(factory)) {
                        LOGGER.warn("JavaToDataCellFactory name \"" + factory.getName() + "\" of factory with id \"" + factory.getIdentifier() + "\" does not follow naming convention (see DataCellFactoryMethod#name()).");
                        LOGGER.warn("Factory will not be registered.");
                        continue;
                    }
                    register(factory);
                    LOGGER.debug("Registered JavaToDataCellConverterFactory from DataCellFactoryMethod annotation for: " + method.getParameterTypes()[0].getName() + " to " + dataType.getName());
                } catch (IncompleteAnnotationException e) {
                    LOGGER.coding("Incomplete DataCellFactoryMethod annotation for " + cellFactoryClass.getName() + "." + method.getName() + ". Will not register.", e);
                } catch (NoSuchMethodException | SecurityException ex) {
                    LOGGER.error("Could not access default constructor or constructor with parameter 'ExecutionContext' of " + "class '" + cellFactoryClass.getName() + "'. Converter will not be registered.", ex);
                }
            }
        }
    }
}
Also used : DataCellFactoryMethod(org.knime.core.data.convert.DataCellFactoryMethod) DataCellFactory(org.knime.core.data.DataCellFactory) DataCellFactoryMethod(org.knime.core.data.convert.DataCellFactoryMethod) Method(java.lang.reflect.Method) DataType(org.knime.core.data.DataType) IncompleteAnnotationException(java.lang.annotation.IncompleteAnnotationException)

Example 63 with DataType

use of org.knime.core.data.DataType in project knime-core by knime.

the class DataCellToJavaConverterRegistry method parseAnnotations.

/*
     * Parse @DataCellFactoryMethod and @DataValueAccessMethod annotations
     */
private void parseAnnotations() {
    final Collection<DataType> availableDataTypes = DataTypeRegistry.getInstance().availableDataTypes();
    final Set<Class<? extends DataValue>> processedValueClasses = new HashSet<>();
    for (final DataType dataType : availableDataTypes) {
        for (final Class<? extends DataValue> valueClass : dataType.getValueClasses()) {
            if (processedValueClasses.contains(valueClass)) {
                // already parsed this value class
                continue;
            }
            // get methods annotated with DataValueAccessMethod
            final Collection<Pair<Method, DataValueAccessMethod>> methodsWithAnnotation = ClassUtil.getMethodsWithAnnotation(valueClass, DataValueAccessMethod.class);
            // register a converter for every DataValueAccessMethod annotation
            for (final Pair<Method, DataValueAccessMethod> pair : methodsWithAnnotation) {
                parseAnnotation(valueClass, pair.getFirst(), pair.getSecond());
            }
            processedValueClasses.add(valueClass);
        }
    }
}
Also used : CollectionDataValue(org.knime.core.data.collection.CollectionDataValue) DataValue(org.knime.core.data.DataValue) DataValueAccessMethod(org.knime.core.data.convert.DataValueAccessMethod) DataType(org.knime.core.data.DataType) Method(java.lang.reflect.Method) DataValueAccessMethod(org.knime.core.data.convert.DataValueAccessMethod) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Pair(org.knime.core.util.Pair)

Example 64 with DataType

use of org.knime.core.data.DataType in project knime-core by knime.

the class BlobSupportDataCellList method init.

private void init(final Collection<? extends DataCell> coll) {
    ArrayList<DataCell> cellList = new ArrayList<DataCell>(coll.size());
    DataType commonType = null;
    for (DataCell c : coll) {
        if (c == null) {
            throw new NullPointerException("List element must not be null");
        }
        DataType cellType;
        if (c instanceof BlobWrapperDataCell) {
            m_containsBlobWrapperCells = true;
            cellList.add(c);
            cellType = DataType.getType(((BlobWrapperDataCell) c).getBlobClass());
        } else if (c instanceof BlobDataCell) {
            m_containsBlobWrapperCells = true;
            cellList.add(new BlobWrapperDataCell((BlobDataCell) c));
            cellType = c.getType();
        } else {
            cellList.add(c);
            cellType = c.getType();
        }
        if (!c.isMissing()) {
            if (commonType == null) {
                commonType = cellType;
            } else {
                commonType = DataType.getCommonSuperType(commonType, cellType);
            }
        }
    }
    if (commonType == null) {
        m_elementType = DataType.getMissingCell().getType();
    } else {
        m_elementType = commonType;
    }
    m_cellList = cellList;
}
Also used : BlobDataCell(org.knime.core.data.container.BlobDataCell) BlobWrapperDataCell(org.knime.core.data.container.BlobWrapperDataCell) ArrayList(java.util.ArrayList) BlobWrapperDataCell(org.knime.core.data.container.BlobWrapperDataCell) BlobDataCell(org.knime.core.data.container.BlobDataCell) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType)

Example 65 with DataType

use of org.knime.core.data.DataType in project knime-core by knime.

the class ColumnRearranger method ensureColumnIsConverted.

/**
 * @param converter
 * @param index
 * @since 2.7
 */
public final void ensureColumnIsConverted(final DataCellTypeConverter converter, final int index) {
    SpecAndFactoryObject current = m_includes.get(index);
    DataType converterOutputType = converter.getOutputType();
    DataColumnSpec colSpec = current.getColSpec();
    if (converterOutputType.equals(colSpec.getType())) {
        LOGGER.debug("Converting column \"" + colSpec.getName() + "\" not required.");
    } else {
        LOGGER.debug("Converting column \"" + colSpec.getName() + "\" required.");
        DataColumnSpecCreator c = new DataColumnSpecCreator(colSpec);
        c.setType(converterOutputType);
        DataCellTypeConverterCellFactory cellConverter = new DataCellTypeConverterCellFactory(c.createSpec(), converter, index);
        replace(cellConverter, index);
    }
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataType(org.knime.core.data.DataType)

Aggregations

DataType (org.knime.core.data.DataType)330 DataColumnSpec (org.knime.core.data.DataColumnSpec)142 DataTableSpec (org.knime.core.data.DataTableSpec)101 DataCell (org.knime.core.data.DataCell)96 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)95 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)71 DoubleValue (org.knime.core.data.DoubleValue)67 DataRow (org.knime.core.data.DataRow)61 ArrayList (java.util.ArrayList)55 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)34 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)32 DefaultRow (org.knime.core.data.def.DefaultRow)24 HashSet (java.util.HashSet)23 HashMap (java.util.HashMap)20 StringCell (org.knime.core.data.def.StringCell)20 NominalValue (org.knime.core.data.NominalValue)18 DoubleCell (org.knime.core.data.def.DoubleCell)18 IntCell (org.knime.core.data.def.IntCell)18 BitVectorValue (org.knime.core.data.vector.bitvector.BitVectorValue)18 ByteVectorValue (org.knime.core.data.vector.bytevector.ByteVectorValue)18