Search in sources :

Example 16 with ReferencedFile

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

the class WorkflowManager method load.

/**
 * Loads the workflow contained in the directory as node into this workflow instance. Loading a whole new project is
 * usually done using {@link WorkflowManager#loadProject(File, ExecutionMonitor, WorkflowLoadHelper)} .
 *
 * @param directory to load from
 * @param exec For progress/cancellation (currently not supported)
 * @param loadHelper callback to load credentials and such (if available) during load of the underlying
 *            <code>SingleNodeContainer</code> (may be null).
 * @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 workflow.
 * @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)
 * @throws LockFailedException if the flow can't be locked for opening
 */
public WorkflowLoadResult load(final File directory, final ExecutionMonitor exec, final WorkflowLoadHelper loadHelper, final boolean keepNodeMessages) throws IOException, InvalidSettingsException, CanceledExecutionException, UnsupportedWorkflowVersionException, LockFailedException {
    ReferencedFile rootFile = new ReferencedFile(directory);
    boolean isTemplate = loadHelper.isTemplateFlow();
    if (!isTemplate) {
        // don't lock read-only templates (as we don't have r/o locks yet)
        if (!rootFile.fileLockRootForVM()) {
            StringBuilder error = new StringBuilder();
            error.append("Unable to lock workflow from \"");
            error.append(rootFile).append("\". ");
            if (rootFile.getFile().exists()) {
                error.append("It is in use by another user/instance.");
            } else {
                error.append("Location does not exist.");
            }
            throw new LockFailedException(error.toString());
        }
    }
    try {
        FileWorkflowPersistor persistor = createLoadPersistor(directory, loadHelper);
        return load(persistor, exec, keepNodeMessages);
    } finally {
        if (!isTemplate) {
            rootFile.fileUnlockRootForVM();
        }
    }
}
Also used : LockFailedException(org.knime.core.util.LockFailedException) ReferencedFile(org.knime.core.internal.ReferencedFile)

Example 17 with ReferencedFile

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

the class WorkflowManager method removeNode.

/**
 * Remove node if possible. Throws an exception if node is "busy" and can not be removed at this time. If the node
 * does not exist, this method returns without exception.
 *
 * @param nodeID id of node to be removed
 */
public void removeNode(final NodeID nodeID) {
    NodeContainer nc;
    try (WorkflowLock lock = lock()) {
        // if node does not exist, simply return
        final NodeContainer node = m_workflow.getNode(nodeID);
        if (node == null) {
            return;
        }
        // check to make sure we can safely remove this node
        CheckUtils.checkState(canRemoveNode(nodeID), "Node cannot be removed (node has state %s)", node.getInternalState());
        // remove lists of in- and outgoing connections.
        while (!m_workflow.getConnectionsByDest(nodeID).isEmpty()) {
            ConnectionContainer toDel = m_workflow.getConnectionsByDest(nodeID).iterator().next();
            removeConnection(toDel);
        }
        while (!m_workflow.getConnectionsBySource(nodeID).isEmpty()) {
            ConnectionContainer toDel = m_workflow.getConnectionsBySource(nodeID).iterator().next();
            removeConnection(toDel);
        }
        // and finally remove node itself as well.
        nc = m_workflow.removeNode(nodeID);
        nc.cleanup();
        // update list of obsolete node directories for non-root wfm
        ReferencedFile ncDir = nc.getNodeContainerDirectory();
        if (this != ROOT && ncDir != null && getNodeContainerDirectory() != null) {
            getNodeContainerDirectory().getDeletedNodesFileLocations().add(ncDir);
        }
        ReferencedFile autoSaveDir = nc.getAutoSaveDirectory();
        if (this != ROOT && autoSaveDir != null && getAutoSaveDirectory() != null) {
            getAutoSaveDirectory().getDeletedNodesFileLocations().add(autoSaveDir);
        }
        lock.queueCheckForNodeStateChangeNotification(true);
    }
    setDirty();
    notifyWorkflowListeners(new WorkflowEvent(WorkflowEvent.Type.NODE_REMOVED, getID(), nc, null));
}
Also used : ReferencedFile(org.knime.core.internal.ReferencedFile)

Example 18 with ReferencedFile

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

