Search in sources :

Example 6 with PluginSynchronizer

use of au.gov.asd.tac.constellation.plugins.PluginSynchronizer in project constellation by constellation-app.

the class QueryPhasePane method runPlugins.

/**
 * Run the plugin for each data access pane in this query pane, optionally
 * waiting first on a list of passed futures. This method will start the
 * plugin execution and return a list of futures representing each plugins
 * execution.
 *
 * @param async if not null, the plugins to be executed will wait till all the
 *     futures in the list have been completed before running
 * @return a list of futures representing the plugins that were executed
 */
public List<Future<?>> runPlugins(final List<Future<?>> async) {
    // Get the global plugin parameters for this query pane
    final Map<String, PluginParameter<?>> globalParams = getGlobalParametersPane().getParams().getParameters();
    // Pre-query validation checking
    for (final DataAccessPreQueryValidation check : PRE_QUERY_VALIDATION) {
        if (!check.execute(this)) {
            return Collections.emptyList();
        }
    }
    // Determine the number of plugins that will be executed
    final int pluginsToRun = Long.valueOf(getDataAccessPanes().stream().filter(DataSourceTitledPane::isQueryEnabled).count()).intValue();
    LOGGER.log(Level.INFO, "\tRunning {0} plugins", pluginsToRun);
    final PluginSynchronizer synchroniser = new PluginSynchronizer(pluginsToRun);
    final List<Future<?>> newAsync = new ArrayList<>(pluginsToRun);
    getDataAccessPanes().stream().filter(DataSourceTitledPane::isQueryEnabled).forEach(pane -> {
        // Copy global parameters into plugin parameters where there
        // is overlap
        PluginParameters parameters = pane.getParameters();
        if (parameters != null) {
            parameters = parameters.copy();
            parameters.getParameters().entrySet().stream().filter(entry -> globalParams.containsKey(entry.getKey())).forEach(entry -> entry.getValue().setObjectValue(globalParams.get(entry.getKey()).getObjectValue()));
        }
        // Get the plugin for this pane and run it
        final Plugin plugin = PluginRegistry.get(pane.getPlugin().getClass().getName());
        LOGGER.log(Level.INFO, "\t\tRunning {0}", plugin.getName());
        final Future<?> pluginResult = PluginExecution.withPlugin(plugin).withParameters(parameters).waitingFor(async).synchronizingOn(synchroniser).executeLater(GraphManager.getDefault().getActiveGraph());
        newAsync.add(pluginResult);
        DataAccessPaneState.addRunningPlugin(pluginResult, plugin.getName());
    });
    return newAsync;
}
Also used : Arrays(java.util.Arrays) VBox(javafx.scene.layout.VBox) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) DataAccessPreQueryValidation(au.gov.asd.tac.constellation.views.dataaccess.templates.DataAccessPreQueryValidation) Future(java.util.concurrent.Future) ScrollPane(javafx.scene.control.ScrollPane) DataAccessPlugin(au.gov.asd.tac.constellation.views.dataaccess.plugins.DataAccessPlugin) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) Map(java.util.Map) PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) PluginRegistry(au.gov.asd.tac.constellation.plugins.PluginRegistry) GraphManager(au.gov.asd.tac.constellation.graph.manager.GraphManager) Lookup(org.openide.util.Lookup) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) MenuItem(javafx.scene.control.MenuItem) Node(javafx.scene.Node) DataAccessPaneState(au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessPaneState) Set(java.util.Set) Logger(java.util.logging.Logger) Platform(javafx.application.Platform) PluginParametersPaneListener(au.gov.asd.tac.constellation.plugins.gui.PluginParametersPaneListener) List(java.util.List) PluginSynchronizer(au.gov.asd.tac.constellation.plugins.PluginSynchronizer) Collections(java.util.Collections) PluginSynchronizer(au.gov.asd.tac.constellation.plugins.PluginSynchronizer) ArrayList(java.util.ArrayList) DataAccessPreQueryValidation(au.gov.asd.tac.constellation.views.dataaccess.templates.DataAccessPreQueryValidation) Future(java.util.concurrent.Future) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) DataAccessPlugin(au.gov.asd.tac.constellation.views.dataaccess.plugins.DataAccessPlugin) Plugin(au.gov.asd.tac.constellation.plugins.Plugin)

Example 7 with PluginSynchronizer

use of au.gov.asd.tac.constellation.plugins.PluginSynchronizer in project constellation by constellation-app.

