Search in sources :

Example 26 with GraphWriteMethods

use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.

the class DataAccessStateIoProviderNGTest method readObject.

@Test
public void readObject() throws IOException {
    final ObjectMapper objectMapper = new ObjectMapper();
    final JsonNode jsonNode = objectMapper.readTree(new FileInputStream(getClass().getResource("resources/dataAccessStateRead.json").getPath()));
    final GraphWriteMethods graph = mock(GraphWriteMethods.class);
    dataAccessStateIoProvider.readObject(ATTRIBUTE_ID, ELEMENT_ID, jsonNode, graph, null, null, null, null);
    // Capture the call on the graph setter, pulling out the state
    final ArgumentCaptor<DataAccessState> captor = ArgumentCaptor.forClass(DataAccessState.class);
    verify(graph, times(1)).setObjectValue(eq(ATTRIBUTE_ID), eq(ELEMENT_ID), captor.capture());
    final List<Map<String, String>> state = captor.getValue().getState();
    // Verify that there are two tabs
    assertEquals(state.size(), 2);
    // Verify the contents of tab 1
    final Map<String, String> expectedTab1 = new HashMap<>();
    expectedTab1.put("key1", "value1");
    expectedTab1.put("key2", null);
    expectedTab1.put("key3", null);
    assertEquals(state.get(0), expectedTab1);
    // Verify the contents of tab 4
    final Map<String, String> expectedTab2 = new HashMap<>();
    expectedTab2.put("key4", "value4");
    assertEquals(state.get(1), expectedTab2);
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) DataAccessState(au.gov.asd.tac.constellation.views.dataaccess.state.DataAccessState) HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 27 with GraphWriteMethods

use of au.gov.asd.tac.constellation.graph.GraphWriteMethods 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 28 with GraphWriteMethods

use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.

the class MergeNodesPluginNGTest method edit.

@Test
public void edit() throws InterruptedException, PluginException, MergeNodeType.MergeException {
    final GraphWriteMethods graph = mock(GraphWriteMethods.class);
    final PluginInteraction interaction = mock(PluginInteraction.class);
    final PluginParameters parameters = mock(PluginParameters.class);
    final PluginExecution pluginExecution = mock(PluginExecution.class);
    final PluginParameter mergeTypeParameter = mock(PluginParameter.class);
    final PluginParameter thresholdParameter = mock(PluginParameter.class);
    final PluginParameter mergerParameter = mock(PluginParameter.class);
    final PluginParameter leadParameter = mock(PluginParameter.class);
    final PluginParameter selectedParameter = mock(PluginParameter.class);
    final Map<String, PluginParameter<?>> pluginParameters = Map.of("MergeNodesPlugin.merge_type", mergeTypeParameter, "MergeNodesPlugin.threshold", thresholdParameter, "MergeNodesPlugin.merger", mergerParameter, "MergeNodesPlugin.lead", leadParameter, "MergeNodesPlugin.selected", selectedParameter);
    when(parameters.getParameters()).thenReturn(pluginParameters);
    when(mergeTypeParameter.getStringValue()).thenReturn(TestMergeType.NAME);
    when(thresholdParameter.getIntegerValue()).thenReturn(TestMergeType.MERGE_SUCCESS_THRESHOLD);
    when(mergerParameter.getStringValue()).thenReturn("Retain lead vertex attributes if present");
    when(leadParameter.getStringValue()).thenReturn("Longest Value");
    when(selectedParameter.getBooleanValue()).thenReturn(true);
    doReturn(null).when(pluginExecution).executeNow(graph);
    try (MockedStatic<PluginExecution> pluginExecutionMockedStatic = Mockito.mockStatic(PluginExecution.class)) {
        pluginExecutionMockedStatic.when(() -> PluginExecution.withPlugin(VisualSchemaPluginRegistry.COMPLETE_SCHEMA)).thenReturn(pluginExecution);
        mergeNodesPlugin.edit(graph, interaction, parameters);
        verify(pluginExecution).executeNow(graph);
        verify(interaction).setProgress(1, 0, "Merged 2 nodes.", true);
    // Due to accessibility issues the call to mergeVerticies and its follow
    // on logic cannot be verified without tying this test to the logic of
    // one of the concrete implementations of GraphElementMerger.
    }
}
Also used : PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) Test(org.testng.annotations.Test)

Example 29 with GraphWriteMethods

use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.

the class CopyGraphUtilitiesNGTest method testCopyGraphTypeElements.

/**
 * Test of copyGraphTypeElements method, of class CopyGraphUtilities.
 */
@Test
public void testCopyGraphTypeElements() {
    final GraphWriteMethods graph = new StoreGraph();
    final GraphElementType type = GraphElementType.GRAPH;
    CopyGraphUtilities.copyGraphTypeElements(source, graph);
    // copied and the default value copied correctly
    final int graphAttribute1Id = graph.getAttribute(type, GRAPH_ATTRIBUTE_1_LABEL);
    final String graphAttribute1Value = graph.getStringValue(graphAttribute1Id, 0);
    Assert.assertEquals(graphAttribute1Value, "graph1 default");
    // copied and the custom value copied correctly
    final int graphAttribute2Id = graph.getAttribute(type, GRAPH_ATTRIBUTE_2_LABEL);
    final String graphAttribute2Value = graph.getStringValue(graphAttribute2Id, 0);
    Assert.assertEquals(graphAttribute2Value, "graph2 custom");
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) GraphElementType(au.gov.asd.tac.constellation.graph.GraphElementType) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) Test(org.testng.annotations.Test)

