Search in sources :

Example 1 with PMMLRuleTranslator

use of org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator 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();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) ArrayList(java.util.ArrayList) PMMLRuleTranslator(org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator) LinkedHashMap(java.util.LinkedHashMap) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataType(org.knime.core.data.DataType) Rule(org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator.Rule) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 2 with PMMLRuleTranslator

use of org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator in project knime-core by knime.

the class RuleEngine2PortsNodeModel method setCondition.

/**
 * Initializes the xml {@link SimpleRule} based on the {@code expression}. After this call, the predicate will be
 * set.
 *
 * @param simpleRule An xml {@link SimpleRule}.
 * @param expression A condition to set.
 */
private void setCondition(final SimpleRule simpleRule, final PMMLPredicate expression) {
    PMMLRuleTranslator translator = new PMMLRuleTranslator();
    translator.setPredicate(simpleRule, expression);
}
Also used : PMMLRuleTranslator(org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator)

Example 3 with PMMLRuleTranslator

use of org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator in project knime-core by knime.

the class RulesToTableNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    PMMLPortObject ruleSetPo = (PMMLPortObject) inData[0];
    PMMLRuleTranslator translator = new PMMLRuleTranslator();
    BufferedDataTable table = new RuleSetToTable(m_settings).execute(exec, ruleSetPo);
    ruleSetPo.initializeModelTranslator(translator);
    List<RuleSelectionMethod> selectionMethodList = translator.getSelectionMethodList();
    if (selectionMethodList.size() != 1 || (selectionMethodList.size() == 1 && !Objects.equals(selectionMethodList.get(0).getCriterion(), RuleSelectionMethod.Criterion.FIRST_HIT))) {
        setWarningMessage("Only a single 'firstHit' rule selection method is supported properly, all others cannot be represented properly. Rule selection methods: " + selectionMethodList);
    }
    return new BufferedDataTable[] { table };
}
Also used : PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) RuleSelectionMethod(org.dmg.pmml.RuleSelectionMethodDocument.RuleSelectionMethod) PMMLRuleTranslator(org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator)

Example 4 with PMMLRuleTranslator

use of org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator in project knime-core by knime.

the class RuleEngine2PortsNodeModel method fillUsingDoc.

/**
 * Fills the results obtained from {@code doc}.
 *
 * @param doc A {@link PMMLDocument}.
 * @param ruleSetModel The {@link RuleSetModel}.
 * @param pmml The {@link PMMLPortObject}.
 */
private void fillUsingDoc(final PMMLDocument doc, final RuleSetModel ruleSetModel, final PMMLPortObject pmml) {
    PMMLRuleTranslator modelTranslator = new PMMLRuleTranslator(m_settings.isProvideStatistics());
    PMMLMiningSchemaTranslator.writeMiningSchema(pmml.getSpec(), ruleSetModel);
    PMMLDataDictionaryTranslator ddTranslator = new PMMLDataDictionaryTranslator();
    ddTranslator.exportTo(doc, pmml.getSpec());
    modelTranslator.initializeFrom(doc);
    pmml.addModelTranslater(modelTranslator);
    pmml.validate();
}
Also used : PMMLRuleTranslator(org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator) PMMLDataDictionaryTranslator(org.knime.core.node.port.pmml.PMMLDataDictionaryTranslator)

Aggregations

PMMLRuleTranslator (org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator)4 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 RuleSelectionMethod (org.dmg.pmml.RuleSelectionMethodDocument.RuleSelectionMethod)1 Rule (org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator.Rule)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 DataType (org.knime.core.data.DataType)1 DefaultRow (org.knime.core.data.def.DefaultRow)1 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)1 BufferedDataTable (org.knime.core.node.BufferedDataTable)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1 PMMLDataDictionaryTranslator (org.knime.core.node.port.pmml.PMMLDataDictionaryTranslator)1 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)1