Search in sources :

Example 1 with GraphWriteMethods

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

the class SpanningTree method createSpanningTree.

/**
 * Create a new graph representing a spanning tree of the original graph.
 *
 * @param isMinimal true for a minimal spanning tree, False for a maximal
 * spanning tree.
 * @param selectTxs if True, select the transactions in the tree in the
 * original graph.
 * @param rootVxId if specified (!=Graph.NOT_FOUND), make the tree's
 * transactions directed.
 *
 * @return A new GraphWriteMethods representing the spanning tree.
 */
public GraphWriteMethods createSpanningTree(final boolean isMinimal, final boolean selectTxs, final int rootVxId) {
    final GraphWriteMethods tree = new StoreGraph();
    final int nradiusTreeAttr = VisualConcept.VertexAttribute.NODE_RADIUS.ensure(tree, false);
    final int nradiusAttr = VisualConcept.VertexAttribute.NODE_RADIUS.ensure(wg);
    final int vxCount = wg.getVertexCount();
    // Get a list of all links sorted by weight.
    final int linkCount = wg.getLinkCount();
    final ArrayList<Integer> links = new ArrayList<>(linkCount);
    for (int position = 0; position < linkCount; position++) {
        final int linkId = wg.getLink(position);
        links.add(linkId);
    }
    Collections.sort(links, new LinkSorter(wg, isMinimal));
    // Put each vertex in its own tree (where a tree is conveniently named after its root.
    final Map<Integer, Set<Integer>> treeVxs = new HashMap<>();
    final int[] vxTrees = new int[wg.getVertexCapacity()];
    Arrays.fill(vxTrees, Graph.NOT_FOUND);
    for (int position = 0; position < vxCount; position++) {
        final int vxId = wg.getVertex(position);
        final Set<Integer> s = new HashSet<>();
        s.add(vxId);
        treeVxs.put(vxId, s);
        vxTrees[vxId] = vxId;
    }
    // Build the new graph to contain the tree we'll build from the original graph.
    origVxToTree = new int[wg.getVertexCapacity()];
    Arrays.fill(origVxToTree, Graph.NOT_FOUND);
    treeVxToOrig = new int[wg.getVertexCapacity()];
    Arrays.fill(treeVxToOrig, Graph.NOT_FOUND);
    for (int position = 0; position < vxCount; position++) {
        final int vxId = wg.getVertex(position);
        final int treeVxId = tree.addVertex();
        final float nradius = nradiusAttr != Graph.NOT_FOUND ? wg.getFloatValue(nradiusAttr, vxId) : 1;
        tree.setFloatValue(nradiusTreeAttr, treeVxId, nradius);
        origVxToTree[vxId] = treeVxId;
        treeVxToOrig[treeVxId] = vxId;
    }
    // Iterate through the sorted links, looking for vertices in different trees.
    final Set<Integer> spanningLinks = new HashSet<>();
    for (final Integer linkId : links) {
        final int vx0Id = wg.getLinkLowVertex(linkId);
        final int vx1Id = wg.getLinkHighVertex(linkId);
        final int tree1Name = vxTrees[vx0Id];
        final int tree2Name = vxTrees[vx1Id];
        if (tree1Name != tree2Name) {
            final Set<Integer> s1 = treeVxs.get(tree1Name);
            final Set<Integer> s2 = treeVxs.get(tree2Name);
            treeVxs.remove(tree2Name);
            s1.addAll(s2);
            for (final Integer vx2 : s2) {
                vxTrees[vx2] = tree1Name;
            }
            spanningLinks.add(linkId);
            final int tvx0Id = origVxToTree[vx0Id];
            final int tvx1Id = origVxToTree[vx1Id];
            tree.addTransaction(tvx0Id, tvx1Id, false);
        }
    }
    // Make sure we have all of the vertices.
    assert tree.getVertexCount() == wg.getVertexCount();
    // This assert will fail if the graph has more than one component.
    assert treeVxs.size() == 1;
    if (rootVxId != Graph.NOT_FOUND) {
        final int treeRootVxId = origVxToTree[rootVxId];
        rootTree(tree, treeRootVxId);
    }
    if (selectTxs) {
        for (final Iterator<Integer> e = spanningLinks.iterator(); e.hasNext(); ) {
            final int slinkId = e.next();
            final int ltxCount = wg.getLinkTransactionCount(slinkId);
            for (int lpos = 0; lpos < ltxCount; lpos++) {
                final int txId = wg.getLinkTransaction(slinkId, lpos);
                wg.setBooleanValue(txSelectedId, txId, true);
            }
        }
    }
    return tree;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) HashSet(java.util.HashSet)

Example 2 with GraphWriteMethods

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

the class ArrangeByNodeAttributePlugin method edit.

