Search in sources :

Example 1 with StoreGraph

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

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

the class MatrixUtilitiesNGTest method testInverseLaplacianEmptyGraph.

/**
 * Test of inverseLaplacian method, of class MatrixUtilities.
 */
@Test
public void testInverseLaplacianEmptyGraph() {
    final SimpleMatrix expResult = new SimpleMatrix(0, 0);
    final SimpleMatrix result = MatrixUtilities.inverseLaplacian(new StoreGraph());
    assertEquals(result.numRows(), expResult.numRows());
    assertEquals(result.numCols(), expResult.numCols());
    assertEquals(result.getNumElements(), 0);
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) Test(org.testng.annotations.Test)

Example 3 with StoreGraph

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

the class MatrixUtilitiesNGTest method testAdjacencyEmptyGraph.

/**
 * Test of adjacency method, of class MatrixUtilities when graph is empty.
 */
@Test
public void testAdjacencyEmptyGraph() {
    final SimpleMatrix expResult = new SimpleMatrix(0, 0);
    final SimpleMatrix result = MatrixUtilities.adjacency(new StoreGraph(), false);
    assertEquals(result.numRows(), expResult.numRows());
    assertEquals(result.numCols(), expResult.numCols());
    assertEquals(result.getNumElements(), 0);
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) Test(org.testng.annotations.Test)

Example 4 with StoreGraph

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

the class DirectedShortestPathsPluginNGTest method setUpMethod.

@BeforeMethod
public void setUpMethod() throws Exception {
    final Schema schema = SchemaFactoryUtilities.getSchemaFactory(VisualSchemaFactory.VISUAL_SCHEMA_ID).createSchema();
    graph = new StoreGraph(schema);
    vxId1 = graph.addVertex();
    vxId2 = graph.addVertex();
    vxId3 = graph.addVertex();
    vxId4 = graph.addVertex();
    vxId5 = graph.addVertex();
    tId1 = graph.addTransaction(vxId1, vxId2, false);
    tId2 = graph.addTransaction(vxId1, vxId3, true);
    tId3 = graph.addTransaction(vxId1, vxId4, true);
    tId4 = graph.addTransaction(vxId4, vxId2, true);
    tId5 = graph.addTransaction(vxId2, vxId5, true);
    tId6 = graph.addTransaction(vxId3, vxId5, true);
    vertexLabelAttribute = VisualConcept.VertexAttribute.LABEL.ensure(graph);
    vertexSelectedAttribute = VisualConcept.VertexAttribute.SELECTED.ensure(graph);
    graph.setStringValue(vertexLabelAttribute, vxId1, "Vertex 1");
    graph.setStringValue(vertexLabelAttribute, vxId2, "Vertex 2");
    graph.setStringValue(vertexLabelAttribute, vxId3, "Vertex 3");
    graph.setStringValue(vertexLabelAttribute, vxId4, "Vertex 4");
    graph.setStringValue(vertexLabelAttribute, vxId5, "Vertex 5");
    graph.setBooleanValue(vertexSelectedAttribute, vxId1, true);
    graph.setBooleanValue(vertexSelectedAttribute, vxId2, true);
    graph.setBooleanValue(vertexSelectedAttribute, vxId3, true);
    graph.setBooleanValue(vertexSelectedAttribute, vxId4, true);
    graph.setBooleanValue(vertexSelectedAttribute, vxId5, true);
}
Also used : Schema(au.gov.asd.tac.constellation.graph.schema.Schema) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 5 with StoreGraph

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

the class BetweennessCentralityPluginNGTest method setUpMethod.

@BeforeMethod
public void setUpMethod() throws Exception {
    // create an analytic graph
    final Schema schema = SchemaFactoryUtilities.getSchemaFactory(AnalyticSchemaFactory.ANALYTIC_SCHEMA_ID).createSchema();
    graph = new StoreGraph(schema);
    // add attributes
    vertexBetweennessAttribute = SnaConcept.VertexAttribute.BETWEENNESS_CENTRALITY.ensure(graph);
    vertexInBetweennessAttribute = SnaConcept.VertexAttribute.IN_BETWEENNESS_CENTRALITY.ensure(graph);
    vertexOutBetweennessAttribute = SnaConcept.VertexAttribute.OUT_BETWEENNESS_CENTRALITY.ensure(graph);
    vertexSelectedAttribute = VisualConcept.VertexAttribute.SELECTED.ensure(graph);
    // add vertices
    vxId0 = graph.addVertex();
    vxId1 = graph.addVertex();
    vxId2 = graph.addVertex();
    vxId3 = graph.addVertex();
    vxId4 = graph.addVertex();
    // add transactions
    txId0 = graph.addTransaction(vxId0, vxId1, true);
    txId1 = graph.addTransaction(vxId1, vxId2, true);
    txId2 = graph.addTransaction(vxId1, vxId3, true);
    txId3 = graph.addTransaction(vxId2, vxId3, true);
    txId4 = graph.addTransaction(vxId3, vxId4, true);
}
Also used : Schema(au.gov.asd.tac.constellation.graph.schema.Schema) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)170 Test (org.testng.annotations.Test)75 Schema (au.gov.asd.tac.constellation.graph.schema.Schema)64 BeforeMethod (org.testng.annotations.BeforeMethod)61 ArrayList (java.util.ArrayList)19 Graph (au.gov.asd.tac.constellation.graph.Graph)17 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)17 HashMap (java.util.HashMap)14 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)13 GraphReadMethods (au.gov.asd.tac.constellation.graph.GraphReadMethods)11 PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)11 HashSet (java.util.HashSet)11 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)10 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)10 PluginParameter (au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)9 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)8 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)8 GraphElementType (au.gov.asd.tac.constellation.graph.GraphElementType)7 GraphWriteMethods (au.gov.asd.tac.constellation.graph.GraphWriteMethods)7 SchemaVertexType (au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType)7