Search in sources :

Example 1 with NodeRecommendation

use of org.knime.workbench.workflowcoach.NodeRecommendationManager.NodeRecommendation in project knime-core by knime.

the class TableColumnSorter method compare.

@SuppressWarnings("null")
private int compare(final Object e1, final Object e2) {
    if (!(e1 instanceof NodeRecommendation[]) || !(e2 instanceof NodeRecommendation[]) || (m_columnIndex == 0)) {
        return 0;
    }
    NodeRecommendation r1 = ((NodeRecommendation[]) e1)[m_columnIndex - 1];
    NodeRecommendation r2 = ((NodeRecommendation[]) e2)[m_columnIndex - 1];
    if ((r1 == null) && (r2 == null)) {
        return 0;
    } else if ((r1 != null) && (r2 == null)) {
        return -1 * m_direction;
    } else if ((r1 == null) && (r2 != null)) {
        return 1 * m_direction;
    } else {
        return r1.getFrequency() - r2.getFrequency();
    }
}
Also used : NodeRecommendation(org.knime.workbench.workflowcoach.NodeRecommendationManager.NodeRecommendation)

Example 2 with NodeRecommendation

use of org.knime.workbench.workflowcoach.NodeRecommendationManager.NodeRecommendation in project knime-core by knime.

the class WorkflowCoachView method updateInput.

private void updateInput(final ISelection selection) {
    if (NodeRecommendationManager.getInstance().getNumLoadedProviders() == 0) {
        // if there is at least one enabled triple provider then the statistics might need to be download first
        if (NodeRecommendationManager.getInstance().getNodeTripleProviders().stream().anyMatch(ntp -> ntp.isEnabled())) {
            if (m_loadState.get() == LoadState.Disposed) {
                return;
            }
            m_loadState.set(LoadState.LoadingNodes);
            updateInput("Loading recommendations ...");
            // try updating the triple provider that are enabled and require an update
            updateTripleProviders(e -> {
                m_loadState.set(LoadState.Initizalized);
                if (e.isPresent()) {
                    updateInputNoProvider();
                } else {
                    try {
                        NodeRecommendationManager.getInstance().loadRecommendations();
                        if (NodeRecommendationManager.getInstance().getNumLoadedProviders() == 0) {
                            // if there are still no triple provider, show link
                            updateInputNoProvider();
                        } else {
                            updateInput("Statistics successfully loaded. Select a node...");
                        }
                    } catch (Exception e1) {
                        updateInputNoProvider();
                    }
                }
            }, true, false);
        } else {
            // no triple provider enabled -> needs to be configured
            updateInputNoProvider();
            return;
        }
    }
    IStructuredSelection structSel = (IStructuredSelection) selection;
    if (structSel.size() > 1) {
        updateInput("No recommendation for multiple selected nodes.");
        return;
    }
    // retrieve first (and only!) selection:
    Iterator<?> selIt = structSel.iterator();
    boolean nodeSelected = selIt.hasNext();
    NodeContainer nc = null;
    if (nodeSelected) {
        Object sel = selIt.next();
        nodeSelected &= (sel instanceof NodeContainerEditPart);
        if (nodeSelected) {
            NodeContainerUI uinc = ((NodeContainerEditPart) sel).getNodeContainer();
            if (!Wrapper.wraps(uinc, NodeContainer.class)) {
                updateInput("Worklfow coach only supports native nodes, so far.\nBut the selected one is of type '" + uinc.getClass().getSimpleName() + "'.");
                return;
            } else {
                nc = Wrapper.unwrapNC(uinc);
                nodeSelected &= nc instanceof NativeNodeContainer;
            }
        }
    }
    // -> in that case no redraw is required
    if (nodeSelected) {
        if (m_lastSelection.equals(nc.getNameWithID())) {
            return;
        } else {
            m_lastSelection = nc.getNameWithID();
        }
    } else {
        if (m_lastSelection.equals("no node selected")) {
            return;
        } else {
            m_lastSelection = "no node selected";
        }
    }
    List<NodeRecommendation>[] recommendations;
    if (nodeSelected) {
        // retrieve node recommendations if exactly one node is selected
        recommendations = NodeRecommendationManager.getInstance().getNodeRecommendationFor((NativeNodeContainer) nc);
    } else if (nc == null) {
        // retrieve node recommendations if no node is selected (most likely the source nodes etc.)
        recommendations = NodeRecommendationManager.getInstance().getNodeRecommendationFor();
    } else {
        Display.getDefault().syncExec(() -> {
            m_viewer.setInput("");
            m_viewer.refresh();
        });
        return;
    }
    if (recommendations == null) {
        // something went wrong with loading the node recommendations, show the configure link
        updateInputNoProvider();
        return;
    }
    // TODO: cache node recommendations??
    int maxSize = 0;
    for (List<NodeRecommendation> l : recommendations) {
        maxSize = Math.max(maxSize, l.size());
    }
    List<NodeRecommendation[]> recommendationsJoined = joinRecommendations(recommendations, maxSize);
    // remove duplicates from list
    Set<String> duplicates = new HashSet<>();
    List<NodeRecommendation[]> recommendationsWithoutDups = new ArrayList<>(recommendationsJoined.size());
    for (NodeRecommendation[] nrs : recommendationsJoined) {
        int idx = getNonNullIdx(nrs);
        if (duplicates.add(nrs[idx].toString())) {
            recommendationsWithoutDups.add(nrs);
        }
    }
    // update viewer
    changeViewerStateTo(ViewerState.RECOMMENDATIONS);
    Display.getDefault().syncExec(() -> {
        m_viewer.setInput(recommendationsWithoutDups);
        m_viewer.refresh();
        m_recommendationsAvailable = true;
        // scroll to the very top
        if (!recommendationsWithoutDups.isEmpty()) {
            m_viewer.getTable().setTopIndex(0);
        }
    });
}
Also used : NodeContainerUI(org.knime.core.ui.node.workflow.NodeContainerUI) NodeRecommendation(org.knime.workbench.workflowcoach.NodeRecommendationManager.NodeRecommendation) ArrayList(java.util.ArrayList) NodeContainer(org.knime.core.node.workflow.NodeContainer) NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) NodeContainerEditPart(org.knime.workbench.editor2.editparts.NodeContainerEditPart) List(java.util.List) ArrayList(java.util.ArrayList) NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer) HashSet(java.util.HashSet)