@Override
public void edit(final GraphWriteMethods wg, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException {
    final boolean threeD = parameters.getParameters().get(THREE_D_PARAMETER_ID).getBooleanValue();
    final String attribute = parameters.getParameters().get(ATTRIBUTE_PARAMETER_ID).getStringValue();
    if (threeD) {
        if (attribute != null) {
            final SelectedInclusionGraph selectedGraph = new SelectedInclusionGraph(wg, SelectedInclusionGraph.Connections.NONE);
            final Attribute attr = new GraphAttribute(wg, wg.getAttribute(GraphElementType.VERTEX, attribute));
            selectedGraph.addAttributeToCopy(attr);
            final GraphWriteMethods ig = selectedGraph.getInclusionGraph();
            final int iattrId = ig.getAttribute(GraphElementType.VERTEX, attribute);
            final LayerArranger arranger = new LayerArranger();
            arranger.setLevelAttr(iattrId);
            arranger.setMaintainMean(!selectedGraph.isArrangingAll());
            arranger.arrange(ig);
            selectedGraph.retrieveCoords();
        }
    } else {
        if (attribute != null) {
            final Arranger inner = new GridArranger();
            final Arranger outer = new GridArranger();
            final GraphTaxonomyArranger arranger = new ArrangeByGroupTaxonomy(inner, outer, Connections.NONE, attribute);
            arranger.setMaintainMean(true);
            final SelectedInclusionGraph selectedGraph = new SelectedInclusionGraph(wg, Connections.NONE);
            selectedGraph.addAttributeToCopy(new GraphAttribute(wg, wg.getAttribute(GraphElementType.VERTEX, attribute)));
            arranger.arrange(selectedGraph.getInclusionGraph());
            selectedGraph.retrieveCoords();
        }
    }
}
Also used : GridArranger(au.gov.asd.tac.constellation.plugins.arrangements.grid.GridArranger) GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) GridArranger(au.gov.asd.tac.constellation.plugins.arrangements.grid.GridArranger) Arranger(au.gov.asd.tac.constellation.plugins.arrangements.Arranger) GraphTaxonomyArranger(au.gov.asd.tac.constellation.plugins.arrangements.GraphTaxonomyArranger) Attribute(au.gov.asd.tac.constellation.graph.Attribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) SelectedInclusionGraph(au.gov.asd.tac.constellation.plugins.arrangements.SelectedInclusionGraph) GraphTaxonomyArranger(au.gov.asd.tac.constellation.plugins.arrangements.GraphTaxonomyArranger)

Example 3 with GraphWriteMethods

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

the class DataAccessStateIoProviderNGTest method readObjectObjectJson.

@Test
public void readObjectObjectJson() throws IOException {
    final ObjectMapper objectMapper = new ObjectMapper();
    JsonNode root = objectMapper.readTree("{}");
    final GraphWriteMethods graph = mock(GraphWriteMethods.class);
    dataAccessStateIoProvider.readObject(ATTRIBUTE_ID, ELEMENT_ID, root, graph, null, null, null, null);
    final ArgumentCaptor<DataAccessState> captor = ArgumentCaptor.forClass(DataAccessState.class);
    verify(graph).setObjectValue(eq(ATTRIBUTE_ID), eq(ELEMENT_ID), captor.capture());
    final List<Map<String, String>> state = captor.getValue().getState();
    assertTrue(state.isEmpty());
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) DataAccessState(au.gov.asd.tac.constellation.views.dataaccess.state.DataAccessState) JsonNode(com.fasterxml.jackson.databind.JsonNode) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 4 with GraphWriteMethods

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

the class DataAccessStateIoProviderNGTest method writeObjectIsDefaultValue.

@Test
public void writeObjectIsDefaultValue() throws IOException {
    final Attribute attribute = mock(Attribute.class);
    when(attribute.getId()).thenReturn(ATTRIBUTE_ID);
    when(attribute.getName()).thenReturn("ATTR NAME");
    final GraphWriteMethods graph = mock(GraphWriteMethods.class);
    when(graph.isDefaultValue(ATTRIBUTE_ID, ELEMENT_ID)).thenReturn(true);
    final JsonFactory factory = new JsonFactory();
    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    final JsonGenerator jsonGenerator = factory.createGenerator(output);
    dataAccessStateIoProvider.writeObject(attribute, ELEMENT_ID, jsonGenerator, graph, null, false);
    jsonGenerator.flush();
    assertEquals(new String(output.toByteArray(), StandardCharsets.UTF_8), "");
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) Attribute(au.gov.asd.tac.constellation.graph.Attribute) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Example 5 with GraphWriteMethods

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

the class DataAccessStateIoProviderNGTest method readObjectNullJson.

@Test
public void readObjectNullJson() throws IOException {
    final ObjectMapper objectMapper = new ObjectMapper();
    JsonNode root = objectMapper.readTree("null");
    final GraphWriteMethods graph = mock(GraphWriteMethods.class);
    dataAccessStateIoProvider.readObject(ATTRIBUTE_ID, ELEMENT_ID, root, graph, null, null, null, null);
    final ArgumentCaptor<DataAccessState> captor = ArgumentCaptor.forClass(DataAccessState.class);
    verify(graph).setObjectValue(eq(ATTRIBUTE_ID), eq(ELEMENT_ID), captor.capture());
    final List<Map<String, String>> state = captor.getValue().getState();
    assertTrue(state.isEmpty());
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) DataAccessState(au.gov.asd.tac.constellation.views.dataaccess.state.DataAccessState) JsonNode(com.fasterxml.jackson.databind.JsonNode) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) 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