Search in sources :

Example 6 with JavaToDataCellConverterFactory

use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.

the class SerializeUtilTest method testStoreAndLoadJavaToDataCell.

@Test
public void testStoreAndLoadJavaToDataCell() throws InvalidSettingsException {
    Optional<JavaToDataCellConverterFactory<Integer>> factory = JavaToDataCellConverterRegistry.getInstance().getConverterFactories(Integer.class, IntCell.TYPE).stream().findFirst();
    assumeTrue(factory.isPresent());
    final NodeSettings testSettings = new NodeSettings(getClass().getName());
    SerializeUtil.storeConverterFactory(factory.get(), testSettings, "the-factory2");
    final Optional<JavaToDataCellConverterFactory<?>> loadedFactory = SerializeUtil.loadJavaToDataCellConverterFactory(testSettings, "the-factory2");
    assertTrue(loadedFactory.isPresent());
    assertEquals(factory.get(), loadedFactory.get());
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory) Test(org.junit.Test)

Example 7 with JavaToDataCellConverterFactory

use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.

the class JavaToDataCellConversionTest method testCollectionTypes.

/**
 * Test Integer[] -> ListCell(IntCell) conversion.
 *
 * @throws Exception When something went wrong
 */
@Test
public void testCollectionTypes() throws Exception {
    final Integer[] coll = { 0, 1, 4, 9, 16, 25, null };
    final Optional<JavaToDataCellConverterFactory<Integer[]>> factory = JavaToDataCellConverterRegistry.getInstance().getConverterFactories(Integer[].class, ListCell.getCollectionType(IntCell.TYPE)).stream().findFirst();
    assertTrue(factory.isPresent());
    final JavaToDataCellConverter<Integer[]> converter = factory.get().create(null);
    assertNotNull(converter);
    final DataCell cell = converter.convert(coll);
    assertTrue(cell instanceof ListCell);
    final ListCell listCell = (ListCell) converter.convert(coll);
    for (int i = 0; i < 6; ++i) {
        assertEquals(i * i, ((IntCell) listCell.get(i)).getIntValue());
    }
    assertTrue(listCell.get(6).isMissing());
}
Also used : ListCell(org.knime.core.data.collection.ListCell) DataCell(org.knime.core.data.DataCell) BinaryObjectDataCell(org.knime.core.data.blob.BinaryObjectDataCell) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory) Test(org.junit.Test)

Example 8 with JavaToDataCellConverterFactory

use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.

the class JavaSnippet method getClassLoadersFor.

/**
 * Get the class loaders required for a specific converter factory
 *
 * @param converterFactoryId ID of the converter factory
 * @return A list of class loaders required for given converter factory
 * @noreference This method is not intended to be referenced by clients.
 */
private static Collection<ClassLoader> getClassLoadersFor(final String converterFactoryId) {
    final Optional<DataCellToJavaConverterFactory<?, ?>> factory = ConverterUtil.getDataCellToJavaConverterFactory(converterFactoryId);
    if (factory.isPresent()) {
        final ArrayList<ClassLoader> clsLoaders = new ArrayList<>(2);
        final ClassLoader sourceCL = factory.get().getSourceType().getClassLoader();
        if (sourceCL != null) {
            clsLoaders.add(sourceCL);
        }
        final ClassLoader destCL = factory.get().getDestinationType().getClassLoader();
        if (destCL != null) {
            clsLoaders.add(destCL);
        }
        return clsLoaders;
    } else {
        final Optional<JavaToDataCellConverterFactory<?>> factory2 = ConverterUtil.getJavaToDataCellConverterFactory(converterFactoryId);
        if (factory2.isPresent()) {
            final ClassLoader cl = factory2.get().getSourceType().getClassLoader();
            if (cl != null) {
                return Collections.singleton(cl);
            }
        }
        return Collections.emptyList();
    }
}
Also used : ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) MultiParentClassLoader(org.knime.core.data.convert.util.MultiParentClassLoader) ModuleClassLoader(org.eclipse.osgi.internal.loader.ModuleClassLoader) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Example 9 with JavaToDataCellConverterFactory