Example 3 with NodeRecommendation

use of org.knime.workbench.workflowcoach.NodeRecommendationManager.NodeRecommendation in project knime-core by knime.

the class WorkflowCoachView method createPartControl.

/**
 * {@inheritDoc}
 */
@Override
public void createPartControl(final Composite parent) {
    m_viewer = new TableViewer(parent, SWT.V_SCROLL | SWT.FULL_SELECTION) {

        @Override
        public ISelection getSelection() {
            ISelection sel = super.getSelection();
            if (!sel.isEmpty() && sel instanceof IStructuredSelection) {
                IStructuredSelection ss = (IStructuredSelection) sel;
                if (ss.getFirstElement() instanceof NodeRecommendation[]) {
                    // turn node recommendation selection into a node template selection
                    NodeRecommendation[] nps = (NodeRecommendation[]) ss.getFirstElement();
                    return new StructuredSelection(new Object[] { getNonNullEntry(nps).getNodeTemplate() });
                }
            }
            return sel;
        }
    };
    getSite().setSelectionProvider(m_viewer);
    m_viewer.setComparator(new TableColumnSorter(m_viewer));
    Table table = m_viewer.getTable();
    // drag & drop
    Transfer[] transfers = new Transfer[] { LocalSelectionTransfer.getTransfer() };
    m_viewer.addDragSupport(DND.DROP_COPY | DND.DROP_MOVE, transfers, new WorkflowCoachDragSource(this));
    // column configuration
    TableColumn column = new TableColumn(table, SWT.LEFT, 0);
    column.setText("Recommended Nodes");
    column.setToolTipText("Nodes recommended to use next (e.g. based on the currently selected node).");
    column.setWidth(200);
    table.setHeaderVisible(true);
    table.setLinesVisible(false);
    m_viewer.setContentProvider(new WorkflowCoachContentProvider());
    m_viewer.setLabelProvider(new WorkflowCoachLabelProvider());
    m_viewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
    getViewSite().getPage().addSelectionListener(this);
    hookDoubleClickAction();
    // actions
    IToolBarManager toolbarMGR = getViewSite().getActionBars().getToolBarManager();
    toolbarMGR.add(new ConfigureAction(m_viewer));
    updateInput("Waiting for node repository to be loaded ...");
    m_loadState.set(LoadState.LoadingNodes);
    Job nodesLoader = new KNIMEJob("Workflow Coach loader", FrameworkUtil.getBundle(getClass())) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            // wait until the repository is fully loaded
            RepositoryManager.INSTANCE.getRoot();
            if (m_loadState.get() == LoadState.Disposed) {
                return Status.CANCEL_STATUS;
            } else if (monitor.isCanceled()) {
                m_loadState.set(LoadState.Initizalized);
                return Status.CANCEL_STATUS;
            } else {
                // check for update if necessary
                updateInput("Loading recommendations...");
                checkForStatisticUpdates();
            }
            m_loadState.set(LoadState.Initizalized);
            NodeRecommendationManager.getInstance().addUpdateListener(WorkflowCoachView.this);
            updateFrequencyColumnHeadersAndToolTips();
            updateInput(StructuredSelection.EMPTY);
            return Status.OK_STATUS;
        }
    };
    nodesLoader.setSystem(true);
    nodesLoader.schedule();
    // if the 'send anonymous statistics'-property has been changed, try updating the workflow coach
    KNIMECorePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(e -> {
        if (e.getProperty().equals(HeadlessPreferencesConstants.P_SEND_ANONYMOUS_STATISTICS)) {
            if (e.getNewValue().equals(Boolean.TRUE)) {
                // enable the community recommendations
                PREFS.setValue(WorkflowCoachPreferenceInitializer.P_COMMUNITY_NODE_TRIPLE_PROVIDER, true);
                try {
                    PREFS.save();
                } catch (Exception e1) {
                    throw new RuntimeException(e1);
                }
                updateInput(StructuredSelection.EMPTY);
            }
        }
    });
}
Also used : Table(org.eclipse.swt.widgets.Table) NodeRecommendation(org.knime.workbench.workflowcoach.NodeRecommendationManager.NodeRecommendation) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) TableColumn(org.eclipse.swt.widgets.TableColumn) KNIMEJob(org.knime.core.util.KNIMEJob) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IToolBarManager(org.eclipse.jface.action.IToolBarManager) ISelection(org.eclipse.jface.viewers.ISelection) LocalSelectionTransfer(org.eclipse.jface.util.LocalSelectionTransfer) Transfer(org.eclipse.swt.dnd.Transfer) GridData(org.eclipse.swt.layout.GridData) KNIMEJob(org.knime.core.util.KNIMEJob) UpdateJob(org.knime.workbench.workflowcoach.prefs.UpdateJob) Job(org.eclipse.core.runtime.jobs.Job) TableViewer(org.eclipse.jface.viewers.TableViewer)

