Search in sources :

Example 26 with StringValue

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

the class DefaultTableTest method createWorkingTables.

// testDefaultTable()
/**
 * Creates some new <code>DefaultTable</code> objects that should ...
 * theoretically ... do.
 */
private void createWorkingTables() {
    // some constructors with (supposedly) no problems
    DefaultTable t1 = new DefaultTable(CASE_1_CONTENT, CASE_1_ROWHEADER, CASE_1_COLHEADER);
    DataTableSpec t1Spec = t1.getDataTableSpec();
    assertEquals(CASE_1_COLHEADER.length, t1Spec.getNumColumns());
    // check spec
    for (int c = 0; c < CASE_1_COLHEADER.length; c++) {
        DataColumnSpec currentColumnSpec = t1Spec.getColumnSpec(c);
        String colName = currentColumnSpec.getName().toString();
        DataType type = currentColumnSpec.getType();
        assertEquals(colName, CASE_1_COLHEADER[c]);
        assertTrue(type.isCompatible(StringValue.class));
    }
    // check content
    int r = 0;
    for (RowIterator it = t1.iterator(); it.hasNext(); r++) {
        DataRow row = it.next();
        assertEquals(row.getNumCells(), CASE_1_COLHEADER.length);
        for (int i = 0; i < CASE_1_COLHEADER.length; i++) {
            StringValue cell = (StringValue) row.getCell(i);
            assertEquals(cell.getStringValue(), CASE_1_CONTENT[r][i]);
        }
    }
    // all one-dimensional arrays (meta-info) are optional
    new DefaultTable(CASE_1_CONTENT, null, CASE_1_COLHEADER);
    new DefaultTable(CASE_1_CONTENT, CASE_1_ROWHEADER, null);
    new DefaultTable(CASE_1_CONTENT, CASE_1_ROWHEADER, CASE_1_COLHEADER);
    new DefaultTable(CASE_1_CONTENT, null, null);
    new DefaultTable(CASE_1_CONTENT, null, CASE_1_COLHEADER);
    new DefaultTable(CASE_1_CONTENT, null, null);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) RowIterator(org.knime.core.data.RowIterator) DataType(org.knime.core.data.DataType) StringValue(org.knime.core.data.StringValue) DataRow(org.knime.core.data.DataRow)

Example 27 with StringValue

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

the class StringToDateTimeNodeDialog method updatePreview.

/**
 * @param colSelectModel settings model of the column filter
 */
private void updatePreview(final SettingsModelColumnFilter2 colSelectModel) {
    final String[] includes = colSelectModel.applyTo(m_spec).getIncludes();
    Arrays.stream(colSelectModel.applyTo(m_spec).getIncludes()).mapToInt(s -> m_spec.findColumnIndex(s)).toArray();
    m_preview = "";
    if (m_dataTable != null) {
        if (!(includes.length == 0 || m_dataTable.size() == 0)) {
            for (final DataRow row : m_dataTable) {
                final DataCell cell = row.getCell(m_spec.findColumnIndex(includes[0]));
                if (cell.isMissing()) {
                    continue;
                } else {
                    m_preview = ((StringValue) cell).getStringValue();
                    break;
                }
            }
        }
    }
    m_previewLabel.setText("Content of the first cell: " + m_preview);
}
Also used : Color(java.awt.Color) Insets(java.awt.Insets) Arrays(java.util.Arrays) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) DataTableSpec(org.knime.core.data.DataTableSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) DataAwareNodeDialogPane(org.knime.core.node.DataAwareNodeDialogPane) NotConfigurableException(org.knime.core.node.NotConfigurableException) DialogComponentString(org.knime.core.node.defaultnodesettings.DialogComponentString) Locale(java.util.Locale) DialogComponentStringSelection(org.knime.core.node.defaultnodesettings.DialogComponentStringSelection) LocalTime(java.time.LocalTime) DateTimeType(org.knime.time.util.DateTimeType) JComboBox(javax.swing.JComboBox) DataCell(org.knime.core.data.DataCell) DialogComponentBoolean(org.knime.core.node.defaultnodesettings.DialogComponentBoolean) DateTimeException(java.time.DateTimeException) StringValue(org.knime.core.data.StringValue) FlowLayout(java.awt.FlowLayout) JButton(javax.swing.JButton) DialogComponentColumnFilter2(org.knime.core.node.defaultnodesettings.DialogComponentColumnFilter2) Collection(java.util.Collection) SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) BorderFactory(javax.swing.BorderFactory) SettingsModelColumnFilter2(org.knime.core.node.defaultnodesettings.SettingsModelColumnFilter2) GridBagConstraints(java.awt.GridBagConstraints) DataRow(org.knime.core.data.DataRow) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) Dimension(java.awt.Dimension) BufferedDataTable(org.knime.core.node.BufferedDataTable) DialogComponentButtonGroup(org.knime.core.node.defaultnodesettings.DialogComponentButtonGroup) JLabel(javax.swing.JLabel) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) GridBagLayout(java.awt.GridBagLayout) PortObject(org.knime.core.node.port.PortObject) JPanel(javax.swing.JPanel) DataCell(org.knime.core.data.DataCell) DialogComponentString(org.knime.core.node.defaultnodesettings.DialogComponentString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow)

