Search in sources :

Example 1 with SchemaType

use of org.apache.xmlbeans.SchemaType 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;
    }
}
Also used : BufferedDataTable(org.knime.core.node.BufferedDataTable) DataTable(org.knime.core.data.DataTable) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) PMMLDocument(org.dmg.pmml.PMMLDocument) PMMLTranslator(org.knime.core.node.port.pmml.PMMLTranslator) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) SchemaType(org.apache.xmlbeans.SchemaType)

Example 2 with SchemaType

use of org.apache.xmlbeans.SchemaType in project knime-core by knime.

the class AbstractTreeModelPMMLTranslator method exportTo.

/**
 * {@inheritDoc}
 */
@Override
public SchemaType exportTo(final PMMLDocument pmmlDoc, final PMMLPortObjectSpec spec) {
    PMML pmml = pmmlDoc.getPMML();
    TreeModelDocument.TreeModel treeModel = pmml.addNewTreeModel();
    AbstractTreeModelExporter<N> exporter = createExporter(new DerivedFieldMapper(pmmlDoc));
    SchemaType st = exporter.writeModelToPMML(treeModel, spec);
    if (exporter.hasWarning()) {
        addWarning(exporter.getWarning());
    }
    return st;
}
Also used : DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) PMML(org.dmg.pmml.PMMLDocument.PMML) TreeModel(org.dmg.pmml.TreeModelDocument.TreeModel) TreeModelDocument(org.dmg.pmml.TreeModelDocument) SchemaType(org.apache.xmlbeans.SchemaType)

Example 3 with SchemaType

use of org.apache.xmlbeans.SchemaType in project knime-core by knime.

the class PMMLPortObject method addModelTranslater.

/**
 * Adds the model of the content translater to the PMML document.
 * @param modelTranslator the model translator containing the model to be
 *      added
 */
