Search in sources :

Example 91 with FlowVariable

use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.

the class JSnippetFieldsController method getFieldReadStatement.

/**
 * Get a statement to inserted in the document of a java snippet to read
 * the given flow variable.
 * @param v the flow variable to be read
 * @return the statement
 */
@SuppressWarnings("rawtypes")
public String getFieldReadStatement(final FlowVariable v) {
    FieldsTableModel model = (FieldsTableModel) m_inFieldsTable.getTable().getModel();
    int index = -1;
    for (int r = 0; r < model.getRowCount(); r++) {
        Object value = model.getValueAt(r, Column.COLUMN);
        if (value instanceof FlowVariable) {
            FlowVariable foo = (FlowVariable) value;
            if (foo.getName().equals(v.getName())) {
                index = r;
                break;
            }
        }
    }
    if (index >= 0) {
        // return java field name
        return (String) model.getValueAt(index, Column.JAVA_FIELD);
    } else {
        // try to add a row for the flow variable
        boolean success = m_inFieldsTable.addRow(v);
        if (success) {
            TableModel tableModel = m_inFieldsTable.getTable().getModel();
            m_inFieldsTable.firePropertyChange(InFieldsTable.PROP_FIELD_ADDED, tableModel.getRowCount() - 1, tableModel.getRowCount());
            // return java field name
            return (String) model.getValueAt(model.getRowCount() - 1, Column.JAVA_FIELD);
        } else {
            // return generic code
            String name = v.getName();
            FlowVariable.Type type = v.getType();
            Class javaType = TypeProvider.getDefault().getTypeConverter(type).getPreferredJavaType();
            return "getFlowVariable(\"" + name + "\", " + Type.getIdentifierFor(javaType) + ")";
        }
    }
}
Also used : TableModel(javax.swing.table.TableModel) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 92 with FlowVariable

use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.

the class OutFieldsTable method createAddButtonListener.

/**
 * {@inheritDoc}
 */
@Override
protected ActionListener createAddButtonListener() {
    return new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            DataColumnSpec defaultColTarget = null;
            if (null != m_spec) {
                Set<String> cols = new HashSet<>();
                for (int r = 0; r < m_model.getRowCount(); r++) {
                    Object value = m_model.getValueAt(r, Column.COLUMN);
                    if (value instanceof DataColumnSpec) {
                        cols.add(((DataColumnSpec) value).getName());
                    }
                }
                for (DataColumnSpec colSpec : m_spec) {
                    if (null == defaultColTarget) {
                        defaultColTarget = colSpec;
                    }
                    if (!cols.contains(colSpec.getName())) {
                        // Add a row and fill it
                        boolean rowAdded = addRow(colSpec);
                        if (rowAdded) {
                            firePropertyChange(PROP_FIELD_ADDED, m_model.getRowCount() - 1, m_model.getRowCount());
                        }
                        return;
                    }
                }
            }
            FlowVariable defaultVarTarget = null;
            if (null != m_flowVars) {
                Set<String> flowVars = new HashSet<>();
                for (int r = 0; r < m_model.getRowCount(); r++) {
                    Object value = m_model.getValueAt(r, Column.COLUMN);
                    if (value instanceof FlowVariable) {
                        flowVars.add(((FlowVariable) value).getName());
                    }
                }
                for (FlowVariable flowVar : m_flowVars.values()) {
                    // created.
                    if (FieldsTableUtil.verifyNameOfFlowVariable(flowVar.getName())) {
                        if (null == defaultVarTarget) {
                            defaultVarTarget = flowVar;
                        }
                        if (!flowVars.contains(flowVar.getName())) {
                            // Add a row and fill it
                            boolean rowAdded = addRow(flowVar);
                            if (rowAdded) {
                                firePropertyChange(PROP_FIELD_ADDED, m_model.getRowCount() - 1, m_model.getRowCount());
                            }
                            return;
                        }
                    }
                }
            }
            boolean rowAdded = false;
            if (null != defaultColTarget) {
                rowAdded = addRow(defaultColTarget);
            } else if (null != defaultVarTarget) {
                rowAdded = addRow(defaultVarTarget);
            } else {
                rowAdded = addRow("var", Type.STRING);
            }
            if (rowAdded) {
                firePropertyChange(PROP_FIELD_ADDED, m_model.getRowCount() - 1, m_model.getRowCount());
            }
        }
    };
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) HashSet(java.util.HashSet) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 93 with FlowVariable

use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.

the class OutFieldsTable method getOutVarFields.

/**
 * Get the field definitions representing output flow variables.
 *
 * @return fields representing output flow variables
 */
public OutVarList getOutVarFields() {
    OutVarList outVars = new OutVarList();
    for (int r = 0; r < m_model.getRowCount(); r++) {
        if (!m_model.validateValues(r)) {
            // there are errors in this row
            continue;
        }
        Object fieldTypeValue = getFieldType(r);
        if (null == fieldTypeValue) {
            continue;
        }
        boolean isFlowVar = fieldTypeValue.equals(FieldType.FlowVariable);
        if (isFlowVar) {
            OutVar outVar = new OutVar();
            outVar.setReplaceExisting((Boolean) m_model.getValueAt(r, Column.REPLACE_EXISTING));
            Object colColValue = m_model.getValueAt(r, Column.COLUMN);
            if (colColValue instanceof FlowVariable) {
                FlowVariable flowVar = (FlowVariable) colColValue;
                outVar.setKnimeName(flowVar.getName());
            } else if (colColValue instanceof String) {
                outVar.setKnimeName(colColValue.toString());
            } else {
                continue;
            }
            Object dataTypeValue = m_model.getValueAt(r, Column.DATA_TYPE);
            if (dataTypeValue instanceof Type) {
                Type type = (Type) dataTypeValue;
                outVar.setFlowVarType(type);
            } else {
                continue;
            }
            outVar.setJavaName((String) m_model.getValueAt(r, Column.JAVA_FIELD));
            Object javaTypeObject = m_model.getValueAt(r, Column.JAVA_TYPE);
            if (javaTypeObject instanceof Class) {
                outVar.setJavaType((Class) javaTypeObject);
            } else {
                continue;
            }
            outVars.add(outVar);
        }
    }
    return outVars;
}
Also used : FieldType(org.knime.base.node.jsnippet.util.field.JavaField.FieldType) Type(org.knime.core.node.workflow.FlowVariable.Type) DataType(org.knime.core.data.DataType) OutVarList(org.knime.base.node.jsnippet.util.JavaFieldList.OutVarList) OutVar(org.knime.base.node.jsnippet.util.field.OutVar) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Aggregations

FlowVariable (org.knime.core.node.workflow.FlowVariable)93 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)24 DataColumnSpec (org.knime.core.data.DataColumnSpec)14 DataType (org.knime.core.data.DataType)13 DataTableSpec (org.knime.core.data.DataTableSpec)11 ArrayList (java.util.ArrayList)10 PortType (org.knime.core.node.port.PortType)8 DefaultListModel (javax.swing.DefaultListModel)7 Type (org.knime.core.node.workflow.FlowVariable.Type)7 IOException (java.io.IOException)6 Map (java.util.Map)6 PortObject (org.knime.core.node.port.PortObject)6 Optional (java.util.Optional)5 Collectors (java.util.stream.Collectors)5 OutVar (org.knime.base.node.jsnippet.util.field.OutVar)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)5 URL (java.net.URL)4 ParseException (java.text.ParseException)4 Collection (java.util.Collection)4 HashSet (java.util.HashSet)4