Example 4 with NodeRecommendation

use of org.knime.workbench.workflowcoach.NodeRecommendationManager.NodeRecommendation in project knime-core by knime.

the class WorkflowCoachView method hookDoubleClickAction.

/**
 * Inserts a node on double click into the workflow editor.
 */
private void hookDoubleClickAction() {
    m_viewer.addDoubleClickListener(event -> {
        Object o = ((IStructuredSelection) event.getSelection()).getFirstElement();
        if (o instanceof NodeRecommendation[]) {
            NodeRecommendation[] nrs = (NodeRecommendation[]) o;
            NodeTemplate tmplt = getNonNullEntry(nrs).getNodeTemplate();
            NodeFactory<? extends NodeModel> nodeFact;
            try {
                nodeFact = tmplt.createFactoryInstance();
            } catch (Exception e) {
                NodeLogger.getLogger(WorkflowCoachView.class).error("Unable to instantiate the selected node " + tmplt.getFactory().getName(), e);
                return;
            }
            boolean added = NodeProvider.INSTANCE.addNode(nodeFact);
            if (added) {
                Display.getDefault().asyncExec(() -> setFocus());
            }
        }
    });
}
Also used : NodeTemplate(org.knime.workbench.repository.model.NodeTemplate) NodeRecommendation(org.knime.workbench.workflowcoach.NodeRecommendationManager.NodeRecommendation) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Aggregations

NodeRecommendation (org.knime.workbench.workflowcoach.NodeRecommendationManager.NodeRecommendation)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)3 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1 IToolBarManager (org.eclipse.jface.action.IToolBarManager)1 LocalSelectionTransfer (org.eclipse.jface.util.LocalSelectionTransfer)1 ISelection (org.eclipse.jface.viewers.ISelection)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 TableViewer (org.eclipse.jface.viewers.TableViewer)1 Transfer (org.eclipse.swt.dnd.Transfer)1 GridData (org.eclipse.swt.layout.GridData)1 Table (org.eclipse.swt.widgets.Table)1 TableColumn (org.eclipse.swt.widgets.TableColumn)1 NativeNodeContainer (org.knime.core.node.workflow.NativeNodeContainer)1 NodeContainer (org.knime.core.node.workflow.NodeContainer)1 NodeContainerUI (org.knime.core.ui.node.workflow.NodeContainerUI)1 KNIMEJob (org.knime.core.util.KNIMEJob)1