Search in sources :

Example 6 with LoadVersion

use of org.knime.core.util.LoadVersion in project knime-core by knime.

the class SubNodeContainer method loadLegacyPortNamesAndDescriptionsFromInOutNodes.

@SuppressWarnings("deprecation")
private void loadLegacyPortNamesAndDescriptionsFromInOutNodes() {
    LoadVersion loadVersion = getWorkflowManager().getLoadVersion();
    if ((m_metadata == null || m_metadata == ComponentMetadata.NONE) && loadVersion != null && loadVersion.isOlderThan(LoadVersion.V4010)) {
        // take node description from virtual input node
        ComponentMetadataBuilder builder = ComponentMetadata.builder();
        builder.description(getVirtualInNodeModel().getSubNodeDescription());
        String[] inPortNames;
        String[] inPortDescriptions;
        String[] outPortNames;
        String[] outPortDescriptions;
        // take port descriptions from virtual input node
        inPortNames = getVirtualInNodeModel().getPortNames();
        inPortDescriptions = getVirtualInNodeModel().getPortDescriptions();
        for (int i = 0; i < inPortNames.length; i++) {
            builder.addInPortNameAndDescription(inPortNames[i], inPortDescriptions[i]);
        }
        // take port descriptions from virtual output node
        outPortNames = getVirtualOutNodeModel().getPortNames();
        outPortDescriptions = getVirtualOutNodeModel().getPortDescriptions();
        for (int i = 0; i < outPortNames.length; i++) {
            builder.addOutPortNameAndDescription(outPortNames[i], outPortDescriptions[i]);
        }
        m_metadata = builder.build();
    }
}
Also used : ComponentMetadataBuilder(org.knime.core.node.workflow.ComponentMetadata.ComponentMetadataBuilder) LoadVersion(org.knime.core.util.LoadVersion)

Example 7 with LoadVersion

use of org.knime.core.util.LoadVersion in project knime-core by knime.

the class WorkflowManagerUtil method loadWorkflow.

/**
 * Loads a workflow into memory. Mainly copied from {@code org.knime.testing.core.ng.WorkflowLoadTest}.
 *
 * @param workflowDir
 * @return the loaded workflow
 * @throws IOException
 * @throws InvalidSettingsException
 * @throws CanceledExecutionException
 * @throws UnsupportedWorkflowVersionException
 * @throws LockFailedException
 */
public static WorkflowManager loadWorkflow(final File workflowDir) throws IOException, InvalidSettingsException, CanceledExecutionException, UnsupportedWorkflowVersionException, LockFailedException {
    WorkflowLoadHelper loadHelper = new WorkflowLoadHelper() {

        /**
         * {@inheritDoc}
         */
        @Override
        public WorkflowContext getWorkflowContext() {
            WorkflowContext.Factory fac = new WorkflowContext.Factory(workflowDir);
            return fac.createContext();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public UnknownKNIMEVersionLoadPolicy getUnknownKNIMEVersionLoadPolicy(final LoadVersion workflowKNIMEVersion, final Version createdByKNIMEVersion, final boolean isNightlyBuild) {
            return UnknownKNIMEVersionLoadPolicy.Try;
        }
    };
    WorkflowLoadResult loadRes = WorkflowManager.loadProject(workflowDir, new ExecutionMonitor(), loadHelper);
    return loadRes.getWorkflowManager();
}
Also used : LoadVersion(org.knime.core.util.LoadVersion) Version(org.knime.core.util.Version) WorkflowContext(org.knime.core.node.workflow.WorkflowContext) WorkflowLoadHelper(org.knime.core.node.workflow.WorkflowLoadHelper) NodeFactory(org.knime.core.node.NodeFactory) DynamicNodeFactory(org.knime.core.node.DynamicNodeFactory) LoadVersion(org.knime.core.util.LoadVersion) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) WorkflowLoadResult(org.knime.core.node.workflow.WorkflowPersistor.WorkflowLoadResult)

Example 8 with LoadVersion

use of org.knime.core.util.LoadVersion in project knime-core by knime.

the class WorkflowManager method save.

/**
 * @param directory The directory to save in
 * @param exec The execution monitor
 * @param saveHelper ...
 * @throws IOException If an IO error occured
 * @throws CanceledExecutionException If the execution was canceled
 * @throws LockFailedException If locking failed
 * @since 2.10
 */
public void save(final File directory, final WorkflowSaveHelper saveHelper, final ExecutionMonitor exec) throws IOException, CanceledExecutionException, LockFailedException {
    if (this == ROOT) {
        throw new IOException("Can't save root workflow");
    }
    if (m_isWorkflowDirectoryReadonly) {
        throw new IOException("Workflow is read-only, can't save");
    }
    try (WorkflowLock lock = lock()) {
        ReferencedFile directoryReference = new ReferencedFile(directory);
        // if it's the location associated with the workflow we will use the same instance (due to VM lock)
        if (directoryReference.equals(getNodeContainerDirectory())) {
            directoryReference = getNodeContainerDirectory();
        } else if (saveHelper.isAutoSave() && directoryReference.equals(getAutoSaveDirectory())) {
            directoryReference = getAutoSaveDirectory();
        }
        directoryReference.writeLock();
        try {
            final boolean isWorkingDirectory = directoryReference.equals(getNodeContainerDirectory());
            final LoadVersion saveVersion = FileWorkflowPersistor.VERSION_LATEST;
            if (m_loadVersion != null && !m_loadVersion.equals(saveVersion)) {
                LOGGER.info("Workflow was created with another version of KNIME (workflow version " + m_loadVersion + "), converting to current version. This may take some time.");
                setDirtyAll();
            }
            if (isWorkingDirectory) {
                m_loadVersion = saveVersion;
            }
            if (m_authorInformation == null) {
                m_authorInformation = new AuthorInformation();
            } else {
                m_authorInformation = new AuthorInformation(m_authorInformation);
            }
            final File workflowDir = directoryReference.getFile();
            workflowDir.mkdirs();
            boolean isTemplate = getTemplateInformation().getRole().equals(Role.Template);
            if (isTemplate) {
                FileWorkflowPersistor.saveAsTemplate(this, directoryReference, exec, saveHelper);
            } else {
                FileWorkflowPersistor.save(this, directoryReference, exec, saveHelper);
                WorkflowSaveHook.runHooks(this, saveHelper.isSaveData(), workflowDir);
            }
        } finally {
            directoryReference.writeUnlock();
        }
    }
}
Also used : IOException(java.io.IOException) LoadVersion(org.knime.core.util.LoadVersion) AuthorInformation(org.knime.core.util.workflowalizer.AuthorInformation) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 9 with LoadVersion