use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.

the class JavaSnippetCellFactory method getCells.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public DataCell[] getCells(final DataRow row) {
    try {
        if (null == m_jsnippet) {
            m_jsnippet = m_snippet.createSnippetInstance();
            // populate the fields in the m_jsnippet that are constant
            // across the rows.
            Field[] fs = m_jsnippet.getClass().getSuperclass().getDeclaredFields();
            for (Field field : fs) {
                if (field.getName().equals("m_flowVars")) {
                    field.setAccessible(true);
                    field.set(m_jsnippet, m_flowVars);
                }
                if (field.getName().equals(JavaSnippet.ROWCOUNT)) {
                    field.setAccessible(true);
                    field.set(m_jsnippet, m_rowCount);
                }
            }
        }
        // populate data structure with the input cells
        Map<String, Cell> cellsMap = createCellsMap(row);
        if (null == m_columns) {
            m_columns = new ArrayList<>();
            m_columns.addAll(cellsMap.keySet());
        }
        Field[] fs = m_jsnippet.getClass().getSuperclass().getDeclaredFields();
        for (Field field : fs) {
            if (field.getName().equals("m_cellsMap")) {
                field.setAccessible(true);
                field.set(m_jsnippet, cellsMap);
            }
            if (field.getName().equals("m_cells")) {
                field.setAccessible(true);
                List<Cell> cells = new ArrayList<>();
                cells.addAll(cellsMap.values());
                field.set(m_jsnippet, cells);
            }
            if (field.getName().equals("m_columns")) {
                field.setAccessible(true);
                field.set(m_jsnippet, m_columns);
            }
            if (field.getName().equals("m_inSpec")) {
                field.setAccessible(true);
                field.set(m_jsnippet, m_spec);
            }
            if (field.getName().equals(JavaSnippet.ROWID)) {
                field.setAccessible(true);
                field.set(m_jsnippet, row.getKey().getString());
            }
            if (field.getName().equals(JavaSnippet.ROWINDEX)) {
                field.setAccessible(true);
                field.set(m_jsnippet, m_rowIndex);
            }
        }
        // populate the system input column fields with data
        for (InCol inCol : m_snippet.getSystemFields().getInColFields()) {
            Field field = m_jsnippet.getClass().getField(inCol.getJavaName());
            final DataCell cell = row.getCell(m_spec.findColumnIndex(inCol.getKnimeName()));
            if (cell.isMissing()) {
                field.set(m_jsnippet, null);
                continue;
            }
            // Get the converter factory for this column
            final Optional<DataCellToJavaConverterFactory<?, ?>> factory = ConverterUtil.getDataCellToJavaConverterFactory(inCol.getConverterFactoryId());
            if (!factory.isPresent()) {
                throw new RuntimeException("Missing converter factory with ID: " + inCol.getConverterFactoryId());
            }
            final Object converted = factory.get().create().convertUnsafe(cell);
            field.set(m_jsnippet, converted);
        }
        // reset the system output fields to null (see also bug 3781)
        for (OutCol outCol : m_snippet.getSystemFields().getOutColFields()) {
            Field field = m_jsnippet.getClass().getField(outCol.getJavaName());
            field.set(m_jsnippet, null);
        }
        // populate the system input flow variable fields with data
        for (InVar inCol : m_snippet.getSystemFields().getInVarFields()) {
            Field field = m_jsnippet.getClass().getField(inCol.getJavaName());
            Object v = m_flowVars.getValueOfType(inCol.getKnimeName(), inCol.getJavaType());
            field.set(m_jsnippet, v);
        }
    } catch (Exception e) {
        // re-throw exception
        throw new RuntimeException(e);
    }
    try {
        // evaluate user script
        m_jsnippet.snippet();
    } catch (Throwable thr) {
        if (thr instanceof Abort) {
            StringBuilder builder = new StringBuilder("Calculation aborted: ");
            String message = thr.getMessage();
            builder.append(message == null ? "<no details>" : message);
            throw new RuntimeException(builder.toString(), thr);
        } else {
            Integer lineNumber = null;
            for (StackTraceElement ste : thr.getStackTrace()) {
                if (ste.getClassName().equals("JSnippet")) {
                    lineNumber = ste.getLineNumber();
                }
            }
            StringBuilder msg = new StringBuilder();
            msg.append("Evaluation of java snippet failed for row \"");
            msg.append(row.getKey());
            msg.append("\". ");
            if (lineNumber != null) {
                msg.append("The exception is caused by line ");
                msg.append(lineNumber);
                msg.append(" of the snippet. ");
            }
            if (thr.getMessage() != null) {
                msg.append("Exception message:");
                msg.append(thr.getMessage());
            }
            LOGGER.warn(msg.toString(), thr);
            OutVarList outVars = m_snippet.getSystemFields().getOutVarFields();
            if (outVars.size() > 0) {
                // Abort if flow variables are defined
                throw new RuntimeException("An error occured in an " + "expression with output flow variables.", thr);
            }
            OutColList outFields = m_snippet.getSystemFields().getOutColFields();
            DataCell[] out = new DataCell[outFields.size()];
            for (int i = 0; i < out.length; i++) {
                // Return missing values for output fields
                out[i] = DataType.getMissingCell();
            }
            m_rowIndex++;
            return out;
        }
    }
    try {
        // update m_flowVars with output flow variable fields.
        for (OutVar var : m_snippet.getSystemFields().getOutVarFields()) {
            Field field = m_jsnippet.getClass().getField(var.getJavaName());
            Object value = field.get(m_jsnippet);
            if (null != value) {
                Type type = var.getFlowVarType();
                FlowVariable flowVar = null;
                if (type.equals(Type.INTEGER)) {
                    flowVar = new FlowVariable(var.getKnimeName(), (Integer) value);
                } else if (type.equals(Type.DOUBLE)) {
                    flowVar = new FlowVariable(var.getKnimeName(), (Double) value);
                } else {
                    // case type.equals(Type.String)
                    flowVar = new FlowVariable(var.getKnimeName(), (String) value);
                }
                m_flowVars.put(flowVar);
            } else {
                throw new RuntimeException("Flow variable \"" + var.getKnimeName() + "\" has no value.");
            }
        }
        // get output column fields
        OutColList outFields = m_snippet.getSystemFields().getOutColFields();
        DataCell[] out = new DataCell[outFields.size()];
        for (int i = 0; i < out.length; i++) {
            OutCol outField = outFields.get(i);
            Field field = m_jsnippet.getClass().getField(outField.getJavaName());
            Object value = field.get(m_jsnippet);
            if (null == value) {
                out[i] = DataType.getMissingCell();
            } else {
                final String id = outField.getConverterFactoryId();
                Optional<JavaToDataCellConverterFactory<?>> factory = ConverterUtil.getJavaToDataCellConverterFactory(id);
                if (!factory.isPresent()) {
                    throw new RuntimeException("Missing converter factory with ID: " + id);
                }
                out[i] = ((JavaToDataCellConverterFactory<Object>) factory.get()).create(m_context).convert(value);
            }
        }
        // Cleanup Closeable inputs
        for (final OutCol outCol : m_snippet.getSystemFields().getOutColFields()) {
            final Field field = m_jsnippet.getClass().getField(outCol.getJavaName());
            final Object value = field.get(m_jsnippet);
            if (value instanceof Closeable) {
                ((Closeable) value).close();
            }
            if (value instanceof AutoCloseable) {
                // From the doc: Calling close more than once *can* have visible side effects!
                ((AutoCloseable) value).close();
            }
        }
        m_rowIndex++;
        return out;
    } catch (Exception e) {
        // but in case re-throw exception
        throw new RuntimeException(e);
    }
}
Also used : OutCol(org.knime.base.node.jsnippet.util.field.OutCol) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) OutColList(org.knime.base.node.jsnippet.util.JavaFieldList.OutColList) InCol(org.knime.base.node.jsnippet.util.field.InCol) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) Field(java.lang.reflect.Field) Abort(org.knime.base.node.jsnippet.expression.Abort) OutVarList(org.knime.base.node.jsnippet.util.JavaFieldList.OutVarList) InVar(org.knime.base.node.jsnippet.util.field.InVar) Cell(org.knime.base.node.jsnippet.expression.Cell) DataCell(org.knime.core.data.DataCell) OutVar(org.knime.base.node.jsnippet.util.field.OutVar) TypeException(org.knime.base.node.jsnippet.expression.TypeException) Type(org.knime.core.node.workflow.FlowVariable.Type) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) FlowVariable(org.knime.core.node.workflow.FlowVariable) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Example 10 with JavaToDataCellConverterFactory

