use of org.knime.base.node.rules.engine.Rule in project knime-core by knime.
the class RuleEngine2PortsNodeModel method computeRearrangerWithoutPMML.
/**
* @param spec
* @param rules
* @param flowVars
* @param ruleIdx
* @param outcomeIdx
* @param outputColumnName
* @return
* @throws InterruptedException
* @throws InvalidSettingsException
*/
private Pair<ColumnRearranger, PortObject> computeRearrangerWithoutPMML(final DataTableSpec spec, final RowInput rules, final Map<String, FlowVariable> flowVars, final int ruleIdx, final int outcomeIdx, final String outputColumnName) throws InterruptedException, InvalidSettingsException {
PortObject po;
ColumnRearranger ret;
RuleFactory factory = RuleFactory.getInstance(RuleNodeSettings.RuleEngine).cloned();
po = InactiveBranchPortObject.INSTANCE;
ret = new ColumnRearranger(spec);
factory.disableMissingComparisons();
factory.disableNaNComparisons();
final List<Rule> ruleList = new ArrayList<>();
int lineNo = 0;
DataRow ruleRow;
while ((ruleRow = rules.poll()) != null) {
lineNo++;
DataCell cell = ruleRow.getCell(ruleIdx);
CheckUtils.checkSetting(!cell.isMissing(), "Missing rule in row: " + ruleRow.getKey());
if (cell instanceof StringValue) {
StringValue sv = (StringValue) cell;
String ruleText = sv.getStringValue();
if (outcomeIdx >= 0) {
try {
ruleText += " => " + m_settings.asStringFailForMissing(ruleRow.getCell(outcomeIdx));
} catch (InvalidSettingsException e) {
if (RuleSupport.isComment(ruleText)) {
ruleText += " => ?";
} else {
throw e;
}
}
}
try {
Rule rule = factory.parse(ruleText, spec, flowVars);
if (rule.getCondition().isEnabled()) {
ruleList.add(rule);
}
} catch (ParseException e) {
ParseException error = Util.addContext(e, ruleText, lineNo);
throw new InvalidSettingsException("Wrong rule in line: " + ruleRow.getKey() + "\n" + error.getMessage(), error);
}
} else {
CheckUtils.checkSetting(false, "Wrong type (" + cell.getType() + ") of rule: " + cell + "\nin row: " + ruleRow.getKey());
}
}
// unfortunately we cannot compute the domain and limits of the output column.
final DataType outType = RuleEngineNodeModel.computeOutputType(ruleList, computeOutcomeType(rules.getDataTableSpec()), RuleNodeSettings.RuleEngine, m_settings.isDisallowLongOutputForCompatibility());
final MutableLong rowIndex = new MutableLong();
final ExecutionMonitor exec = new ExecutionMonitor();
final boolean disallowLongOutputForCompatibility = m_settings.isDisallowLongOutputForCompatibility();
VariableProvider.SingleCellFactoryProto fac = new VariableProvider.SingleCellFactoryProto(new DataColumnSpecCreator(outputColumnName, outType).createSpec()) {
@Override
public DataCell getCell(final DataRow row) {
setProgress(rowIndex.longValue(), m_rowCount, row.getKey(), exec);
rowIndex.increment();
return RuleEngineNodeModel.getRulesOutcome(outType, row, ruleList, disallowLongOutputForCompatibility, this);
}
@Override
public Object readVariable(final String name, final Class<?> type) {
return RuleEngine2PortsNodeModel.this.readVariable(name, type);
}
@Deprecated
@Override
public int getRowCount() {
return RuleEngine2PortsNodeModel.this.getRowCount();
}
@Override
public long getRowCountLong() {
return RuleEngine2PortsNodeModel.this.getRowCountLong();
}
};
if (m_settings.isReplaceColumn()) {
ret.replace(fac, outputColumnName);
} else {
ret.append(fac);
}
return Pair.create(ret, po);
}
use of org.knime.base.node.rules.engine.Rule in project knime-core by knime.
the class RuleEngineVariable2PortsNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
BufferedDataTable ruleTable = (BufferedDataTable) inData[RuleEngine2PortsNodeModel.RULE_PORT];
List<Rule> rules = parseRules(ruleTable);
performExecute(rules, outcomeColumnType(ruleTable));
return new PortObject[] { FlowVariablePortObject.INSTANCE };
}
use of org.knime.base.node.rules.engine.Rule in project knime-core by knime.
the class RuleEngineVariable2PortsNodeModel method parseRules.
/**
* Parses all rules from {@code rulesTable}.
*
* @param rulesTable The input rules table.
* @return a list of parsed rules
* @throws ParseException if a rule cannot be parsed
* @throws InvalidSettingsException Missing values in outcomes are not supported.
*/
protected List<Rule> parseRules(final BufferedDataTable rulesTable) throws ParseException, InvalidSettingsException {
ArrayList<Rule> rules = new ArrayList<Rule>();
final Map<String, FlowVariable> availableFlowVariables = getAvailableFlowVariables();
final RuleFactory factory = RuleFactory.getInstance(RuleNodeSettings.VariableRule).cloned();
factory.disableNaNComparisons();
final DataTableSpec spec = new DataTableSpec();
int line = 0;
for (String s : rules(rulesTable, m_settings, RuleNodeSettings.VariableRule)) {
++line;
try {
final Rule rule = factory.parse(s, spec, availableFlowVariables);
if (rule.getCondition().isEnabled()) {
rules.add(rule);
}
} catch (ParseException e) {
throw Util.addContext(e, s, line);
}
}
return rules;
}
Aggregations