Search in sources :

Example 6 with NodeIDSuffix

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

the class ReferenceReaderDataUtil method writeReferenceReaderData.

/**
 * Writes the 'reference reader node'-data of a workflow to file.
 *
 * @param wfm a {@link WorkflowManager}
 * @param portObjectReaderSufIds a set of the referenced reader node identifiers
 * @param tmpDataDir the data directory
 * @param exec a {@link ExecutionMonitor}
 * @throws IOException
 * @throws CanceledExecutionException
 * @throws URISyntaxException
 * @throws InvalidSettingsException
 */
public static void writeReferenceReaderData(final WorkflowManager wfm, final Set<NodeIDSuffix> portObjectReaderSufIds, final File tmpDataDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException, URISyntaxException, InvalidSettingsException {
    // reconfigure reference reader nodes and store their data in temp directory
    exec.setMessage(() -> "Introducing reference reader nodes.");
    for (NodeIDSuffix portObjectReaderSufId : portObjectReaderSufIds) {
        final NodeID portObjectReaderId = portObjectReaderSufId.prependParent(wfm.getID());
        final var portObjectReaderNC = wfm.findNodeContainer(portObjectReaderId);
        assert portObjectReaderNC instanceof NativeNodeContainer;
        final var portObjectReaderNM = ((NativeNodeContainer) portObjectReaderNC).getNodeModel();
        assert portObjectReaderNM instanceof PortObjectInNodeModel;
        final PortObjectInNodeModel portObjectReader = (PortObjectInNodeModel) portObjectReaderNM;
        final Optional<PortObject> poOpt = portObjectReader.getPortObject();
        assert poOpt.isPresent();
        PortObject po = poOpt.get();
        if (po instanceof WorkflowPortObject) {
            // also write the data for potential reference reader nodes within a referenced workflow segment
            // AP-16062
            po = writeReferenceReaderDataForWorkflowPort((WorkflowPortObject) po, tmpDataDir, exec);
        }
        final var poFileName = portObjectReaderSufId.toString().replace(":", "_") + "_" + System.identityHashCode(po);
        final var poFileRelativeURI = new URI("knime://knime.workflow/data/" + poFileName);
        final var tmpPoFile = new File(tmpDataDir, poFileName);
        final PortObjectIDSettings poSettings = portObjectReader.getInputNodeSettingsCopy();
        if (po instanceof BufferedDataTable) {
            final BufferedDataTable table = (BufferedDataTable) po;
            DataContainer.writeToZip(table, tmpPoFile, exec.createSubProgress(.2 / portObjectReaderSufIds.size()));
            poSettings.setFileReference(poFileRelativeURI, true);
        } else {
            PortUtil.writeObjectToFile(po, tmpPoFile, exec.createSubProgress(.2 / portObjectReaderSufIds.size()));
            poSettings.setFileReference(poFileRelativeURI, false);
        }
        final var settings = new NodeSettings("root");
        portObjectReaderNC.getParent().saveNodeSettings(portObjectReaderId, settings);
        final NodeSettingsWO modelSettings = settings.addNodeSettings("model");
        poSettings.saveSettings(modelSettings);
        portObjectReaderNC.getParent().loadNodeSettings(portObjectReaderId, settings);
    }
}
Also used : PortObjectIDSettings(org.knime.core.node.exec.dataexchange.PortObjectIDSettings) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) URI(java.net.URI) NodeSettings(org.knime.core.node.NodeSettings) NodeIDSuffix(org.knime.core.node.workflow.NodeID.NodeIDSuffix) PortObjectInNodeModel(org.knime.core.node.exec.dataexchange.in.PortObjectInNodeModel) NodeID(org.knime.core.node.workflow.NodeID) BufferedDataTable(org.knime.core.node.BufferedDataTable) PortObject(org.knime.core.node.port.PortObject) File(java.io.File) NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer)

Example 7 with NodeIDSuffix

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

the class WorkflowPortObjectSpec method saveSpecMetadata.

/**
 * Saves the workflow port object spec data to the supplied model object (without the actual workflow data!)
 *
 * @param model the model to save the metadata to
 */
public void saveSpecMetadata(final ModelContentWO model) {
    model.addString("name", m_ws.getName());
    ModelContentWO refNodeIds = model.addModelContent("ref_node_ids");
    refNodeIds.addInt("num_ids", m_ws.getPortObjectReferenceReaderNodes().size());
    int i = 0;
    for (NodeIDSuffix id : m_ws.getPortObjectReferenceReaderNodes()) {
        refNodeIds.addIntArray("ref_node_id_" + i, id.getSuffixArray());
        i++;
    }
    ModelContentWO inputPorts = model.addModelContent("input_ports");
    saveInputs(inputPorts, m_ws.getConnectedInputs(), m_inputIDs);
    ModelContentWO outputPorts = model.addModelContent("output_ports");
    saveOutputs(outputPorts, m_ws.getConnectedOutputs(), m_outputIDs);
    if (m_customWorkflowName != null) {
        model.addString("custom_workflow_name", m_customWorkflowName);
    }
}
Also used : NodeIDSuffix(org.knime.core.node.workflow.NodeID.NodeIDSuffix) ModelContentWO(org.knime.core.node.ModelContentWO)

