Search in sources :

Example 31 with StringValue

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

the class RuleEngineVariable2PortsNodeModel method rules.

/**
 * Helper method to read the rules similarly in all (Dictionary) nodes from rule tables.
 *
 * @param rules The rules as {@link DataRow}s.
 * @param settings The configuration settings.
 * @param ruleType The kind of the node.
 * @return The list of rules read.
 * @throws InvalidSettingsException Missing values in outcomes are not supported.
 * @throws InterruptedException When the processing was interrupted.
 */
static List<String> rules(final RowInput rules, final RuleEngine2PortsSimpleSettings settings, final RuleNodeSettings ruleType) throws InvalidSettingsException, InterruptedException {
    List<String> ret = new ArrayList<>();
    int ruleIdx = rules.getDataTableSpec().findColumnIndex(settings.getRuleColumn()), outcomeIdx = rules.getDataTableSpec().findColumnIndex(settings.getOutcomeColumn());
    assert ruleIdx >= 0 : ruleIdx;
    DataRow ruleRow;
    while ((ruleRow = rules.poll()) != null) {
        DataCell ruleCell = ruleRow.getCell(ruleIdx);
        CheckUtils.checkArgument(ruleCell instanceof StringValue, "The rule in the row: " + ruleRow.getKey() + " is not a String: " + ruleCell.getType() + " (" + ruleCell + ")");
        StringValue ruleSv = (StringValue) ruleCell;
        String rule = ruleSv.getStringValue().replaceAll("[\r\n]+", " ");
        if (outcomeIdx >= 0) {
            String outcomeString;
            try {
                outcomeString = settings.asStringFailForMissing(ruleRow.getCell(outcomeIdx));
            } catch (InvalidSettingsException e) {
                if (RuleSupport.isComment(rule)) {
                    outcomeString = "?";
                } else {
                    throw e;
                }
            }
            if (ruleType.onlyBooleanOutcome()) {
                if ("\"TRUE\"".equalsIgnoreCase(outcomeString)) {
                    outcomeString = "TRUE";
                } else if ("\"FALSE\"".equalsIgnoreCase(outcomeString)) {
                    outcomeString = "FALSE";
                }
            }
            rule += " => " + outcomeString;
        }
        ret.add(rule);
    }
    rules.close();
    return ret;
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ArrayList(java.util.ArrayList) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) DataRow(org.knime.core.data.DataRow)

Example 32 with StringValue

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

the class RuleEngineNodeModel method updateColSpec.

/**
 * Updates the prediction column specification if the rule outcomes are computable in advance.
 * <br/>
 * This will add all outcomes, not just the possibles.
 * <br/>
 * Sorry for the high complexity.
 *
 * @param rules The {@link Rule}s we want to analyse.
 * @param outType The output data type.
 * @param colSpecCreator The column creator.
 */
private static void updateColSpec(final List<Rule> rules, final DataType outType, final DataColumnSpecCreator colSpecCreator, final FlowVariableProvider nm) {
    List<DataValue> results = new ArrayList<DataValue>(rules.size());
    for (Rule rule : rules) {
        try {
            DataValue result = rule.getOutcome().getComputedResult(new DefaultRow("", new double[0]), new VariableProvider() {

                /**
                 * {@inheritDoc}
                 */
                @Override
                @Deprecated
                public int getRowCount() {
                    throw new IllegalStateException("We will catch this.");
                }

                /**
                 * {@inheritDoc}
                 */
                @Override
                public long getRowCountLong() {
                    throw new IllegalStateException("We will catch this.");
                }

                /**
                 * {@inheritDoc}
                 */
                @Override
                @Deprecated
                public int getRowIndex() {
                    throw new IllegalStateException("We will catch this.");
                }

                /**
                 * {@inheritDoc}
                 */
                @Override
                public long getRowIndexLong() {
                    throw new IllegalStateException("We will catch this.");
                }

                /**
                 * {@inheritDoc}
                 */
                @Override
                public Object readVariable(final String arg0, final Class<?> arg1) {
                    return nm.readVariable(arg0, arg1);
                }
            });
            results.add(result);
        } catch (RuntimeException e) {
            // We stop, cannot update properly
            return;
        }
    }
    Set<DataCell> values = new LinkedHashSet<DataCell>(results.size());
    if (outType.equals(StringCell.TYPE)) {
        for (DataValue dataValue : results) {
            if (dataValue instanceof StringCell) {
                values.add((StringCell) dataValue);
            } else if (dataValue instanceof StringValue) {
                StringValue sv = (StringValue) dataValue;
                values.add(new StringCell(sv.getStringValue()));
            } else {
                values.add(new StringCell(dataValue.toString()));
            }
        }
        colSpecCreator.setDomain(new DataColumnDomainCreator(values).createDomain());
    } else if (outType.isCompatible(DoubleValue.class)) {
        DataCell min = new DoubleCell(Double.POSITIVE_INFINITY), max = new DoubleCell(Double.NEGATIVE_INFINITY);
        for (DataValue dataValue : results) {
            if (dataValue instanceof DoubleValue) {
                DoubleValue dv = (DoubleValue) dataValue;
                double d = dv.getDoubleValue();
                min = d < ((DoubleValue) min).getDoubleValue() ? (DataCell) dv : min;
                max = d > ((DoubleValue) max).getDoubleValue() ? (DataCell) dv : max;
                values.add((DataCell) dv);
            }
        }
        DataColumnDomainCreator dcdc = new DataColumnDomainCreator();
        if (min instanceof DoubleValue && max instanceof DoubleValue) {
            double mi = ((DoubleValue) min).getDoubleValue(), ma = ((DoubleValue) max).getDoubleValue();
            if (mi != Double.POSITIVE_INFINITY && ma != Double.NEGATIVE_INFINITY && !Double.isNaN(mi) && !Double.isNaN(ma)) {
                dcdc.setLowerBound(min);
                dcdc.setUpperBound(max);
            }
        }
        colSpecCreator.setDomain(dcdc.createDomain());
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataValue(org.knime.core.data.DataValue) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) StringCell(org.knime.core.data.def.StringCell) DoubleValue(org.knime.core.data.DoubleValue) FlowVariableProvider(org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) StringValue(org.knime.core.data.StringValue)

Example 33 with StringValue

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

the class NodeViewUtil method renderDataCell.

/**
 * @param cell the data cell to render
 * @param buffer write to this buffer
 */
public static void renderDataCell(final DataCell cell, final StringBuilder buffer) {
    if (cell.isMissing()) {
        buffer.append("<td></td>");
        return;
    }
    if (cell.getType().isCompatible(IntValue.class)) {
        IntValue value = (IntValue) cell;
        buffer.append("<td class=\"numeric\">");
        buffer.append(value.getIntValue());
        buffer.append("</td>");
    } else if (cell.getType().isCompatible(DoubleValue.class)) {
        DoubleValue value = (DoubleValue) cell;
        buffer.append("<td class=\"numeric\">");
        buffer.append(DoubleFormat.formatDouble(value.getDoubleValue()));
        buffer.append("</td>");
    } else if (cell.getType().isCompatible(StringValue.class)) {
        StringValue value = (StringValue) cell;
        buffer.append("<td class=\"left\">");
        buffer.append(value.getStringValue());
        buffer.append("</td>");
    } else {
        buffer.append("<td class=\"left\">");
        buffer.append(cell.toString());
        buffer.append("</td>");
    }
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) StringValue(org.knime.core.data.StringValue) IntValue(org.knime.core.data.IntValue)

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