Search in sources :

Example 36 with PluginException

use of au.gov.asd.tac.constellation.plugins.PluginException 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 37 with PluginException

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

the class SelectTopNPlugin method edit.

@Override
protected void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    final String mode = parameters.getParameters().get(MODE_PARAMETER_ID).getStringValue();
    final String typeCategory = parameters.getParameters().get(TYPE_CATEGORY_PARAMETER_ID).getStringValue();
    final List<String> subTypes = parameters.getParameters().get(TYPE_PARAMETER_ID).getMultiChoiceValue().getChoices();
    final int limit = parameters.getParameters().get(LIMIT_PARAMETER_ID).getIntegerValue();
    if (mode == null || (!mode.equals(NODE) && !mode.equals(TRANSACTION))) {
        throw new PluginException(PluginNotificationLevel.ERROR, "Invalid mode value provided");
    }
    if (typeCategory == null) {
        throw new PluginException(PluginNotificationLevel.ERROR, "Select a type category");
    }
    if (subTypes.isEmpty()) {
        throw new PluginException(PluginNotificationLevel.ERROR, "Select some types to perform the calculation");
    }
    final int vertexLabelAttribute = VisualConcept.VertexAttribute.LABEL.get(graph);
    if (vertexLabelAttribute == Graph.NOT_FOUND) {
        throw new PluginException(PluginNotificationLevel.ERROR, String.format(MISSING_PROPERTY_FORMAT, VisualConcept.VertexAttribute.LABEL.getName()));
    }
    final int vertexSelectedAttribute = VisualConcept.VertexAttribute.SELECTED.get(graph);
    if (vertexSelectedAttribute == Graph.NOT_FOUND) {
        throw new PluginException(PluginNotificationLevel.ERROR, String.format(MISSING_PROPERTY_FORMAT, VisualConcept.VertexAttribute.SELECTED.getName()));
    }
    final int vertexTypeAttribute = AnalyticConcept.VertexAttribute.TYPE.get(graph);
    if (vertexTypeAttribute == Graph.NOT_FOUND) {
        throw new PluginException(PluginNotificationLevel.ERROR, String.format(MISSING_PROPERTY_FORMAT, AnalyticConcept.VertexAttribute.TYPE.getName()));
    }
    final int transactionTypeAttribute = AnalyticConcept.TransactionAttribute.TYPE.get(graph);
    if (transactionTypeAttribute == Graph.NOT_FOUND) {
        throw new PluginException(PluginNotificationLevel.ERROR, String.format(MISSING_PROPERTY_FORMAT, AnalyticConcept.TransactionAttribute.TYPE.getName()));
    }
    // make a set of the highlighted nodes
    final Set<Integer> selectedNodes = new HashSet<>();
    final int vertexCount = graph.getVertexCount();
    for (int position = 0; position < vertexCount; position++) {
        final int vxId = graph.getVertex(position);
        if (graph.getBooleanValue(vertexSelectedAttribute, vxId)) {
            selectedNodes.add(vxId);
        }
    }
    if (selectedNodes.isEmpty()) {
        throw new PluginException(PluginNotificationLevel.ERROR, "Select at least 1 node");
    }
    // calculate the occurrences of destination vertices
    int txId;
    int sourceVxId;
    int destinationVxId;
    int targetVxId;
    int step = 0;
    SchemaVertexType destinationVertexType;
    SchemaTransactionType transactionType;
    final Map<Integer, Integer> occurrences = new HashMap<>();
    for (final Integer vxId : selectedNodes) {
        final String label = graph.getStringValue(vertexLabelAttribute, vxId);
        interaction.setProgress(++step, selectedNodes.size(), String.format("Calculating top %s for %s", limit, label), true);
        final int transactionCount = graph.getVertexTransactionCount(vxId);
        for (int position = 0; position < transactionCount; position++) {
            txId = graph.getVertexTransaction(vxId, position);
            sourceVxId = graph.getTransactionSourceVertex(txId);
            destinationVxId = graph.getTransactionDestinationVertex(txId);
            targetVxId = vxId == sourceVxId ? destinationVxId : sourceVxId;
            switch(mode) {
                case NODE:
                    destinationVertexType = graph.getObjectValue(vertexTypeAttribute, targetVxId);
                    if (destinationVertexType != null && subTypes.contains(destinationVertexType.getName())) {
                        if (!occurrences.containsKey(targetVxId)) {
                            occurrences.put(targetVxId, 0);
                        }
                        occurrences.put(targetVxId, occurrences.get(targetVxId) + 1);
                    }
                    break;
                case TRANSACTION:
                    transactionType = graph.getObjectValue(transactionTypeAttribute, txId);
                    if (transactionType != null && subTypes.contains(transactionType.getName())) {
                        if (!occurrences.containsKey(targetVxId)) {
                            occurrences.put(targetVxId, 0);
                        }
                        occurrences.put(targetVxId, occurrences.get(targetVxId) + 1);
                    }
                    break;
                default:
                    break;
            }
        }
        // make a map sorted by the count in descending order
        final LinkedHashMap<Integer, Integer> sortedMap = occurrences.entrySet().stream().sorted(Map.Entry.comparingByValue(Collections.reverseOrder())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
        sortedMap.keySet().stream().limit(limit).forEach(id -> graph.setBooleanValue(vertexSelectedAttribute, id, true));
        interaction.setProgress(1, 0, "Selected " + sortedMap.size() + " nodes.", true);
    }
    interaction.setProgress(1, 0, "Completed successfully", true);
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) SchemaTransactionType(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType) SchemaVertexTypeUtilities(au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexTypeUtilities) IntegerParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.IntegerParameterType.IntegerParameterValue) SchemaTransactionTypeUtilities(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionTypeUtilities) ParameterChange(au.gov.asd.tac.constellation.plugins.parameters.ParameterChange) SingleChoiceParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.SingleChoiceParameterType) MultiChoiceParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.MultiChoiceParameterType.MultiChoiceParameterValue) PluginType(au.gov.asd.tac.constellation.plugins.PluginType) HashMap(java.util.HashMap) VisualConcept(au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept) ArrayList(java.util.ArrayList) Graph(au.gov.asd.tac.constellation.graph.Graph) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) DataAccessPlugin(au.gov.asd.tac.constellation.views.dataaccess.plugins.DataAccessPlugin) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) ServiceProviders(org.openide.util.lookup.ServiceProviders) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) Map(java.util.Map) ServiceProvider(org.openide.util.lookup.ServiceProvider) PluginTags(au.gov.asd.tac.constellation.plugins.templates.PluginTags) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) MultiChoiceParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.MultiChoiceParameterType) Set(java.util.Set) Logger(java.util.logging.Logger) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) Collectors(java.util.stream.Collectors) PluginNotificationLevel(au.gov.asd.tac.constellation.plugins.PluginNotificationLevel) SchemaVertexType(au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType) PluginInfo(au.gov.asd.tac.constellation.plugins.PluginInfo) List(java.util.List) AnalyticConcept(au.gov.asd.tac.constellation.graph.schema.analytic.concept.AnalyticConcept) IntegerParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.IntegerParameterType) SimpleQueryPlugin(au.gov.asd.tac.constellation.plugins.templates.SimpleQueryPlugin) DataAccessPluginCoreType(au.gov.asd.tac.constellation.views.dataaccess.plugins.DataAccessPluginCoreType) Messages(org.openide.util.NbBundle.Messages) SingleChoiceParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.SingleChoiceParameterType.SingleChoiceParameterValue) Collections(java.util.Collections) SchemaVertexType(au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) SchemaTransactionType(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 38 with PluginException

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

the class MergeNodesPlugin method edit.

@Override
public void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    int mergedCount = 0;
    interaction.setProgress(0, 0, "Merging nodes...", true);
    final String mergeNodeTypeName = parameters.getParameters().get(MERGE_TYPE_PARAMETER_ID).getStringValue();
    if (mergeNodeTypeName == null) {
        throw new PluginException(PluginNotificationLevel.ERROR, "Select a Merge By option.");
    }
    if (!MERGE_TYPES.containsKey(mergeNodeTypeName)) {
        throw new PluginException(PluginNotificationLevel.FATAL, String.format("Merge node type %s not found.", mergeNodeTypeName));
    }
    final int threshold = parameters.getParameters().get(THRESHOLD_PARAMETER_ID).getIntegerValue();
    final GraphElementMerger merger = MERGERS.get(parameters.getParameters().get(MERGER_PARAMETER_ID).getStringValue());
    final Comparator<String> leadNodeChooser = VERTEX_CHOOSER.get(parameters.getParameters().get(LEAD_PARAMETER_ID).getStringValue());
    final boolean selectedOnly = parameters.getParameters().get(SELECTED_PARAMETER_ID).getBooleanValue();
    final MergeNodeType mergeNodeType = MERGE_TYPES.get(mergeNodeTypeName);
    final Map<Integer, Set<Integer>> nodesToMerge;
    try {
        nodesToMerge = mergeNodeType.getNodesToMerge(graph, leadNodeChooser, threshold, selectedOnly);
    } catch (MergeException ex) {
        throw new PluginException(PluginNotificationLevel.ERROR, ex);
    }
    // perform the merge
    for (final Map.Entry<Integer, Set<Integer>> entry : nodesToMerge.entrySet()) {
        mergedCount += mergeVertices(graph, entry.getValue(), entry.getKey(), merger);
    }
    interaction.setProgress(1, 0, "Merged " + mergedCount + " nodes.", true);
    PluginExecution.withPlugin(VisualSchemaPluginRegistry.COMPLETE_SCHEMA).executeNow(graph);
}
Also used : Set(java.util.Set) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) MergeException(au.gov.asd.tac.constellation.views.dataaccess.plugins.clean.MergeNodeType.MergeException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IgnoreSurvivingGraphElementMerger(au.gov.asd.tac.constellation.graph.mergers.IgnoreSurvivingGraphElementMerger) PriorityMergedGraphElementMerger(au.gov.asd.tac.constellation.graph.mergers.PriorityMergedGraphElementMerger) GraphElementMerger(au.gov.asd.tac.constellation.graph.GraphElementMerger) PrioritySurvivingGraphElementMerger(au.gov.asd.tac.constellation.graph.mergers.PrioritySurvivingGraphElementMerger) IgnoreMergedGraphElementMerger(au.gov.asd.tac.constellation.graph.mergers.IgnoreMergedGraphElementMerger)

Example 39 with PluginException

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

the class TestParametersPluginNGTest method testQueryException1.

/**
 * Test of query method, of class TestParametersPlugin. Tests throwing of
 * debug pluginException
 */
@Test(expectedExceptions = PluginException.class)
public void testQueryException1() throws Exception {
    System.out.println("throw pluginexception1");
    final TestParametersPlugin instance = new TestParametersPlugin();
    final PluginParameters result = instance.createParameters();
    final GraphRecordStore recordStore = new GraphRecordStore();
    final DefaultPluginInteraction interaction = new DefaultPluginInteraction(null, null);
    // Set plugin query name here before execution
    result.getParameters().get(CoreGlobalParameters.QUERY_NAME_PARAMETER_ID).setStringValue("TESTPARAMETERSPLUGIN");
    // Set plugin parameters here before execution
    result.getParameters().get(TestParametersPlugin.LEVEL_PARAMETER_ID).setStringValue("Debug");
    try {
        instance.query(recordStore, interaction, result);
    } catch (final PluginException ex) {
        assertEquals(ex.getNotificationLevel(), PluginNotificationLevel.DEBUG);
        throw ex;
    }
}
Also used : PluginException(au.gov.asd.tac.constellation.plugins.PluginException) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) DefaultPluginInteraction(au.gov.asd.tac.constellation.graph.node.plugins.DefaultPluginInteraction) Test(org.testng.annotations.Test)