Example 8 with NodeIDSuffix

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

the class WorkflowCaptureOperation method capture.

/**
 * Carries out the actual capture-operation and returns the captured sub-workflow as a {@link WorkflowSegment}.
 *
 * @return the captured sub-workflow
 */
public WorkflowSegment capture() {
    WorkflowManager tempParent = null;
    try (WorkflowLock lock = m_wfm.lock()) {
        NodeID endNodeID = m_endNode.getID();
        WorkflowCreationHelper workflowCreationHelper = new WorkflowCreationHelper();
        Optional.ofNullable(NodeContext.getContext()).map(NodeContext::getWorkflowManager).map(WorkflowManager::getContext).ifPresent(workflowCreationHelper::setWorkflowContext);
        tempParent = WorkflowManager.EXTRACTED_WORKFLOW_ROOT.createAndAddProject("Capture-" + endNodeID, workflowCreationHelper);
        // "scope body" -- will copy those nodes later
        List<NodeContainer> nodesInScope = m_wfm.getWorkflow().getNodesInScope(m_endNode);
        // "scope body" and port object ref readers -- will determine bounding box and move them to the top left
        List<NodeContainer> nodesToDetermineBoundingBox = new ArrayList<>(nodesInScope);
        // copy nodes in scope body
        WorkflowCopyContent.Builder copyContent = WorkflowCopyContent.builder();
        NodeID[] allIDs = nodesInScope.stream().map(NodeContainer::getID).toArray(NodeID[]::new);
        HashSet<NodeID> allIDsHashed = new HashSet<>(Arrays.asList(allIDs));
        // port object reader nodes grouped by the original node
        Map<NodeID, List<NodeID>> portObjectReaderGroups = new HashMap<>();
        Set<NodeIDSuffix> addedPortObjectReaderNodes = new HashSet<>();
        NodeID[] allButScopeIDs = ArrayUtils.removeElements(allIDs, endNodeID, m_startNode.getID());
        copyContent.setNodeIDs(allButScopeIDs);
        copyContent.setIncludeInOutConnections(false);
        final int[] boundingBox = NodeUIInformation.getBoundingBoxOf(nodesToDetermineBoundingBox);
        final int[] moveUIDist = new int[] { -boundingBox[0] + 50, -boundingBox[1] + 50 };
        copyContent.setPositionOffset(moveUIDist);
        tempParent.copyFromAndPasteHere(m_wfm, copyContent.build());
        // collect nodes outside the scope body but connected to the scope body (only incoming)
        // maps to 'pasted' node id
        Map<Pair<NodeID, Integer>, NodeID> visitedSrcPorts = new HashMap<>();
        FlowVirtualScopeContext virtualScopeContext = getVirtualScopeContext(m_startNode);
        for (int i = 0; i < allButScopeIDs.length; i++) {
            NodeContainer oldNode = m_wfm.getNodeContainer(allButScopeIDs[i]);
            for (int p = 0; p < oldNode.getNrInPorts(); p++) {
                ConnectionContainer c = m_wfm.getIncomingConnectionFor(allButScopeIDs[i], p);
                if (c == null) {
                // ignore: no incoming connection
                } else if (allIDsHashed.contains(c.getSource())) {
                // ignore: connection already retained by paste persistor
                } else {
                    Pair<NodeID, Integer> currentSrcPort = Pair.create(c.getSource(), c.getSourcePort());
                    if (!visitedSrcPorts.containsKey(currentSrcPort)) {
                        // only add portObjectReader if not inserted already in previous loop iteration
                        // add port object reader
                        NodeID pastedID = addPortObjectReferenceReader(m_wfm, tempParent, c, virtualScopeContext);
                        NodeIDSuffix pastedIDSuffix = NodeIDSuffix.create(tempParent.getID(), pastedID);
                        // position
                        NodeID sourceID = c.getSource();
                        NodeUIInformation sourceUIInformation = getSourceNodeAndUIInformation(sourceID, c.getDest(), nodesToDetermineBoundingBox);
                        tempParent.getNodeContainer(pastedID).setUIInformation(sourceUIInformation);
                        // keeping track
                        visitedSrcPorts.put(Pair.create(c.getSource(), c.getSourcePort()), pastedID);
                        addedPortObjectReaderNodes.add(pastedIDSuffix);
                        portObjectReaderGroups.computeIfAbsent(currentSrcPort.getFirst(), id -> new ArrayList<>()).add(pastedID);
                    }
                    // connect all new port object readers to the in-scope-nodes
                    NodeIDSuffix destID = NodeIDSuffix.create(m_wfm.getID(), c.getDest());
                    tempParent.addConnection(visitedSrcPorts.get(currentSrcPort), 1, destID.prependParent(tempParent.getID()), c.getDestPort());
                }
            }
        }
        groupAndPositionPortObjectReaders(tempParent, portObjectReaderGroups, addedPortObjectReaderNodes, moveUIDist);
        // transfer editor settings, too
        tempParent.setEditorUIInformation(m_wfm.getEditorUIInformation());
        List<Input> workflowFragmentInputs = getInputs();
        List<Output> workflowFragmentOutputs = getOutputs();
        return new WorkflowSegment(tempParent, workflowFragmentInputs, workflowFragmentOutputs, addedPortObjectReaderNodes);
    } catch (Exception e) {
        if (tempParent != null) {
            WorkflowManager.EXTRACTED_WORKFLOW_ROOT.removeNode(tempParent.getID());
            tempParent = null;
        }
        throw e;
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Input(org.knime.core.node.workflow.capture.WorkflowSegment.Input) Output(org.knime.core.node.workflow.capture.WorkflowSegment.Output) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Pair(org.knime.core.util.Pair) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) FlowVirtualScopeContext(org.knime.core.node.workflow.virtual.parchunk.FlowVirtualScopeContext) WorkflowSegment(org.knime.core.node.workflow.capture.WorkflowSegment) NodeIDSuffix(org.knime.core.node.workflow.NodeID.NodeIDSuffix)

Example 9 with NodeIDSuffix

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

the class SinglePageManager method createWizardPageViewValueMap.

/**
 * Creates a map of node id string to JSON view value string for all appropriate wizard nodes from a given node id.
 *
 * @param containerNodeID the node id to create the view value map for
 * @return a map containing all appropriate view values
 * @throws IOException on serialization error
 */
public Map<String, String> createWizardPageViewValueMap(final NodeID containerNodeID) throws IOException {
    SinglePageWebResourceController sec = getController(containerNodeID);
    Map<NodeIDSuffix, WebViewContent> viewMap = sec.getWizardPageViewValueMap();
    Map<String, String> resultMap = new HashMap<String, String>();
    for (Entry<NodeIDSuffix, WebViewContent> entry : viewMap.entrySet()) {
        WebViewContent c = entry.getValue();
        resultMap.put(entry.getKey().toString(), new String(((ByteArrayOutputStream) c.saveToStream()).toByteArray()));
    }
    return resultMap;
}
Also used : WebViewContent(org.knime.core.node.web.WebViewContent) NodeIDSuffix(org.knime.core.node.workflow.NodeID.NodeIDSuffix) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SinglePageWebResourceController(org.knime.core.node.workflow.SinglePageWebResourceController)

Example 10 with NodeIDSuffix

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

the class TestSubnodeView method testExecuteAndCreateSubnodeView.

/**
 * Simple test if a combined view can succesfully be created and contains the layout and expected nodes for display
 * @throws Exception
 */
@Test
public void testExecuteAndCreateSubnodeView() throws Exception {
    initialExecute();
    SinglePageWebResourceController spc = new SinglePageWebResourceController(getManager(), m_subnodeID);
    assertTrue("Should have subnode view", spc.isSubnodeViewAvailable());
    WizardPageContent page = spc.getWizardPage();
    assertNotNull("Page content should be available", page);
    @SuppressWarnings("rawtypes") Map<NodeIDSuffix, WizardNode> pageMap = page.getPageMap();
    assertNotNull("Page map should be available", pageMap);
    assertEquals("Page should contain three nodes", 3, pageMap.size());
    String layout = page.getLayoutInfo();
    assertNotNull("Page layout should be available", layout);
    assertTrue("Layout should contain test string", layout.contains("testString"));
}
Also used : NodeIDSuffix(org.knime.core.node.workflow.NodeID.NodeIDSuffix) WizardNode(org.knime.core.node.wizard.WizardNode) WizardPageContent(org.knime.core.node.workflow.WebResourceController.WizardPageContent) Test(org.junit.Test)

Aggregations

NodeIDSuffix (org.knime.core.node.workflow.NodeID.NodeIDSuffix)19 NodeID (org.knime.core.node.workflow.NodeID)7 HashSet (java.util.HashSet)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 WizardNode (org.knime.core.node.wizard.WizardNode)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 NativeNodeContainer (org.knime.core.node.workflow.NativeNodeContainer)4 SubNodeContainer (org.knime.core.node.workflow.SubNodeContainer)4 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 PortObjectInNodeModel (org.knime.core.node.exec.dataexchange.in.PortObjectInNodeModel)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 LinkedHashSet (java.util.LinkedHashSet)2 ScrolledComposite (org.eclipse.swt.custom.ScrolledComposite)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 Font (org.eclipse.swt.graphics.Font)2