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();
}
}
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);
}
});
}
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);
}
}
});
}
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());
}
}
});
}
Aggregations