public void addModelTranslater(final PMMLTranslator modelTranslator) {
    SchemaType type = modelTranslator.exportTo(m_pmmlDoc, m_spec);
    LocalTransformations localTransformations = moveDerivedFields(type);
    /* Remove mining fields from mining schema that where created as a
         * derived field. In KNIME the origin of columns is not distinguished
         * and all columns are added to the mining schema. But in PMML this
         * results in duplicate entries. Those columns should only appear once
         * as derived field in the transformation dictionary or local
         * transformations. */
    Set<String> derivedFields = new HashSet<String>();
    for (DerivedField derivedField : getDerivedFields()) {
        derivedFields.add(derivedField.getName());
    }
    MiningSchema miningSchema = PMMLUtils.getFirstMiningSchema(m_pmmlDoc, type);
    if (miningSchema == null) {
        LOGGER.info("No mining schema found.");
        return;
    }
    MiningField[] miningFieldArray = miningSchema.getMiningFieldArray();
    List<MiningField> miningFields = new ArrayList<MiningField>(Arrays.asList(miningFieldArray));
    Set<String> miningFieldNames = new HashSet<String>();
    for (MiningField miningField : miningFieldArray) {
        String miningFieldName = miningField.getName();
        if (derivedFields.contains(miningFieldName)) {
            LOGGER.debug("Removing field \"" + miningFieldName + "\" from MiningFields as it is a DerivedField.");
            miningFields.remove(miningField);
        } else {
            miningFieldNames.add(miningFieldName);
        }
    }
    /* According to the PMML Spec DerivedFields must ultimately refer back
         * to active MiningFields of the model's MiningSchema. Therefore we
         * have to add all referred DataFields to the MiningSchema. */
    String fullPath = NAMESPACE_DECLARATION + "$this/pmml:DerivedField/*/@field" + "| $this/pmml:DerivedField//pmml:FieldColumnPair/@field";
    XmlObject[] xmlDescendants = localTransformations.selectPath(fullPath);
    Set<String> referencedFields = new LinkedHashSet<String>();
    // collect all referred field names
    for (XmlObject xo : xmlDescendants) {
        XmlCursor xmlCursor = xo.newCursor();
        referencedFields.add(xmlCursor.getTextValue());
        xmlCursor.dispose();
    }
    for (String referencedField : referencedFields) {
        if (!derivedFields.contains(referencedField) && !miningFieldNames.contains(referencedField)) {
            /* Add them to the mining schema if they are not already
                 * contained there and if they don't refer to derived fields. */
            MiningField miningField = MiningField.Factory.newInstance();
            miningField.setName(referencedField);
            miningField.setInvalidValueTreatment(INVALIDVALUETREATMENTMETHOD.AS_IS);
            LOGGER.debug("Adding field \"" + referencedField + "\" to MiningSchema because it is referenced in " + "LocalTransformations.");
            miningFields.add(miningField);
        }
    }
    miningSchema.setMiningFieldArray(miningFields.toArray(new MiningField[0]));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MiningField(org.dmg.pmml.MiningFieldDocument.MiningField) ArrayList(java.util.ArrayList) SchemaType(org.apache.xmlbeans.SchemaType) XmlCursor(org.apache.xmlbeans.XmlCursor) LocalTransformations(org.dmg.pmml.LocalTransformationsDocument.LocalTransformations) MiningSchema(org.dmg.pmml.MiningSchemaDocument.MiningSchema) XmlObject(org.apache.xmlbeans.XmlObject) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with SchemaType

use of org.apache.xmlbeans.SchemaType in project knime-core by knime.

the class AbstractGBTModelPMMLTranslator method exportTo.

/**
 * {@inheritDoc}
 */
@Override
public SchemaType exportTo(final PMMLDocument pmmlDoc, final PMMLPortObjectSpec spec) {
    PMML pmml = pmmlDoc.getPMML();
    AbstractGBTModelExporter<M> exporter = createExporter(new DerivedFieldMapper(pmmlDoc));
    SchemaType st = exporter.writeModelToPMML(pmml.addNewMiningModel(), spec);
    if (exporter.hasWarning()) {
        addWarning(exporter.getWarning());
    }
    return st;
}
Also used : DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) PMML(org.dmg.pmml.PMMLDocument.PMML) SchemaType(org.apache.xmlbeans.SchemaType)

Example 5 with SchemaType

use of org.apache.xmlbeans.SchemaType in project arctic-sea by 52North.

the class SensorMLEncoderv101 method createComponents.

/**
 * Creates the components section of the SensorML description.
 *
 * @param sosComponents
 *            SOS SWE representation.
 *
 * @return encoded sml:components
 *
 * @throws EncodingException
 *             if the encoding fails
 */
