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