Search in sources :

Example 96 with WorkflowManager

use of org.knime.core.node.workflow.WorkflowManager in project knime-core by knime.

the class SandboxedNodeCreator method copyExistingTablesIntoSandboxContainer.

/**
 * Copies the tables (port and internal) into the context of the corresponding node in the targetWFM. The execution
 * result must fit to the passed node container.
 *
 * @param execResult the object holding the result of the sourceNC. If the sourceNC is a workflow, this must hold
 *            all results of all contained nodes.
 * @param sourceNC the node that produced the execution result.
 * @param targetNC the context into which the tables are copied into
 * @param progressMon For progress information
 * @param copyDataIntoNewContext as per {@link #setCopyData(boolean)}
 * @throws CanceledExecutionException
 * @throws IOException
 */
public static void copyExistingTablesIntoSandboxContainer(final NodeContainerExecutionResult execResult, final NodeContainer sourceNC, final NodeContainer targetNC, final ExecutionMonitor progressMon, final boolean copyDataIntoNewContext) throws CanceledExecutionException, IOException {
    assert targetNC.getNrOutPorts() == sourceNC.getNrOutPorts();
    if (execResult instanceof NativeNodeContainerExecutionResult) {
        NativeNodeContainerExecutionResult sncResult = (NativeNodeContainerExecutionResult) execResult;
        // execResult and node types must match
        assert sourceNC instanceof NativeNodeContainer;
        assert targetNC instanceof NativeNodeContainer;
        // data is to copy ... get the correct execution context
        ExecutionContext targetExec = copyDataIntoNewContext ? ((SingleNodeContainer) targetNC).createExecutionContext() : null;
        NodeExecutionResult ner = sncResult.getNodeExecutionResult();
        // TODO this copy process has to take place in a different place
        // though it needs the final execution context for correct copy
        // of BDT objects
        PortObject[] resultTables = new PortObject[targetNC.getNrOutPorts()];
        int copyCount = resultTables.length;
        // copy also the internally held tables (such as for instance
        // the table in the table view) -- use the copy of the outports
        // if they match (likely they don't)
        PortObject[] oldInternTables = ner.getInternalHeldPortObjects();
        PortObject[] newInternTables = null;
        if (oldInternTables != null) {
            newInternTables = new PortObject[oldInternTables.length];
            copyCount += newInternTables.length;
        }
        // skip flow variable output
        for (int i = 0; i < resultTables.length; i++) {
            ExecutionMonitor sub = progressMon.createSubProgress(1.0 / copyCount);
            progressMon.setMessage("Port " + i);
            PortObject o = ner.getPortObject(i);
            PortObject newPO = copyPortObject(o, sub, targetExec);
            if (newInternTables != null) {
                for (int j = 0; j < oldInternTables.length; j++) {
                    if (oldInternTables[j] == o) {
                        newInternTables[j] = newPO;
                    }
                }
            }
            sub.setProgress(1.0);
            resultTables[i] = newPO;
        }
        if (newInternTables != null) {
            for (int i = 0; i < newInternTables.length; i++) {
                ExecutionMonitor sub = progressMon.createSubProgress(1.0 / copyCount);
                progressMon.setMessage("Internal Table " + i);
                if (newInternTables[i] == null) {
                    PortObject oldT = oldInternTables[i];
                    PortObject newT = copyPortObject(oldT, sub, targetExec);
                    newInternTables[i] = newT;
                }
                sub.setProgress(1.0);
            }
        }
        if (oldInternTables != null) {
            ner.setInternalHeldPortObjects(newInternTables);
        }
        ner.setPortObjects(resultTables);
    } else if (execResult instanceof WorkflowExecutionResult) {
        WorkflowExecutionResult wfmResult = (WorkflowExecutionResult) execResult;
        // exec result and node types must match
        WorkflowManager targetWFM = (WorkflowManager) targetNC;
        WorkflowManager sourceWFM = (WorkflowManager) sourceNC;
        copyIntoSandboxContainerRecursive(sourceWFM, targetWFM, wfmResult, progressMon, copyDataIntoNewContext);
    } else if (execResult instanceof SubnodeContainerExecutionResult) {
        SubnodeContainerExecutionResult subResult = (SubnodeContainerExecutionResult) execResult;
        WorkflowExecutionResult wfmResult = subResult.getWorkflowExecutionResult();
        WorkflowManager targetWFM = ((SubNodeContainer) targetNC).getWorkflowManager();
        WorkflowManager sourceWFM = ((SubNodeContainer) sourceNC).getWorkflowManager();
        copyIntoSandboxContainerRecursive(sourceWFM, targetWFM, wfmResult, progressMon, copyDataIntoNewContext);
    } else {
        throw new IllegalStateException("Unsupported node result type: " + execResult.getClass().getSimpleName());
    }
}
Also used : NodeExecutionResult(org.knime.core.node.workflow.execresult.NodeExecutionResult) WorkflowManager(org.knime.core.node.workflow.WorkflowManager) WorkflowExecutionResult(org.knime.core.node.workflow.execresult.WorkflowExecutionResult) SubNodeContainer(org.knime.core.node.workflow.SubNodeContainer) ExecutionContext(org.knime.core.node.ExecutionContext) NativeNodeContainerExecutionResult(org.knime.core.node.workflow.execresult.NativeNodeContainerExecutionResult) SubnodeContainerExecutionResult(org.knime.core.node.workflow.execresult.SubnodeContainerExecutionResult) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) PortObject(org.knime.core.node.port.PortObject) NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer)