use of org.knime.core.util.LoadVersion 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, isTemplateProject());
            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 repo if workflow is a project?
    WorkflowDataRepository workflowDataRepository = new WorkflowDataRepository();
    // ordinary workflow is loaded
    if (templateInfo == null) {
        persistor = new FileWorkflowPersistor(workflowDataRepository, 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(workflowDataRepository, workflowDotKNIME, this, version, !isTemplateFlow());
                break;
            case SubNode:
                final ReferencedFile settingsDotXML = new ReferencedFile(dotKNIMERef.getParent(), SingleNodeContainerPersistor.SETTINGS_FILE_NAME);
                persistor = new FileSubNodeContainerPersistor(settingsDotXML, this, version, workflowDataRepository, true);
                break;
            default:
                throw new IllegalStateException("Unsupported template type");
        }
    }
    if (templateInfo != null) {
        persistor.setOverwriteTemplateInformation(templateInfo.createLink(templateSourceURI, isTemplateProject()));
        if (templateSourceURI != null) {
            final String path = templateSourceURI.getPath();
            persistor.setNameOverwrite(path.substring(path.lastIndexOf('/') + 1));
        } else {
            persistor.setNameOverwrite(directory.getName());
        }
    }
    if (isSetDirtyAfterLoad) {
        persistor.setDirtyAfterLoad();
    }
    return persistor;
}
Also used : IOException(java.io.IOException) ReferencedFile(org.knime.core.internal.ReferencedFile) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) LoadVersion(org.knime.core.util.LoadVersion) Version(org.knime.core.util.Version) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) LoadVersion(org.knime.core.util.LoadVersion) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 10 with LoadVersion

use of org.knime.core.util.LoadVersion in project knime-core by knime.

the class WorkflowManager method load.

/**
 * Implementation of {@link #load(FileWorkflowPersistor, ExecutionMonitor, boolean)}.
 *
 * @noreference This method is not intended to be referenced by clients.
 */
public void load(final TemplateNodeContainerPersistor persistor, final MetaNodeLinkUpdateResult result, final ExecutionMonitor exec, final boolean keepNodeMessages) throws IOException, InvalidSettingsException, CanceledExecutionException, UnsupportedWorkflowVersionException {
    final ReferencedFile refDirectory = persistor.getMetaPersistor().getNodeContainerDirectory();
    exec.setMessage("Loading workflow structure from \"" + refDirectory + "\"");
    exec.checkCanceled();
    LoadVersion version = persistor.getLoadVersion();
    LOGGER.debug("Loading workflow from \"" + refDirectory + "\" (version \"" + version + "\" with loader class \"" + persistor.getClass().getSimpleName() + "\")");
    // data files are loaded using a repository of reference tables;
    Map<Integer, BufferedDataTable> tblRep = new HashMap<Integer, BufferedDataTable>();
    persistor.preLoadNodeContainer(null, null, result);
    NodeContainerTemplate loadedInstance = null;
    boolean isIsolatedProject = persistor.isProject();
    InsertWorkflowPersistor insertPersistor = new InsertWorkflowPersistor(persistor);
    ReentrantLock lock = isIsolatedProject ? new ReentrantLock() : m_workflowLock.getReentrantLock();
    lock.lock();
    try {
        m_loadVersion = persistor.getLoadVersion();
        NodeID[] newIDs = loadContent(insertPersistor, tblRep, null, exec, result, keepNodeMessages).getNodeIDs();
        if (newIDs.length != 1) {
            throw new InvalidSettingsException("Loading workflow failed, " + "couldn't identify child sub flow (typically " + "a project)");
        }
        loadedInstance = (NodeContainerTemplate) getNodeContainer(newIDs[0]);
    } finally {
        lock.unlock();
    }
    exec.setProgress(1.0);
    result.setLoadedInstance(loadedInstance);
    result.setGUIMustReportDataLoadErrors(persistor.mustWarnOnDataLoadError());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ReferencedFile(org.knime.core.internal.ReferencedFile) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) BufferedDataTable(org.knime.core.node.BufferedDataTable) LoadVersion(org.knime.core.util.LoadVersion)

Aggregations

LoadVersion (org.knime.core.util.LoadVersion)10 Version (org.knime.core.util.Version)5 File (java.io.File)4 ReferencedFile (org.knime.core.internal.ReferencedFile)3 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)3 WorkflowLoadResult (org.knime.core.node.workflow.WorkflowPersistor.WorkflowLoadResult)3 IOException (java.io.IOException)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 AssertionFailedError (junit.framework.AssertionFailedError)1 Test (org.junit.Test)1 BufferedDataTable (org.knime.core.node.BufferedDataTable)1 DynamicNodeFactory (org.knime.core.node.DynamicNodeFactory)1 NodeFactory (org.knime.core.node.NodeFactory)1 PortObject (org.knime.core.node.port.PortObject)1