use of org.knime.core.data.DataType in project knime-core by knime.
the class RuleSetToTable method execute.
/**
* Performs the conversion.
*
* @param exec An {@link ExecutionContext}.
* @param pmmlPo The input {@link PMMLPortObject}.
* @return The created {@link BufferedDataTable}.
* @throws CanceledExecutionException Execition was cancelled.
* @throws InvalidSettingsException No or more than one RuleSet model is in the PMML input.
*/
public BufferedDataTable execute(final ExecutionContext exec, final PMMLPortObject pmmlPo) throws CanceledExecutionException, InvalidSettingsException {
// TODO should the rule selection method be an output flow variable?
if (pmmlPo.getPMMLValue().getModels(PMMLModelType.RuleSetModel).size() != 1) {
throw new InvalidSettingsException("Only a single RuleSet model is supported.");
}
PMMLRuleTranslator ruleTranslator = new PMMLRuleTranslator();
pmmlPo.initializeModelTranslator(ruleTranslator);
List<Rule> rules = ruleTranslator.getRules();
final DataTableSpec confSpec = configure(pmmlPo.getSpec());
final List<String> scoreValues = new ArrayList<>();
final DataTableSpec properSpec = confSpec != null ? confSpec : properSpec(rules, scoreValues);
BufferedDataContainer container = exec.createDataContainer(properSpec);
List<DataColumnSpec> targetCols = pmmlPo.getSpec().getTargetCols();
DataType outcomeType = targetCols.get(0).getType();
long idx = 0L;
int rulesSize = rules.size();
Map<String, DataType> types = new LinkedHashMap<>();
for (DataColumnSpec col : pmmlPo.getSpec().getLearningCols()) {
types.put(col.getName(), col.getType());
}
for (Rule rule : rules) {
exec.checkCanceled();
exec.setProgress(1.0 * idx++ / rulesSize);
container.addRowToTable(new DefaultRow(RowKey.createRowKey(idx), createRow(rule, outcomeType, types, scoreValues)));
}
container.close();
return container.getTable();
}
use of org.knime.core.data.DataType 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.DataType in project knime-core by knime.
the class RuleEngineVariable2PortsNodeModel method computeOutputType.
/**
* Computes the result's {@link DataType}.
*
* @param rules The rules.
* @return The common base type of the outcomes.
*/
static DataType computeOutputType(final List<Rule> rules, final DataType columnType) {
// determine output type
List<DataType> types = new ArrayList<DataType>();
// add outcome column types
for (Rule r : rules) {
types.add(r.getOutcome().getType());
}
final DataType outType;
if (types.size() > 0) {
DataType temp = types.get(0);
for (int i = 1; i < types.size(); i++) {
temp = DataType.getCommonSuperType(temp, types.get(i));
}
if ((temp.getValueClasses().size() == 1) && temp.getValueClasses().contains(DataValue.class)) {
// a non-native type, we replace it with string
temp = StringCell.TYPE;
}
outType = temp;
} else {
outType = columnType;
}
return outType;
}
use of org.knime.core.data.DataType 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.DataType in project knime-core by knime.
the class StringManipulationSettings method getNewColSpec.
/**
* The column spec of the generated column.
* @return The col spec.
* @throws InvalidSettingsException If settings are inconsistent.
*/
public DataColumnSpec getNewColSpec() throws InvalidSettingsException {
Class<?> returnType = getReturnType();
String colName = getColName();
boolean isArrayReturn = false;
DataType type = null;
for (JavaSnippetType<?, ?, ?> t : JavaSnippetType.TYPES) {
if (t.getJavaClass(false).equals(returnType)) {
type = t.getKNIMEDataType(isArrayReturn);
}
}
if (type == null) {
throw new InvalidSettingsException("Illegal return type: " + returnType.getName());
}
return new DataColumnSpecCreator(colName, type).createSpec();
}
Aggregations