use of org.knime.core.node.workflow.action.ReplaceNodeResult 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());
}
use of org.knime.core.node.workflow.action.ReplaceNodeResult 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());
}
use of org.knime.core.node.workflow.action.ReplaceNodeResult 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);
}
}
Aggregations