Search in sources :

Example 1 with SimpleMonitor

use of net.sourceforge.ondex.tools.threading.monitoring.SimpleMonitor in project knetbuilder by Rothamsted.

the class OnePairShortestPathFilter method callFilter.

/**
 * Calls backend filter.
 */
private void callFilter() {
    if (startSet != null && startSet.size() > 0 && endSet != null && endSet.size() > 0) {
        final SimpleMonitor monitor = new SimpleMonitor("preparing...", (startSet.size() * endSet.size()) + 1);
        Thread filterThread = new Thread("shortest path filter thread") {

            public void run() {
                // contains results
                Set<ONDEXConcept> concepts = null;
                Set<ONDEXRelation> relations = null;
                boolean useweights = !(ANselection == null || ANselection.equals(// no nullpointer danger because of
                defVal));
                // lazy
                // evaluation!
                boolean aborted = false;
                StringBuilder messages = new StringBuilder("Errors occurred during operation:\n");
                long before = System.currentTimeMillis();
                // multiple filter calls
                Iterator<ONDEXConcept> it = startSet.iterator();
                while (it.hasNext()) {
                    ONDEXConcept startConcept = it.next();
                    Iterator<ONDEXConcept> it2 = endSet.iterator();
                    while (it2.hasNext()) {
                        ONDEXConcept endConcept = it2.next();
                        if (startConcept.equals(endConcept))
                            continue;
                        String message = null;
                        // create new filter
                        Filter filter = new Filter();
                        try {
                            // construct filter arguments
                            ONDEXPluginArguments fa = new ONDEXPluginArguments(filter.getArgumentDefinitions());
                            fa.addOption(ArgumentNames.STARTCONCEPT_ARG, startConcept.getId());
                            fa.addOption(ArgumentNames.STOPCONCEPT_ARG, endConcept.getId());
                            fa.addOption(ArgumentNames.ONLYDIRECTED_ARG, dirBox.isSelected());
                            fa.addOption(ArgumentNames.USEWEIGHTS_ARG, useweights);
                            if (useweights)
                                fa.addOption(ArgumentNames.WEIGHTATTRIBUTENAME_ARG, ANselection);
                            fa.addOption(ArgumentNames.INVERSE_WEIGHT_ARG, invBox.isSelected());
                            filter.addONDEXListener(new ONDEXLogger());
                            filter.setONDEXGraph(graph);
                            filter.setArguments(fa);
                            filter.start();
                        } catch (Exception e) {
                            if (e.getMessage() != null)
                                message = e.getMessage();
                            else
                                message = e.toString();
                        }
                        if (message == null) {
                            if (concepts == null) {
                                concepts = filter.getVisibleConcepts();
                                relations = filter.getVisibleRelations();
                            } else {
                                concepts = BitSetFunctions.or(concepts, filter.getVisibleConcepts());
                                relations = BitSetFunctions.or(relations, filter.getVisibleRelations());
                            }
                        } else {
                            messages.append(message + "\n");
                        }
                        aborted = !monitor.next(MonitoringToolKit.calculateWaitingTime(before, monitor));
                        if (aborted) {
                            break;
                        }
                    }
                    if (aborted) {
                        break;
                    }
                }
                if (!aborted) {
                    if (concepts != null) {
                        StateEdit edit = new StateEdit(new VisibilityUndo(viewer.getONDEXJUNGGraph()), this.getName());
                        OVTK2Desktop desktop = OVTK2Desktop.getInstance();
                        desktop.setRunningProcess(this.getName());
                        // set all relations to invisible
                        for (ONDEXRelation r : graph.getRelations()) {
                            graph.setVisibility(r, false);
                        }
                        // set all concepts to invisible
                        for (ONDEXConcept c : graph.getConcepts()) {
                            graph.setVisibility(c, false);
                        }
                        // first set concepts visible
                        for (ONDEXConcept c : concepts) {
                            graph.setVisibility(c, true);
                        }
                        // second set relations visible
                        for (ONDEXRelation r : relations) {
                            graph.setVisibility(r, true);
                        }
                        // propagate change to viewer
                        viewer.getVisualizationViewer().getModel().fireStateChanged();
                        edit.end();
                        viewer.getUndoManager().addEdit(edit);
                        desktop.getOVTK2Menu().updateUndoRedo(viewer);
                        desktop.notifyTerminationOfProcess();
                        monitor.complete();
                    } else {
                        monitor.complete();
                        JOptionPane.showMessageDialog(OVTK2Desktop.getInstance().getMainFrame(), messages.toString(), "Error", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        };
        OVTKProgressMonitor.start(OVTK2Desktop.getInstance().getMainFrame(), "Shortest path filter", monitor);
        filterThread.start();
    }
}
Also used : OVTK2Desktop(net.sourceforge.ondex.ovtk2.ui.OVTK2Desktop) ONDEXPluginArguments(net.sourceforge.ondex.ONDEXPluginArguments) StateEdit(javax.swing.undo.StateEdit) VisibilityUndo(net.sourceforge.ondex.ovtk2.graph.VisibilityUndo) SimpleMonitor(net.sourceforge.ondex.tools.threading.monitoring.SimpleMonitor) ONDEXConcept(net.sourceforge.ondex.core.ONDEXConcept) OVTK2Filter(net.sourceforge.ondex.ovtk2.filter.OVTK2Filter) Filter(net.sourceforge.ondex.filter.onepairshortestpath.Filter) ONDEXRelation(net.sourceforge.ondex.core.ONDEXRelation) ONDEXLogger(net.sourceforge.ondex.logging.ONDEXLogger)

Example 2 with SimpleMonitor

use of net.sourceforge.ondex.tools.threading.monitoring.SimpleMonitor in project knetbuilder by Rothamsted.

the class OVTK2CustomFunctions method applyPlugin.

/**
 * Invoke a Plugin, which is available for the Workflow Integrator.
 *
 * @param name
 *            The full qualified name of the Plugin. This name is shown for
 *            each Plugin in the Integrator's documentation panel.
 * @param graphInput
 *            Graph the Plugin should work on.
 * @param args
 *            String arguments to be passed to the plugin. If a single
 *            Argument accepts multiple lines, those are separated using \n.
 *            If you don't want to specifiy a certain Argument, but use the
 *            Plugin's default, pass <code>null</code>.
 * @throws FunctionException
 */
@SuppressWarnings("unchecked")
public static void applyPlugin(String name, ONDEXGraph graphInput, String... args) throws FunctionException {
    SimpleMonitor monitor = new SimpleMonitor("initializing PluginRegistry", 1);
    OVTKProgressMonitor.start("applyPlugin", monitor);
    try {
        PluginUtils.initPluginRegistry();
    } catch (Exception e) {
        e.printStackTrace();
        ErrorDialog.show(false, new PluginUtils.MissingPluginException("PluginRegistry could not be initialized. applyPlugin() failed.", e), Thread.currentThread());
        return;
    } finally {
        monitor.complete();
    }
    ArgumentDefinition<?>[] argumentDefinitions = null;
    try {
        Class<ONDEXPlugin> cf = (Class<ONDEXPlugin>) Class.forName(name, true, PluginRegistry.getInstance().getClassLoader());
        ONDEXPlugin plugin = cf.getDeclaredConstructor(new Class<?>[] {}).newInstance();
        argumentDefinitions = plugin.getArgumentDefinitions();
        System.out.println("Executing plugin: " + plugin.getClass().getCanonicalName());
        if (args.length > 0 || plugin.requiresIndexedGraph()) {
            ONDEXPluginArguments fa = new ONDEXPluginArguments(plugin.getArgumentDefinitions());
            for (int i = 0; i < argumentDefinitions.length; i++) {
                if (args[i] != null) {
                    // optional parameters may be null
                    if (argumentDefinitions[i].isAllowedMultipleInstances()) {
                        fa.addOptions(argumentDefinitions[i].getName(), args[i].split("\n"));
                    } else {
                        fa.addOption(argumentDefinitions[i].getName(), argumentDefinitions[i].parseString(args[i]));
                    }
                }
            }
            if (plugin.requiresIndexedGraph()) {
                LuceneEnv lenv = loadLuceneEnv(graphInput);
                LuceneRegistry.sid2luceneEnv.put(graphInput.getSID(), lenv);
            }
            plugin.setArguments(fa);
            System.out.println("Setting arguments: " + fa.getOptions());
        }
        if (plugin instanceof RequiresGraph) {
            System.out.println("Setting graph: " + graphInput.getName());
            ((RequiresGraph) plugin).setONDEXGraph(graphInput);
        }
        plugin.start();
    } catch (Exception e) {
        e.printStackTrace();
        // give the user a little hint about the arguments to pass to the
        // given plugin
        StringBuffer argumentList = new StringBuffer();
        if (argumentDefinitions != null)
            for (ArgumentDefinition<?> ad : argumentDefinitions) argumentList.append(ad.getName() + "\n");
        else
            argumentList.append("unknown");
        throw new FunctionException("applyPlugin for plugin " + name + " failed: " + e + "\nAccepted arguments are:\n" + argumentList.toString(), -4);
    } finally {
        System.runFinalization();
    }
}
Also used : ArgumentDefinition(net.sourceforge.ondex.args.ArgumentDefinition) FunctionException(net.sourceforge.ondex.scripting.FunctionException) ONDEXPlugin(net.sourceforge.ondex.ONDEXPlugin) ONDEXPluginArguments(net.sourceforge.ondex.ONDEXPluginArguments) FunctionException(net.sourceforge.ondex.scripting.FunctionException) SimpleMonitor(net.sourceforge.ondex.tools.threading.monitoring.SimpleMonitor) LuceneEnv(net.sourceforge.ondex.core.searchable.LuceneEnv) RequiresGraph(net.sourceforge.ondex.RequiresGraph)

Aggregations

ONDEXPluginArguments (net.sourceforge.ondex.ONDEXPluginArguments)2 SimpleMonitor (net.sourceforge.ondex.tools.threading.monitoring.SimpleMonitor)2 StateEdit (javax.swing.undo.StateEdit)1 ONDEXPlugin (net.sourceforge.ondex.ONDEXPlugin)1 RequiresGraph (net.sourceforge.ondex.RequiresGraph)1 ArgumentDefinition (net.sourceforge.ondex.args.ArgumentDefinition)1 ONDEXConcept (net.sourceforge.ondex.core.ONDEXConcept)1 ONDEXRelation (net.sourceforge.ondex.core.ONDEXRelation)1 LuceneEnv (net.sourceforge.ondex.core.searchable.LuceneEnv)1 Filter (net.sourceforge.ondex.filter.onepairshortestpath.Filter)1 ONDEXLogger (net.sourceforge.ondex.logging.ONDEXLogger)1 OVTK2Filter (net.sourceforge.ondex.ovtk2.filter.OVTK2Filter)1 VisibilityUndo (net.sourceforge.ondex.ovtk2.graph.VisibilityUndo)1 OVTK2Desktop (net.sourceforge.ondex.ovtk2.ui.OVTK2Desktop)1 FunctionException (net.sourceforge.ondex.scripting.FunctionException)1