Search in sources :

Example 41 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class WorkflowManager method load.

/**
 * Loads the content of the argument persistor into this node.
 *
 * @param persistor The persistor containing the node(s) to be loaded as children to this node.
 * @param exec For progress/cancellation (currently not supported)
 * @param keepNodeMessages Whether to keep the messages that are associated with the nodes in the loaded workflow
 *            (mostly false but true when remotely computed results are loaded).
 * @return A workflow load result, which also contains the loaded node(s).
 * @throws IOException If errors reading the "important" files fails due to I/O problems (file not present, e.g.)
 * @throws InvalidSettingsException If parsing the "important" files fails.
 * @throws CanceledExecutionException If canceled.
 * @throws UnsupportedWorkflowVersionException If the version of the workflow is unknown (future version)
 */
public WorkflowLoadResult load(final FileWorkflowPersistor persistor, final ExecutionMonitor exec, final boolean keepNodeMessages) throws IOException, InvalidSettingsException, CanceledExecutionException, UnsupportedWorkflowVersionException {
    final ReferencedFile refDirectory = persistor.getMetaPersistor().getNodeContainerDirectory();
    final File directory = refDirectory.getFile();
    final WorkflowLoadResult result = new WorkflowLoadResult(directory.getName());
    load(persistor, result, exec, keepNodeMessages);
    final WorkflowManager manager = result.getWorkflowManager();
    if (!directory.canWrite()) {
        result.addWarning("Workflow directory \"" + directory.getName() + "\" is read-only; saving a modified workflow " + "will not be possible");
        manager.m_isWorkflowDirectoryReadonly = true;
    }
    boolean fixDataLoadProblems = false;
    // check for it and silently overwrite the workflow
    switch(result.getType()) {
        case DataLoadError:
            if (!persistor.mustWarnOnDataLoadError() && !manager.m_isWorkflowDirectoryReadonly) {
                LOGGER.debug("Workflow was apparently ex/imported without " + "data, silently fixing states and writing changes");
                try {
                    manager.save(directory, new ExecutionMonitor(), true);
                    fixDataLoadProblems = true;
                } catch (Throwable t) {
                    LOGGER.warn("Failed in an attempt to write workflow to file (workflow was ex/imported " + "without data; could not write the \"corrected\" flow.)", t);
                }
            }
            break;
        default:
    }
    StringBuilder message = new StringBuilder("Loaded workflow from \"");
    message.append(directory.getAbsolutePath()).append("\" ");
    switch(result.getType()) {
        case Ok:
            message.append(" with no errors");
            break;
        case Warning:
            message.append(" with warnings");
            break;
        case DataLoadError:
            message.append(" with errors during data load. ");
            if (fixDataLoadProblems) {
                message.append("Problems were fixed and (silently) saved.");
            } else {
                message.append("Problems were fixed but not saved!");
            }
            break;
        case Error:
            message.append(" with errors");
            break;
        default:
            message.append("with ").append(result.getType());
    }
    LOGGER.debug(message.toString());
    return result;
}
Also used : ExecutionMonitor(org.knime.core.node.ExecutionMonitor) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File) WorkflowLoadResult(org.knime.core.node.workflow.WorkflowPersistor.WorkflowLoadResult)

Example 42 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class WorkflowManager method setAutoSaveDirectoryDirtyRecursivly.

/**
 * Marks the workflow and all nodes contained as dirty in the auto-save location.
 *
 * @noreference This method is not intended to be referenced by clients.
 * @since 2.10
 */
public void setAutoSaveDirectoryDirtyRecursivly() {
    try (WorkflowLock lock = lock()) {
        ReferencedFile autoSaveDirectory = getAutoSaveDirectory();
        setDirty(autoSaveDirectory);
        if (autoSaveDirectory != null) {
            for (NodeContainer nc : getNodeContainers()) {
                nc.setDirty(nc.getAutoSaveDirectory());
            }
        }
    }
}
Also used : ReferencedFile(org.knime.core.internal.ReferencedFile)

Example 43 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class WorkflowManager method setNodeContainerDirectory.

/**
 * {@inheritDoc}
 */
