Search in sources :

Example 1 with WorkflowFileStoreHandlerRepository

use of org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository in project knime-core by knime.

the class FileNativeNodeContainerPersistor method loadNCAndWashModelSettings.

/**
 * {@inheritDoc}
 */
@Override
NodeSettingsRO loadNCAndWashModelSettings(final NodeSettingsRO settingsForNode, final NodeSettingsRO modelSettings, final Map<Integer, BufferedDataTable> tblRep, final ExecutionMonitor exec, final LoadResult result) throws InvalidSettingsException, CanceledExecutionException, IOException {
    final FileNodePersistor nodePersistor = createNodePersistor(settingsForNode);
    nodePersistor.preLoad(m_node, result);
    NodeSettingsRO washedModelSettings = modelSettings;
    try {
        if (modelSettings != null) {
            // null if the node never had settings - no reason to load them
            m_node.validateModelSettings(modelSettings);
            m_node.loadModelSettingsFrom(modelSettings);
            // previous versions of KNIME (2.7 and before) kept the model settings only in the node;
            // NodeModel#saveSettingsTo was always called before the dialog was opened (some dialog implementations
            // rely on the exact structure of the NodeSettings ... which may change between versions).
            // We wash the settings through the node so that the model settings are updated (they possibly
            // no longer map to the variable settings loaded further down below - if so, the inconsistency
            // is warned later during configuration)
            NodeSettings washedSettings = new NodeSettings("model");
            m_node.saveModelSettingsTo(washedSettings);
            washedModelSettings = washedSettings;
        }
    } catch (Exception e) {
        final String error;
        if (e instanceof InvalidSettingsException) {
            error = "Loading model settings failed: " + e.getMessage();
        } else {
            error = "Caught \"" + e.getClass().getSimpleName() + "\", " + "Loading model settings failed: " + e.getMessage();
        }
        final LoadNodeModelSettingsFailPolicy pol = getModelSettingsFailPolicy(getMetaPersistor().getState(), nodePersistor.isInactive());
        switch(pol) {
            case IGNORE:
                if (!(e instanceof InvalidSettingsException)) {
                    getLogger().coding(error, e);
                }
                break;
            case FAIL:
                result.addError(error);
                m_node.createErrorMessageAndNotify(error, e);
                setNeedsResetAfterLoad();
                break;
            case WARN:
                m_node.createWarningMessageAndNotify(error, e);
                result.addWarning(error);
                setDirtyAfterLoad();
                break;
        }
    }
    try {
        HashMap<Integer, ContainerTable> globalTableRepository = getGlobalTableRepository();
        WorkflowFileStoreHandlerRepository fileStoreHandlerRepository = getFileStoreHandlerRepository();
        nodePersistor.load(m_node, getParentPersistor(), exec, tblRep, globalTableRepository, fileStoreHandlerRepository, result);
    } catch (final Exception e) {
        String error = "Error loading node content: " + e.getMessage();
        getLogger().warn(error, e);
        needsResetAfterLoad();
        result.addError(error);
    }
    if (nodePersistor.isDirtyAfterLoad()) {
        setDirtyAfterLoad();
    }
    if (nodePersistor.needsResetAfterLoad()) {
        setNeedsResetAfterLoad();
    }
    return washedModelSettings;
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) WorkflowFileStoreHandlerRepository(org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) LoadNodeModelSettingsFailPolicy(org.knime.core.node.NodePersistor.LoadNodeModelSettingsFailPolicy) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) FileNodePersistor(org.knime.core.node.FileNodePersistor) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) NodeFactoryUnknownException(org.knime.core.node.workflow.WorkflowPersistor.NodeFactoryUnknownException) IOException(java.io.IOException) ContainerTable(org.knime.core.data.container.ContainerTable)

Example 2 with WorkflowFileStoreHandlerRepository

use of org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository in project knime-core by knime.

the class WorkflowLoadHelper method createTemplateLoadPersistor.

