use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class RuleEngine2PortsNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
BufferedDataTable data = (BufferedDataTable) inData[DATA_PORT];
m_rowCount = data.size();
try {
Pair<ColumnRearranger, PortObject> rearrangerPair = createColumnRearranger((DataTableSpec) inData[DATA_PORT].getSpec(), new DataTableRowInput((DataTable) inData[RULE_PORT]));
BufferedDataTable predictedTable = exec.createColumnRearrangeTable(data, rearrangerPair.getFirst(), exec);
PortObject second = rearrangerPair.getSecond();
if (m_settings.isPMMLRuleSet()) {
if (m_settings.isProvideStatistics()) {
PMMLPortObject po = (PMMLPortObject) rearrangerPair.getSecond();
PMMLPortObject pmmlPortObject = new PMMLPortObject(m_copy.getSpec(), po);
// Remove extra model.
pmmlPortObject.addModelTranslater(new PMMLTranslator() {
@Override
public void initializeFrom(final PMMLDocument pmmlDoc) {
}
@Override
public SchemaType exportTo(final PMMLDocument pmmlDoc, final PMMLPortObjectSpec spec) {
return null;
}
});
second = pmmlPortObject;
} else {
second = m_copy;
}
}
return new PortObject[] { predictedTable, second };
} finally {
m_rowCount = -1;
}
}
use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class RuleEngine2PortsNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
private StreamInternalWithPortObject m_internals;
/**
* {@inheritDoc}
*/
@Override
public void loadInternals(final StreamableOperatorInternals internals) {
m_internals = (StreamInternalWithPortObject) internals;
}
/**
* {@inheritDoc}
*/
@Override
public void runIntermediate(final PortInput[] inputs, final ExecutionContext exec) throws Exception {
// count number of rows
long count = 0;
final RowInput rowInput = (RowInput) inputs[DATA_PORT];
while (rowInput.poll() != null) {
count++;
}
if (inputs[RULE_PORT] instanceof RowInput) {
final RowInput ruleInput = (RowInput) inputs[RULE_PORT];
final Pair<ColumnRearranger, PortObject> pair = createColumnRearranger(rowInput.getDataTableSpec(), ruleInput);
final ColumnRearranger rearranger = pair.getFirst();
final DataTableSpec spec = rearranger.createSpec();
m_internals.setTableSpec(spec);
if (pair.getSecond() instanceof PMMLPortObject) {
PMMLPortObject po = (PMMLPortObject) pair.getSecond();
m_internals.setObject(po);
} else {
m_internals.setObject(null);
}
}
m_internals.setRowCount(count);
}
/**
* {@inheritDoc}
*/
@Override
public StreamableOperatorInternals saveInternals() {
return m_internals;
}
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
if (m_internals.getTableSpec() != null) {
m_rowCount = m_internals.getRowCount();
}
final Pair<ColumnRearranger, PortObject> pair = createColumnRearranger((DataTableSpec) inSpecs[DATA_PORT], (RowInput) inputs[RULE_PORT]);
pair.getFirst().createStreamableFunction(0, 0).runFinal(inputs, outputs, exec);
if (pair.getSecond() != null) {
((PortObjectOutput) outputs[1]).setPortObject(pair.getSecond());
}
}
};
}
use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class FromDecisionTreeNodeModel method execute.
/**
* {@inheritDoc}
* @throws CanceledExecutionException Execution cancelled.
* @throws InvalidSettingsException No or more than one RuleSet model is in the PMML input.
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, InvalidSettingsException {
PMMLPortObject decTreeModel = (PMMLPortObject) inData[0];
PMMLDecisionTreeTranslator treeTranslator = new PMMLDecisionTreeTranslator();
decTreeModel.initializeModelTranslator(treeTranslator);
DecisionTree decisionTree = treeTranslator.getDecisionTree();
decisionTree.getRootNode();
PMMLPortObject ruleSetModel = new PMMLPortObject(decTreeModel.getSpec());
PMMLDocument document = PMMLDocument.Factory.newInstance();
PMML pmml = document.addNewPMML();
PMMLPortObjectSpec.writeHeader(pmml);
pmml.setVersion(PMMLPortObject.PMML_V4_2);
new PMMLDataDictionaryTranslator().exportTo(document, decTreeModel.getSpec());
RuleSetModel newRuleSetModel = pmml.addNewRuleSetModel();
PMMLMiningSchemaTranslator.writeMiningSchema(decTreeModel.getSpec(), newRuleSetModel);
newRuleSetModel.setFunctionName(MININGFUNCTION.CLASSIFICATION);
newRuleSetModel.setAlgorithmName("RuleSet");
RuleSet ruleSet = newRuleSetModel.addNewRuleSet();
ruleSet.addNewRuleSelectionMethod().setCriterion(Criterion.FIRST_HIT);
addRules(ruleSet, new ArrayList<DecisionTreeNode>(), decisionTree.getRootNode());
// TODO: Return a BufferedDataTable for each output port
PMMLPortObject pmmlPortObject = new PMMLPortObject(ruleSetModel.getSpec(), document);
return new PortObject[] { pmmlPortObject, new RuleSetToTable(m_rulesToTable).execute(exec, pmmlPortObject) };
}
use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class PMMLRuleEditorNodeModel method finishStreamableExecution.
/**
* {@inheritDoc}
*/
@Override
public void finishStreamableExecution(final StreamableOperatorInternals internals, final ExecutionContext exec, final PortOutput[] output) throws Exception {
final StreamInternalForPMMLPortObject poInternals = (StreamInternalForPMMLPortObject) internals;
PMMLPortObject ret = poInternals.getObject();
ret.validate();
((PortObjectOutput) output[1]).setPortObject(ret);
}
use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class PMMLRuleEditorNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
final DataTableSpec tableSpec = (DataTableSpec) inSpecs[0];
return new StreamableOperator() {
private ColumnRearranger m_rearrangerx;
private PMMLPortObject m_portObject;
{
try {
final PMMLDocument doc = PMMLDocument.Factory.newInstance();
final PMML pmml = doc.addNewPMML();
RuleSetModel ruleSetModel = pmml.addNewRuleSetModel();
RuleSet ruleSet = ruleSetModel.addNewRuleSet();
PMMLRuleParser parser = new PMMLRuleParser(tableSpec, getAvailableInputFlowVariables());
m_rearrangerx = createRearranger(tableSpec, ruleSet, parser);
} catch (ParseException e) {
throw new InvalidSettingsException(e);
}
}
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
m_rearrangerx.createStreamableFunction(0, 0).runFinal(inputs, outputs, exec);
}
/**
* {@inheritDoc}
*/
@Override
public void loadInternals(final StreamableOperatorInternals internals) {
super.loadInternals(internals);
m_portObject = ((StreamInternalForPMMLPortObject) internals).getObject();
}
/**
* {@inheritDoc}
*/
@Override
public StreamableOperatorInternals saveInternals() {
return createInitialStreamableOperatorInternals().setObject(m_portObject);
}
};
}
Aggregations