the class WorkflowManager method saveAs.

/**
 * Saves the workflow to a new location, setting the argument directory as the new NC dir. It will first copy the
 * "old" directory, point the NC dir to the new location and then do an incremental save.
 *
 * @param newContext the new workflow context, including the changed path
 * @param exec The execution monitor
 * @throws IOException If an IO error occured
 * @throws CanceledExecutionException If the execution was canceled
 * @throws LockFailedException If locking failed
 * @since 3.3
 */
public void saveAs(final WorkflowContext newContext, final ExecutionMonitor exec) throws IOException, CanceledExecutionException, LockFailedException {
    if (this == ROOT) {
        throw new IOException("Can't save root workflow");
    }
    try (WorkflowLock lock = lock()) {
        ReferencedFile ncDirRef = getNodeContainerDirectory();
        if (!isProject()) {
            throw new IOException("Cannot call save-as on a non-project workflow");
        }
        File directory = newContext.getCurrentLocation();
        directory.mkdirs();
        if (!directory.isDirectory() || !directory.canWrite()) {
            throw new IOException("Cannot write to " + directory);
        }
        boolean isNCDirNullOrRootReferenceFolder = ncDirRef == null || ncDirRef.getParent() == null;
        if (!isNCDirNullOrRootReferenceFolder) {
            throw new IOException("Referenced directory pointer is not hierarchical: " + ncDirRef);
        }
        m_workflowContext = newContext;
        ReferencedFile autoSaveDirRef = getAutoSaveDirectory();
        ExecutionMonitor saveExec;
        File ncDir = ncDirRef != null ? ncDirRef.getFile() : null;
        if (!ConvenienceMethods.areEqual(ncDir, directory)) {
            // new location
            // cannot be null
            ncDirRef.writeLock();
            try {
                ExecutionMonitor copyExec = exec.createSubProgress(0.5);
                final String copymsg = String.format("Copying existing workflow to new location " + "(from \"%s\" to \"%s\")", ncDir, directory);
                exec.setMessage(copymsg);
                LOGGER.debug(copymsg);
                copyExec.setProgress(1.0);
                FileUtils.copyDirectory(ncDir, directory, /* all but .knimeLock */
                FileFilterUtils.notFileFilter(FileFilterUtils.nameFileFilter(VMFileLocker.LOCK_FILE, IOCase.SENSITIVE)));
                exec.setMessage("Incremental save");
                ncDirRef.changeRoot(directory);
                if (autoSaveDirRef != null) {
                    File newLoc = WorkflowSaveHelper.getAutoSaveDirectory(ncDirRef);
                    final File autoSaveDir = autoSaveDirRef.getFile();
                    if (autoSaveDir.exists()) {
                        try {
                            FileUtils.moveDirectory(autoSaveDir, newLoc);
                            autoSaveDirRef.changeRoot(newLoc);
                            LOGGER.debugWithFormat("Moved auto-save directory from \"%s\" to \"%s\"", autoSaveDir.getAbsolutePath(), newLoc.getAbsolutePath());
                        } catch (IOException ioe) {
                            LOGGER.error(String.format("Couldn't move auto save directory \"%s\" to \"%s\": %s", autoSaveDir.getAbsolutePath(), newLoc.getAbsolutePath(), ioe.getMessage()), ioe);
                        }
                    } else {
                        autoSaveDirRef.changeRoot(newLoc);
                    }
                }
                m_isWorkflowDirectoryReadonly = false;
            } finally {
                ncDirRef.writeUnlock();
            }
            saveExec = exec.createSubProgress(0.5);
        } else {
            saveExec = exec;
        }
        save(directory, saveExec, true);
    }
}
Also used : IOException(java.io.IOException) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 19 with ReferencedFile

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

the class WorkflowManager method getName.

/**
 * Get the name of the workflow. If none has been set, a name is derived from the workflow directory name. If no
 * directory has been set, a static string is returned. This method never returns null. {@inheritDoc}
 */
@Override
public String getName() {
    if (m_name != null) {
        return m_name;
    }
    ReferencedFile refFile = getNodeContainerDirectory();
    if (refFile != null) {
        File file = refFile.getFile();
        String dirName = file.getName();
        if (dirName != null) {
            return dirName;
        }
    }
    return "Workflow Manager";
}
Also used : ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 20 with ReferencedFile

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