the class DefaultPluginEnvironmentNGTest method testExecutePluginLaterThrowsInterruptedException.

@Test
public void testExecutePluginLaterThrowsInterruptedException() throws ExecutionException, InterruptedException, PluginException {
    System.out.println("executePluginLater");
    Graph graph = mock(Graph.class);
    Plugin plugin = mock(Plugin.class);
    PluginParameters parameters = mock(PluginParameters.class);
    boolean interactive = false;
    PluginSynchronizer synchronizer = mock(PluginSynchronizer.class);
    List<Future<?>> async = null;
    InterruptedException interruptedException = mock(InterruptedException.class);
    final ExecutorService executorService = mock(ExecutorService.class);
    doThrow(interruptedException).when(plugin).run(any(PluginGraphs.class), any(PluginInteraction.class), any(PluginParameters.class));
    DefaultPluginEnvironment instance = spy(new DefaultPluginEnvironment());
    doReturn(executorService).when(instance).getPluginExecutor();
    when(executorService.submit(any(Callable.class))).thenAnswer(iom -> {
        final Callable callable = iom.getArgument(0);
        callable.call();
        return CompletableFuture.completedFuture(null);
    });
    Object expResult = null;
    Future future = instance.executePluginLater(graph, plugin, parameters, interactive, async, synchronizer);
    Object result = future.get();
    assertEquals(result, expResult);
}
Also used : PluginSynchronizer(au.gov.asd.tac.constellation.plugins.PluginSynchronizer) PluginGraphs(au.gov.asd.tac.constellation.plugins.PluginGraphs) Callable(java.util.concurrent.Callable) Graph(au.gov.asd.tac.constellation.graph.Graph) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) ExecutorService(java.util.concurrent.ExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Example 8 with PluginSynchronizer

use of au.gov.asd.tac.constellation.plugins.PluginSynchronizer in project constellation by constellation-app.

the class DefaultPluginEnvironmentNGTest method testExecutePluginLaterThrowsPluginException.

@Test
public void testExecutePluginLaterThrowsPluginException() throws ExecutionException, InterruptedException, PluginException {
    System.out.println("executePluginLater");
    Graph graph = mock(Graph.class);
    Plugin plugin = mock(Plugin.class);
    PluginParameters parameters = mock(PluginParameters.class);
    boolean interactive = false;
    PluginSynchronizer synchronizer = mock(PluginSynchronizer.class);
    List<Future<?>> async = null;
    PluginException pluginException = mock(PluginException.class);
    final ExecutorService executorService = mock(ExecutorService.class);
    doThrow(pluginException).when(plugin).run(any(PluginGraphs.class), any(PluginInteraction.class), any(PluginParameters.class));
    when(pluginException.getNotificationLevel()).thenReturn(PluginNotificationLevel.FATAL);
    DefaultPluginEnvironment instance = spy(new DefaultPluginEnvironment());
    doReturn(executorService).when(instance).getPluginExecutor();
    when(executorService.submit(any(Callable.class))).thenAnswer(iom -> {
        final Callable callable = iom.getArgument(0);
        callable.call();
        return CompletableFuture.completedFuture(null);
    });
    Object expResult = null;
    Future future = instance.executePluginLater(graph, plugin, parameters, interactive, async, synchronizer);
    Object result = future.get();
    assertEquals(result, expResult);
}
Also used : PluginSynchronizer(au.gov.asd.tac.constellation.plugins.PluginSynchronizer) PluginGraphs(au.gov.asd.tac.constellation.plugins.PluginGraphs) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) Callable(java.util.concurrent.Callable) Graph(au.gov.asd.tac.constellation.graph.Graph) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) ExecutorService(java.util.concurrent.ExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Example 9 with PluginSynchronizer

use of au.gov.asd.tac.constellation.plugins.PluginSynchronizer in project constellation by constellation-app.

the class DefaultPluginEnvironmentNGTest method testExecutePluginLaterWithAsyncThrowsInterruptedException.

@Test
public void testExecutePluginLaterWithAsyncThrowsInterruptedException() throws ExecutionException, InterruptedException {
    System.out.println("executePluginLater");
    Graph graph = mock(Graph.class);
    Plugin plugin = mock(Plugin.class);
    PluginParameters parameters = mock(PluginParameters.class);
    boolean interactive = false;
    PluginSynchronizer synchronizer = mock(PluginSynchronizer.class);
    Future<Object> mockedFuture = mock(Future.class);
    List<Future<?>> async = Arrays.asList(mockedFuture);
    when(mockedFuture.get()).thenThrow(InterruptedException.class);
    final ExecutorService executorService = mock(ExecutorService.class);
    DefaultPluginEnvironment instance = spy(new DefaultPluginEnvironment());
    doReturn(executorService).when(instance).getPluginExecutor();
    when(executorService.submit(any(Callable.class))).thenAnswer(iom -> {
        final Callable callable = iom.getArgument(0);
        callable.call();
        return CompletableFuture.completedFuture(null);
    });
    Object expResult = null;
    Future future = instance.executePluginLater(graph, plugin, parameters, interactive, async, synchronizer);
    Object result = future.get();
    assertEquals(result, expResult);
}
Also used : PluginSynchronizer(au.gov.asd.tac.constellation.plugins.PluginSynchronizer) Graph(au.gov.asd.tac.constellation.graph.Graph) ExecutorService(java.util.concurrent.ExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) Callable(java.util.concurrent.Callable) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Example 10 with PluginSynchronizer

use of au.gov.asd.tac.constellation.plugins.PluginSynchronizer in project constellation by constellation-app.

the class CascadingQueryPlugin method run.

@Override
public final void run(final PluginGraphs graphs, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    final Graph graph = graphs.getGraph();
    // If the graph no longer exists
    if (graph == null) {
        return;
    }
    // Make the graph appear busy
    interaction.setBusy(graph.getId(), true);
    try {
        // Make the progress bar appear nondeterminent
        try {
            interaction.setProgress(0, 0, "Executing child plugins", true);
            Map<Plugin, PluginParameters> childPlugins = getChildPlugins(parameters);
            PluginSynchronizer pluginSynchronizer = new PluginSynchronizer(childPlugins.size() + 1);
            for (final Entry<Plugin, PluginParameters> entry : childPlugins.entrySet()) {
                PluginExecution.withPlugin(entry.getKey()).withParameters(entry.getValue()).synchronizingOn(pluginSynchronizer).executeLater(graph);
            }
            // Wait at gate 0 for CascadingQueryPlugin to finish reading
            graphs.waitAtGate(0);
            // Wait for child plugins at gate 1 to finish reading
            pluginSynchronizer.waitForGate(1);
            // Wait for all plugins to finish reading and querying
            interaction.setProgress(0, 0, "Waiting For Other Plugins...", true);
            // Wait at gate 1 for any SimpleQueryPlugins to finish reading
            graphs.waitAtGate(1);
        } catch (DuplicateKeyException ex) {
            interaction.notify(PluginNotificationLevel.ERROR, ex.getMessage());
        } finally {
            interaction.setProgress(2, 1, "Finished", true);
        }
    } finally {
        interaction.setBusy(graph.getId(), false);
    }
}
Also used : PluginSynchronizer(au.gov.asd.tac.constellation.plugins.PluginSynchronizer) Graph(au.gov.asd.tac.constellation.graph.Graph) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) DuplicateKeyException(au.gov.asd.tac.constellation.graph.DuplicateKeyException) AbstractPlugin(au.gov.asd.tac.constellation.plugins.AbstractPlugin) Plugin(au.gov.asd.tac.constellation.plugins.Plugin)

Aggregations

PluginSynchronizer (au.gov.asd.tac.constellation.plugins.PluginSynchronizer)11 Graph (au.gov.asd.tac.constellation.graph.Graph)9 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)9 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)9 Future (java.util.concurrent.Future)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 Test (org.testng.annotations.Test)7 Callable (java.util.concurrent.Callable)6 ExecutorService (java.util.concurrent.ExecutorService)6 PluginGraphs (au.gov.asd.tac.constellation.plugins.PluginGraphs)3 PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)3 GraphManager (au.gov.asd.tac.constellation.graph.manager.GraphManager)2 PluginExecution (au.gov.asd.tac.constellation.plugins.PluginExecution)2 PluginRegistry (au.gov.asd.tac.constellation.plugins.PluginRegistry)2 PluginParameter (au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)2 DataAccessPlugin (au.gov.asd.tac.constellation.views.dataaccess.plugins.DataAccessPlugin)2 DuplicateKeyException (au.gov.asd.tac.constellation.graph.DuplicateKeyException)1 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)1 AbstractPlugin (au.gov.asd.tac.constellation.plugins.AbstractPlugin)1 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)1