@Override
protected void setNodeContainerDirectory(final ReferencedFile directory) {
    ReferencedFile ncDir = getNodeContainerDirectory();
    if (ConvenienceMethods.areEqual(directory, ncDir)) {
        return;
    }
    final boolean isProject = isProject();
    if (isProject && ncDir != null) {
        ncDir.fileUnlockRootForVM();
    }
    if (isProject && !directory.fileLockRootForVM()) {
        // need to lock projects (but not metanodes)
        throw new IllegalStateException("Workflow root directory \"" + directory + "\" can't be locked although it should have " + "been locked by the save routines");
    }
    super.setNodeContainerDirectory(directory);
}
Also used : ReferencedFile(org.knime.core.internal.ReferencedFile)

Example 44 with ReferencedFile

use of org.knime.core.internal.ReferencedFile 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 45 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class SandboxedNodeCreator method deepCopyFilesInWorkflowDir.

/**
 * Deep copies data and drop folders contained in the source directory to the target directory.
 * @param source Source node
 * @param targetParent Target node's parent
 */
private static void deepCopyFilesInWorkflowDir(final NodeContainer source, final WorkflowManager targetParent) {
    NodeContainer target = targetParent.getNodeContainer(targetParent.getID().createChild(source.getID().getIndex()));
    ReferencedFile sourceDirRef = source.getNodeContainerDirectory();
    ReferencedFile targetDirRef = target.getNodeContainerDirectory();
    if (sourceDirRef == null) {
        // The source node has never been saved, there are no files to copy
        return;
    }
    File sourceDir = sourceDirRef.getFile();
    File targetDir = targetDirRef.getFile();
    for (String magicFolderName : MAGIC_DATA_FOLDERS) {
        File dataSourceDir = new File(sourceDir, magicFolderName);
        if (dataSourceDir.isDirectory()) {
            File dataTargetDir = new File(targetDir, magicFolderName);
            try {
                FileUtils.copyDirectory(dataSourceDir, dataTargetDir);
                LOGGER.debugWithFormat("Copied directory \"%s\" to \"%s\"", dataSourceDir.getAbsolutePath(), dataTargetDir.getAbsolutePath());
            } catch (IOException ex) {
                LOGGER.error(String.format("Could not copy directory \"%s\" to \"%s\": %s", dataSourceDir.getAbsolutePath(), dataTargetDir.getAbsolutePath(), ex.getMessage()), ex);
            }
        }
    }
    Collection<NodeContainer> childrenList = Collections.emptyList();
    WorkflowManager childTargetParent = null;
    if (source instanceof WorkflowManager) {
        childrenList = ((WorkflowManager) source).getNodeContainers();
        childTargetParent = (WorkflowManager) target;
    } else if (source instanceof SubNodeContainer) {
        childrenList = ((SubNodeContainer) source).getWorkflowManager().getNodeContainers();
        childTargetParent = ((SubNodeContainer) target).getWorkflowManager();
    }
    for (NodeContainer child : childrenList) {
        deepCopyFilesInWorkflowDir(child, childTargetParent);
    }
}
Also used : SubNodeContainer(org.knime.core.node.workflow.SubNodeContainer) WorkflowManager(org.knime.core.node.workflow.WorkflowManager) NodeContainer(org.knime.core.node.workflow.NodeContainer) NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer) SubNodeContainer(org.knime.core.node.workflow.SubNodeContainer) SingleNodeContainer(org.knime.core.node.workflow.SingleNodeContainer) IOException(java.io.IOException) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Aggregations

ReferencedFile (org.knime.core.internal.ReferencedFile)46 File (java.io.File)29 IOException (java.io.IOException)22 BufferedInputStream (java.io.BufferedInputStream)11 FileInputStream (java.io.FileInputStream)11 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)8 PortObject (org.knime.core.node.port.PortObject)8 FlowVariablePortObject (org.knime.core.node.port.flowvariable.FlowVariablePortObject)8 FileStorePortObject (org.knime.core.data.filestore.FileStorePortObject)7 InactiveBranchPortObject (org.knime.core.node.port.inactive.InactiveBranchPortObject)7 HashMap (java.util.HashMap)6 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)6 FileOutputStream (java.io.FileOutputStream)5 Map (java.util.Map)5 ContainerTable (org.knime.core.data.container.ContainerTable)5 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)5 FlowVariablePortObjectSpec (org.knime.core.node.port.flowvariable.FlowVariablePortObjectSpec)5 InactiveBranchPortObjectSpec (org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4