use of org.knime.core.node.workflow.WorkflowPersistor.ConnectionContainerTemplate in project knime-core by knime.
the class SubNodeContainer method getConvertToMetaNodeCopyPersistor.
/**
* @return a persistor containing all but the virtual nodes and that is also fixing the in/out connections
* once the node is unwrapped to a metanode.
*/
WorkflowPersistor getConvertToMetaNodeCopyPersistor() {
assert isLockedByCurrentThread();
Collection<WorkflowAnnotationID> workflowAnnotations = m_wfm.getWorkflowAnnotationIDs();
// all but virtual in and output node
NodeID[] nodes = m_wfm.getNodeContainers().stream().map(nc -> nc.getID()).filter(id -> id.getIndex() != m_virtualInNodeIDSuffix).filter(id -> id.getIndex() != m_virtualOutNodeIDSuffix).toArray(NodeID[]::new);
WorkflowCopyContent.Builder cnt = WorkflowCopyContent.builder();
cnt.setNodeIDs(nodes);
cnt.setAnnotationIDs(workflowAnnotations.toArray(new WorkflowAnnotationID[workflowAnnotations.size()]));
cnt.setIncludeInOutConnections(true);
WorkflowPersistor persistor = m_wfm.copy(true, cnt.build());
final Set<ConnectionContainerTemplate> additionalConnectionSet = persistor.getAdditionalConnectionSet();
for (Iterator<ConnectionContainerTemplate> it = additionalConnectionSet.iterator(); it.hasNext(); ) {
ConnectionContainerTemplate c = it.next();
if (c.getSourceSuffix() == m_virtualInNodeIDSuffix) {
if (c.getSourcePort() == 0) {
it.remove();
continue;
}
c.setSourceSuffix(-1);
c.setSourcePort(c.getSourcePort() - 1);
}
if (c.getDestSuffix() == m_virtualOutNodeIDSuffix) {
if (c.getDestPort() == 0) {
it.remove();
continue;
}
c.setDestSuffix(-1);
c.setDestPort(c.getDestPort() - 1);
}
}
return persistor;
}
use of org.knime.core.node.workflow.WorkflowPersistor.ConnectionContainerTemplate in project knime-core by knime.
the class WorkflowManager method copy.
/**
* Copy the nodes with the given ids.
*
* @param isUndoableDeleteCommand <code>true</code> if the returned persistor is used in the delete command (which
* supports undo). This has two effects:
* <ol>
* <li>It keeps the locations of the node's directories (e.g. <workflow>/File Reader (#xy)/). This
* is true if the copy serves as backup of an undoable delete command (undoable = undo enabled). If it is
* undone, the directories must not be cleared before the next save (in order to keep the drop folder)
* </li>
* <li>The returned persistor will insert a reference to the contained workflow annotations instead of
* copying them (enables undo on previous move or edit commands.</li>
* </ol>
* @param content The content to copy (must exist).
* @return A workflow persistor hosting the node templates, ready to be used in the
* {@link #paste(WorkflowPersistor)} method.
*/
public WorkflowPersistor copy(final boolean isUndoableDeleteCommand, final WorkflowCopyContent content) {
NodeID[] nodeIDs = content.getNodeIDs();
HashSet<NodeID> idsHashed = new HashSet<NodeID>(Arrays.asList(nodeIDs));
if (idsHashed.size() != nodeIDs.length) {
throw new IllegalArgumentException("argument list contains duplicates");
}
Map<Integer, NodeContainerPersistor> loaderMap = new LinkedHashMap<Integer, NodeContainerPersistor>();
Set<ConnectionContainerTemplate> connTemplates = new HashSet<ConnectionContainerTemplate>();
Set<ConnectionContainerTemplate> additionalConnTemplates = new HashSet<ConnectionContainerTemplate>();
boolean isIncludeInOut = content.isIncludeInOutConnections();
try (WorkflowLock lock = lock()) {
for (int i = 0; i < nodeIDs.length; i++) {
// throws exception if not present in workflow
NodeContainer cont = getNodeContainer(nodeIDs[i]);
final NodeContainerPersistor copyPersistor = cont.getCopyPersistor(false, isUndoableDeleteCommand);
NodeContainerMetaPersistor copyMetaPersistor = copyPersistor.getMetaPersistor();
NodeUIInformation overwrittenUIInfo = content.getOverwrittenUIInfo(nodeIDs[i]);
Integer suggestedNodeIDSuffix = content.getSuggestedNodIDSuffix(nodeIDs[i]);
if (overwrittenUIInfo != null) {
copyMetaPersistor.setUIInfo(overwrittenUIInfo);
}
int nodeIDSuffix;
if (suggestedNodeIDSuffix != null) {
nodeIDSuffix = suggestedNodeIDSuffix.intValue();
copyMetaPersistor.setNodeIDSuffix(nodeIDSuffix);
} else {
nodeIDSuffix = cont.getID().getIndex();
}
loaderMap.put(nodeIDSuffix, copyPersistor);
NodeUIInformation uiInfo = copyMetaPersistor.getUIInfo();
if (uiInfo != null) {
//
content.getPositionOffset().map(//
off -> NodeUIInformation.builder(uiInfo).translate(off).build()).ifPresent(ui -> copyMetaPersistor.setUIInfo(ui));
}
for (ConnectionContainer out : m_workflow.getConnectionsBySource(nodeIDs[i])) {
if (idsHashed.contains(out.getDest())) {
ConnectionContainerTemplate t = new ConnectionContainerTemplate(out, false);
t.fixPostionOffsetIfPresent(content.getPositionOffset());
connTemplates.add(t);
} else if (isIncludeInOut) {
ConnectionContainerTemplate t = new ConnectionContainerTemplate(out, false);
t.fixPostionOffsetIfPresent(content.getPositionOffset());
additionalConnTemplates.add(t);
}
}
if (isIncludeInOut) {
for (ConnectionContainer in : m_workflow.getConnectionsByDest(nodeIDs[i])) {
ConnectionContainerTemplate t = new ConnectionContainerTemplate(in, false);
t.fixPostionOffsetIfPresent(content.getPositionOffset());
additionalConnTemplates.add(t);
}
}
}
return new PasteWorkflowContentPersistor(loaderMap, connTemplates, additionalConnTemplates, getWorkflowAnnotations(content.getAnnotationIDs()), isUndoableDeleteCommand);
}
}
use of org.knime.core.node.workflow.WorkflowPersistor.ConnectionContainerTemplate in project knime-core by knime.
the class WorkflowManager method addConnectionsFromTemplates.
/**
* @param connections
* @param loadResult
* @param translationMap
*/
private void addConnectionsFromTemplates(final Set<ConnectionContainerTemplate> connections, final LoadResult loadResult, final Map<Integer, NodeID> translationMap, final boolean currentlyLoadingFlow) {
for (ConnectionContainerTemplate c : connections) {
int sourceSuffix = c.getSourceSuffix();
int destSuffix = c.getDestSuffix();
assert sourceSuffix == -1 || sourceSuffix != destSuffix : "Can't insert connection, source and destination are equal";
ConnectionType type = ConnectionType.STD;
NodeID source;
NodeID dest;
if ((sourceSuffix == -1) && (destSuffix == -1)) {
source = getID();
dest = getID();
type = ConnectionType.WFMTHROUGH;
} else if (sourceSuffix == -1) {
source = getID();
dest = translationMap.get(destSuffix);
type = ConnectionType.WFMIN;
} else if (destSuffix == -1) {
dest = getID();
source = translationMap.get(sourceSuffix);
type = ConnectionType.WFMOUT;
} else {
dest = translationMap.get(destSuffix);
source = translationMap.get(sourceSuffix);
}
if (!canAddConnection(source, c.getSourcePort(), dest, c.getDestPort(), true, currentlyLoadingFlow)) {
String warn = "Unable to insert connection \"" + c + "\"";
LOGGER.warn(warn);
loadResult.addError(warn);
continue;
}
ConnectionContainer cc = addConnection(source, c.getSourcePort(), dest, c.getDestPort(), currentlyLoadingFlow);
cc.setUIInfo(c.getUiInfo());
cc.setDeletable(c.isDeletable());
assert cc.getType().equals(type);
}
}
Aggregations