Example 30 with GraphWriteMethods

use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.

the class CopyGraphUtilitiesNGTest method testCopyGraphToGraph.

/**
 * Test of copyGraphToGraph method, of class CopyGraphUtilities.
 */
@Test
public void testCopyGraphToGraph() {
    final GraphWriteMethods graph = new StoreGraph();
    final boolean copyAll = true;
    CopyGraphUtilities.copyGraphToGraph(source, graph, copyAll);
    final int metaAttribute1Id = graph.getAttribute(GraphElementType.META, META_ATTRIBUTE_1_LABEL);
    final int metaAttribute2Id = graph.getAttribute(GraphElementType.META, META_ATTRIBUTE_2_LABEL);
    final int graphAttribute1Id = graph.getAttribute(GraphElementType.GRAPH, GRAPH_ATTRIBUTE_1_LABEL);
    final int graphAttribute2Id = graph.getAttribute(GraphElementType.GRAPH, GRAPH_ATTRIBUTE_2_LABEL);
    final int vertexAttribute1Id = graph.getAttribute(GraphElementType.VERTEX, VERTEX_ATTRIBUTE_1_LABEL);
    final int vertexAttribute2Id = graph.getAttribute(GraphElementType.VERTEX, VERTEX_ATTRIBUTE_2_LABEL);
    final int transactionAttribute1Id = graph.getAttribute(GraphElementType.TRANSACTION, TRANSACTION_ATTRIBUTE_1_LABEL);
    final int transactionAttribute2Id = graph.getAttribute(GraphElementType.TRANSACTION, TRANSACTION_ATTRIBUTE_2_LABEL);
    final int edgeAttribute1Id = graph.getAttribute(GraphElementType.EDGE, EDGE_ATTRIBUTE_1_LABEL);
    final int edgeAttribute2Id = graph.getAttribute(GraphElementType.EDGE, EDGE_ATTRIBUTE_2_LABEL);
    final int linkAttribute1Id = graph.getAttribute(GraphElementType.LINK, LINK_ATTRIBUTE_1_LABEL);
    final int linkAttribute2Id = graph.getAttribute(GraphElementType.LINK, LINK_ATTRIBUTE_2_LABEL);
    // not going to copy meta attributes
    Assert.assertEquals(graph.getStringValue(metaAttribute1Id, 0), "meta1 default");
    Assert.assertEquals(graph.getStringValue(metaAttribute2Id, 0), "meta2 default");
    // everything else will be copied
    Assert.assertEquals(graph.getStringValue(graphAttribute1Id, 0), "graph1 default");
    Assert.assertEquals(graph.getStringValue(graphAttribute2Id, 0), "graph2 custom");
    Assert.assertEquals(graph.getStringValue(vertexAttribute1Id, vx0), "vertex1 default");
    Assert.assertEquals(graph.getStringValue(vertexAttribute2Id, vx0), "vertex2 custom vx0");
    Assert.assertEquals(graph.getStringValue(vertexAttribute1Id, vx1), "vertex1 default");
    Assert.assertEquals(graph.getStringValue(vertexAttribute2Id, vx1), "vertex2 custom vx1");
    Assert.assertEquals(graph.getStringValue(transactionAttribute1Id, tx0), "transaction1 default");
    Assert.assertEquals(graph.getStringValue(transactionAttribute2Id, tx0), "transaction2 custom tx0");
    Assert.assertEquals(graph.getStringValue(transactionAttribute1Id, tx1), "transaction1 default");
    Assert.assertEquals(graph.getStringValue(transactionAttribute2Id, tx1), "transaction2 custom tx1");
    Assert.assertEquals(graph.getStringValue(edgeAttribute1Id, ed0), "edge1 default");
    Assert.assertEquals(graph.getStringValue(edgeAttribute2Id, ed0), "edge2 custom ed0");
    Assert.assertEquals(graph.getStringValue(edgeAttribute1Id, ed1), "edge1 default");
    Assert.assertEquals(graph.getStringValue(edgeAttribute2Id, ed1), "edge2 custom ed1");
    Assert.assertEquals(graph.getStringValue(linkAttribute1Id, lk0), "link1 default");
    Assert.assertEquals(graph.getStringValue(linkAttribute2Id, lk0), "link2 custom lk0");
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) Test(org.testng.annotations.Test)

Aggregations

GraphWriteMethods (au.gov.asd.tac.constellation.graph.GraphWriteMethods)40 Test (org.testng.annotations.Test)26 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)16 PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)15 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)10 HashMap (java.util.HashMap)9 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)8 PluginParameter (au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 Map (java.util.Map)8 Attribute (au.gov.asd.tac.constellation.graph.Attribute)7 VisualConcept (au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept)7 ArrayList (java.util.ArrayList)7 AnalyticConcept (au.gov.asd.tac.constellation.graph.schema.analytic.concept.AnalyticConcept)6 SimpleEditPlugin (au.gov.asd.tac.constellation.plugins.templates.SimpleEditPlugin)6 List (java.util.List)6 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)5 PluginInfo (au.gov.asd.tac.constellation.plugins.PluginInfo)5 PluginType (au.gov.asd.tac.constellation.plugins.PluginType)5