Search in sources :

Example 1 with ModifiableNodeCreationConfiguration

use of org.knime.core.node.context.ModifiableNodeCreationConfiguration in project knime-core by knime.

the class EnhSRV2696_ReplaceNodePorts method testInsertPortBeforeAnotherPort.

/**
 * Tests that connections remain if a port is inserted before another already
 * connected one.
 */
@Test
public void testInsertPortBeforeAnotherPort() {
    WorkflowManager wfm = getManager();
    NodeID modelwriter_11 = new NodeID(wfm.getID(), 11);
    NativeNodeContainer oldNC = (NativeNodeContainer) wfm.getNodeContainer(modelwriter_11);
    assertTrue("node annotation expected to be the default one", oldNC.getNodeAnnotation().getData().isDefault());
    // add new port
    ModifiableNodeCreationConfiguration creationConfig = oldNC.getNode().getCopyOfCreationConfig().get();
    creationConfig.getPortConfig().get().getExtendablePorts().get("File System Connection").addPort(FileSystemPortObject.TYPE);
    ReplaceNodeResult replaceRes = wfm.replaceNode(modelwriter_11, creationConfig);
    // check that connection remains
    NativeNodeContainer newNC = (NativeNodeContainer) wfm.getNodeContainer(modelwriter_11);
    assertThat("connection is gone unexpectedly", wfm.getIncomingConnectionFor(modelwriter_11, 2), notNullValue());
    assertTrue("node annotation expected to be the default one", newNC.getNodeAnnotation().getData().isDefault());
    // try undo
    replaceRes.undo();
    newNC = (NativeNodeContainer) wfm.getNodeContainer(modelwriter_11);
    assertThat("connection is gone unexpectedly", wfm.getIncomingConnectionFor(modelwriter_11, 1), notNullValue());
    assertTrue("node annotation expected to be the default one", newNC.getNodeAnnotation().getData().isDefault());
}
Also used : ReplaceNodeResult(org.knime.core.node.workflow.action.ReplaceNodeResult) ModifiableNodeCreationConfiguration(org.knime.core.node.context.ModifiableNodeCreationConfiguration) Test(org.junit.Test)

Example 2 with ModifiableNodeCreationConfiguration

use of org.knime.core.node.context.ModifiableNodeCreationConfiguration in project knime-core by knime.

the class EnhSRV2696_ReplaceNodePorts method testReplaceNodeUndo.

/**
 * Tests {@link ReplaceNodeResult#undo()} and makes sure that connections removed during the node replacement are
 * added back again on undo.
 */
@Test
public void testReplaceNodeUndo() {
    WorkflowManager wfm = getManager();
    NativeNodeContainer oldNC = (NativeNodeContainer) wfm.getNodeContainer(m_concatenate_2);
    // add port and connect
    ModifiableNodeCreationConfiguration creationConfig = oldNC.getNode().getCopyOfCreationConfig().get();
    creationConfig.getPortConfig().get().getExtendablePorts().get("input").addPort(BufferedDataTable.TYPE);
    wfm.replaceNode(m_concatenate_2, creationConfig);
    ConnectionContainer newCC = wfm.addConnection(m_datagenerator_1, 1, m_concatenate_2, 3);
    NativeNodeContainer newNC = (NativeNodeContainer) wfm.getNodeContainer(m_concatenate_2);
    assertThat("unexpected number of input ports", newNC.getNrInPorts(), is(4));
    // remove port again
    ReplaceNodeResult replaceRes = wfm.replaceNode(m_concatenate_2, oldNC.getNode().getCopyOfCreationConfig().get());
    newNC = (NativeNodeContainer) wfm.getNodeContainer(m_concatenate_2);
    assertThat("unexpected number of input ports", newNC.getNrInPorts(), is(3));
    assertThat("unexpected connection", wfm.getConnection(newCC.getID()), nullValue());
    assertThat("canUndo expected to be possible", replaceRes.canUndo(), is(true));
    // undo remove operation
    replaceRes.undo();
    NativeNodeContainer undoNC = (NativeNodeContainer) wfm.getNodeContainer(m_concatenate_2);
    assertTrue(undoNC != newNC);
    assertThat("unexpected number of input ports", undoNC.getNrInPorts(), is(4));
    assertThat("connection missing", wfm.getConnection(newCC.getID()), notNullValue());
}
Also used : ReplaceNodeResult(org.knime.core.node.workflow.action.ReplaceNodeResult) ModifiableNodeCreationConfiguration(org.knime.core.node.context.ModifiableNodeCreationConfiguration) Test(org.junit.Test)

Example 3 with ModifiableNodeCreationConfiguration

use of org.knime.core.node.context.ModifiableNodeCreationConfiguration in project knime-core by knime.

the class WorkflowManager method replaceNode.

/**
 * Replaces a node with same type of node but another {@link NodeCreationConfiguration}, e.g. in order to change the
 * ports.
 *
 * Operation is only applicable for {@link NativeNodeContainer}s. Otherwise an {@link IllegalStateException} will
 * be thrown.
 *
 * Node settings and annotation will be transfered to the new node. Incoming and outgoing connections will be kept
 * as far as possible (if the respective input and output port is still there and compatible).
 *
 * @param id the id of the node to replace
 * @param creationConfig node creation configuration to create the new node, can be <code>null</code>
 * @return a result that contains all information necessary to undo the operation
 * @throws IllegalStateException if the node cannot be replaced (e.g. because there are executing successors)
 * @throws IllegalArgumentException if there is no node for the given id
 * @since 4.2
 */
