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);
}
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);
}
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.
}
}
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");
}
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");
}
Aggregations