use of org.knime.core.data.DataCell in project knime-core by knime.
the class RuleSetToTable method createRow.
/**
* Creates a row, {@link DataCell} values based on {@code rule} and the other parameters.
*
* @param rule A PMML {@link Rule}.
* @param outcomeType The expected outcome.
* @param types The types of the input column.
* @return The cells for the {@code rule}.
*/
private DataCell[] createRow(final Rule rule, final DataType outcomeType, final Map<String, DataType> types, final List<String> scoreValues) {
List<DataCell> ret = new ArrayList<>();
boolean usePrecedence = !m_settings.getAdditionalParentheses().getBooleanValue();
if (m_settings.getSplitRules().getBooleanValue()) {
ret.add(new StringCell(convertToString(rule.getCondition(), usePrecedence, types)));
ret.add(convertToExpectedType(rule.getOutcome(), outcomeType));
} else {
ret.add(new StringCell(convertToString(rule.getCondition(), usePrecedence, types) + " => " + toString(convertToExpectedType(rule.getOutcome(), outcomeType))));
}
if (m_settings.getConfidenceAndWeight().getBooleanValue()) {
ret.add(toCell(rule.getConfidence()));
ret.add(toCell(rule.getWeight()));
}
if (m_settings.getProvideStatistics().getBooleanValue()) {
ret.add(toCell(rule.getRecordCount()));
ret.add(toCell(rule.getNbCorrect()));
}
final Map<String, ScoreProbabilityAndRecordCount> scoreDistribution = rule.getScoreDistribution();
if (m_settings.getScoreTableRecordCount().isEnabled() && m_settings.getScoreTableRecordCount().getBooleanValue()) {
for (final String value : scoreValues) {
if (scoreDistribution.containsKey(value)) {
ret.add(new DoubleCell(scoreDistribution.get(value).getRecordCount()));
} else {
ret.add(DataType.getMissingCell());
}
}
}
if (m_settings.getScoreTableProbability().isEnabled() && m_settings.getScoreTableProbability().getBooleanValue()) {
for (final String value : scoreValues) {
if (scoreDistribution.containsKey(value)) {
final BigDecimal probability = scoreDistribution.get(value).getProbability();
ret.add(probability == null ? DataType.getMissingCell() : new DoubleCell(probability.doubleValue()));
} else {
ret.add(DataType.getMissingCell());
}
}
}
return ret.toArray(new DataCell[ret.size()]);
}
use of org.knime.core.data.DataCell in project knime-core by knime.
the class RuleEngine2PortsSimpleNodeDialog method updateErrorsAndWarnings.
/**
* Updates the errors table, the warning text area and the computed outcome type.
*/
protected void updateErrorsAndWarnings() {
m_errorsModel.setRowCount(0);
hideErrors();
m_warnings.setText("");
m_outcomeType.setIcon(DataType.getMissingCell().getType().getIcon());
// Checking data from second input port
final int ruleIdx = getRules() == null ? -1 : getRules().getSpec().findColumnIndex(m_ruleColumn.getSelectedColumn());
final int outcomeIdx = getRules() == null ? -1 : getRules().getSpec().findColumnIndex(m_outcomeColumn.getSelectedColumn());
if (getRules() != null && isSpecAvailable() && ruleIdx >= 0) {
RuleFactory factory = ruleFactory();
long lineNo = 0;
boolean wasCatchAll = false;
final boolean firstHit = isFirstHit();
List<Rule> rules = new ArrayList<>();
for (DataRow dataRow : getRules()) {
++lineNo;
DataCell ruleCell = dataRow.getCell(ruleIdx);
if (ruleCell.isMissing()) {
// String cellValue = "?";
// if (ruleCell instanceof MissingValue) {
// cellValue += " (" + ((MissingValue)ruleCell).getError() + ")";
// }
m_errorsModel.addRow(new Object[] { dataRow.getKey(), ruleCell, "Missing cell" });
showErrors();
}
if (ruleCell instanceof StringValue) {
StringValue ruleSV = (StringValue) ruleCell;
String ruleText = ruleSV.getStringValue().replaceAll("[\r\n]+", " ");
if (outcomeIdx >= 0) {
DataCell outcome = dataRow.getCell(outcomeIdx);
String outcomeString;
try {
outcomeString = m_settings.asStringFailForMissing(outcome);
} catch (InvalidSettingsException e) {
outcomeString = "?";
}
if (m_ruleType.onlyBooleanOutcome()) {
if ("\"TRUE\"".equalsIgnoreCase(outcomeString)) {
outcomeString = "TRUE";
} else if ("\"FALSE\"".equalsIgnoreCase(outcomeString)) {
outcomeString = "FALSE";
}
}
ruleText += " => " + outcomeString;
}
try {
Rule rule = factory.parse(ruleText, getDataSpec(), getAvailableFlowVariables());
rules.add(rule);
String origWarning = !m_warnings.getText().isEmpty() ? m_warnings.getText() + "\n" : "";
Condition cond = rule.getCondition();
if (cond.isEnabled()) {
// not comment
if (cond.isCatchAll() && !wasCatchAll && firstHit && lineNo < getRules().size()) {
m_warnings.setText(origWarning + "No rules will match after line " + lineNo + " (" + dataRow.getKey() + "). Because of rule: " + ruleText);
}
wasCatchAll |= cond.isCatchAll() && firstHit;
if (!wasCatchAll && cond.isConstantFalse()) {
m_warnings.setText(origWarning + "The rule in line " + lineNo + " (" + dataRow.getKey() + ") will never match: " + ruleText);
}
}
} catch (ParseException e) {
m_errorsModel.addRow(new Object[] { dataRow.getKey(), ruleText, e.getMessage() });
showErrors();
}
} else {
// Missings were handled previously
if (!ruleCell.isMissing()) {
m_errorsModel.addRow(new Object[] { dataRow.getKey(), ruleCell.toString(), "Wrong type: " + ruleCell.getType() });
}
}
}
final DataColumnSpec outcomeSpec = m_outcomeColumn.getSelectedColumnAsSpec();
DataType dataType = RuleEngineNodeModel.computeOutputType(rules, outcomeSpec == null ? StringCell.TYPE : outcomeSpec.getType(), m_ruleType, getSettings().isDisallowLongOutputForCompatibility());
if (dataType != null) {
m_outcomeType.setIcon(dataType.getIcon());
}
}
}
use of org.knime.core.data.DataCell in project knime-core by knime.
the class RuleEngineVariable2PortsNodeModel method performExecute.
/**
* Creates the flow variable according to the computed value.
*
* @param rules The rules to check for match.
* @throws InvalidSettingsException When there is an error in the settings.
*/
private void performExecute(final List<Rule> rules, final DataType outcomeColumnType) throws InvalidSettingsException {
String newFlowVar = m_newVariableName;
if (newFlowVar == null || newFlowVar.isEmpty()) {
newFlowVar = DEFAULT_VARIABLE_NAME;
}
final DataType outType = computeOutputType(rules, outcomeColumnType);
final VariableProvider provider = new VariableProvider() {
@Override
public Object readVariable(final String name, final Class<?> type) {
return RuleEngineVariable2PortsNodeModel.this.readVariable(name, type);
}
@Override
@Deprecated
public int getRowCount() {
throw new IllegalStateException("Row count is not available.");
}
@Override
public long getRowCountLong() {
throw new IllegalStateException("Row count is not available.");
}
@Override
@Deprecated
public int getRowIndex() {
throw new IllegalStateException("Row index is not available.");
}
@Override
public long getRowIndexLong() {
throw new IllegalStateException("Row index is not available.");
}
};
boolean wasMatch = false;
for (Rule r : rules) {
if (r.getCondition().matches(null, provider).getOutcome() == MatchState.matchedAndStop) {
Outcome outcome2 = r.getOutcome();
// r.getSideEffect().perform(row, this);
final DataCell cell = (DataCell) outcome2.getComputedResult(null, provider);
wasMatch = true;
if (outType.equals(StringCell.TYPE) && !cell.isMissing() && !cell.getType().equals(StringCell.TYPE)) {
pushFlowVariableString(newFlowVar, cell.toString());
break;
} else {
if (cell.isMissing()) {
throw new UnsupportedOperationException("Missing result, TODO");
}
if (outType.equals(IntCell.TYPE)) {
pushFlowVariableInt(newFlowVar, ((IntValue) cell).getIntValue());
break;
} else if (outType.equals(DoubleCell.TYPE)) {
pushFlowVariableDouble(newFlowVar, ((DoubleValue) cell).getDoubleValue());
break;
} else if (outType.equals(StringCell.TYPE)) {
pushFlowVariableString(newFlowVar, ((StringValue) cell).getStringValue());
break;
} else {
// TODO
throw new UnsupportedOperationException("Wrong type: " + cell.getClass());
}
}
}
}
if (!wasMatch) {
if (outType.equals(StringCell.TYPE)) {
pushFlowVariableString(newFlowVar, "");
} else if (outType.equals(IntCell.TYPE)) {
pushFlowVariableInt(newFlowVar, 0);
} else {
pushFlowVariableDouble(newFlowVar, 0.0);
}
}
}
use of org.knime.core.data.DataCell in project knime-core by knime.
the class MovingAverageNodeModel method addMissingCells.
/**
* adds a fixed number of missing cells to the given row.
* or replaces the original ones with missings.
* @param colindexex
*/
private DataRow addMissingCells(final int nrMiss, final int[] colindexex, final DataRow oldrow) {
DataCell[] cell = new DataCell[oldrow.getNumCells() + nrMiss];
int counter = 0;
for (DataCell c : oldrow) {
cell[counter] = c;
counter++;
}
if (nrMiss <= 0) {
// we replace the colindexex with missings.
for (int i : colindexex) {
cell[i] = DataType.getMissingCell();
}
} else {
for (; counter < cell.length; counter++) {
cell[counter] = DataType.getMissingCell();
}
}
return new DefaultRow(oldrow.getKey(), cell);
}
use of org.knime.core.data.DataCell in project knime-core by knime.
the class NodeMonitorView method updateDataTable.
/*
* Put (static and simple) content of one output port table into table.
*/
private void updateDataTable(final NodeContainer nc, final int port) {
assert Display.getCurrent().getThread() == Thread.currentThread();
m_info.setText("Port Output");
m_table.removeAll();
for (TableColumn tc : m_table.getColumns()) {
tc.dispose();
}
// check if we can display something at all:
int index = port;
if (nc instanceof SingleNodeContainer) {
// we don't care about (hidden) variable OutPort
index++;
}
if (nc.getNrOutPorts() <= index) {
// no (real) port available
TableItem item = new TableItem(m_table, SWT.NONE);
item.setText(0, "No output ports");
return;
}
NodeOutPort nop = nc.getOutPort(index);
PortObject po = nop.getPortObject();
if ((po == null) || !(po instanceof BufferedDataTable)) {
// no table in port - ignore.
TableItem item = new TableItem(m_table, SWT.NONE);
item.setText(0, "Unknown or no PortObject");
return;
}
// retrieve table
BufferedDataTable bdt = (BufferedDataTable) po;
TableColumn column = new TableColumn(m_table, SWT.NONE);
column.setText("ID");
for (int i = 0; i < bdt.getDataTableSpec().getNumColumns(); i++) {
column = new TableColumn(m_table, SWT.NONE);
column.setText(bdt.getDataTableSpec().getColumnSpec(i).getName());
}
int rowIndex = 0;
Iterator<DataRow> rowIt = bdt.iteratorFailProve();
while (rowIndex < 42 && rowIt.hasNext()) {
DataRow thisRow = rowIt.next();
TableItem item = new TableItem(m_table, SWT.NONE);
item.setText(0, thisRow.getKey().getString());
for (int i = 0; i < thisRow.getNumCells(); i++) {
DataCell c = thisRow.getCell(i);
String s = c.toString().replaceAll("\\p{Cntrl}", "_");
item.setText(i + 1, s);
}
rowIndex++;
}
for (int i = 0; i < m_table.getColumnCount(); i++) {
m_table.getColumn(i).pack();
}
}
Aggregations