Example 28 with StringValue

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

the class StringToDurationPeriodNodeModel method detectTypes.

private void detectTypes(final RowInput rowInput) throws InterruptedException {
    final DataTableSpec spec = rowInput.getDataTableSpec();
    final String[] includes = m_colSelect.applyTo(spec).getIncludes();
    if (m_detectedTypes == null) {
        m_detectedTypes = new DataType[includes.length];
    }
    if (m_type.getStringValue().equals(OutputType.Duration.name())) {
        Arrays.fill(m_detectedTypes, DurationCellFactory.TYPE);
    }
    if (m_type.getStringValue().equals(OutputType.Period.name())) {
        Arrays.fill(m_detectedTypes, PeriodCellFactory.TYPE);
    }
    if (m_type.getStringValue().equals(OutputType.Automatic.name())) {
        DataRow row;
        while ((row = rowInput.poll()) != null) {
            boolean isCellMissing = false;
            for (int i = 0; i < includes.length; i++) {
                if (m_detectedTypes[i] == null) {
                    final DataCell cell = row.getCell(spec.findColumnIndex(includes[i]));
                    if (cell.isMissing()) {
                        isCellMissing = true;
                    } else {
                        final String string = ((StringValue) cell).getStringValue();
                        try {
                            DurationPeriodFormatUtils.parseDuration(string);
                            m_detectedTypes[i] = DurationCellFactory.TYPE;
                        } catch (DateTimeParseException e1) {
                            try {
                                DurationPeriodFormatUtils.parsePeriod(string);
                                m_detectedTypes[i] = PeriodCellFactory.TYPE;
                            } catch (DateTimeParseException e2) {
                                isCellMissing = true;
                            }
                        }
                    }
                }
            }
            if (!isCellMissing) {
                // finished - every column type is detected
                break;
            }
        }
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) DataTableSpec(org.knime.core.data.DataTableSpec) DataCell(org.knime.core.data.DataCell) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) StringValue(org.knime.core.data.StringValue) ReplacedColumnsDataRow(org.knime.base.data.replace.ReplacedColumnsDataRow) DataRow(org.knime.core.data.DataRow)

Example 29 with StringValue

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

the class RuleEngine2PortsNodeModel method computeRearrangerWithoutPMML.

/**
 * @param spec
 * @param rules
 * @param flowVars
 * @param ruleIdx
 * @param outcomeIdx
 * @param outputColumnName
 * @return
 * @throws InterruptedException
 * @throws InvalidSettingsException
 */
