Search in sources :

Example 26 with InvalidSettingsException

use of org.knime.core.node.InvalidSettingsException in project knime-core by knime.

the class RuleEngineNodeModel method createRearranger.

private ColumnRearranger createRearranger(final DataTableSpec inSpec, final List<Rule> rules, final long rowCount, final boolean updateColSpec) throws InvalidSettingsException {
    if (m_settings.isAppendColumn() && m_settings.getNewColName().isEmpty()) {
        throw new InvalidSettingsException("No name for prediction column provided");
    }
    ColumnRearranger crea = new ColumnRearranger(inSpec);
    String newColName = m_settings.isAppendColumn() ? DataTableSpec.getUniqueColumnName(inSpec, m_settings.getNewColName()) : m_settings.getReplaceColumn();
    final DataType outType = computeOutputType(rules, RuleNodeSettings.RuleEngine, m_settings.isDisallowLongOutputForCompatibility());
    DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(newColName, outType);
    if (updateColSpec) {
        // only update in configure, execute will compute properly
        updateColSpec(rules, outType, colSpecCreator, this);
    }
    DataColumnSpec cs = colSpecCreator.createSpec();
    final boolean disallowLongOutputForCompatibility = m_settings.isDisallowLongOutputForCompatibility();
    VariableProvider.SingleCellFactoryProto cellFactory = new VariableProvider.SingleCellFactoryProto(cs) {

        private long m_rowIndex = -1L;

        @Override
        public DataCell getCell(final DataRow row) {
            m_rowIndex++;
            return getRulesOutcome(outType, row, rules, disallowLongOutputForCompatibility, this);
        }

        @Override
        public Object readVariable(final String name, final Class<?> type) {
            return RuleEngineNodeModel.this.readVariable(name, type);
        }

        @Deprecated
        @Override
        public int getRowIndex() {
            return (int) m_rowIndex;
        }

        @Override
        public long getRowIndexLong() {
            return m_rowIndex;
        }

        @Deprecated
        @Override
        public int getRowCount() {
            return (int) rowCount;
        }

        @Override
        public long getRowCountLong() {
            return rowCount;
        }
    };
    if (m_settings.isAppendColumn()) {
        crea.append(cellFactory);
    } else {
        crea.replace(cellFactory, m_settings.getReplaceColumn());
    }
    return crea;
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) FlowVariableProvider(org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider) DataType(org.knime.core.data.DataType) DataRow(org.knime.core.data.DataRow)

Example 27 with InvalidSettingsException

use of org.knime.core.node.InvalidSettingsException in project knime-core by knime.

the class RulePanel method loadSettingsFrom.

/**
 * Loads the settings from the serialized model ({@code settings}).
 *
 * @param settings The container for the model.
 * @param specs The current {@link DataTableSpec}s.
 * @param availableFlowVariables The {@link FlowVariable}s.
 * @throws NotConfigurableException The information in the {@code settings} model is invalid.
 */
public void loadSettingsFrom(final NodeSettingsRO settings, final DataTableSpec[] specs, final Map<String, FlowVariable> availableFlowVariables) throws NotConfigurableException {
    if (specs == null || specs.length == 0 || specs[0] == null) /*|| specs[0].getNumColumns() == 0*/
    {
        throw new NotConfigurableException("No columns available!");
    }
    m_spec = specs[0];
    m_parser.setDataTableSpec(m_spec);
    m_parser.setFlowVariables(availableFlowVariables);
    RuleEngineSettings ruleSettings = new RuleEngineSettings();
    ruleSettings.loadSettingsForDialog(settings);
    update(specs[0], availableFlowVariables);
    if (m_nodeType.hasOutput()) {
        String newColName = ruleSettings.getNewColName();
        m_newColumnName.setText(newColName);
        if (m_replaceColumn != null) {
            m_outputGroup.setSelected(m_outputGroup.getElements().nextElement().getModel(), ruleSettings.isAppendColumn());
            m_outputGroup.setSelected(m_replaceColRadio.getModel(), !ruleSettings.isAppendColumn());
            for (ActionListener listener : m_replaceColRadio.getActionListeners()) {
                listener.actionPerformed(null);
            }
            m_replaceColumn.setSelectedColumn(ruleSettings.getReplaceColumn());
        }
    }
    m_disallowLongOutputForCompatibility = ruleSettings.isDisallowLongOutputForCompatibility();
    update(m_spec, availableFlowVariables);
    final KnimeSyntaxTextArea textEditor = m_mainPanel.getTextEditor();
    textEditor.setText("");
    StringBuilder text = new StringBuilder();
    for (Iterator<String> it = ruleSettings.rules().iterator(); it.hasNext(); ) {
        final String rs = it.next();
        text.append(rs);
        if (it.hasNext()) {
            text.append('\n');
        }
    }
    if (!ruleSettings.rules().iterator().hasNext()) {
        final String defaultText = m_nodeType.defaultText();
        final String noText = RuleSupport.toComment(defaultText);
        textEditor.setText(noText);
        text.append(noText);
    }
    if (!m_nodeType.hasOutput()) {
        try {
            final SettingsModelBoolean includeOnMatch = new SettingsModelBoolean(RuleEngineFilterNodeModel.CFGKEY_INCLUDE_ON_MATCH, RuleEngineFilterNodeModel.DEFAULT_INCLUDE_ON_MATCH);
            includeOnMatch.loadSettingsFrom(settings);
            m_top.setSelected(includeOnMatch.getBooleanValue());
            m_bottom.setSelected(!includeOnMatch.getBooleanValue());
        } catch (InvalidSettingsException e) {
            boolean defaultTop = true;
            m_top.setSelected(defaultTop);
            m_bottom.setSelected(!defaultTop);
        }
    }
    updateText(text.toString());
}
Also used : NotConfigurableException(org.knime.core.node.NotConfigurableException) SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) ActionListener(java.awt.event.ActionListener) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) KnimeSyntaxTextArea(org.knime.base.node.util.KnimeSyntaxTextArea)