the class WorkflowManager method loadContent.

/**
 * {@inheritDoc}
 */
@Override
WorkflowCopyContent loadContent(final NodeContainerPersistor nodePersistor, final Map<Integer, BufferedDataTable> tblRep, final FlowObjectStack ignoredStack, final ExecutionMonitor exec, final LoadResult loadResult, final boolean preserveNodeMessage) throws CanceledExecutionException {
    exec.checkCanceled();
    if (!(nodePersistor instanceof WorkflowPersistor)) {
        throw new IllegalStateException("Expected " + WorkflowPersistor.class.getSimpleName() + " persistor object, got " + nodePersistor.getClass().getSimpleName());
    }
    WorkflowPersistor persistor = (WorkflowPersistor) nodePersistor;
    assert this != ROOT || persistor.getConnectionSet().isEmpty() : "ROOT workflow has no connections: " + persistor.getConnectionSet();
    LinkedHashMap<NodeID, NodeContainerPersistor> persistorMap = new LinkedHashMap<NodeID, NodeContainerPersistor>();
    Map<Integer, ? extends NodeContainerPersistor> nodeLoaderMap = persistor.getNodeLoaderMap();
    exec.setMessage("annotations");
    List<WorkflowAnnotation> annos = persistor.getWorkflowAnnotations();
    for (WorkflowAnnotation w : annos) {
        addWorkflowAnnotationInternal(w);
    }
    exec.setMessage("node & connection information");
    Map<Integer, NodeID> translationMap = loadNodesAndConnections(nodeLoaderMap, persistor.getConnectionSet(), loadResult);
    for (Map.Entry<Integer, NodeID> e : translationMap.entrySet()) {
        NodeID id = e.getValue();
        NodeContainerPersistor p = nodeLoaderMap.get(e.getKey());
        assert p != null : "Deficient translation map";
        persistorMap.put(id, p);
    }
    persistor.postLoad(this, loadResult);
    try {
        postLoad(persistorMap, tblRep, persistor.mustWarnOnDataLoadError(), exec, loadResult, preserveNodeMessage);
    } catch (CanceledExecutionException cee) {
        for (NodeID insertedNodeID : translationMap.values()) {
            removeNode(insertedNodeID);
        }
        throw cee;
    }
    NodeSettingsRO wizardState = persistor.getWizardExecutionControllerState();
    if (wizardState != null) {
        try {
            m_executionController = new WizardExecutionController(this, wizardState);
        } catch (InvalidSettingsException e1) {
            String msg = "Failed to restore wizard controller from file: " + e1.getMessage();
            LOGGER.debug(msg, e1);
            loadResult.addError(msg);
        }
    }
    // of the workflow can't be properly read from the workflow.knime)
    if (persistor.needsResetAfterLoad() || persistor.isDirtyAfterLoad()) {
        setDirty();
    }
    ReferencedFile ncDirectory = getNodeContainerDirectory();
    if (ncDirectory != null) {
        ncDirectory.getDeletedNodesFileLocations().addAll(persistor.getObsoleteNodeDirectories());
    }
    ReferencedFile autoSaveDirectory = getAutoSaveDirectory();
    if (autoSaveDirectory != null) {
        autoSaveDirectory.getDeletedNodesFileLocations().addAll(persistor.getObsoleteNodeDirectories());
    }
    Collection<NodeID> resultColl = persistorMap.keySet();
    NodeID[] newIDs = resultColl.toArray(new NodeID[resultColl.size()]);
    WorkflowAnnotation[] newAnnotations = annos.toArray(new WorkflowAnnotation[annos.size()]);
    addConnectionsFromTemplates(persistor.getAdditionalConnectionSet(), loadResult, translationMap, false);
    WorkflowCopyContent.Builder result = WorkflowCopyContent.builder();
    result.setAnnotation(newAnnotations);
    result.setNodeIDs(newIDs);
    return result.build();
}
Also used : ReferencedFile(org.knime.core.internal.ReferencedFile) LinkedHashMap(java.util.LinkedHashMap) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

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