Search in sources :

Example 1 with WorkflowEvent

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

the class ProjectWorkflowMap method replace.

/**
 * @param newPath the new path of the {@link IProject} after renaming
 * @param nc the {@link WorkflowManager} with a project new associated name
 * @param oldPath the old {@link IProject} path, under which the opened
 *  {@link WorkflowManager} is stored in the map
 */
public static void replace(final URI newPath, final WorkflowManagerUI nc, final URI oldPath) {
    if (oldPath == null) {
        throw new IllegalArgumentException("Old path must not be null (old is null, new is " + newPath + ")");
    }
    final MapWFKey oldKey = new MapWFKey(oldPath);
    NodeContainerUI removed = PROJECTS.remove(oldKey);
    if (removed == null) {
        throw new IllegalArgumentException("No project registered on URI " + oldPath);
    }
    Set<Object> clientList = WORKFLOW_CLIENTS.remove(oldKey);
    WF_LISTENER.workflowChanged(new WorkflowEvent(WorkflowEvent.Type.NODE_REMOVED, removed.getID(), Wrapper.unwrapNC(removed), null));
    putWorkflowUI(newPath, nc);
    if (clientList != null) {
        WORKFLOW_CLIENTS.put(new MapWFKey(newPath), clientList);
    }
    WF_LISTENER.workflowChanged(new WorkflowEvent(WorkflowEvent.Type.NODE_ADDED, nc.getID(), null, nc));
    NSC_LISTENER.stateChanged(new NodeStateEvent(nc.getID()));
}
Also used : NodeContainerUI(org.knime.core.ui.node.workflow.NodeContainerUI) WorkflowEvent(org.knime.core.node.workflow.WorkflowEvent) NodeStateEvent(org.knime.core.node.workflow.NodeStateEvent)

Example 2 with WorkflowEvent

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

the class WorkflowEventTest method testGetters.

@Test
public void testGetters() {
    Object oldVal = new Object();
    Object newVal = new Object();
    WorkflowEvent we = new WorkflowEvent(WorkflowEvent.Type.NODE_ADDED, new NodeID(10), oldVal, newVal);
    assertEquals(we.getType(), WorkflowEvent.Type.NODE_ADDED);
    assertEquals(we.getID(), new NodeID(10));
    assertEquals(we.getOldValue(), oldVal);
    assertEquals(we.getNewValue(), newVal);
}
Also used : NodeID(org.knime.core.node.workflow.NodeID) WorkflowEvent(org.knime.core.node.workflow.WorkflowEvent) Test(org.junit.Test)

Example 3 with WorkflowEvent

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

the class ProjectWorkflowMap method remove.

/**
 * Removes the {@link WorkflowManager} from the map, typically when the
 *  referring editor is closed and the WorkflowEditor is disposed.
 * @param path URI of the directory under which the
 * {@link WorkflowManager} is stored in the map.
 */
public static void remove(final URI path) {
    MapWFKey p = new MapWFKey(path);
    final WorkflowManagerUI manager = (WorkflowManagerUI) PROJECTS.get(p);
    // workflow is only in client map if there is at least one client
    if (manager != null && !WORKFLOW_CLIENTS.containsKey(p)) {
        Wrapper.unwrapWFMOptional(manager).ifPresent(wm -> NodeContext.pushContext(wm));
        try {
            PROJECTS.remove(p);
            WF_LISTENER.workflowChanged(new WorkflowEvent(WorkflowEvent.Type.NODE_REMOVED, manager.getID(), Wrapper.unwrapWFM(manager), null));
            manager.removeListener(WF_LISTENER);
            manager.removeNodeStateChangeListener(NSC_LISTENER);
            manager.removeNodeMessageListener(MSG_LISTENER);
            manager.removeNodePropertyChangedListener(NODE_PROP_LISTENER);
            try {
                manager.shutdown();
            } catch (Throwable t) {
                // at least we have tried it
                LOGGER.error("Could not cancel workflow manager for workflow " + p, t);
            } finally {
                if (manager.getNodeContainerState().isExecutionInProgress()) {
                    ThreadUtils.threadWithContext(() -> {
                        final int timeout = 20;
                        LOGGER.debugWithFormat("Workflow still in execution after canceling it - " + "waiting %d seconds max....", timeout);
                        try {
                            manager.waitWhileInExecution(timeout, TimeUnit.SECONDS);
                        } catch (InterruptedException ie) {
                            LOGGER.fatal("interrupting thread that no one has access to", ie);
                        }
                        if (manager.getNodeContainerState().isExecutionInProgress()) {
                            LOGGER.errorWithFormat("Workflow did not react on cancel within %d seconds, giving up", timeout);
                        } else {
                            LOGGER.debug("Workflow now canceled - will remove from parent");
                        }
                        WorkflowManager.ROOT.removeProject(manager.getID());
                    }, "Removal workflow - " + manager.getNameWithID()).start();
                } else {
                    WorkflowManager.ROOT.removeProject(manager.getID());
                }
            }
        } finally {
            Wrapper.unwrapWFMOptional(manager).ifPresent(wm -> NodeContext.removeLastContext());
        }
    }
}
Also used : WorkflowManagerUI(org.knime.core.ui.node.workflow.WorkflowManagerUI) WorkflowEvent(org.knime.core.node.workflow.WorkflowEvent)

Example 4 with WorkflowEvent

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

the class ProjectWorkflowMap method putWorkflowUI.

/**
 * Adds a {@link WorkflowManagerUI} of an opened workflow with the URI of
 * the workflow directory to the map. Used by the WorkflowEditor.
 *
 * @param path URI of the directory containing the workflow.knime file
 * @param manager {@link WorkflowManagerUI} in memory holding the workflow
 */
public static void putWorkflowUI(final URI path, final WorkflowManagerUI manager) {
    MapWFKey p = new MapWFKey(path);
    // in case the manager is replaced
    // -> unregister listeners from the old one
    NodeContainerUI oldOne = PROJECTS.get(p);
    if (oldOne != null) {
        oldOne.removeNodeStateChangeListener(NSC_LISTENER);
        ((WorkflowManagerUI) oldOne).removeListener(WF_LISTENER);
        oldOne.removeNodeMessageListener(MSG_LISTENER);
        oldOne.removeNodePropertyChangedListener(NODE_PROP_LISTENER);
    }
    PROJECTS.put(p, manager);
    manager.addNodeStateChangeListener(NSC_LISTENER);
    manager.addListener(WF_LISTENER);
    manager.addNodeMessageListener(MSG_LISTENER);
    manager.addNodePropertyChangedListener(NODE_PROP_LISTENER);
    // with WorkflowManagerUI instances, too!
    if (Wrapper.wraps(manager, WorkflowManager.class)) {
        WF_LISTENER.workflowChanged(new WorkflowEvent(WorkflowEvent.Type.NODE_ADDED, manager.getID(), null, Wrapper.unwrapWFM(manager)));
    }
}
Also used : NodeContainerUI(org.knime.core.ui.node.workflow.NodeContainerUI) WorkflowManagerUI(org.knime.core.ui.node.workflow.WorkflowManagerUI) WorkflowEvent(org.knime.core.node.workflow.WorkflowEvent)

Aggregations

WorkflowEvent (org.knime.core.node.workflow.WorkflowEvent)4 NodeContainerUI (org.knime.core.ui.node.workflow.NodeContainerUI)2 WorkflowManagerUI (org.knime.core.ui.node.workflow.WorkflowManagerUI)2 Test (org.junit.Test)1 NodeID (org.knime.core.node.workflow.NodeID)1 NodeStateEvent (org.knime.core.node.workflow.NodeStateEvent)1