Search in sources :

Example 1 with ColumnReference

use of org.knime.base.node.rules.Rule.ColumnReference in project knime-core by knime.

the class RuleEngineNodeModel method createRearranger.

private ColumnRearranger createRearranger(final DataTableSpec inSpec, final List<Rule> rules) throws InvalidSettingsException {
    ColumnRearranger crea = new ColumnRearranger(inSpec);
    String newColName = DataTableSpec.getUniqueColumnName(inSpec, m_settings.getNewColName());
    final int defaultLabelColumnIndex;
    if (m_settings.getDefaultLabelIsColumn()) {
        if (m_settings.getDefaultLabel().length() < 3) {
            throw new InvalidSettingsException("Default label is not a column reference");
        }
        if (!m_settings.getDefaultLabel().startsWith("$") || !m_settings.getDefaultLabel().endsWith("$")) {
            throw new InvalidSettingsException("Column references in default label must be enclosed in $");
        }
        String colRef = m_settings.getDefaultLabel().substring(1, m_settings.getDefaultLabel().length() - 1);
        defaultLabelColumnIndex = inSpec.findColumnIndex(colRef);
        if (defaultLabelColumnIndex == -1) {
            throw new InvalidSettingsException("Column '" + m_settings.getDefaultLabel() + "' for default label does not exist in input table");
        }
    } else {
        defaultLabelColumnIndex = -1;
    }
    // determine output type
    List<DataType> types = new ArrayList<DataType>();
    // add outcome column types
    for (Rule r : rules) {
        if (r.getOutcome() instanceof ColumnReference) {
            types.add(((ColumnReference) r.getOutcome()).spec.getType());
        } else if (r.getOutcome() instanceof Double) {
            types.add(DoubleCell.TYPE);
        } else if (r.getOutcome() instanceof Integer) {
            types.add(IntCell.TYPE);
        } else if (r.getOutcome().toString().length() > 0) {
            types.add(StringCell.TYPE);
        }
    }
    if (defaultLabelColumnIndex >= 0) {
        types.add(inSpec.getColumnSpec(defaultLabelColumnIndex).getType());
    } else if (m_settings.getDefaultLabel().length() > 0) {
        try {
            Integer.parseInt(m_settings.getDefaultLabel());
            types.add(IntCell.TYPE);
        } catch (NumberFormatException ex) {
            try {
                Double.parseDouble(m_settings.getDefaultLabel());
                types.add(DoubleCell.TYPE);
            } catch (NumberFormatException ex1) {
                types.add(StringCell.TYPE);
            }
        }
    }
    final DataType outType;
    if (types.size() > 0) {
        DataType temp = types.get(0);
        for (int i = 1; i < types.size(); i++) {
            temp = DataType.getCommonSuperType(temp, types.get(i));
        }
        if ((temp.getValueClasses().size() == 1) && temp.getValueClasses().contains(DataValue.class)) {
            // a non-native type, we replace it with string
            temp = StringCell.TYPE;
        }
        outType = temp;
    } else {
        outType = StringCell.TYPE;
    }
    DataColumnSpec cs = new DataColumnSpecCreator(newColName, outType).createSpec();
    crea.append(new SingleCellFactory(cs) {

        @Override
        public DataCell getCell(final DataRow row) {
            for (Rule r : rules) {
                if (r.matches(row)) {
                    Object outcome = r.getOutcome();
                    if (outcome instanceof ColumnReference) {
                        DataCell cell = row.getCell(((ColumnReference) outcome).index);
                        if (outType.equals(StringCell.TYPE) && !cell.isMissing() && !cell.getType().equals(StringCell.TYPE)) {
                            return new StringCell(cell.toString());
                        } else {
                            return cell;
                        }
                    } else if (outType.equals(IntCell.TYPE)) {
                        return new IntCell((Integer) outcome);
                    } else if (outType.equals(DoubleCell.TYPE)) {
                        return new DoubleCell((Double) outcome);
                    } else {
                        return new StringCell(outcome.toString());
                    }
                }
            }
            if (defaultLabelColumnIndex >= 0) {
                DataCell cell = row.getCell(defaultLabelColumnIndex);
                if (outType.equals(StringCell.TYPE) && !cell.getType().equals(StringCell.TYPE)) {
                    return new StringCell(cell.toString());
                } else {
                    return cell;
                }
            } else if (m_settings.getDefaultLabel().length() > 0) {
                String l = m_settings.getDefaultLabel();
                if (outType.equals(StringCell.TYPE)) {
                    return new StringCell(l);
                }
                try {
                    int i = Integer.parseInt(l);
                    return new IntCell(i);
                } catch (NumberFormatException ex) {
                    try {
                        double d = Double.parseDouble(l);
                        return new DoubleCell(d);
                    } catch (NumberFormatException ex1) {
                        return new StringCell(l);
                    }
                }
            } else {
                return DataType.getMissingCell();
            }
        }
    });
    return crea;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataValue(org.knime.core.data.DataValue) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) ColumnReference(org.knime.base.node.rules.Rule.ColumnReference)