private Components createComponents(List<SmlComponent> sosComponents) throws EncodingException {
    Components components = Components.Factory.newInstance(getXmlOptions());
    ComponentList componentList = components.addNewComponentList();
    for (SmlComponent sosSMLComponent : sosComponents) {
        Component component = componentList.addNewComponent();
        if (sosSMLComponent.getName() != null) {
            component.setName(sosSMLComponent.getName());
        }
        if (sosSMLComponent.getHref() != null) {
            component.setHref(sosSMLComponent.getHref());
            if (sosSMLComponent.getTitle() != null) {
                component.setTitle(sosSMLComponent.getTitle());
            }
        } else if (sosSMLComponent.getProcess() != null) {
            XmlObject xmlObject = null;
            if (sosSMLComponent.getProcess().isSetXml()) {
                try {
                    xmlObject = XmlObject.Factory.parse(sosSMLComponent.getProcess().getXml());
                } catch (XmlException xmle) {
                    throw new EncodingException("Error while encoding SensorML child procedure description " + "from stored SensorML encoded sensor description with XMLBeans", xmle);
                }
            } else {
                if (sosSMLComponent.getProcess() instanceof SensorML) {
                    xmlObject = createSensorDescriptionFromObject(((SensorML) sosSMLComponent.getProcess()).getMembers().iterator().next());
                } else if (sosSMLComponent.getProcess() instanceof AbstractProcess) {
                    xmlObject = createSensorDescriptionFromObject(sosSMLComponent.getProcess());
                }
            }
            if (xmlObject != null) {
                AbstractProcessType xbProcess = null;
                if (xmlObject instanceof SensorMLDocument) {
                    final SensorMLDocument smlDoc = (SensorMLDocument) xmlObject;
                    for (final Member member : smlDoc.getSensorML().getMemberArray()) {
                        xbProcess = member.getProcess();
                        break;
                    }
                } else if (xmlObject instanceof AbstractProcessType) {
                    xbProcess = (AbstractProcessType) xmlObject;
                }
                if (xbProcess == null) {
                    throw new EncodingException("The sensor type is not supported by this SOS");
                }
                // TODO add feature/parentProcs/childProcs to component - is
                // this already done?
                SchemaType schemaType = xbProcess.schemaType();
                component.addNewProcess().substitute(getQnameForType(schemaType), schemaType).set(xbProcess);
            }
        }
    }
    return components;
}
Also used : AbstractProcessType(net.opengis.sensorML.x101.AbstractProcessType) EncodingException(org.n52.svalbard.encode.exception.EncodingException) AbstractProcess(org.n52.shetland.ogc.sensorML.AbstractProcess) ComponentList(net.opengis.sensorML.x101.ComponentsDocument.Components.ComponentList) SensorML(org.n52.shetland.ogc.sensorML.SensorML) AbstractSensorML(org.n52.shetland.ogc.sensorML.AbstractSensorML) SchemaType(org.apache.xmlbeans.SchemaType) Components(net.opengis.sensorML.x101.ComponentsDocument.Components) SensorMLDocument(net.opengis.sensorML.x101.SensorMLDocument) XmlException(org.apache.xmlbeans.XmlException) XmlObject(org.apache.xmlbeans.XmlObject) SmlComponent(org.n52.shetland.ogc.sensorML.elements.SmlComponent) Component(net.opengis.sensorML.x101.ComponentsDocument.Components.ComponentList.Component) SweAbstractDataComponent(org.n52.shetland.ogc.swe.SweAbstractDataComponent) Member(net.opengis.sensorML.x101.SensorMLDocument.SensorML.Member) SmlComponent(org.n52.shetland.ogc.sensorML.elements.SmlComponent)

Aggregations

SchemaType (org.apache.xmlbeans.SchemaType)6 HashSet (java.util.HashSet)2 XmlObject (org.apache.xmlbeans.XmlObject)2 MiningField (org.dmg.pmml.MiningFieldDocument.MiningField)2 MiningSchema (org.dmg.pmml.MiningSchemaDocument.MiningSchema)2 PMML (org.dmg.pmml.PMMLDocument.PMML)2 TreeModel (org.dmg.pmml.TreeModelDocument.TreeModel)2 DerivedFieldMapper (org.knime.core.node.port.pmml.preproc.DerivedFieldMapper)2 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 AbstractProcessType (net.opengis.sensorML.x101.AbstractProcessType)1 Components (net.opengis.sensorML.x101.ComponentsDocument.Components)1 ComponentList (net.opengis.sensorML.x101.ComponentsDocument.Components.ComponentList)1 Component (net.opengis.sensorML.x101.ComponentsDocument.Components.ComponentList.Component)1 SensorMLDocument (net.opengis.sensorML.x101.SensorMLDocument)1 Member (net.opengis.sensorML.x101.SensorMLDocument.SensorML.Member)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlException (org.apache.xmlbeans.XmlException)1 AssociationModel (org.dmg.pmml.AssociationModelDocument.AssociationModel)1 DerivedField (org.dmg.pmml.DerivedFieldDocument.DerivedField)1