Example 40 with PluginException

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

the class TestParametersPluginNGTest method testQueryException3.

/**
 * Test of query method, of class TestParametersPlugin. Tests throwing of
 * warning pluginException
 */
@Test(expectedExceptions = PluginException.class)
public void testQueryException3() throws Exception {
    System.out.println("throw pluginexception3");
    final TestParametersPlugin instance = new TestParametersPlugin();
    final PluginParameters result = instance.createParameters();
    final GraphRecordStore recordStore = new GraphRecordStore();
    final DefaultPluginInteraction interaction = new DefaultPluginInteraction(null, null);
    // Set plugin query name here before execution
    result.getParameters().get(CoreGlobalParameters.QUERY_NAME_PARAMETER_ID).setStringValue("TESTPARAMETERSPLUGIN");
    // Set plugin parameters here before execution
    result.getParameters().get(TestParametersPlugin.LEVEL_PARAMETER_ID).setStringValue("Warning");
    try {
        instance.query(recordStore, interaction, result);
    } catch (final PluginException ex) {
        assertEquals(ex.getNotificationLevel(), PluginNotificationLevel.WARNING);
        throw ex;
    }
}
Also used : PluginException(au.gov.asd.tac.constellation.plugins.PluginException) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) DefaultPluginInteraction(au.gov.asd.tac.constellation.graph.node.plugins.DefaultPluginInteraction) Test(org.testng.annotations.Test)

Aggregations

PluginException (au.gov.asd.tac.constellation.plugins.PluginException)60 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)28 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)23 Graph (au.gov.asd.tac.constellation.graph.Graph)21 ArrayList (java.util.ArrayList)20 PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)19 Test (org.testng.annotations.Test)16 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)14 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)12 IOException (java.io.IOException)12 List (java.util.List)12 GraphWriteMethods (au.gov.asd.tac.constellation.graph.GraphWriteMethods)10 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)10 File (java.io.File)10 PluginNotificationLevel (au.gov.asd.tac.constellation.plugins.PluginNotificationLevel)9 Map (java.util.Map)9 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)8 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)8 VisualConcept (au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept)8 PluginGraphs (au.gov.asd.tac.constellation.plugins.PluginGraphs)8