use of au.gov.asd.tac.constellation.plugins.PluginGraphs in project constellation by constellation-app.
the class PluginReportPane method saveToClipboard.
/**
* Saves a text representation of this PluginReport to the clipboard.
*/
private void saveToClipboard() {
CharArrayWriter writer = new CharArrayWriter();
try (PrintWriter out = new PrintWriter(writer)) {
out.append("Name: " + pluginReport.getPluginName() + SeparatorConstants.NEWLINE);
out.append("Description: " + pluginReport.getPluginDescription() + SeparatorConstants.NEWLINE);
out.append("Message: " + pluginReport.getMessage() + SeparatorConstants.NEWLINE);
out.append("Tags: " + Arrays.toString(pluginReport.getTags()) + SeparatorConstants.NEWLINE);
out.append("Start: " + dateFormat.format(new Date(pluginReport.getStartTime())) + SeparatorConstants.NEWLINE);
out.append("Stop: " + dateFormat.format(new Date(pluginReport.getStopTime())) + SeparatorConstants.NEWLINE);
if (pluginReport.getError() != null) {
out.append("Error: " + pluginReport.getError().getMessage() + "\n\n");
pluginReport.getError().printStackTrace(out);
}
}
final Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
content.putString(writer.toString());
clipboard.setContent(content);
// TODO: can't do this because of circular dependancy
// ClipboardUtilities.copyToClipboard(writer.toString());
PluginExecution.withPlugin(new SimplePlugin("Copy To Clipboard") {
@Override
protected void execute(PluginGraphs graphs, PluginInteraction interaction, PluginParameters parameters) throws InterruptedException, PluginException {
ConstellationLoggerHelper.copyPropertyBuilder(this, writer.toString().length(), ConstellationLoggerHelper.SUCCESS);
}
}).executeLater(null);
}
use of au.gov.asd.tac.constellation.plugins.PluginGraphs in project constellation by constellation-app.
the class ExportToExcelFilePlugin method execute.
@Override
public void execute(final PluginGraphs graphs, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
try (final SXSSFWorkbook workbook = new SXSSFWorkbook(SXSSFWorkbook.DEFAULT_WINDOW_SIZE)) {
final Sheet sheet = workbook.createSheet(sheetName);
// get the indexes of all visible columns
final List<Integer> visibleIndices = table.getColumns().stream().filter(column -> column.isVisible()).map(column -> table.getColumns().indexOf(column)).collect(Collectors.toList());
// iterate through the visible columns and print each ones name to the sheet
final Row headerRow = sheet.createRow(0);
visibleIndices.forEach(index -> {
final TableColumn<ObservableList<String>, ?> column = table.getColumns().get(index);
final Cell headerCell = headerRow.createCell(visibleIndices.indexOf(index));
headerCell.setCellValue(column.getText());
});
// store the current page so we can reset it after the export
final int currentPage = pagination.getCurrentPageIndex();
if (selectedOnly) {
// iterate through all the pages in the table and write the selected rows to the sheet
for (int i = 0; i < pagination.getPageCount(); i++) {
pagination.getPageFactory().call(i);
// Calculates the start index in the sheet based on the current table page
// Because only selected rows are included this could leave a gap in the sheet
// + 1 to skip the header
// + 1 to skip the header
final int startIndex = rowsPerPage * i + 1;
final Thread writeSheetThread = new Thread("Export to Excel File: Writing Sheet") {
@Override
public void run() {
// get a copy of the table data so that users can continue working
final List<ObservableList<String>> data = getTable().getSelectionModel().getSelectedItems();
writeRecords(sheet, visibleIndices, data, startIndex);
}
};
writeSheetThread.start();
writeSheetThread.join();
}
} else {
// iterate through all the pages in the table and write their rows to the sheet
for (int i = 0; i < pagination.getPageCount(); i++) {
pagination.getPageFactory().call(i);
// Calculates the start index in the sheet based on the current table page
// + 1 to skip the header
final int startIndex = rowsPerPage * i + 1;
final Thread writeSheetThread = new Thread("Export to Excel File: Writing Sheet") {
@Override
public void run() {
// get a copy of the table data so that users can continue working
final List<ObservableList<String>> data = getTable().getItems();
writeRecords(sheet, visibleIndices, data, startIndex);
}
};
writeSheetThread.start();
writeSheetThread.join();
}
}
// call the page factory function once more to go back to the original page index
pagination.getPageFactory().call(currentPage);
// The sheet has now been created. Time to write it to the file
final Thread outputThread = new Thread("Export to Excel File: Writing File") {
@Override
public void run() {
try (final FileOutputStream fileStream = new FileOutputStream(getFile())) {
workbook.write(fileStream);
LOGGER.log(Level.INFO, "Table View data written to Excel file");
} catch (final IOException ex) {
interaction.notify(PluginNotificationLevel.ERROR, ex.getLocalizedMessage());
}
workbook.dispose();
}
};
outputThread.start();
outputThread.join();
} catch (final IOException ex) {
throw new PluginException(PluginNotificationLevel.ERROR, ex);
}
}
use of au.gov.asd.tac.constellation.plugins.PluginGraphs in project constellation by constellation-app.
the class WorkflowQueryPlugin method execute.
@Override
protected void execute(final PluginGraphs pluginGraphs, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
final Graph graph = pluginGraphs.getGraph();
final ReadableGraph readableGraph = graph.getReadableGraph();
// buildId batches
try {
queryBatches = GraphRecordStoreUtilities.getSelectedVerticesBatches(readableGraph, parameters.getIntegerValue(BATCH_SIZE_PARAMETER_ID));
} finally {
readableGraph.release();
}
pluginGraphs.waitAtGate(1);
// create a service for executing jobs, limiting concurrent executions to the max concurrent plugins parameter.
final int maxConcurrentPlugins = parameters.getIntegerValue(MAX_CONCURRENT_PLUGINS_PARAMETER_ID);
final ExecutorService workflowExecutor = Executors.newFixedThreadPool(maxConcurrentPlugins);
// schedule a job for each batch, where the job is to execute the defined workflow
final List<Future<?>> workerPlugins = new ArrayList<>();
final List<PluginException> exceptions = new ArrayList<>();
if (queryBatches.isEmpty()) {
queryBatches.add(new GraphRecordStore());
}
// run plugin once for every batch record store
queryBatches.forEach(batch -> {
final StoreGraph batchGraph = new StoreGraph(graph.getSchema() != null ? graph.getSchema().getFactory().createSchema() : null);
batchGraph.getSchema().newGraph(batchGraph);
CopyGraphUtilities.copyGraphTypeElements(readableGraph, batchGraph);
GraphRecordStoreUtilities.addRecordStoreToGraph(batchGraph, batch, true, true, null);
final WorkerQueryPlugin worker = new WorkerQueryPlugin(getWorkflow(), batchGraph, exceptions, getErrorHandlingPlugin(), addPartialResults());
workerPlugins.add(workflowExecutor.submit(() -> {
Thread.currentThread().setName(THREAD_POOL_NAME);
try {
PluginExecution.withPlugin(worker).withParameters(parameters).executeNow(graph);
} catch (InterruptedException | PluginException ex) {
throw new RuntimeException(ex);
}
}));
});
final int[] workerFailCount = { 0 };
for (Future<?> worker : workerPlugins) {
try {
worker.get();
} catch (InterruptedException ex) {
workerPlugins.forEach(workerToInterrupt -> workerToInterrupt.cancel(true));
throw ex;
} catch (ExecutionException ex) {
workerFailCount[0]++;
}
}
workflowExecutor.shutdown();
// if there were any errors, collect them and display them to the user
if (!exceptions.isEmpty()) {
final StringBuilder entireException = new StringBuilder();
entireException.append(workerFailCount[0]).append(" workers failed.").append(SeparatorConstants.NEWLINE);
exceptions.forEach(ex -> entireException.append(ex.getMessage()).append(SeparatorConstants.NEWLINE));
throw new PluginException(PluginNotificationLevel.ERROR, entireException.toString());
}
}
use of au.gov.asd.tac.constellation.plugins.PluginGraphs in project constellation by constellation-app.
the class WorkflowQueryPluginNGTest method testExecute.
/**
* Test of execute method, of class WorkflowQueryPlugin.
*/
@Test
public void testExecute() throws Exception {
System.out.println("execute");
final Schema schema = SchemaFactoryUtilities.getSchemaFactory(AnalyticSchemaFactory.ANALYTIC_SCHEMA_ID).createSchema();
final DualGraph graph = new DualGraph(schema);
// mock PluginGraphs
final PluginGraphs pluginGraphs = mock(PluginGraphs.class);
when(pluginGraphs.getGraph()).thenReturn(graph);
final PluginInteraction interaction = new TextPluginInteraction();
final WorkflowQueryPlugin instance = new WorkflowQueryPluginImpl();
final PluginParameters parameters = instance.createParameters();
instance.execute(pluginGraphs, interaction, parameters);
}
use of au.gov.asd.tac.constellation.plugins.PluginGraphs in project constellation by constellation-app.
the class DefaultPluginEnvironment method executePluginNow.
@Override
public Object executePluginNow(final Graph graph, final Plugin plugin, final PluginParameters parameters, final boolean interactive) throws InterruptedException, PluginException {
if (graph == null) {
LOGGER.log(Level.FINE, GRAPH_NULL_WARNING_MESSAGE, plugin.getName());
}
final ThreadConstraints callingConstraints = ThreadConstraints.getConstraints();
final int silentCount = callingConstraints.getSilentCount();
final boolean alwaysSilent = callingConstraints.isAlwaysSilent();
callingConstraints.setSilentCount(0);
callingConstraints.setAlwaysSilent(alwaysSilent || silentCount > 0);
final GraphReport graphReport = graph == null ? null : GraphReportManager.getGraphReport(graph.getId());
PluginReport parentReport = null;
PluginReport currentReport = null;
if (graphReport != null) {
parentReport = callingConstraints.getCurrentReport();
if (parentReport == null) {
currentReport = graphReport.addPluginReport(plugin);
} else {
currentReport = parentReport.addChildReport(plugin);
}
callingConstraints.setCurrentReport(currentReport);
}
final PluginManager manager = new PluginManager(DefaultPluginEnvironment.this, plugin, graph, interactive, null);
final PluginGraphs graphs = new DefaultPluginGraphs(manager);
final PluginInteraction interaction = new DefaultPluginInteraction(manager, currentReport);
try {
ConstellationLogger.getDefault().pluginStarted(plugin, parameters, graph);
} catch (final Exception ex) {
LOGGER.log(Level.SEVERE, ex.getLocalizedMessage());
}
try {
if (parameters != null) {
plugin.updateParameters(graph, parameters);
}
if (interactive && parameters != null) {
if (interaction.prompt(plugin.getName(), parameters)) {
plugin.run(graphs, interaction, parameters);
}
} else {
plugin.run(graphs, interaction, parameters);
}
} catch (final InterruptedException ex) {
auditPluginError(plugin, ex);
reportException(plugin.getName(), interaction, currentReport, null, ex);
Thread.currentThread().interrupt();
throw ex;
} catch (final PluginException ex) {
auditPluginError(plugin, ex);
reportException(plugin.getName(), interaction, currentReport, ex.getNotificationLevel(), ex);
throw ex;
} catch (final Exception ex) {
auditPluginError(plugin, ex);
reportException(plugin.getName(), interaction, currentReport, PluginNotificationLevel.ERROR, ex);
throw ex;
} finally {
callingConstraints.setSilentCount(silentCount);
callingConstraints.setAlwaysSilent(alwaysSilent);
if (currentReport != null) {
currentReport.stop();
callingConstraints.setCurrentReport(parentReport);
currentReport.firePluginReportChangedEvent();
}
try {
ConstellationLogger.getDefault().pluginStopped(plugin, parameters);
} catch (final Exception ex) {
LOGGER.log(Level.SEVERE, ex.getLocalizedMessage());
}
}
return null;
}
Aggregations