public ReplaceNodeResult replaceNode(final NodeID id, final ModifiableNodeCreationConfiguration creationConfig) {
    CheckUtils.checkState(canReplaceNode(id), "Node cannot be replaced");
    try (WorkflowLock lock = lock()) {
        NativeNodeContainer nnc = (NativeNodeContainer) getNodeContainer(id);
        // keep the node's settings
        final NodeSettings settings = new NodeSettings("node settings");
        try {
            saveNodeSettings(id, settings);
        } catch (InvalidSettingsException e) {
        // no valid settings available, skip
        }
        // keep some node properties to be transfered to the new node
        NodeUIInformation uiInfo = nnc.getUIInformation();
        NodeFactory<?> factory = nnc.getNode().getFactory();
        NodeAnnotation nodeAnnotation = nnc.getNodeAnnotation();
        Set<ConnectionContainer> incomingConnections = getIncomingConnectionsFor(id);
        Set<ConnectionContainer> outgoingConnections = getOutgoingConnectionsFor(id);
        // keep old node creation config
        ModifiableNodeCreationConfiguration oldNodeCreationConfig = nnc.getNode().getCopyOfCreationConfig().orElse(null);
        // delete the old node
        removeNode(id);
        // add new node
        addNodeAndApplyContext(factory, creationConfig, id.getIndex());
        nnc = (NativeNodeContainer) getNodeContainer(id);
        // load the previously stored settings
        try {
            loadNodeSettings(id, settings);
        } catch (InvalidSettingsException e) {
        // ignore
        }
        // transfer old node properties, such as position and annotation
        nnc.setUIInformation(uiInfo);
        if (!nodeAnnotation.getData().isDefault()) {
            nnc.getNodeAnnotation().copyFrom(nodeAnnotation.getData(), true);
        }
        // restore connections if possible
        List<ConnectionContainer> removedConnections = reconnect(id, oldNodeCreationConfig.getPortConfig().get().mapInputPorts(creationConfig.getPortConfig().get()), oldNodeCreationConfig.getPortConfig().get().mapOutputPorts(creationConfig.getPortConfig().get()), incomingConnections, outgoingConnections);
        return new ReplaceNodeResult(this, id, removedConnections, oldNodeCreationConfig);
    }
}
Also used : ReplaceNodeResult(org.knime.core.node.workflow.action.ReplaceNodeResult) NodeSettings(org.knime.core.node.NodeSettings) ModifiableNodeCreationConfiguration(org.knime.core.node.context.ModifiableNodeCreationConfiguration) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 4 with ModifiableNodeCreationConfiguration

use of org.knime.core.node.context.ModifiableNodeCreationConfiguration in project knime-core by knime.

the class EnhSRV2696_ReplaceNodePorts method testReplaceNode.

/**
 * Tests {@link WorkflowManager#replaceNode(NodeID, ModifiableNodeCreationConfiguration)}.
 */
@Test
public void testReplaceNode() {
    WorkflowManager wfm = getManager();
    NativeNodeContainer oldNC = (NativeNodeContainer) wfm.getNodeContainer(m_concatenate_2);
    ModifiableNodeCreationConfiguration creationConfig = oldNC.getNode().getCopyOfCreationConfig().get();
    creationConfig.getPortConfig().get().getExtendablePorts().get("input").addPort(BufferedDataTable.TYPE);
    creationConfig.getPortConfig().get().getExtendablePorts().get("input").addPort(BufferedDataTable.TYPE);
    wfm.replaceNode(m_concatenate_2, creationConfig);
    NativeNodeContainer newNC = (NativeNodeContainer) wfm.getNodeContainer(m_concatenate_2);
    assertThat("node hasn't been replaced", newNC, is(not(oldNC)));
    assertThat("node annotations not the same", newNC.getNodeAnnotation().getData().getText(), is(oldNC.getNodeAnnotation().getData().getText()));
    assertThat("node's x postion changed", newNC.getUIInformation().getBounds()[0], is(oldNC.getUIInformation().getBounds()[0]));
    assertThat("node's y postion changed", newNC.getUIInformation().getBounds()[1], is(oldNC.getUIInformation().getBounds()[1]));
    assertThat("unexpected number of input ports", newNC.getNrInPorts(), is(5));
    assertThat("missing in connections", wfm.getIncomingConnectionsFor(m_concatenate_2).size(), is(2));
    assertThat("missing out connections", wfm.getOutgoingConnectionsFor(m_concatenate_2).size(), is(1));
    // test if node cannot be replaced -> exception
    IllegalStateException e = assertThrows(IllegalStateException.class, () -> wfm.replaceNode(m_metanode_4, creationConfig));
    assertThat("unexpected exception message", e.getMessage(), is("Node cannot be replaced"));
}
Also used : ModifiableNodeCreationConfiguration(org.knime.core.node.context.ModifiableNodeCreationConfiguration) Test(org.junit.Test)

Aggregations

ModifiableNodeCreationConfiguration (org.knime.core.node.context.ModifiableNodeCreationConfiguration)4 Test (org.junit.Test)3 ReplaceNodeResult (org.knime.core.node.workflow.action.ReplaceNodeResult)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1 NodeSettings (org.knime.core.node.NodeSettings)1