use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.

the class JavaToDataCellConversionTest method testSimpleConversion.

/**
 * Generic test for simple {@link JavaToDataCellConverterFactory}s.
 *
 * @param sourceType type of the input to convert
 * @param dataType {@link DataType} of the {@link DataCell} to convert to.
 * @param destType type of the expected output {@link DataCell} subtype.
 * @param sourceValue a value to convert.
 * @return <code>sourceValue</code> converted to <code>destType</code>.
 * @throws Exception
 */
protected <S, D> D testSimpleConversion(final Class<S> sourceType, final DataType dataType, final Class<D> destType, final S sourceValue) throws Exception {
    final Optional<JavaToDataCellConverterFactory<S>> factory = JavaToDataCellConverterRegistry.getInstance().getConverterFactories(sourceType, dataType).stream().findFirst();
    assertTrue(factory.isPresent());
    final JavaToDataCellConverter<S> converter = factory.get().create(null);
    assertNotNull(converter);
    final DataCell converted = converter.convert(sourceValue);
    assertTrue(destType.isInstance(converted));
    // checked in the above assert.
    @SuppressWarnings("unchecked") D d = (D) converted;
    return d;
}
Also used : DataCell(org.knime.core.data.DataCell) BinaryObjectDataCell(org.knime.core.data.blob.BinaryObjectDataCell) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Aggregations