Example 97 with WorkflowManager

use of org.knime.core.node.workflow.WorkflowManager 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)

Example 98 with WorkflowManager

use of org.knime.core.node.workflow.WorkflowManager in project knime-core by knime.

the class SandboxedNodeCreator method getFlowVariablesOnPort.

/**
 * Checks which flow variables are available on a port by looking on the output port connected to this input port.
 *
 * @param portIdx input port of the {@link NodeContainer} {@link #m_nc}
 * @return the flow variables available at this port
 */
private List<FlowVariable> getFlowVariablesOnPort(final int portIdx) {
    WorkflowManager wfm = m_nc.getParent();
    Optional<Stream<FlowVariable>> nodeInputFlowVariables = wfm.getNodeInputFlowVariables(m_nc.getID(), portIdx);
    if (nodeInputFlowVariables.isPresent()) {
        List<FlowVariable> result = nodeInputFlowVariables.get().filter(fv -> !fv.isGlobalConstant()).collect(Collectors.toList());
        // getNodeInputFlowVariables returns top down, make sure iterations on list return oldest entry first
        // (will be pushed onto node stack using an iterator)
        Collections.reverse(result);
        return result;
    }
    return Collections.emptyList();
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ReferencedFile(org.knime.core.internal.ReferencedFile) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) WorkflowContext(org.knime.core.node.workflow.WorkflowContext) NativeNodeContainerExecutionResult(org.knime.core.node.workflow.execresult.NativeNodeContainerExecutionResult) NodeContainer(org.knime.core.node.workflow.NodeContainer) Map(java.util.Map) PortObjectInNodeModel(org.knime.core.node.exec.dataexchange.in.PortObjectInNodeModel) PortObjectInNodeFactory(org.knime.core.node.exec.dataexchange.in.PortObjectInNodeFactory) PortType(org.knime.core.node.port.PortType) LockFailedException(org.knime.core.util.LockFailedException) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) Collection(java.util.Collection) ConnectionID(org.knime.core.node.workflow.ConnectionID) WorkflowCreationHelper(org.knime.core.node.workflow.WorkflowCreationHelper) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ConnectionContainer(org.knime.core.node.workflow.ConnectionContainer) List(java.util.List) BufferedDataTable(org.knime.core.node.BufferedDataTable) Stream(java.util.stream.Stream) PortObjectIDSettings(org.knime.core.node.exec.dataexchange.PortObjectIDSettings) Optional(java.util.Optional) CredentialsStore(org.knime.core.node.workflow.CredentialsStore) CheckUtils(org.knime.core.node.util.CheckUtils) PortObject(org.knime.core.node.port.PortObject) NodeContainerExecutionResult(org.knime.core.node.workflow.execresult.NodeContainerExecutionResult) IntStream(java.util.stream.IntStream) NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer) NodeExecutionJobManagerPool(org.knime.core.node.util.NodeExecutionJobManagerPool) FlowVariable(org.knime.core.node.workflow.FlowVariable) NodeSettings(org.knime.core.node.NodeSettings) BDTInNodeFactory(org.knime.core.node.exec.dataexchange.in.BDTInNodeFactory) ArrayList(java.util.ArrayList) SubNodeContainer(org.knime.core.node.workflow.SubNodeContainer) ExecutionContext(org.knime.core.node.ExecutionContext) NodeExecutionJobManager(org.knime.core.node.workflow.NodeExecutionJobManager) NodeLogger(org.knime.core.node.NodeLogger) WorkflowExecutionResult(org.knime.core.node.workflow.execresult.WorkflowExecutionResult) NodeFactory(org.knime.core.node.NodeFactory) NodeInPort(org.knime.core.node.workflow.NodeInPort) WorkflowManager(org.knime.core.node.workflow.WorkflowManager) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) NodeContext(org.knime.core.node.workflow.NodeContext) WorkflowCopyContent(org.knime.core.node.workflow.WorkflowCopyContent) File(java.io.File) PortObjectRepository(org.knime.core.node.exec.dataexchange.PortObjectRepository) NodeExecutionResult(org.knime.core.node.workflow.execresult.NodeExecutionResult) NodeID(org.knime.core.node.workflow.NodeID) FileUtil(org.knime.core.util.FileUtil) IFileStoreHandler(org.knime.core.data.filestore.internal.IFileStoreHandler) SubnodeContainerExecutionResult(org.knime.core.node.workflow.execresult.SubnodeContainerExecutionResult) Collections(java.util.Collections) SingleNodeContainer(org.knime.core.node.workflow.SingleNodeContainer) WorkflowManager(org.knime.core.node.workflow.WorkflowManager) Stream(java.util.stream.Stream) IntStream(java.util.stream.IntStream) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 99 with WorkflowManager