/**
 * Create persistor for a workflow or template.
 * @noreference Clients should only be required to load projects using
 * {@link WorkflowManager#loadProject(File, ExecutionMonitor, WorkflowLoadHelper)}
 * @param directory The directory to load from
 * @param templateSourceURI URI of the link to the template (will load a template and when the template is
 * instantiated and put into the workflow a link is created)
 * @return The persistor
 * @throws IOException If an IO error occured
 * @throws UnsupportedWorkflowVersionException If the workflow is of an unsupported version
 */
public final TemplateNodeContainerPersistor createTemplateLoadPersistor(final File directory, final URI templateSourceURI) throws IOException, UnsupportedWorkflowVersionException {
    if (directory == null) {
        throw new NullPointerException("Arguments must not be null.");
    }
    String fileName = getDotKNIMEFileName();
    if (!directory.isDirectory() || !directory.canRead()) {
        throw new IOException("Can't read metanode/template directory " + directory);
    }
    // template.knime or workflow.knime
    ReferencedFile dotKNIMERef = new ReferencedFile(new ReferencedFile(directory), fileName);
    File dotKNIME = dotKNIMERef.getFile();
    if (!dotKNIME.isFile()) {
        throw new IOException("No \"" + fileName + "\" file in directory \"" + directory.getAbsolutePath() + "\"");
    }
    NodeSettingsRO settings = NodeSettings.loadFromXML(new BufferedInputStream(new FileInputStream(dotKNIME)));
    // CeBIT 2006 version did not contain a version string.
    String versionString;
    if (settings.containsKey(WorkflowLoadHelper.CFG_VERSION)) {
        try {
            versionString = settings.getString(WorkflowLoadHelper.CFG_VERSION);
        } catch (InvalidSettingsException e) {
            throw new IOException("Can't read version number from \"" + dotKNIME.getAbsolutePath() + "\"", e);
        }
    } else {
        versionString = "0.9.0";
    }
    // might also be FUTURE
    LoadVersion version = FileWorkflowPersistor.parseVersion(versionString);
    boolean isSetDirtyAfterLoad = false;
    StringBuilder versionDetails = new StringBuilder(versionString);
    String createdBy = settings.getString(WorkflowLoadHelper.CFG_CREATED_BY, null);
    Version createdByVersion = null;
    if (createdBy != null) {
        versionDetails.append(" (created by KNIME ").append(createdBy).append(")");
        try {
            createdByVersion = new Version(createdBy);
        } catch (IllegalArgumentException e) {
            // ideally this goes into the 'LoadResult' but it's not instantiated yet
            LOGGER.warn(String.format("Unable to parse version string \"%s\" (file \"%s\"): %s", createdBy, dotKNIME.getAbsolutePath(), e.getMessage()), e);
        }
    }
    // added 3.5.0
    boolean isNightly = settings.getBoolean(CFG_NIGHTLY, false);
    boolean isRunningNightly = KNIMEConstants.isNightlyBuild();
    boolean isFutureWorkflow = createdByVersion != null && !new Version(KNIMEConstants.VERSION).isSameOrNewer(createdByVersion);
    if (version == LoadVersion.FUTURE || isFutureWorkflow || (!isRunningNightly && isNightly)) {
        switch(getUnknownKNIMEVersionLoadPolicy(version, createdByVersion, isNightly)) {
            case Abort:
                StringBuilder e = new StringBuilder("Unable to load ");
                e.append(isTemplateFlow() ? "template, " : "workflow, ");
                if (version == LoadVersion.FUTURE || isFutureWorkflow) {
                    e.append("it was created with a future version of KNIME (").append(createdBy).append("). ");
                    e.append("You are running ").append(KNIMEConstants.VERSION).append(".");
                } else {
                    e.append("it was created with a nightly build of KNIME (version ").append(createdBy).append("). ");
                    e.append("You are running ").append(KNIMEConstants.VERSION).append(".");
                }
                throw new UnsupportedWorkflowVersionException(e.toString());
            default:
                isSetDirtyAfterLoad = true;
        }
    } else if (version.isOlderThan(LoadVersion.V200)) {
        LOGGER.warn("The current KNIME version (" + KNIMEConstants.VERSION + ") is different from the one that " + "created the workflow (" + version + ") you are trying to load. In some rare cases, it  " + "might not be possible to load all data or some nodes can't be configured. " + "Please re-configure and/or re-execute these nodes.");
    }
    final MetaNodeTemplateInformation templateInfo;
    if (isTemplateFlow() && templateSourceURI != null) {
        try {
            templateInfo = MetaNodeTemplateInformation.load(settings, version);
            CheckUtils.checkSetting(Role.Template.equals(templateInfo.getRole()), "Role is not '%s' but '%s'", Role.Template, templateInfo.getRole());
        } catch (InvalidSettingsException e) {
            throw new IOException(String.format("Attempting to load template from \"%s\" but can't locate template information: %s", dotKNIME.getAbsolutePath(), e.getMessage()), e);
        }
    } else if (isTemplateFlow()) {
        // LOGGER.coding("Supposed to instantiate a template but the link URI is not set");
        // metanode template from node repository
        templateInfo = null;
    } else {
        templateInfo = null;
    }
    final TemplateNodeContainerPersistor persistor;
    // TODO only create new hash map if workflow is a project?
    HashMap<Integer, ContainerTable> tableRep = new GlobalTableRepository();
    WorkflowFileStoreHandlerRepository fileStoreHandlerRepository = new WorkflowFileStoreHandlerRepository();
    // ordinary workflow is loaded
    if (templateInfo == null) {
        persistor = new FileWorkflowPersistor(tableRep, fileStoreHandlerRepository, dotKNIMERef, this, version, !isTemplateFlow());
    } else {
        // some template is loaded
        switch(templateInfo.getNodeContainerTemplateType()) {
            case MetaNode:
                final ReferencedFile workflowDotKNIME;
                if (version.isOlderThan(LoadVersion.V2100)) {
                    // before 2.10 everything was stored in template.knime
                    workflowDotKNIME = dotKNIMERef;
                } else {
                    workflowDotKNIME = new ReferencedFile(dotKNIMERef.getParent(), WorkflowPersistor.WORKFLOW_FILE);
                }
                persistor = new FileWorkflowPersistor(tableRep, fileStoreHandlerRepository, workflowDotKNIME, this, version, !isTemplateFlow());
                break;
            case SubNode:
                final ReferencedFile settingsDotXML = new ReferencedFile(dotKNIMERef.getParent(), SingleNodeContainerPersistor.SETTINGS_FILE_NAME);
                persistor = new FileSubNodeContainerPersistor(settingsDotXML, this, version, tableRep, fileStoreHandlerRepository, true);
                break;
            default:
                throw new IllegalStateException("Unsupported template type");
        }
    }
    if (templateInfo != null) {
        persistor.setOverwriteTemplateInformation(templateInfo.createLink(templateSourceURI));
        persistor.setNameOverwrite(directory.getName());
    }
    if (isSetDirtyAfterLoad) {
        persistor.setDirtyAfterLoad();
    }
    return persistor;
}
Also used : WorkflowFileStoreHandlerRepository(org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository) ReferencedFile(org.knime.core.internal.ReferencedFile) ContainerTable(org.knime.core.data.container.ContainerTable) BufferedInputStream(java.io.BufferedInputStream) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) Version(org.knime.core.util.Version) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 3 with WorkflowFileStoreHandlerRepository

