use of org.dmg.pmml.PMMLDocument 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.dmg.pmml.PMMLDocument 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.dmg.pmml.PMMLDocument 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);
}
};
}
use of org.dmg.pmml.PMMLDocument in project knime-core by knime.
the class PMMLGeneralRegressionModelWrapper method createPMMLDocument.
/**
* {@inheritDoc}
*/
@Override
public PMMLDocument createPMMLDocument(final DataDictionary dataDict) {
PMMLDocument doc = createEmptyDocument(dataDict);
doc.getPMML().setGeneralRegressionModelArray(new GeneralRegressionModel[] { m_model });
return doc;
}
use of org.dmg.pmml.PMMLDocument in project knime-core by knime.
the class PMMLPortObject method loadFrom.
/**
* Initializes the pmml port object based on the xml input stream.
* @param spec the referring spec of this object
* @param is the pmml input stream
* @throws IOException if the file cannot be found
* @throws XmlException if something goes wrong during reading
*/
public void loadFrom(final PMMLPortObjectSpec spec, final InputStream is) throws IOException, XmlException {
// disallow close in the factory -- we had indeterministic behavior
// where close was called more than once (which should be OK) but as
// the argument input stream is a NonClosableZipInput, which delegates
// close to closeEntry(), we have to make sure that close is only
// called once.
// TODO: The document is read twice here. Could we "probe" into the file to check the version?
XmlObject xmlDoc = XmlObject.Factory.parse(new NonClosableInputStream(is));
is.close();
if (xmlDoc instanceof PMMLDocument) {
m_pmmlDoc = (PMMLDocument) xmlDoc;
} else {
/* Try to recover when reading a PMML 3.x/4.0 document that
* was produced by KNIME by just replacing the PMML version and
* namespace. */
if (PMMLUtils.isOldKNIMEPMML(xmlDoc) || PMMLUtils.is4_1PMML(xmlDoc)) {
try {
String updatedPMML = PMMLUtils.getUpdatedVersionAndNamespace(xmlDoc);
/* Parse the modified document and assign it to a
* PMMLDocument.*/
m_pmmlDoc = PMMLDocument.Factory.parse(updatedPMML);
} catch (Exception e) {
throw new RuntimeException("Parsing of PMML v 3.x/4.0 document failed.", e);
}
LOGGER.info("KNIME produced PMML 3.x/4.0 converted to PMML 4.1.");
} else {
throw new RuntimeException("Parsing of PMML v 3.x/4.0 document failed.");
}
}
m_spec = spec;
}
Aggregations