use of org.knime.core.node.workflow.NodeContainer in project knime-core by knime.
the class WorkflowSelectionDialog method createDialogArea.
/**
* {@inheritDoc}
*/
@Override
protected Control createDialogArea(final Composite parent) {
// a tree viewer to select a workflow group or workflow
Group overall = new Group(parent, SWT.SHADOW_ETCHED_IN);
overall.setText("Export selection:");
overall.setLayout(new GridLayout(1, false));
GridData shellLayout = new GridData(GridData.FILL_BOTH);
shellLayout.widthHint = 300;
shellLayout.heightHint = 350;
overall.setLayoutData(shellLayout);
GridData fillBoth = new GridData(GridData.FILL_BOTH);
m_viewer = new TreeViewer(overall, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI);
m_viewer.getTree().setLayoutData(fillBoth);
m_viewer.setLabelProvider(new KnimeResourceLabelProviderWithRoot());
m_viewer.setContentProvider(new KnimeResourceContentProviderWithRoot());
m_viewer.addFilter(new ViewerFilter() {
@Override
public boolean select(final Viewer viewer, final Object parentElement, final Object element) {
if (element instanceof IWorkspaceRoot) {
return true;
}
IResource resource = null;
if (element instanceof NodeContainer) {
ProjectWorkflowMap.findProjectFor(((NodeContainer) element).getID());
} else if (element instanceof IResource) {
resource = (IResource) element;
}
if (KnimeResourceUtil.isWorkflow(resource)) {
return true;
}
if (KnimeResourceUtil.isWorkflowGroup(resource)) {
return true;
}
return false;
}
});
m_viewer.setInput(ResourcesPlugin.getWorkspace().getRoot());
m_viewer.addPostSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(final SelectionChangedEvent event) {
m_selectedObjs = (IStructuredSelection) m_viewer.getSelection();
}
});
if (m_initialSelection != null) {
m_viewer.setSelection(m_initialSelection);
}
m_viewer.expandAll();
return overall;
}
use of org.knime.core.node.workflow.NodeContainer in project knime-core by knime.
the class SandboxedNodeCreator method copyIntoSandboxContainerRecursive.
/**
* @param sourceWFM
* @param targetWFM
* @param wfmResult
* @param progressMon
* @param copyDataIntoNewContext
* @throws CanceledExecutionException
* @throws IOException
*/
private static void copyIntoSandboxContainerRecursive(final WorkflowManager sourceWFM, final WorkflowManager targetWFM, final WorkflowExecutionResult wfmResult, final ExecutionMonitor progressMon, final boolean copyDataIntoNewContext) throws CanceledExecutionException, IOException {
assert wfmResult.getBaseID().equals(sourceWFM.getID());
Map<NodeID, NodeContainerExecutionResult> resultMap = wfmResult.getExecutionResultMap();
for (Map.Entry<NodeID, NodeContainerExecutionResult> e : resultMap.entrySet()) {
ExecutionMonitor sub = progressMon.createSubProgress(1.0 / resultMap.size());
NodeID sourceID = e.getKey();
NodeContainerExecutionResult r = e.getValue();
NodeID targetID = new NodeID(targetWFM.getID(), sourceID.getIndex());
NodeContainer nextTarget = targetWFM.getNodeContainer(targetID);
NodeContainer nextSource = sourceWFM.getNodeContainer(sourceID);
progressMon.setMessage(nextSource.getNameWithID());
copyExistingTablesIntoSandboxContainer(r, nextSource, nextTarget, sub, copyDataIntoNewContext);
sub.setProgress(1.0);
}
}
use of org.knime.core.node.workflow.NodeContainer in project knime-core by knime.
the class SandboxedNodeCreator method createSandbox.
/**
* Creates that temporary mini workflow that is executed remotely on the cluster/stream executor.
* The returned value should be {@link SandboxedNode#close()} when done (using try-with-resources). After this
* method is called no other set-method should be called.
*
* @param exec for progress/cancelation
* @return the index of the node that represents this node (the node to execute) in the temporary mini workflow
* @throws InvalidSettingsException
* @throws IOException
* @throws CanceledExecutionException
* @throws LockFailedException
* @throws InterruptedException
*/
public SandboxedNode createSandbox(final ExecutionMonitor exec) throws InvalidSettingsException, IOException, CanceledExecutionException, LockFailedException, InterruptedException {
exec.setMessage("Creating virtual workflow");
final WorkflowManager parent = m_nc.getParent();
// derive workflow context via NodeContext as the parent could only a be a metanode in a metanode...
final WorkflowContext origContext = NodeContext.getContext().getWorkflowManager().getContext();
WorkflowContext.Factory ctxFactory;
// (specifically reading knime://knime.workflow files)
if (!m_copyDataIntoNewContext) {
ctxFactory = new WorkflowContext.Factory(origContext);
if (m_localWorkflowDir != null) {
ctxFactory.setOriginalLocation(origContext.getCurrentLocation()).setCurrentLocation(m_localWorkflowDir);
}
} else if (m_localWorkflowDir != null) {
ctxFactory = new WorkflowContext.Factory(m_localWorkflowDir);
} else {
ctxFactory = new WorkflowContext.Factory(FileUtil.createTempDir("sandbox-" + m_nc.getNameWithID()));
}
// We have to use the same location for the temporary files
ctxFactory.setTempLocation(origContext.getTempLocation());
origContext.getMountpointURI().ifPresent(u -> ctxFactory.setMountpointURI(u));
WorkflowCreationHelper creationHelper = new WorkflowCreationHelper();
creationHelper.setWorkflowContext(ctxFactory.createContext());
if (!m_copyDataIntoNewContext) {
creationHelper.setDataHandlers(parent.getGlobalTableRepository(), parent.getFileStoreHandlerRepository());
}
WorkflowManager tempWFM = m_rootWFM.createAndAddProject("Sandbox Exec on " + m_nc.getNameWithID(), creationHelper);
// Add the workflow variables
List<FlowVariable> workflowVariables = parent.getProjectWFM().getWorkflowVariables();
tempWFM.addWorkflowVariables(true, workflowVariables.toArray(new FlowVariable[workflowVariables.size()]));
// update credentials store of the workflow
CredentialsStore cs = tempWFM.getCredentialsStore();
workflowVariables.stream().filter(f -> f.getType().equals(FlowVariable.Type.CREDENTIALS)).filter(f -> !cs.contains(f.getName())).forEach(cs::addFromFlowVariable);
final int inCnt = m_inData.length;
// port object IDs in static port object map, one entry for
// each connected input (no value for unconnected optional inputs)
List<Integer> portObjectRepositoryIDs = new ArrayList<Integer>(inCnt);
try {
NodeID[] ins = new NodeID[inCnt];
for (int i = 0; i < inCnt; i++) {
final PortObject in = m_inData[i];
final NodeInPort inPort = m_nc.getInPort(i);
final PortType portType = inPort.getPortType();
if (in == null) {
// unconnected optional input
CheckUtils.checkState(portType.isOptional(), "No data at port %d, although port is mandatory (port type %s)", i, portType.getName());
continue;
}
int portObjectRepositoryID = PortObjectRepository.add(in);
portObjectRepositoryIDs.add(portObjectRepositoryID);
boolean isTable = BufferedDataTable.TYPE.equals(portType);
NodeID inID = tempWFM.createAndAddNode(isTable ? TABLE_READ_NODE_FACTORY : OBJECT_READ_NODE_FACTORY);
NodeSettings s = new NodeSettings("temp_data_in");
tempWFM.saveNodeSettings(inID, s);
List<FlowVariable> flowVars = getFlowVariablesOnPort(i);
PortObjectInNodeModel.setInputNodeSettings(s, portObjectRepositoryID, flowVars, m_copyDataIntoNewContext);
// update credentials store of the workflow
flowVars.stream().filter(f -> f.getType().equals(FlowVariable.Type.CREDENTIALS)).filter(f -> !cs.contains(f.getName())).forEach(cs::addFromFlowVariable);
tempWFM.loadNodeSettings(inID, s);
ins[i] = inID;
}
// execute inPort object nodes to store the input data in them
if (ins.length > 0 && !tempWFM.executeAllAndWaitUntilDoneInterruptibly()) {
String error = "Unable to execute virtual workflow, status sent to log facilities";
LOGGER.debug(error + ":");
LOGGER.debug(tempWFM.toString());
throw new RuntimeException(error);
}
// add the target node to the workflow
WorkflowCopyContent.Builder content = WorkflowCopyContent.builder();
content.setNodeIDs(m_nc.getID());
final NodeID targetNodeID = tempWFM.copyFromAndPasteHere(parent, content.build()).getNodeIDs()[0];
NodeContainer targetNode = tempWFM.getNodeContainer(targetNodeID);
// connect target node to inPort object nodes, skipping unconnected (optional) inputs
IntStream.range(0, inCnt).filter(i -> ins[i] != null).forEach(i -> tempWFM.addConnection(ins[i], 1, targetNodeID, i));
if (m_forwardConnectionProgressEvents) {
setupConnectionProgressEventListeners(m_nc, targetNode);
}
// copy the existing tables into the (meta) node (e.g. an executed file reader that's necessary
// for other nodes to execute)
exec.setMessage("Copying tables into temp flow");
NodeContainerExecutionResult origResult = m_nc.createExecutionResult(exec);
ExecutionMonitor copyExec = exec.createSubProgress(0.0);
copyExistingTablesIntoSandboxContainer(origResult, m_nc, targetNode, copyExec, m_copyDataIntoNewContext);
CopyContentIntoTempFlowNodeExecutionJobManager copyDataIntoTmpFlow = new CopyContentIntoTempFlowNodeExecutionJobManager(origResult);
NodeExecutionJobManager oldJobManager = targetNode.getJobManager();
tempWFM.setJobManager(targetNodeID, copyDataIntoTmpFlow);
tempWFM.executeAllAndWaitUntilDoneInterruptibly();
tempWFM.setJobManager(targetNodeID, oldJobManager);
// do not use the cluster executor on the cluster...
tempWFM.setJobManager(targetNodeID, NodeExecutionJobManagerPool.getDefaultJobManagerFactory().getInstance());
if (!m_copyDataIntoNewContext) {
copyFileStoreHandlerReference(targetNode, parent, false);
}
// save workflow in the local job dir
if (m_localWorkflowDir != null) {
tempWFM.save(m_localWorkflowDir, exec, true);
deepCopyFilesInWorkflowDir(m_nc, tempWFM);
}
return new SandboxedNode(tempWFM, targetNodeID);
} finally {
portObjectRepositoryIDs.stream().forEach(PortObjectRepository::remove);
}
}
use of org.knime.core.node.workflow.NodeContainer in project knime-core by knime.
the class SandboxedNodeCreator method copyFileStoreHandlerReference.
/**
* Sets the file store handlers set on the original node recursively into the sandboxed node. This is
* only done when the data is _not_ to be copied as the sandboxed node should use the data (includes file stores)
* from the original node.
* @param runNC The sandbox node container
* @param origNCParent the parent of the original workflow
* @param nullIt <code>true</code> to set a <code>null</code> file store handler - used in
* {@link SandboxedNode#close()} (otherwise the file store handler is cleared when the temp flow is disposed).
*/
private void copyFileStoreHandlerReference(final NodeContainer runNC, final WorkflowManager origNCParent, final boolean nullIt) {
final NodeID origParentID = origNCParent.getID();
final int runNCIndex = runNC.getID().getIndex();
if (runNC instanceof NativeNodeContainer) {
NativeNodeContainer runNNC = (NativeNodeContainer) runNC;
NativeNodeContainer origNNC = origNCParent.getNodeContainer(origParentID.createChild(runNCIndex), NativeNodeContainer.class, true);
if (origNNC.getNodeContainerState().isExecutionInProgress()) {
final IFileStoreHandler fsHdl = nullIt ? null : origNNC.getNode().getFileStoreHandler();
if (!nullIt) {
runNNC.clearFileStoreHandler();
}
runNNC.getNode().setFileStoreHandler(fsHdl);
}
} else if (runNC instanceof WorkflowManager) {
WorkflowManager runWFM = (WorkflowManager) runNC;
WorkflowManager origWFM = origNCParent.getNodeContainer(origParentID.createChild(runNCIndex), WorkflowManager.class, true);
runWFM.getNodeContainers().stream().forEach(n -> copyFileStoreHandlerReference(n, origWFM, nullIt));
} else {
WorkflowManager runSubWFM = ((SubNodeContainer) runNC).getWorkflowManager();
WorkflowManager origSubWFM = origNCParent.getNodeContainer(origParentID.createChild(runNCIndex), SubNodeContainer.class, true).getWorkflowManager();
runSubWFM.getNodeContainers().stream().forEach(n -> copyFileStoreHandlerReference(n, origSubWFM, nullIt));
}
}
use of org.knime.core.node.workflow.NodeContainer in project knime-core by knime.
the class InteractiveViewDelegate method setWorkflowManagerAndNodeID.
public void setWorkflowManagerAndNodeID(final WorkflowManager wfm, final NodeID id) {
m_wfm = wfm;
m_nodeID = id;
NodeContainer nc = m_wfm.getNodeContainer(m_nodeID);
// !! constructor argument matches this info...
assert m_wfm.getNodeContainer(m_nodeID) == nc;
if (!(nc instanceof NativeNodeContainer) && !(nc instanceof SubNodeContainer)) {
throw new RuntimeException("Internal Error: Wrong type of node in " + this.getClass().getName());
}
if (nc instanceof NativeNodeContainer) {
NodeModel nm = ((NativeNodeContainer) nc).getNodeModel();
if (!(nm instanceof InteractiveNode)) {
throw new RuntimeException("Internal Error: Wrong type of node in " + this.getClass().getName());
}
}
}
Aggregations