Search in sources :

Example 1 with FlowVariableProvider

use of org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider 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)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 DataCell (org.knime.core.data.DataCell)1 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)1 DataValue (org.knime.core.data.DataValue)1 DoubleValue (org.knime.core.data.DoubleValue)1 StringValue (org.knime.core.data.StringValue)1 DefaultRow (org.knime.core.data.def.DefaultRow)1 DoubleCell (org.knime.core.data.def.DoubleCell)1 StringCell (org.knime.core.data.def.StringCell)1 FlowVariableProvider (org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider)1