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;
}
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());
}
}
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>");
}
}
Aggregations