JavaToDataCellConverterFactory (org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)13 DataType (org.knime.core.data.DataType)6 FieldType (org.knime.base.node.jsnippet.util.field.JavaField.FieldType)4 DataCellToJavaConverterFactory (org.knime.core.data.convert.java.DataCellToJavaConverterFactory)4 FlowVariable (org.knime.core.node.workflow.FlowVariable)4 Optional (java.util.Optional)3 Test (org.junit.Test)3 OutCol (org.knime.base.node.jsnippet.util.field.OutCol)3 DataCell (org.knime.core.data.DataCell)3 DataColumnSpec (org.knime.core.data.DataColumnSpec)3 Type (org.knime.core.node.workflow.FlowVariable.Type)3 Component (java.awt.Component)2 ArrayList (java.util.ArrayList)2 TypeProvider (org.knime.base.node.jsnippet.type.TypeProvider)2 TypeConverter (org.knime.base.node.jsnippet.type.flowvar.TypeConverter)2 OutColList (org.knime.base.node.jsnippet.util.JavaFieldList.OutColList)2 OutVar (org.knime.base.node.jsnippet.util.field.OutVar)2 BinaryObjectDataCell (org.knime.core.data.blob.BinaryObjectDataCell)2 ListCell (org.knime.core.data.collection.ListCell)2 ArrayToCollectionConverterFactory (org.knime.core.data.convert.datacell.ArrayToCollectionConverterFactory)2