use of org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository in project knime-core by knime.

the class FileNodePersistor method loadBufferedDataTable.

private BufferedDataTable loadBufferedDataTable(final Node node, final NodeSettingsRO settings, final ExecutionMonitor execMon, final Map<Integer, BufferedDataTable> loadTblRep, final int index, final HashMap<Integer, ContainerTable> tblRep, final FileStoreHandlerRepository fileStoreHandlerRepository) throws InvalidSettingsException, IOException, CanceledExecutionException {
    // in 1.1.x and before the settings.xml contained the location
    // of the data table specs file (spec_0.xml, e.g.). From 1.2.0 on,
    // the spec is saved in data/data_0/spec.xml
    boolean isVersion11x = settings.containsKey(CFG_SPEC_FILES);
    ExecutionMonitor execSubData = execMon.createSubProgress(0.25);
    ReferencedFile nodeDirectory = getNodeDirectory();
    if (isVersion11x) {
        /* In version 1.1.x the data was stored in a different way. The
             * data.xml that is now contained in the data/data_x/ directory was
             * aggregated in a data.xml file directly in the m_nodeDir. Also the
             * spec was located at a different location.
             */
        String dataConfigFileName = settings.getString(CFG_DATA_FILE);
        File nodeDir = nodeDirectory.getFile();
        // dataConfigFile = data.xml in node dir
        File dataConfigFile = new File(nodeDir, dataConfigFileName);
        NodeSettingsRO dataSettings = NodeSettings.loadFromXML(new BufferedInputStream(new FileInputStream(dataConfigFile)));
        String dataPath = dataSettings.getString(CFG_DATA_FILE_DIR);
        // dataDir = /data
        ReferencedFile dataDirRef = new ReferencedFile(nodeDirectory, dataPath);
        // note: we do not check for existence here - in some cases
        // this directory may not exist (when exported and empty
        // directories are pruned)
        NodeSettingsRO portSettings = dataSettings.getNodeSettings(CFG_OUTPUT_PREFIX + index);
        String dataName = portSettings.getString(CFG_DATA_FILE_DIR);
        // dir = /data/data_i
        ReferencedFile dirRef = new ReferencedFile(dataDirRef, dataName);
        BufferedDataTable t = BufferedDataTable.loadFromFile(dirRef, portSettings, execSubData, loadTblRep, // no blobs or file stores in 1.1.x
        new HashMap<Integer, ContainerTable>(), new WorkflowFileStoreHandlerRepository());
        t.setOwnerRecursively(node);
        return t;
    } else {
        NodeSettingsRO dataSettings = settings.getNodeSettings(CFG_DATA_FILE);
        String dataDirStr = dataSettings.getString(CFG_DATA_FILE_DIR);
        ReferencedFile dataDirRef = new ReferencedFile(nodeDirectory, dataDirStr);
        NodeSettingsRO portSettings = dataSettings.getNodeSettings(CFG_OUTPUT_PREFIX + index);
        String dataName = portSettings.getString(CFG_DATA_FILE_DIR);
        ReferencedFile dirRef = new ReferencedFile(dataDirRef, dataName);
        File dir = dirRef.getFile();
        readDirectory(dir);
        BufferedDataTable t = BufferedDataTable.loadFromFile(dirRef, /* ignored in 1.2.0+ */
        null, execMon, loadTblRep, tblRep, fileStoreHandlerRepository);
        t.setOwnerRecursively(node);
        return t;
    }
}
Also used : WorkflowFileStoreHandlerRepository(org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository) ReferencedFile(org.knime.core.internal.ReferencedFile) FileInputStream(java.io.FileInputStream) ContainerTable(org.knime.core.data.container.ContainerTable) BufferedInputStream(java.io.BufferedInputStream) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Aggregations

ContainerTable (org.knime.core.data.container.ContainerTable)3 WorkflowFileStoreHandlerRepository (org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository)3 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 ReferencedFile (org.knime.core.internal.ReferencedFile)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)2 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)1 FileNodePersistor (org.knime.core.node.FileNodePersistor)1 LoadNodeModelSettingsFailPolicy (org.knime.core.node.NodePersistor.LoadNodeModelSettingsFailPolicy)1 NodeSettings (org.knime.core.node.NodeSettings)1 LoadVersion (org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion)1 NodeFactoryUnknownException (org.knime.core.node.workflow.WorkflowPersistor.NodeFactoryUnknownException)1 Version (org.knime.core.util.Version)1