Search in sources :

Example 31 with PluginInteraction

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

the class ExtractWordsFromTextPlugin method edit.

@Override
public void edit(final GraphWriteMethods wg, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    interaction.setProgress(0, 0, "Extracting...", true);
    /*
         Retrieving attributes
         */
    final Map<String, PluginParameter<?>> extractEntityParameters = parameters.getParameters();
    final String contentAttribute = extractEntityParameters.get(ATTRIBUTE_PARAMETER_ID).getStringValue();
    final String words = extractEntityParameters.get(WORDS_PARAMETER_ID).getStringValue() == null ? null : extractEntityParameters.get(WORDS_PARAMETER_ID).getStringValue().trim();
    final boolean useRegex = extractEntityParameters.get(USE_REGEX_PARAMETER_ID).getBooleanValue();
    final boolean wholeWordOnly = extractEntityParameters.get(WHOLE_WORDS_ONLY_PARAMETER_ID).getBooleanValue();
    final int wordLength = parameters.getParameters().get(MIN_WORD_LENGTH_PARAMETER_ID).getIntegerValue();
    final boolean removeSpecialChars = extractEntityParameters.get(REMOVE_SPECIAL_CHARS_PARAMETER_ID).getBooleanValue();
    final boolean toLowerCase = extractEntityParameters.get(LOWER_CASE_PARAMETER_ID).getBooleanValue();
    final boolean types = extractEntityParameters.get(SCHEMA_TYPES_PARAMETER_ID).getBooleanValue();
    final String inOrOut = extractEntityParameters.get(IN_OR_OUT_PARAMETER_ID).getStringValue();
    final boolean selectedOnly = extractEntityParameters.get(SELECTED_ONLY_PARAMETER_ID).getBooleanValue();
    final boolean regexOnly = extractEntityParameters.get(REGEX_ONLY_PARAMETER_ID).getBooleanValue();
    if (!OUTGOING.equals(inOrOut) && !INCOMING.equals(inOrOut)) {
        var msg = String.format("Parameter %s must be '%s' or '%s'", REGEX_ONLY_PARAMETER_ID, OUTGOING, INCOMING);
        throw new PluginException(PluginNotificationLevel.ERROR, msg);
    }
    final boolean outgoing = OUTGOING.equals(inOrOut);
    /*
         Retrieving attribute IDs
         */
    final int vertexIdentifierAttributeId = VisualConcept.VertexAttribute.IDENTIFIER.ensure(wg);
    final int vertexTypeAttributeId = AnalyticConcept.VertexAttribute.TYPE.ensure(wg);
    final int transactionTypeAttributeId = AnalyticConcept.TransactionAttribute.TYPE.ensure(wg);
    final int transactionDatetimeAttributeId = TemporalConcept.TransactionAttribute.DATETIME.ensure(wg);
    final int transactionContentAttributeId = wg.getAttribute(GraphElementType.TRANSACTION, contentAttribute);
    final int transactionSelectedAttributeId = VisualConcept.TransactionAttribute.SELECTED.ensure(wg);
    // 
    if (transactionContentAttributeId == Graph.NOT_FOUND) {
        final NotifyDescriptor nd = new NotifyDescriptor.Message(String.format("The specified attribute %s does not exist.", contentAttribute), NotifyDescriptor.WARNING_MESSAGE);
        DialogDisplayer.getDefault().notify(nd);
        return;
    }
    final int transactionCount = wg.getTransactionCount();
    if (regexOnly) {
        // This choice ignores several other parameters, so is a bit simpler
        // even if there code commonalities, but combining the if/else
        // code would make things even more complex.
        // 
        // The input words are treated as trusted regular expressions,
        // so the caller has to know what they're doing.
        // This is power-use mode.
        // 
        // Each line of the input words is a regex.
        // Use them as-is for the power users.
        // 
        final List<Pattern> patterns = new ArrayList<>();
        if (StringUtils.isNotBlank(words)) {
            for (String word : words.split(SeparatorConstants.NEWLINE)) {
                word = word.strip();
                if (!word.isEmpty()) {
                    final Pattern pattern = Pattern.compile(word);
                    patterns.add(pattern);
                }
            }
        }
        if (!patterns.isEmpty()) {
            // Use a set to hold the words.
            // If a word is found multiple times, there's no point adding multiple nodes.
            // 
            final Set<String> matched = new HashSet<>();
            // 
            for (int transactionPosition = 0; transactionPosition < transactionCount; transactionPosition++) {
                final int transactionId = wg.getTransaction(transactionPosition);
                final boolean selectedTx = wg.getBooleanValue(transactionSelectedAttributeId, transactionId);
                if (selectedOnly && !selectedTx) {
                    continue;
                }
                final String content = wg.getStringValue(transactionContentAttributeId, transactionId);
                /*
                     Does the transaction have content?
                     */
                if (StringUtils.isBlank(content)) {
                    continue;
                }
                /*
                     Ignore other "referenced" transactions because that's not useful
                     */
                if (wg.getObjectValue(transactionTypeAttributeId, transactionId) != null && wg.getObjectValue(transactionTypeAttributeId, transactionId).equals(AnalyticConcept.TransactionType.REFERENCED)) {
                    continue;
                }
                patterns.stream().map(pattern -> pattern.matcher(content)).forEach(matcher -> {
                    while (matcher.find()) {
                        if (matcher.groupCount() == 0) {
                            // The regex doesn't have an explicit capture group, so capture the lot.
                            // 
                            final String g = matcher.group();
                            matched.add(toLowerCase ? g.toLowerCase() : g);
                        } else {
                            // 
                            for (int i = 1; i <= matcher.groupCount(); i++) {
                                final String g = matcher.group(i);
                                matched.add(toLowerCase ? g.toLowerCase() : g);
                            }
                        }
                    }
                });
                // 
                if (!matched.isEmpty()) {
                    /*
                         Retrieving information needed to create new transactions
                         */
                    final int sourceVertexId = wg.getTransactionSourceVertex(transactionId);
                    final int destinationVertexId = wg.getTransactionDestinationVertex(transactionId);
                    final ZonedDateTime datetime = wg.getObjectValue(transactionDatetimeAttributeId, transactionId);
                    matched.forEach(word -> {
                        final int newVertexId = wg.addVertex();
                        wg.setStringValue(vertexIdentifierAttributeId, newVertexId, word);
                        wg.setObjectValue(vertexTypeAttributeId, newVertexId, AnalyticConcept.VertexType.WORD);
                        final int newTransactionId = outgoing ? wg.addTransaction(sourceVertexId, newVertexId, true) : wg.addTransaction(newVertexId, destinationVertexId, true);
                        wg.setObjectValue(transactionDatetimeAttributeId, newTransactionId, datetime);
                        wg.setObjectValue(transactionTypeAttributeId, newTransactionId, AnalyticConcept.TransactionType.REFERENCED);
                        wg.setStringValue(transactionContentAttributeId, newTransactionId, content);
                    });
                }
            }
        }
    // End of regexOnly.
    } else {
        // The original logic.
        final List<Pattern> patterns = patternsFromWords(words, useRegex, wholeWordOnly);
        /*
             Iterating over all the transactions in the graph
             */
        final List<String> foundWords = new ArrayList<>();
        for (int transactionPosition = 0; transactionPosition < transactionCount; transactionPosition++) {
            foundWords.clear();
            final int transactionId = wg.getTransaction(transactionPosition);
            final boolean selectedTx = wg.getBooleanValue(transactionSelectedAttributeId, transactionId);
            if (selectedOnly && !selectedTx) {
                continue;
            }
            String content = wg.getStringValue(transactionContentAttributeId, transactionId);
            /*
                 Does the transaction have content?
                 */
            if (StringUtils.isBlank(content)) {
                continue;
            }
            /*
                 Ignore other "referenced" transactions because that's not useful
                 */
            if (wg.getObjectValue(transactionTypeAttributeId, transactionId) != null && wg.getObjectValue(transactionTypeAttributeId, transactionId).equals(AnalyticConcept.TransactionType.REFERENCED)) {
                continue;
            }
            /*
                 Retrieving information needed to create new transactions
                 */
            final int sourceVertexId = wg.getTransactionSourceVertex(transactionId);
            final int destinationVertexId = wg.getTransactionDestinationVertex(transactionId);
            final ZonedDateTime datetime = wg.getObjectValue(transactionDatetimeAttributeId, transactionId);
            final HashSet<String> typesExtracted = new HashSet<>();
            /*
                 Extracting Schema Types
                 */
            if (types) {
                final List<ExtractedVertexType> extractedTypes = SchemaVertexTypeUtilities.extractVertexTypes(content);
                final Map<String, SchemaVertexType> identifiers = new HashMap<>();
                extractedTypes.forEach(extractedType -> identifiers.put(extractedType.getIdentifier(), extractedType.getType()));
                for (String identifier : identifiers.keySet()) {
                    final int newVertexId = wg.addVertex();
                    wg.setStringValue(vertexIdentifierAttributeId, newVertexId, identifier);
                    wg.setObjectValue(vertexTypeAttributeId, newVertexId, identifiers.get(identifier));
                    final int newTransactionId = outgoing ? wg.addTransaction(sourceVertexId, newVertexId, true) : wg.addTransaction(newVertexId, destinationVertexId, true);
                    wg.setObjectValue(transactionDatetimeAttributeId, newTransactionId, datetime);
                    wg.setObjectValue(transactionTypeAttributeId, newTransactionId, AnalyticConcept.TransactionType.REFERENCED);
                    wg.setStringValue(transactionContentAttributeId, newTransactionId, content);
                    typesExtracted.add(identifier.toLowerCase());
                }
            }
            if (StringUtils.isBlank(words)) {
                /*
                     Extracting all words of the specified length if no word list has been provided
                     */
                for (String word : content.split(" ")) {
                    if (toLowerCase) {
                        word = word.toLowerCase();
                    }
                    if (removeSpecialChars) {
                        word = word.replaceAll("\\W", "");
                    }
                    if (word.length() < wordLength) {
                        continue;
                    }
                    foundWords.add(word);
                }
            } else {
                patterns.stream().map(pattern -> pattern.matcher(content)).forEach(matcher -> {
                    while (matcher.find()) {
                        final String g = matcher.group();
                        foundWords.add(toLowerCase ? g.toLowerCase() : g);
                    }
                });
            }
            /*
                 Add words to graph
                 */
            for (String word : foundWords) {
                if (types && typesExtracted.contains(word.toLowerCase())) {
                    continue;
                }
                final int newVertexId = wg.addVertex();
                wg.setStringValue(vertexIdentifierAttributeId, newVertexId, word);
                wg.setObjectValue(vertexTypeAttributeId, newVertexId, AnalyticConcept.VertexType.WORD);
                final int newTransactionId = outgoing ? wg.addTransaction(sourceVertexId, newVertexId, true) : wg.addTransaction(newVertexId, destinationVertexId, true);
                wg.setObjectValue(transactionDatetimeAttributeId, newTransactionId, datetime);
                wg.setObjectValue(transactionTypeAttributeId, newTransactionId, AnalyticConcept.TransactionType.REFERENCED);
                wg.setStringValue(transactionContentAttributeId, newTransactionId, content);
            }
        }
    }
    PluginExecutor.startWith(VisualSchemaPluginRegistry.COMPLETE_SCHEMA).followedBy(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(wg);
    interaction.setProgress(1, 0, "Completed successfully", true);
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) StringParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.StringParameterType) SingleChoiceParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.SingleChoiceParameterType) ZonedDateTime(java.time.ZonedDateTime) StringAttributeDescription(au.gov.asd.tac.constellation.graph.attribute.StringAttributeDescription) PluginType(au.gov.asd.tac.constellation.plugins.PluginType) StringUtils(org.apache.commons.lang3.StringUtils) DataAccessPlugin(au.gov.asd.tac.constellation.views.dataaccess.plugins.DataAccessPlugin) Map(java.util.Map) PluginExecutor(au.gov.asd.tac.constellation.plugins.PluginExecutor) StringParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.StringParameterValue) InteractiveGraphPluginRegistry(au.gov.asd.tac.constellation.graph.interaction.InteractiveGraphPluginRegistry) SeparatorConstants(au.gov.asd.tac.constellation.utilities.text.SeparatorConstants) BooleanParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.BooleanParameterType.BooleanParameterValue) Set(java.util.Set) PluginNotificationLevel(au.gov.asd.tac.constellation.plugins.PluginNotificationLevel) PluginInfo(au.gov.asd.tac.constellation.plugins.PluginInfo) List(java.util.List) NotifyDescriptor(org.openide.NotifyDescriptor) IntegerParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.IntegerParameterType) VisualSchemaPluginRegistry(au.gov.asd.tac.constellation.graph.schema.visual.VisualSchemaPluginRegistry) Pattern(java.util.regex.Pattern) Messages(org.openide.util.NbBundle.Messages) GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) SchemaVertexTypeUtilities(au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexTypeUtilities) IntegerParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.IntegerParameterType.IntegerParameterValue) ParameterChange(au.gov.asd.tac.constellation.plugins.parameters.ParameterChange) 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) 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) ServiceProvider(org.openide.util.lookup.ServiceProvider) PluginTags(au.gov.asd.tac.constellation.plugins.templates.PluginTags) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) ContentConcept(au.gov.asd.tac.constellation.graph.schema.analytic.concept.ContentConcept) GraphElementType(au.gov.asd.tac.constellation.graph.GraphElementType) DialogDisplayer(org.openide.DialogDisplayer) ExtractedVertexType(au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexTypeUtilities.ExtractedVertexType) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) BooleanParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.BooleanParameterType) SchemaVertexType(au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType) AnalyticConcept(au.gov.asd.tac.constellation.graph.schema.analytic.concept.AnalyticConcept) TemporalConcept(au.gov.asd.tac.constellation.graph.schema.analytic.concept.TemporalConcept) SimpleQueryPlugin(au.gov.asd.tac.constellation.plugins.templates.SimpleQueryPlugin) DataAccessPluginCoreType(au.gov.asd.tac.constellation.views.dataaccess.plugins.DataAccessPluginCoreType) SingleChoiceParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.SingleChoiceParameterType.SingleChoiceParameterValue) Pattern(java.util.regex.Pattern) SchemaVertexType(au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType) HashMap(java.util.HashMap) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) ArrayList(java.util.ArrayList) ExtractedVertexType(au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexTypeUtilities.ExtractedVertexType) NotifyDescriptor(org.openide.NotifyDescriptor) ZonedDateTime(java.time.ZonedDateTime) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) HashSet(java.util.HashSet)