Example 28 with InvalidSettingsException

use of org.knime.core.node.InvalidSettingsException in project knime-core by knime.

the class RulePanel method saveSettings.

/**
 * Saves the settings to the model ({@code settings}).
 *
 * @param settings The container for the model.
 * @throws InvalidSettingsException When there should be a column/flow variable outcome, but no column/flow variable
 *             is selected or the rules do not parse without error.
 */
public void saveSettings(final NodeSettingsWO settings) throws InvalidSettingsException {
    RuleEngineSettings ruleSettings = new RuleEngineSettings();
    if (m_nodeType.hasOutput()) {
        ruleSettings.setNewcolName(m_newColumnName.getText());
        if (m_replaceColumn != null) {
            ruleSettings.setAppendColumn(m_newColumnName.isEnabled());
            ruleSettings.setReplaceColumn(m_replaceColumn.getSelectedColumn());
            if (m_outputGroup.isSelected(m_replaceColRadio.getModel()) && m_replaceColumn.getSelectedColumn() == null) {
                throw new InvalidSettingsException("No column was selected for replacement!");
            }
        }
    } else {
        ruleSettings.setNewcolName("");
    }
    // Check the rule integrity, configure will also check.
    try {
        computeRules();
    } catch (ParseException e) {
        throw new InvalidSettingsException(e.getMessage(), e);
    }
    for (String r : m_mainPanel.getExpression().split("\n", -1)) {
        if (!r.trim().isEmpty()) {
            ruleSettings.addRule(r);
        }
    }
    ruleSettings.saveSettings(settings);
    if (!m_nodeType.hasOutput()) {
        final SettingsModelBoolean settingsModelBoolean = new SettingsModelBoolean(RuleEngineFilterNodeModel.CFGKEY_INCLUDE_ON_MATCH, RuleEngineFilterNodeModel.DEFAULT_INCLUDE_ON_MATCH);
        settingsModelBoolean.setBooleanValue(m_top.isSelected());
        settingsModelBoolean.saveSettingsTo(settings);
    }
}
Also used : SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ParseException(java.text.ParseException)

Example 29 with InvalidSettingsException

use of org.knime.core.node.InvalidSettingsException in project knime-core by knime.

the class PMMLRuleEditorNodeModel method computeFinalOutputSpecs.

/**
 * {@inheritDoc}
 */
@Override
public PortObjectSpec[] computeFinalOutputSpecs(final StreamableOperatorInternals internals, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    final PortObjectSpec[] computeFinalOutputSpecs = super.computeFinalOutputSpecs(internals, inSpecs);
    // TODO should this be done some place else (finish)?
    StreamInternalForPMMLPortObject poInternals = (StreamInternalForPMMLPortObject) internals;
    try {
        DataTableSpec tableSpec = (DataTableSpec) inSpecs[0];
        RearrangerAndPMMLModel m = createRearrangerAndPMMLModel(tableSpec);
        poInternals.setObject(m.getPMMLPortObject());
    } catch (ParseException e) {
        throw new InvalidSettingsException(e);
    }
    return computeFinalOutputSpecs;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) ParseException(java.text.ParseException)

Example 30 with InvalidSettingsException

use of org.knime.core.node.InvalidSettingsException in project knime-core by knime.

