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);
}
}
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);
}
}
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);
}
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.");
}
}
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);
}
Aggregations