Search in sources :

Example 6 with PluginGraphs

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);
}
Also used : PluginGraphs(au.gov.asd.tac.constellation.plugins.PluginGraphs) ClipboardContent(javafx.scene.input.ClipboardContent) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) SimplePlugin(au.gov.asd.tac.constellation.plugins.templates.SimplePlugin) Clipboard(javafx.scene.input.Clipboard) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) CharArrayWriter(java.io.CharArrayWriter) Date(java.util.Date) PrintWriter(java.io.PrintWriter)

Example 7 with PluginGraphs

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);
    }
}
Also used : PluginType(au.gov.asd.tac.constellation.plugins.PluginType) Level(java.util.logging.Level) TableColumn(javafx.scene.control.TableColumn) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) Cell(org.apache.poi.ss.usermodel.Cell) PluginTags(au.gov.asd.tac.constellation.plugins.templates.PluginTags) TableView(javafx.scene.control.TableView) Sheet(org.apache.poi.ss.usermodel.Sheet) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) PluginGraphs(au.gov.asd.tac.constellation.plugins.PluginGraphs) Logger(java.util.logging.Logger) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) Collectors(java.util.stream.Collectors) File(java.io.File) PluginNotificationLevel(au.gov.asd.tac.constellation.plugins.PluginNotificationLevel) PluginInfo(au.gov.asd.tac.constellation.plugins.PluginInfo) List(java.util.List) Row(org.apache.poi.ss.usermodel.Row) ObservableList(javafx.collections.ObservableList) SimplePlugin(au.gov.asd.tac.constellation.plugins.templates.SimplePlugin) Pagination(javafx.scene.control.Pagination) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObservableList(javafx.collections.ObservableList) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) FileOutputStream(java.io.FileOutputStream) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Example 8 with PluginGraphs

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());
    }
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) CopyGraphUtilities(au.gov.asd.tac.constellation.graph.utilities.io.CopyGraphUtilities) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) IntegerParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.IntegerParameterType.IntegerParameterValue) SimpleEditPlugin(au.gov.asd.tac.constellation.plugins.templates.SimpleEditPlugin) ArrayList(java.util.ArrayList) Graph(au.gov.asd.tac.constellation.graph.Graph) Future(java.util.concurrent.Future) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) PluginParametersPane(au.gov.asd.tac.constellation.plugins.gui.PluginParametersPane) PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) PluginRegistry(au.gov.asd.tac.constellation.plugins.PluginRegistry) ExecutorService(java.util.concurrent.ExecutorService) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) SeparatorConstants(au.gov.asd.tac.constellation.utilities.text.SeparatorConstants) Set(java.util.Set) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) GraphRecordStoreUtilities(au.gov.asd.tac.constellation.graph.processing.GraphRecordStoreUtilities) PluginGraphs(au.gov.asd.tac.constellation.plugins.PluginGraphs) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) Executors(java.util.concurrent.Executors) PluginNotificationLevel(au.gov.asd.tac.constellation.plugins.PluginNotificationLevel) ExecutionException(java.util.concurrent.ExecutionException) GlobalParameters(au.gov.asd.tac.constellation.views.dataaccess.GlobalParameters) List(java.util.List) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) IntegerParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.IntegerParameterType) VisualSchemaPluginRegistry(au.gov.asd.tac.constellation.graph.schema.visual.VisualSchemaPluginRegistry) SimplePlugin(au.gov.asd.tac.constellation.plugins.templates.SimplePlugin) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) ArrayList(java.util.ArrayList) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) ExecutionException(java.util.concurrent.ExecutionException) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph)

Example 9 with PluginGraphs

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);
}
Also used : PluginGraphs(au.gov.asd.tac.constellation.plugins.PluginGraphs) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) TextPluginInteraction(au.gov.asd.tac.constellation.plugins.text.TextPluginInteraction) Schema(au.gov.asd.tac.constellation.graph.schema.Schema) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) TextPluginInteraction(au.gov.asd.tac.constellation.plugins.text.TextPluginInteraction) Test(org.testng.annotations.Test)

Example 10 with PluginGraphs

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;
}
Also used : PluginGraphs(au.gov.asd.tac.constellation.plugins.PluginGraphs) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) GraphReport(au.gov.asd.tac.constellation.plugins.reporting.GraphReport) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) ExecutionException(java.util.concurrent.ExecutionException) PluginReport(au.gov.asd.tac.constellation.plugins.reporting.PluginReport) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction)

Aggregations

PluginGraphs (au.gov.asd.tac.constellation.plugins.PluginGraphs)10 PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)10 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)8 SimplePlugin (au.gov.asd.tac.constellation.plugins.templates.SimplePlugin)7 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)6 Graph (au.gov.asd.tac.constellation.graph.Graph)3 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)3 ExecutionException (java.util.concurrent.ExecutionException)3 PluginExecution (au.gov.asd.tac.constellation.plugins.PluginExecution)2 PluginNotificationLevel (au.gov.asd.tac.constellation.plugins.PluginNotificationLevel)2 PluginParametersSwingDialog (au.gov.asd.tac.constellation.plugins.gui.PluginParametersSwingDialog)2 GraphReport (au.gov.asd.tac.constellation.plugins.reporting.GraphReport)2 PluginReport (au.gov.asd.tac.constellation.plugins.reporting.PluginReport)2 WaitForQueriesToCompleteTask (au.gov.asd.tac.constellation.views.dataaccess.tasks.WaitForQueriesToCompleteTask)2 File (java.io.File)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 GraphWriteMethods (au.gov.asd.tac.constellation.graph.GraphWriteMethods)1 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)1