the class PMMLRuleEditorNodeModel method createRearranger.

/**
 * Creates the {@link ColumnRearranger} that can compute the new column.
 *
 * @param tableSpec The spec of the input table.
 * @param ruleSet The {@link RuleSet} xml object where the rules should be added.
 * @param parser The parser for the rules.
 * @return The {@link ColumnRearranger}.
 * @throws ParseException Problem during parsing.
 * @throws InvalidSettingsException if settings are invalid
 */
private ColumnRearranger createRearranger(final DataTableSpec tableSpec, final RuleSet ruleSet, final PMMLRuleParser parser) throws ParseException, InvalidSettingsException {
    if (m_settings.isAppendColumn() && m_settings.getNewColName().isEmpty()) {
        throw new InvalidSettingsException("No name for prediction column provided");
    }
    Set<String> outcomes = new LinkedHashSet<String>();
    List<DataType> outcomeTypes = new ArrayList<DataType>();
    int line = 0;
    final List<Pair<PMMLPredicate, Expression>> rules = new ArrayList<Pair<PMMLPredicate, Expression>>();
    for (String ruleText : m_settings.rules()) {
        ++line;
        if (RuleSupport.isComment(ruleText)) {
            continue;
        }
        try {
            ParseState state = new ParseState(ruleText);
            PMMLPredicate expression = parser.parseBooleanExpression(state);
            SimpleRule simpleRule = ruleSet.addNewSimpleRule();
            setCondition(simpleRule, expression);
            state.skipWS();
            state.consumeText("=>");
            state.skipWS();
            Expression outcome = parser.parseOutcomeOperand(state, null);
            // Only constants are allowed in the outcomes.
            assert outcome.isConstant() : outcome;
            rules.add(new Pair<PMMLPredicate, Expression>(expression, outcome));
            outcomeTypes.add(outcome.getOutputType());
            simpleRule.setScore(outcome.toString());
            // simpleRule.setConfidence(confidenceForRule(simpleRule, line, ruleText));
            simpleRule.setWeight(weightForRule(simpleRule, line, ruleText));
            outcomes.add(simpleRule.getScore());
        } catch (ParseException e) {
            throw Util.addContext(e, ruleText, line);
        }
    }
    DataType outcomeType = RuleEngineNodeModel.computeOutputType(outcomeTypes, true);
    ColumnRearranger rearranger = new ColumnRearranger(tableSpec);
    DataColumnSpecCreator specProto = new DataColumnSpecCreator(m_settings.isAppendColumn() ? DataTableSpec.getUniqueColumnName(tableSpec, m_settings.getNewColName()) : m_settings.getReplaceColumn(), outcomeType);
    specProto.setDomain(new DataColumnDomainCreator(toCells(outcomes, outcomeType)).createDomain());
    SingleCellFactory cellFactory = new SingleCellFactory(true, specProto.createSpec()) {

        @Override
        public DataCell getCell(final DataRow row) {
            for (Pair<PMMLPredicate, Expression> pair : rules) {
                if (pair.getFirst().evaluate(row, tableSpec) == Boolean.TRUE) {
                    return pair.getSecond().evaluate(row, null).getValue();
                }
            }
            return DataType.getMissingCell();
        }
    };
    if (m_settings.isAppendColumn()) {
        rearranger.append(cellFactory);
    } else {
        rearranger.replace(cellFactory, m_settings.getReplaceColumn());
    }
    return rearranger;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ArrayList(java.util.ArrayList) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) ParseState(org.knime.base.node.rules.engine.BaseRuleParser.ParseState) DataRow(org.knime.core.data.DataRow) SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Expression(org.knime.base.node.rules.engine.Expression) DataType(org.knime.core.data.DataType) ParseException(java.text.ParseException) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) Pair(org.knime.core.util.Pair)

Aggregations

InvalidSettingsException (org.knime.core.node.InvalidSettingsException)818 DataTableSpec (org.knime.core.data.DataTableSpec)278 DataColumnSpec (org.knime.core.data.DataColumnSpec)211 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)153 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)121 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)113 IOException (java.io.IOException)109 DataCell (org.knime.core.data.DataCell)99 ArrayList (java.util.ArrayList)96 DataType (org.knime.core.data.DataType)89 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)82 File (java.io.File)72 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)69 DataRow (org.knime.core.data.DataRow)66 DoubleValue (org.knime.core.data.DoubleValue)58 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)48 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)47 FileInputStream (java.io.FileInputStream)43 LinkedHashMap (java.util.LinkedHashMap)42 NotConfigurableException (org.knime.core.node.NotConfigurableException)41