private Pair<ColumnRearranger, PortObject> computeRearrangerWithoutPMML(final DataTableSpec spec, final RowInput rules, final Map<String, FlowVariable> flowVars, final int ruleIdx, final int outcomeIdx, final String outputColumnName) throws InterruptedException, InvalidSettingsException {
    PortObject po;
    ColumnRearranger ret;
    RuleFactory factory = RuleFactory.getInstance(RuleNodeSettings.RuleEngine).cloned();
    po = InactiveBranchPortObject.INSTANCE;
    ret = new ColumnRearranger(spec);
    factory.disableMissingComparisons();
    factory.disableNaNComparisons();
    final List<Rule> ruleList = new ArrayList<>();
    int lineNo = 0;
    DataRow ruleRow;
    while ((ruleRow = rules.poll()) != null) {
        lineNo++;
        DataCell cell = ruleRow.getCell(ruleIdx);
        CheckUtils.checkSetting(!cell.isMissing(), "Missing rule in row: " + ruleRow.getKey());
        if (cell instanceof StringValue) {
            StringValue sv = (StringValue) cell;
            String ruleText = sv.getStringValue();
            if (outcomeIdx >= 0) {
                try {
                    ruleText += " => " + m_settings.asStringFailForMissing(ruleRow.getCell(outcomeIdx));
                } catch (InvalidSettingsException e) {
                    if (RuleSupport.isComment(ruleText)) {
                        ruleText += " => ?";
                    } else {
                        throw e;
                    }
                }
            }
            try {
                Rule rule = factory.parse(ruleText, spec, flowVars);
                if (rule.getCondition().isEnabled()) {
                    ruleList.add(rule);
                }
            } catch (ParseException e) {
                ParseException error = Util.addContext(e, ruleText, lineNo);
                throw new InvalidSettingsException("Wrong rule in line: " + ruleRow.getKey() + "\n" + error.getMessage(), error);
            }
        } else {
            CheckUtils.checkSetting(false, "Wrong type (" + cell.getType() + ") of rule: " + cell + "\nin row: " + ruleRow.getKey());
        }
    }
    // unfortunately we cannot compute the domain and limits of the output column.
    final DataType outType = RuleEngineNodeModel.computeOutputType(ruleList, computeOutcomeType(rules.getDataTableSpec()), RuleNodeSettings.RuleEngine, m_settings.isDisallowLongOutputForCompatibility());
    final MutableLong rowIndex = new MutableLong();
    final ExecutionMonitor exec = new ExecutionMonitor();
    final boolean disallowLongOutputForCompatibility = m_settings.isDisallowLongOutputForCompatibility();
    VariableProvider.SingleCellFactoryProto fac = new VariableProvider.SingleCellFactoryProto(new DataColumnSpecCreator(outputColumnName, outType).createSpec()) {

        @Override
        public DataCell getCell(final DataRow row) {
            setProgress(rowIndex.longValue(), m_rowCount, row.getKey(), exec);
            rowIndex.increment();
            return RuleEngineNodeModel.getRulesOutcome(outType, row, ruleList, disallowLongOutputForCompatibility, this);
        }

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

        @Deprecated
        @Override
        public int getRowCount() {
            return RuleEngine2PortsNodeModel.this.getRowCount();
        }

        @Override
        public long getRowCountLong() {
            return RuleEngine2PortsNodeModel.this.getRowCountLong();
        }
    };
    if (m_settings.isReplaceColumn()) {
        ret.replace(fac, outputColumnName);
    } else {
        ret.append(fac);
    }
    return Pair.create(ret, po);
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) RuleFactory(org.knime.base.node.rules.engine.RuleFactory) ArrayList(java.util.ArrayList) DataRow(org.knime.core.data.DataRow) MutableLong(org.apache.commons.lang3.mutable.MutableLong) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) VariableProvider(org.knime.base.node.rules.engine.VariableProvider) FlowVariableProvider(org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) Rule(org.knime.base.node.rules.engine.Rule) SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) ParseException(java.text.ParseException) StringValue(org.knime.core.data.StringValue) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject)

Example 30 with StringValue

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

the class RuleEngine2PortsNodeModel method computeRearrangerWithPMML.

/**
 * @param spec
 * @param rules
 * @param flowVars
 * @param ruleIdx
 * @param outcomeIdx
 * @param confidenceIdx
 * @param weightIdx
 * @param validationIdx
 * @param outputColumnName
 * @return
 * @throws InterruptedException
 * @throws InvalidSettingsException
 */