Example 32 with PluginInteraction

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

the class SplitNodesPlugin method edit.

@Override
public void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    final Map<String, PluginParameter<?>> splitParameters = parameters.getParameters();
    final String character = splitParameters.get(SPLIT_PARAMETER_ID) != null && splitParameters.get(SPLIT_PARAMETER_ID).getStringValue() != null ? splitParameters.get(SPLIT_PARAMETER_ID).getStringValue() : "";
    final ParameterValue transactionTypeChoice = splitParameters.get(TRANSACTION_TYPE_PARAMETER_ID).getSingleChoice();
    final String linkType = transactionTypeChoice != null ? transactionTypeChoice.toString() : AnalyticConcept.TransactionType.CORRELATION.getName();
    final boolean allOccurrences = splitParameters.get(ALL_OCCURRENCES_PARAMETER_ID).getBooleanValue();
    final boolean splitIntoSameLevel = splitParameters.get(DUPLICATE_TRANSACTIONS_PARAMETER_ID).getBooleanValue();
    final int vertexSelectedAttributeId = VisualConcept.VertexAttribute.SELECTED.ensure(graph);
    final int vertexIdentifierAttributeId = VisualConcept.VertexAttribute.IDENTIFIER.ensure(graph);
    final List<Integer> newVertices = new ArrayList<>();
    final int graphVertexCount = graph.getVertexCount();
    for (int position = 0; position < graphVertexCount; position++) {
        final int currentVertexId = graph.getVertex(position);
        if (graph.getBooleanValue(vertexSelectedAttributeId, currentVertexId)) {
            final String identifier = graph.getStringValue(vertexIdentifierAttributeId, currentVertexId);
            if (identifier != null && identifier.contains(character) && identifier.indexOf(character) < identifier.length() - character.length()) {
                String leftNodeIdentifier = "";
                if (allOccurrences) {
                    final String[] substrings = Arrays.stream(identifier.split(character)).filter(value -> value != null && value.length() > 0).toArray(size -> new String[size]);
                    if (substrings.length <= 0) {
                        continue;
                    }
                    leftNodeIdentifier = substrings[0];
                    for (int i = 1; i < substrings.length; i++) {
                        newVertices.add(createNewNode(graph, position, substrings[i], linkType, splitIntoSameLevel));
                    }
                } else {
                    final int i = identifier.indexOf(character);
                    leftNodeIdentifier = identifier.substring(0, i);
                    if (StringUtils.isNotBlank(leftNodeIdentifier)) {
                        newVertices.add(createNewNode(graph, position, identifier.substring(i + 1), linkType, splitIntoSameLevel));
                    } else {
                        leftNodeIdentifier = identifier.substring(i + 1);
                    }
                }
                // Rename the selected node
                if (StringUtils.isNotBlank(leftNodeIdentifier)) {
                    graph.setStringValue(vertexIdentifierAttributeId, currentVertexId, leftNodeIdentifier);
                    newVertices.add(currentVertexId);
                }
            }
        }
    }
    if (!newVertices.isEmpty()) {
        // Reset the view
        graph.validateKey(GraphElementType.VERTEX, true);
        graph.validateKey(GraphElementType.TRANSACTION, true);
        final PluginExecutor arrangement = completionArrangement();
        if (arrangement != null) {
            // run the arrangement
            final VertexListInclusionGraph vlGraph = new VertexListInclusionGraph(graph, AbstractInclusionGraph.Connections.NONE, newVertices);
            arrangement.executeNow(vlGraph.getInclusionGraph());
            vlGraph.retrieveCoords();
        }
        if (splitParameters.get(COMPLETE_WITH_SCHEMA_OPTION_ID).getBooleanValue()) {
            PluginExecution.withPlugin(VisualSchemaPluginRegistry.COMPLETE_SCHEMA).executeNow(graph);
        }
        PluginExecutor.startWith(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
    }
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) SchemaTransactionType(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType) Arrays(java.util.Arrays) StringParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.StringParameterType) 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) SimpleEditPlugin(au.gov.asd.tac.constellation.plugins.templates.SimpleEditPlugin) PluginType(au.gov.asd.tac.constellation.plugins.PluginType) VisualConcept(au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Graph(au.gov.asd.tac.constellation.graph.Graph) ArrangementPluginRegistry(au.gov.asd.tac.constellation.plugins.arrangements.ArrangementPluginRegistry) 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) PluginExecutor(au.gov.asd.tac.constellation.plugins.PluginExecutor) PluginTags(au.gov.asd.tac.constellation.plugins.templates.PluginTags) StringParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.StringParameterValue) PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) AbstractInclusionGraph(au.gov.asd.tac.constellation.plugins.arrangements.AbstractInclusionGraph) VertexListInclusionGraph(au.gov.asd.tac.constellation.plugins.arrangements.VertexListInclusionGraph) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) InteractiveGraphPluginRegistry(au.gov.asd.tac.constellation.graph.interaction.InteractiveGraphPluginRegistry) GraphElementType(au.gov.asd.tac.constellation.graph.GraphElementType) BooleanParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.BooleanParameterType.BooleanParameterValue) ParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.ParameterValue) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) BooleanParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.BooleanParameterType) PluginInfo(au.gov.asd.tac.constellation.plugins.PluginInfo) List(java.util.List) AnalyticConcept(au.gov.asd.tac.constellation.graph.schema.analytic.concept.AnalyticConcept) VisualSchemaPluginRegistry(au.gov.asd.tac.constellation.graph.schema.visual.VisualSchemaPluginRegistry) DataAccessPluginCoreType(au.gov.asd.tac.constellation.views.dataaccess.plugins.DataAccessPluginCoreType) NbBundle(org.openide.util.NbBundle) SingleChoiceParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.SingleChoiceParameterType.SingleChoiceParameterValue) StringParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.StringParameterValue) BooleanParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.BooleanParameterType.BooleanParameterValue) ParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.ParameterValue) SingleChoiceParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.SingleChoiceParameterType.SingleChoiceParameterValue) ArrayList(java.util.ArrayList) PluginExecutor(au.gov.asd.tac.constellation.plugins.PluginExecutor) VertexListInclusionGraph(au.gov.asd.tac.constellation.plugins.arrangements.VertexListInclusionGraph) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)