use of org.knime.core.node.workflow.WorkflowManager in project knime-core by knime.

the class StreamingTestNodeExecutionJob method removeNodeCopies.

private void removeNodeCopies(final NativeNodeContainer[] nodeContainers) {
    // (or better sandboxed)
    for (int i = 1; i < nodeContainers.length; i++) {
        nodeContainers[i].getNode().setFileStoreHandler(null);
        NodeID id = nodeContainers[i].getID();
        WorkflowManager workflowManager = nodeContainers[i].getParent();
        workflowManager.removeNode(id);
    }
}
Also used : WorkflowManager(org.knime.core.node.workflow.WorkflowManager) NodeID(org.knime.core.node.workflow.NodeID)

Aggregations

WorkflowManager (org.knime.core.node.workflow.WorkflowManager)99 NodeID (org.knime.core.node.workflow.NodeID)38 NodeContainer (org.knime.core.node.workflow.NodeContainer)22 SubNodeContainer (org.knime.core.node.workflow.SubNodeContainer)19 NativeNodeContainer (org.knime.core.node.workflow.NativeNodeContainer)15 NodeContainerEditPart (org.knime.workbench.editor2.editparts.NodeContainerEditPart)15 File (java.io.File)14 ConnectionContainer (org.knime.core.node.workflow.ConnectionContainer)14 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)10 SingleNodeContainer (org.knime.core.node.workflow.SingleNodeContainer)10 WorkflowAnnotation (org.knime.core.node.workflow.WorkflowAnnotation)10 MessageBox (org.eclipse.swt.widgets.MessageBox)9 NodeContainerUI (org.knime.core.ui.node.workflow.NodeContainerUI)9 List (java.util.List)8 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)8 NodeUIInformation (org.knime.core.node.workflow.NodeUIInformation)8 WorkflowContext (org.knime.core.node.workflow.WorkflowContext)8 WorkflowCopyContent (org.knime.core.node.workflow.WorkflowCopyContent)8