Search in sources :

Example 26 with ModelContentRO

use of org.knime.core.node.ModelContentRO in project knime-core by knime.

the class PortUtil method readObjectFromStream.

public static PortObject readObjectFromStream(final InputStream input, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    try (ZipInputStream in = new ZipInputStream(new BufferedInputStream(input))) {
        ZipEntry entry = in.getNextEntry();
        if (!"content.xml".equals(entry.getName())) {
            throw new IOException("Invalid stream, expected zip entry \"content.xml\", got \"" + entry.getName() + "\"");
        }
        ModelContentRO toc = ModelContent.loadFromXML(new NonClosableInputStream.Zip(in));
        String specClassName = toc.getString("port_spec_class");
        String objClassName = toc.getString("port_object_class");
        // reads "objectSpec.file"
        PortObjectSpec spec = readObjectSpec(specClassName, in);
        entry = in.getNextEntry();
        if (!"object.file".equals(entry.getName())) {
            throw new IOException("Invalid stream, expected zip entry \"object.file\", got \"" + entry.getName() + "\"");
        }
        Class<?> cl;
        try {
            cl = Class.forName(objClassName);
        } catch (ClassNotFoundException e) {
            throw new IOException("Can't load class \"" + specClassName + "\"", e);
        }
        if (!PortObject.class.isAssignableFrom(cl)) {
            throw new IOException("Class \"" + cl.getSimpleName() + "\" does not a sub-class \"" + PortObject.class.getSimpleName() + "\"");
        }
        PortObject portObject;
        try (PortObjectZipInputStream objIn = getPortObjectZipInputStream(new NonClosableInputStream.Zip(in))) {
            PortObjectSerializer<?> objSer = PortTypeRegistry.getInstance().getObjectSerializer(cl.asSubclass(PortObject.class)).get();
            portObject = objSer.loadPortObject(objIn, spec, exec);
        }
        if (portObject instanceof FileStorePortObject) {
            ModelContentRO fileStoreModelContent = toc.getModelContent("filestores");
            UUID iFileStoreHandlerUUID = UUID.fromString(fileStoreModelContent.getString("handlerUUID"));
            ModelContentRO fileStoreKeysModel = fileStoreModelContent.getModelContent("port_file_store_keys");
            List<FileStoreKey> fileStoreKeys = new ArrayList<>();
            for (String key : fileStoreKeysModel.keySet()) {
                fileStoreKeys.add(FileStoreKey.load(fileStoreKeysModel.getModelContent(key)));
            }
            NotInWorkflowWriteFileStoreHandler notInWorkflowFSHandler = new NotInWorkflowWriteFileStoreHandler(iFileStoreHandlerUUID);
            entry = in.getNextEntry();
            if (entry != null && "filestores/".equals(entry.getName())) {
                File fileStoreDir = FileUtil.createTempDir("knime_fs_" + cl.getSimpleName() + "-");
                FileUtil.unzip(in, fileStoreDir, 1);
                notInWorkflowFSHandler.setBaseDir(fileStoreDir);
            }
            FileStoreUtil.retrieveFileStoreHandlerFrom((FileStorePortObject) portObject, fileStoreKeys, notInWorkflowFSHandler.getFileStoreHandlerRepository());
        }
        return portObject;
    } catch (InvalidSettingsException ex) {
        throw new IOException("Unable to parse content.xml in port object file", ex);
    }
}
Also used : ModelContentRO(org.knime.core.node.ModelContentRO) NotInWorkflowWriteFileStoreHandler(org.knime.core.data.filestore.internal.NotInWorkflowWriteFileStoreHandler) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) FileStoreKey(org.knime.core.data.filestore.FileStoreKey) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NonClosableInputStream(org.knime.core.data.util.NonClosableInputStream) UUID(java.util.UUID) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) File(java.io.File)

Example 27 with ModelContentRO

use of org.knime.core.node.ModelContentRO in project knime-core by knime.

the class PortUtil method readObjectSpecFromStream.

/**
 * Read spec from stream. Argument will be wrapped in zip input stream, the spec is extracted and the stream is
 * closed.
 *
 * @param stream to read from.
 * @return The spec.
 * @throws IOException IO problems and unexpected content.
 * @since 2.6
 */