Example 33 with PluginInteraction

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

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

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

the class ExtractTypesFromTextPluginNGTest method testQuery.

/**
 * Test of query method, of class ExtractTypesFromTextPlugin.
 *
 * @throws java.lang.Exception
 */
@Test
public void testQuery() throws Exception {
    RecordStore query = new GraphRecordStore();
    ExtractTypesFromTextPlugin instance = new ExtractTypesFromTextPlugin();
    PluginInteraction interaction = new TextPluginInteraction();
    PluginParameters parameters = instance.createParameters();
    parameters.getParameters().get(ExtractTypesFromTextPlugin.TEXT_PARAMETER_ID).setStringValue("Email Person Communication");
    RecordStore expResult = new GraphRecordStore();
    RecordStore result = instance.query(query, interaction, parameters);
    assertEquals(result, expResult);
}
Also used : PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) TextPluginInteraction(au.gov.asd.tac.constellation.plugins.text.TextPluginInteraction) RecordStore(au.gov.asd.tac.constellation.graph.processing.RecordStore) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) TextPluginInteraction(au.gov.asd.tac.constellation.plugins.text.TextPluginInteraction) Test(org.testng.annotations.Test)

Aggregations

PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)51 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)44 Test (org.testng.annotations.Test)33 PluginParameter (au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)16 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)15 ArrayList (java.util.ArrayList)15 TextPluginInteraction (au.gov.asd.tac.constellation.plugins.text.TextPluginInteraction)14 GraphWriteMethods (au.gov.asd.tac.constellation.graph.GraphWriteMethods)13 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)12 PluginGraphs (au.gov.asd.tac.constellation.plugins.PluginGraphs)10 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)9 Graph (au.gov.asd.tac.constellation.graph.Graph)8 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)8 RecordStore (au.gov.asd.tac.constellation.graph.processing.RecordStore)8 List (java.util.List)8 VisualConcept (au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept)7 PluginInfo (au.gov.asd.tac.constellation.plugins.PluginInfo)7 PluginTags (au.gov.asd.tac.constellation.plugins.templates.PluginTags)7 SimpleEditPlugin (au.gov.asd.tac.constellation.plugins.templates.SimpleEditPlugin)7 PluginExecution (au.gov.asd.tac.constellation.plugins.PluginExecution)6