Example 2 with ColumnReference

use of org.knime.base.node.rules.Rule.ColumnReference in project knime-core by knime.

the class RuleEngineNodeDialog method createButtonPart.

/*
     * Button part (from top to bottom): - move box: up btn, down btn) - edit
     * box: edit button, remove button
     */
private Box createButtonPart() {
    /*
         * Button "Up"
         */
    JButton up = new JButton("Up");
    up.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent arg0) {
            int pos = m_rules.getSelectedIndex();
            if (pos > 0) {
                Rule r = (Rule) m_rules.getSelectedValue();
                m_ruleModel.remove(pos);
                m_ruleModel.insertElementAt(r, pos - 1);
                m_rules.setSelectedIndex(pos - 1);
            }
        }
    });
    /*
         * Button "Down"
         */
    JButton down = new JButton("Down");
    down.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent arg0) {
            int pos = m_rules.getSelectedIndex();
            if (pos != -1 && pos < m_ruleModel.getSize() - 1) {
                Rule r = (Rule) m_rules.getSelectedValue();
                m_ruleModel.remove(pos);
                m_ruleModel.insertElementAt(r, pos + 1);
                m_rules.setSelectedIndex(pos + 1);
            }
        }
    });
    /*
         * Move buttons
         */
    Box moveBtns = Box.createVerticalBox();
    moveBtns.setBorder(BorderFactory.createTitledBorder("Move:"));
    moveBtns.add(up);
    moveBtns.add(Box.createVerticalStrut(10));
    moveBtns.add(down);
    moveBtns.add(Box.createVerticalGlue());
    moveBtns.setMaximumSize(moveBtns.getPreferredSize());
    /*
         * Button "Edit"
         */
    JButton edit = new JButton("Edit");
    edit.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent arg0) {
            if ((m_ruleEditor.getText() != RULE_LABEL) && (m_ruleEditor.getText().trim().length() > 0)) {
                if (JOptionPane.showConfirmDialog(getPanel(), "Override currently edited rule?", "Confirm...", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
                    return;
                }
            }
            int rPos = m_rules.getSelectedIndex();
            Rule r = (Rule) m_ruleModel.get(rPos);
            m_ruleEditor.setText(r.getCondition());
            Object outcome = r.getOutcome();
            if (outcome instanceof ColumnReference) {
                m_ruleLabelEditor.setText(r.getOutcome().toString());
                m_outcomeIsColumn.setSelected(true);
            } else if (outcome instanceof Number) {
                m_ruleLabelEditor.setText(outcome.toString());
                m_outcomeIsColumn.setSelected(false);
            } else {
                String s = r.getOutcome().toString();
                s = s.replaceAll("\"", "");
                m_ruleLabelEditor.setText(s);
                m_outcomeIsColumn.setSelected(false);
            }
            m_ruleModel.removeElement(r);
        }
    });
    /*
         * Button "Remove"
         */
    JButton remove = new JButton("Remove");
    remove.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            int pos = m_rules.getSelectedIndex();
            if (pos >= 0 && JOptionPane.showConfirmDialog(getPanel(), "Remove selected rule?", "Confirm...", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
                m_ruleModel.remove(pos);
            }
        }
    });
    // make all buttons the same width
    remove.setMaximumSize(remove.getPreferredSize());
    up.setMinimumSize(remove.getPreferredSize());
    down.setMinimumSize(remove.getPreferredSize());
    edit.setMinimumSize(remove.getPreferredSize());
    /*
         * Edit buttons
         */
    Box editBtns = Box.createVerticalBox();
    editBtns.setBorder(BorderFactory.createTitledBorder("Edit/Remove"));
    editBtns.add(edit);
    editBtns.add(Box.createVerticalStrut(20));
    editBtns.add(remove);
    editBtns.add(Box.createVerticalGlue());
    editBtns.setMaximumSize(editBtns.getPreferredSize());
    Box bottomRight = Box.createVerticalBox();
    bottomRight.add(moveBtns);
    bottomRight.add(editBtns);
    bottomRight.add(Box.createVerticalGlue());
    return bottomRight;
}
Also used : ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) Box(javax.swing.Box) JCheckBox(javax.swing.JCheckBox) ColumnReference(org.knime.base.node.rules.Rule.ColumnReference)

Aggregations

ColumnReference (org.knime.base.node.rules.Rule.ColumnReference)2 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ArrayList (java.util.ArrayList)1 Box (javax.swing.Box)1 JButton (javax.swing.JButton)1 JCheckBox (javax.swing.JCheckBox)1 DataCell (org.knime.core.data.DataCell)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)1 DataRow (org.knime.core.data.DataRow)1 DataType (org.knime.core.data.DataType)1 DataValue (org.knime.core.data.DataValue)1 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)1 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)1 DoubleCell (org.knime.core.data.def.DoubleCell)1 IntCell (org.knime.core.data.def.IntCell)1 StringCell (org.knime.core.data.def.StringCell)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1