public static PortObjectSpec readObjectSpecFromStream(final InputStream stream) throws IOException {
    try (ZipInputStream in = new ZipInputStream(stream)) {
        ZipEntry entry = in.getNextEntry();
        if (entry == null) {
            throw new IOException("Invalid file: No zip entry found");
        }
        if (!"content.xml".equals(entry.getName())) {
            throw new IOException("Invalid stream, expected zip entry \"content.xml\", got \"" + entry.getName() + "\"");
        }
        ModelContentRO toc = ModelContent.loadFromXML(new NonClosableInputStream.Zip(in));
        String specClassName;
        try {
            specClassName = toc.getString("port_spec_class");
        } catch (InvalidSettingsException e1) {
            throw new IOException("Can't parse content file", e1);
        }
        return readObjectSpec(specClassName, in);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ModelContentRO(org.knime.core.node.ModelContentRO) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NonClosableInputStream(org.knime.core.data.util.NonClosableInputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 28 with ModelContentRO

use of org.knime.core.node.ModelContentRO in project knime-core by knime.

the class NaiveBayesPortObjectSpec method load.

/**
 * {@inheritDoc}
 */
@Override
protected void load(final ModelContentRO model) throws InvalidSettingsException {
    final Config specModel = model.getConfig(CNFG_SPEC);
    m_tableSpec = DataTableSpec.load(specModel);
    final ModelContentRO classColModel = model.getModelContent(CNFG_CLASS_COL);
    m_classColumn = DataColumnSpec.load(classColModel);
}
Also used : ModelContentRO(org.knime.core.node.ModelContentRO) Config(org.knime.core.node.config.Config)

Example 29 with ModelContentRO

use of org.knime.core.node.ModelContentRO in project knime-core by knime.

the class DecTreePredictorNodeModel method loadInternals.

/**
 * Load internals.
 *
 * @param nodeInternDir The intern node directory to load tree from.
 * @param exec Used to report progress or cancel saving.
 * @throws IOException Always, since this method has not been implemented
 *             yet.
 * @see org.knime.core.node.NodeModel
 *      #loadInternals(java.io.File,ExecutionMonitor)
 */
@Override
protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException {
    // read the decision tree
    File internalsFile = new File(nodeInternDir, INTERNALS_FILE_NAME);
    if (!internalsFile.exists()) {
        // file to load internals from not available
        setWarningMessage("Internal model could not be loaded.");
        return;
    }
    BufferedInputStream in2 = new BufferedInputStream(new GZIPInputStream(new FileInputStream(internalsFile)));
    ModelContentRO binModel = ModelContent.loadFromXML(in2);
    try {
        m_decTree = new DecisionTree(binModel);
    } catch (InvalidSettingsException ise) {
        LOGGER.warn("Model (internals) could not be loaded.", ise);
        setWarningMessage("Internal model could not be loaded.");
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) ModelContentRO(org.knime.core.node.ModelContentRO) BufferedInputStream(java.io.BufferedInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 30 with ModelContentRO

use of org.knime.core.node.ModelContentRO in project knime-core by knime.

the class LinRegLearnerNodeModel method loadInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void loadInternals(final File internDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    File inFile = new File(internDir, FILE_SAVE);
    ModelContentRO c = ModelContent.loadFromXML(new BufferedInputStream(new GZIPInputStream(new FileInputStream(inFile))));
    try {
        m_nrRows = c.getInt(CFG_NR_ROWS);
        m_nrRowsSkipped = c.getInt(CFG_NR_ROWS_SKIPPED);
        m_error = c.getDouble(CFG_ERROR);
        ModelContentRO specContent = c.getModelContent(CFG_SPEC);
        DataTableSpec outSpec = DataTableSpec.load(specContent);
        m_actualUsedColumns = specContent.getStringArray(CFG_USED_COLUMNS, (String[]) null);
        ModelContentRO parContent = c.getModelContent(CFG_PARAMS);
        m_params = LinearRegressionContent.instantiateAndLoad(parContent, outSpec);
    } catch (InvalidSettingsException ise) {
        IOException ioe = new IOException("Unable to restore state: " + ise.getMessage());
        ioe.initCause(ise);
        throw ioe;
    }
    File dataFile = new File(internDir, FILE_DATA);
    ContainerTable t = DataContainer.readFromZip(dataFile);
    int rowCount = t.getRowCount();
    m_rowContainer = new DefaultDataArray(t, 1, rowCount, exec);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) DataTableSpec(org.knime.core.data.DataTableSpec) ModelContentRO(org.knime.core.node.ModelContentRO) BufferedInputStream(java.io.BufferedInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) ContainerTable(org.knime.core.data.container.ContainerTable)

Aggregations

ModelContentRO (org.knime.core.node.ModelContentRO)32 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)19 IOException (java.io.IOException)16 File (java.io.File)15 FileInputStream (java.io.FileInputStream)14 BufferedInputStream (java.io.BufferedInputStream)12 GZIPInputStream (java.util.zip.GZIPInputStream)11 DataTableSpec (org.knime.core.data.DataTableSpec)6 DecisionTree (org.knime.base.node.mine.decisiontree2.model.DecisionTree)5 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)5 ArrayList (java.util.ArrayList)3 ZipEntry (java.util.zip.ZipEntry)3 DefaultDataArray (org.knime.base.node.util.DefaultDataArray)3 ZipInputStream (java.util.zip.ZipInputStream)2 GroupKey (org.knime.base.node.preproc.groupby.GroupKey)2 ContainerTable (org.knime.core.data.container.ContainerTable)2 NonClosableInputStream (org.knime.core.data.util.NonClosableInputStream)2 ObjectInputStream (java.io.ObjectInputStream)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1