private Pair<ColumnRearranger, PortObject> computeRearrangerWithPMML(final DataTableSpec spec, final RowInput rules, final Map<String, FlowVariable> flowVars, final int ruleIdx, final int outcomeIdx, final int confidenceIdx, final int weightIdx, final int validationIdx, final String outputColumnName) throws InterruptedException, InvalidSettingsException {
    PortObject po;
    ColumnRearranger ret;
    PMMLDocument doc = PMMLDocument.Factory.newInstance();
    final PMML pmmlObj = doc.addNewPMML();
    RuleSetModel ruleSetModel = pmmlObj.addNewRuleSetModel();
    RuleSet ruleSet = ruleSetModel.addNewRuleSet();
    List<DataType> outcomeTypes = new ArrayList<>();
    PMMLRuleParser parser = new PMMLRuleParser(spec, flowVars);
    int lineNo = 0;
    DataRow ruleRow;
    while ((ruleRow = rules.poll()) != null) {
        ++lineNo;
        DataCell rule = ruleRow.getCell(ruleIdx);
        CheckUtils.checkSetting(!rule.isMissing(), "Missing rule in row: " + ruleRow.getKey());
        if (rule instanceof StringValue) {
            StringValue ruleText = (StringValue) rule;
            String r = ruleText.getStringValue().replaceAll("[\r\n]+", " ");
            if (RuleSupport.isComment(r)) {
                continue;
            }
            if (outcomeIdx >= 0) {
                r += " => " + m_settings.asStringFailForMissing(ruleRow.getCell(outcomeIdx));
            }
            ParseState state = new ParseState(r);
            try {
                PMMLPredicate condition = parser.parseBooleanExpression(state);
                SimpleRule simpleRule = ruleSet.addNewSimpleRule();
                setCondition(simpleRule, condition);
                state.skipWS();
                state.consumeText("=>");
                state.skipWS();
                Expression outcome = parser.parseOutcomeOperand(state, null);
                simpleRule.setScore(outcome.toString());
                if (confidenceIdx >= 0) {
                    DataCell confidenceCell = ruleRow.getCell(confidenceIdx);
                    if (!confidenceCell.isMissing()) {
                        if (confidenceCell instanceof DoubleValue) {
                            DoubleValue dv = (DoubleValue) confidenceCell;
                            double confidence = dv.getDoubleValue();
                            simpleRule.setConfidence(confidence);
                        }
                    }
                }
                if (weightIdx >= 0) {
                    DataCell weightCell = ruleRow.getCell(weightIdx);
                    boolean missing = true;
                    if (!weightCell.isMissing()) {
                        if (weightCell instanceof DoubleValue) {
                            DoubleValue dv = (DoubleValue) weightCell;
                            double weight = dv.getDoubleValue();
                            simpleRule.setWeight(weight);
                            missing = false;
                        }
                    }
                    if (missing && m_settings.isHasDefaultWeight()) {
                        simpleRule.setWeight(m_settings.getDefaultWeight());
                    }
                }
                CheckUtils.checkSetting(outcome.isConstant(), "Outcome is not constant in line " + lineNo + " (" + ruleRow.getKey() + ") for rule: " + rule);
                outcomeTypes.add(outcome.getOutputType());
            } catch (ParseException e) {
                ParseException error = Util.addContext(e, r, lineNo);
                throw new InvalidSettingsException("Wrong rule in line: " + ruleRow.getKey() + "\n" + error.getMessage(), error);
            }
        } else {
            CheckUtils.checkSetting(false, "Wrong type (" + rule.getType() + ") of rule: " + rule + "\nin row: " + ruleRow.getKey());
        }
    }
    ColumnRearranger dummy = new ColumnRearranger(spec);
    if (!m_settings.isReplaceColumn()) {
        dummy.append(new SingleCellFactory(new DataColumnSpecCreator(outputColumnName, RuleEngineNodeModel.computeOutputType(outcomeTypes, computeOutcomeType(rules.getDataTableSpec()), true, m_settings.isDisallowLongOutputForCompatibility())).createSpec()) {

            @Override
            public DataCell getCell(final DataRow row) {
                return null;
            }
        });
    }
    PMMLPortObject pmml = createPMMLPortObject(doc, ruleSetModel, ruleSet, parser, dummy.createSpec());
    po = pmml;
    m_copy = copy(pmml);
    String predictionConfidenceColumn = m_settings.getPredictionConfidenceColumn();
    if (predictionConfidenceColumn == null || predictionConfidenceColumn.isEmpty()) {
        predictionConfidenceColumn = RuleEngine2PortsSettings.DEFAULT_PREDICTION_CONFIDENCE_COLUMN;
    }
    ret = PMMLRuleSetPredictorNodeModel.createRearranger(pmml, spec, m_settings.isReplaceColumn(), outputColumnName, m_settings.isComputeConfidence(), DataTableSpec.getUniqueColumnName(dummy.createSpec(), predictionConfidenceColumn), validationIdx);
    return Pair.create(ret, po);
}
Also used : RuleSetModel(org.dmg.pmml.RuleSetModelDocument.RuleSetModel) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ArrayList(java.util.ArrayList) PMMLRuleParser(org.knime.base.node.rules.engine.pmml.PMMLRuleParser) 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) DataType(org.knime.core.data.DataType) StringValue(org.knime.core.data.StringValue) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) RuleSet(org.dmg.pmml.RuleSetDocument.RuleSet) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) Expression(org.knime.base.node.rules.engine.Expression) DoubleValue(org.knime.core.data.DoubleValue) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMML(org.dmg.pmml.PMMLDocument.PMML) DataCell(org.knime.core.data.DataCell) PMMLDocument(org.dmg.pmml.PMMLDocument) ParseException(java.text.ParseException)

Aggregations

StringValue (org.knime.core.data.StringValue)33 DataCell (org.knime.core.data.DataCell)25 DataRow (org.knime.core.data.DataRow)22 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)13 DataColumnSpec (org.knime.core.data.DataColumnSpec)10 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)10 DoubleValue (org.knime.core.data.DoubleValue)8 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)8 ArrayList (java.util.ArrayList)7 DataTableSpec (org.knime.core.data.DataTableSpec)7 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)7 StringCell (org.knime.core.data.def.StringCell)7 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)6 ParseException (java.text.ParseException)5 DataType (org.knime.core.data.DataType)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)5 PortObject (org.knime.core.node.port.PortObject)5 IOException (java.io.IOException)4 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)4 BitVectorType (org.knime.core.data.vector.bitvector.BitVectorType)3