Search in sources :

Example 46 with WritableGraph

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

the class CompositesNGTest method ensureCompositeStateImmutabilityTest.

// This test checks that expanding composite node states that have been copied to another graph do not affect the
// composite node states in the original graph.
@Test
public void ensureCompositeStateImmutabilityTest() throws InterruptedException, PluginException {
    final WritableGraph wg = graph.getWritableGraph("test", true);
    wg.getSchema().newGraph(wg);
    int v0, v1, v2;
    final String v0name, v1name, v2name;
    final int nameAttr;
    final int selectedAttr;
    final Plugin copyPlugin;
    try {
        // Add three vertices
        v0 = wg.addVertex();
        wg.getSchema().newVertex(wg, v0);
        v1 = wg.addVertex();
        wg.getSchema().newVertex(wg, v1);
        v2 = wg.addVertex();
        wg.getSchema().newVertex(wg, v2);
        // Store the names of these vertices
        nameAttr = VisualConcept.VertexAttribute.LABEL.get(wg);
        v0name = wg.getStringValue(nameAttr, v0);
        v1name = wg.getStringValue(nameAttr, v1);
        v2name = wg.getStringValue(nameAttr, v2);
        // Add a transaction from v0 to v1
        final int t0_1 = wg.addTransaction(v0, v1, true);
        wg.getSchema().newTransaction(wg, t0_1);
        // Select v0 and v2, but not v1
        selectedAttr = VisualConcept.VertexAttribute.SELECTED.get(wg);
        wg.setBooleanValue(selectedAttr, v0, true);
        wg.setBooleanValue(selectedAttr, v2, true);
        wg.setBooleanValue(selectedAttr, v1, false);
    } finally {
        wg.commit();
    }
    // Make a composite from the selection, then select everything, and copy to a new graph
    copyPlugin = PluginRegistry.get(InteractiveGraphPluginRegistry.COPY_TO_NEW_GRAPH);
    PluginExecutor.startWith(InteractiveGraphPluginRegistry.CREATE_COMPOSITE_FROM_SELECTION).followedBy(VisualGraphPluginRegistry.SELECT_ALL).followedBy(copyPlugin).executeNow(graph);
    final WritableGraph copyWg = ((CopyToNewGraphPlugin) copyPlugin).getCopy().getWritableGraph("test", true);
    try {
        // Expand all composites
        PluginExecution.withPlugin(InteractiveGraphPluginRegistry.EXPAND_ALL_COMPOSITES).executeNow(copyWg);
    } finally {
        copyWg.commit();
    }
    // Now we check there has been no effect on the composite state on the original graph
    // by expanding it and then checking it is the same as the orignal graph
    final WritableGraph wg2 = graph.getWritableGraph("test", true);
    try {
        // Expand all composites
        PluginExecution.withPlugin(InteractiveGraphPluginRegistry.EXPAND_ALL_COMPOSITES).executeNow(wg2);
    } finally {
        wg2.commit();
    }
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        // Assert that there are three vertices all with their original names
        assertEquals(rg.getVertexCount(), 3);
        for (int i = 0; i < 3; i++) {
            final int id = rg.getVertex(i);
            final String name = rg.getStringValue(nameAttr, id);
            if (name.equals(v0name)) {
                v0 = id;
            } else if (name.equals(v1name)) {
                v1 = id;
            } else {
                v2 = id;
            }
        }
        assertEquals(rg.getStringValue(nameAttr, v0), v0name);
        assertEquals(rg.getStringValue(nameAttr, v1), v1name);
        assertEquals(rg.getStringValue(nameAttr, v2), v2name);
        // Assert that the two transactions exist as expected and no others
        assertEquals(rg.getTransactionCount(), 1);
        final int l0_1 = rg.getLink(v0, v1);
        assertEquals(rg.getLinkTransactionCount(l0_1), 1);
        final int t0_1 = rg.getLinkTransaction(l0_1, 0);
        assertFalse(rg.getTransactionDirection(t0_1) == Graph.FLAT);
        assertEquals(rg.getTransactionSourceVertex(t0_1), v0);
        final int l1_2 = rg.getLink(v1, v2);
        assertEquals(l1_2, Graph.NOT_FOUND);
        final int l0_2 = rg.getLink(v0, v2);
        assertEquals(l0_2, Graph.NOT_FOUND);
    } finally {
        rg.release();
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) DeselectAllPlugin(au.gov.asd.tac.constellation.graph.visual.plugins.select.DeselectAllPlugin) CopyToNewGraphPlugin(au.gov.asd.tac.constellation.graph.interaction.plugins.clipboard.CopyToNewGraphPlugin) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Example 47 with WritableGraph

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

the class CompositesNGTest method contractCompositesNoCompositesTest.

@Test
public void contractCompositesNoCompositesTest() throws InterruptedException, PluginException {
    final WritableGraph wg = graph.getWritableGraph("test", true);
    // Add two vertices then contract all composites
    try {
        wg.addVertex();
        wg.addVertex();
        PluginExecution.withPlugin(InteractiveGraphPluginRegistry.CONTRACT_ALL_COMPOSITES).executeNow(wg);
    } finally {
        wg.commit();
    }
    // Assert that nothing happened to the graph since there were no composites
    ReadableGraph rg = graph.getReadableGraph();
    try {
        final int compositeAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.get(rg);
        assertEquals(rg.getVertexCount(), 2);
        assertEquals(compositeAttr, Graph.NOT_FOUND);
    } finally {
        rg.release();
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) Test(org.testng.annotations.Test)

Example 48 with WritableGraph

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

the class CompositesNGTest method makeExpandDeleteNodeContractCompositeTest.

@Test
public void makeExpandDeleteNodeContractCompositeTest() throws InterruptedException, PluginException {
    final WritableGraph wg = graph.getWritableGraph("test", true);
    wg.getSchema().newGraph(wg);
    final int v0, v1, v2;
    final String v0name, v1name, v2name;
    final int nameAttr;
    final int selectedAttr;
    final int compositeAttr;
    try {
        // Add three vertices
        v0 = wg.addVertex();
        wg.getSchema().newVertex(wg, v0);
        v1 = wg.addVertex();
        wg.getSchema().newVertex(wg, v1);
        v2 = wg.addVertex();
        wg.getSchema().newVertex(wg, v2);
        // Store the names of these vertices
        nameAttr = VisualConcept.VertexAttribute.LABEL.get(wg);
        v0name = wg.getStringValue(nameAttr, v0);
        v1name = wg.getStringValue(nameAttr, v1);
        v2name = wg.getStringValue(nameAttr, v2);
        // Add transactions from v0 to v1, and from v1 to v2
        final int t0_1 = wg.addTransaction(v0, v1, true);
        wg.getSchema().newTransaction(wg, t0_1);
        final int t1_2 = wg.addTransaction(v1, v2, true);
        wg.getSchema().newTransaction(wg, t1_2);
        // Select v0 and v1, but not v2
        selectedAttr = VisualConcept.VertexAttribute.SELECTED.get(wg);
        wg.setBooleanValue(selectedAttr, v0, true);
        wg.setBooleanValue(selectedAttr, v1, true);
        wg.setBooleanValue(selectedAttr, v2, false);
        // Make a composite from the selection then expand all composites
        PluginExecutor.startWith(PluginRegistry.get(InteractiveGraphPluginRegistry.CREATE_COMPOSITE_FROM_SELECTION)).followedBy(PluginRegistry.get(InteractiveGraphPluginRegistry.EXPAND_ALL_COMPOSITES)).executeNow(wg);
        compositeAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.get(wg);
        // Delete the expanded vertex corresponding to v0 then contract all composites
        final int v2pos = wg.getVertexPosition(v2);
        final int comp0 = v2pos != 0 ? wg.getVertex(0) : wg.getVertex(2);
        final int comp1 = v2pos != 1 ? wg.getVertex(1) : wg.getVertex(2);
        final int expanded_v0 = wg.getStringValue(nameAttr, comp0).equals(v0name) ? comp0 : comp1;
        wg.removeVertex(expanded_v0);
        PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.CONTRACT_ALL_COMPOSITES)).executeNow(wg);
    } finally {
        wg.commit();
    }
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        // Assert that there are two vertices, that the composite node has a composite state containing one node,
        // and that the non-composite node has a null composite state and its original name.
        assertEquals(rg.getVertexCount(), 2);
        assertEquals(rg.getStringValue(nameAttr, v2), v2name);
        assertNull(rg.getObjectValue(compositeAttr, v2));
        final int compositeNode = rg.getVertex(0) != v2 ? rg.getVertex(0) : rg.getVertex(1);
        final CompositeNodeState compositeState = (CompositeNodeState) rg.getObjectValue(compositeAttr, compositeNode);
        assertNotNull(compositeState);
        assertEquals(compositeState.getNumberOfNodes(), 1);
        assertTrue(compositeState.isComposite());
        // Assert that the link between the composite node and the non-composite node still exists,
        // and contains one transaction from the composite node to the non-composite node
        final int compLink = rg.getLink(compositeNode, v2);
        assertEquals(rg.getLinkTransactionCount(compLink), 1);
        final int compTrans = rg.getLinkTransaction(compLink, 0);
        assertFalse(rg.getTransactionDirection(compTrans) == Graph.FLAT);
        assertEquals(rg.getTransactionSourceVertex(compTrans), compositeNode);
    } finally {
        rg.release();
    }
    final WritableGraph wg2 = graph.getWritableGraph("test", true);
    try {
        PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.EXPAND_ALL_COMPOSITES)).executeNow(wg2);
    } finally {
        wg2.commit();
    }
    final ReadableGraph rg2 = graph.getReadableGraph();
    final int expanded_v1;
    try {
        // Assert that there are two nodes, that v2 is the same with a null composite state,
        // and that the other node corresponds to v1 with an expanded composite states
        assertEquals(rg2.getVertexCount(), 2);
        assertEquals(rg2.getStringValue(nameAttr, v2), v2name);
        assertNull(rg2.getObjectValue(compositeAttr, v2));
        final int v2pos = rg2.getVertexPosition(v2);
        expanded_v1 = v2pos != 0 ? rg2.getVertex(0) : rg2.getVertex(1);
        final CompositeNodeState compositeState = (CompositeNodeState) rg2.getObjectValue(compositeAttr, expanded_v1);
        assertNotNull(compositeState);
        assertEquals(compositeState.getNumberOfNodes(), 1);
        assertTrue(compositeState.comprisesAComposite());
        assertEquals(rg2.getStringValue(nameAttr, expanded_v1), v1name);
        // Assert that the transaction between exapnded v1 and v2 exist, and no others.
        assertEquals(rg2.getTransactionCount(), 1);
        final int l0_1 = rg2.getLink(expanded_v1, v2);
        assertEquals(rg2.getLinkTransactionCount(l0_1), 1);
        final int t0_1 = rg2.getLinkTransaction(l0_1, 0);
        assertEquals(rg2.getTransactionSourceVertex(t0_1), expanded_v1);
        assertFalse(rg2.getTransactionDirection(t0_1) == Graph.FLAT);
    } finally {
        rg2.release();
    }
    final WritableGraph wg3 = graph.getWritableGraph("test", true);
    try {
        // Delete v1 and then contract all composites
        wg.removeVertex(expanded_v1);
        PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.CONTRACT_ALL_COMPOSITES)).executeNow(wg3);
    } finally {
        wg3.commit();
    }
    final ReadableGraph rg3 = graph.getReadableGraph();
    try {
        // Assert that there is only one vertex - v2 with a null composite state
        assertEquals(rg3.getVertexCount(), 1);
        assertEquals(rg3.getStringValue(nameAttr, v2), v2name);
        assertNull(rg3.getObjectValue(compositeAttr, v2));
        // Assert that there are no transactions
        assertEquals(rg3.getTransactionCount(), 0);
    } finally {
        rg3.release();
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) CompositeNodeState(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) Test(org.testng.annotations.Test)

Example 49 with WritableGraph

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

the class CompositesNGTest method makeExpandDeleteNodeWithTransactionContractCompositeTest.

@Test
public void makeExpandDeleteNodeWithTransactionContractCompositeTest() throws InterruptedException, PluginException {
    final WritableGraph wg = graph.getWritableGraph("test", true);
    wg.getSchema().newGraph(wg);
    final int v0, v1, v2;
    final String v0name, v1name, v2name;
    final int nameAttr;
    final int selectedAttr;
    final int compositeAttr;
    try {
        // Add three vertices
        v0 = wg.addVertex();
        wg.getSchema().newVertex(wg, v0);
        v1 = wg.addVertex();
        wg.getSchema().newVertex(wg, v1);
        v2 = wg.addVertex();
        wg.getSchema().newVertex(wg, v2);
        // Store the names of these vertices
        nameAttr = VisualConcept.VertexAttribute.LABEL.get(wg);
        v0name = wg.getStringValue(nameAttr, v0);
        v1name = wg.getStringValue(nameAttr, v1);
        v2name = wg.getStringValue(nameAttr, v2);
        // Add transactions from v0 to v1, and from v1 to v2
        final int t0_1 = wg.addTransaction(v0, v1, true);
        wg.getSchema().newTransaction(wg, t0_1);
        final int t1_2 = wg.addTransaction(v1, v2, true);
        wg.getSchema().newTransaction(wg, t1_2);
        // Select v0 and v1, but not v2
        selectedAttr = VisualConcept.VertexAttribute.SELECTED.get(wg);
        wg.setBooleanValue(selectedAttr, v0, true);
        wg.setBooleanValue(selectedAttr, v1, true);
        wg.setBooleanValue(selectedAttr, v2, false);
        // Make a composite from the selection then expand all composites
        PluginExecutor.startWith(PluginRegistry.get(InteractiveGraphPluginRegistry.CREATE_COMPOSITE_FROM_SELECTION)).followedBy(PluginRegistry.get(InteractiveGraphPluginRegistry.EXPAND_ALL_COMPOSITES)).executeNow(wg);
        compositeAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.get(wg);
        // Delete the expanded vertex corresponding to v1 then contract all composites
        final int v2pos = wg.getVertexPosition(v2);
        final int comp0 = v2pos != 0 ? wg.getVertex(0) : wg.getVertex(2);
        final int comp1 = v2pos != 1 ? wg.getVertex(1) : wg.getVertex(2);
        final int expanded_v1 = wg.getStringValue(nameAttr, comp0).equals(v1name) ? comp0 : comp1;
        wg.removeVertex(expanded_v1);
        PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.CONTRACT_ALL_COMPOSITES)).executeNow(wg);
    } finally {
        wg.commit();
    }
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        // Assert that there are two vertices, that the composite node has a composite state containing one node,
        // and that the non-composite node has a null composite state and its original name.
        assertEquals(rg.getVertexCount(), 2);
        assertEquals(rg.getStringValue(nameAttr, v2), v2name);
        assertNull(rg.getObjectValue(compositeAttr, v2));
        final int compositeNode = rg.getVertex(0) != v2 ? rg.getVertex(0) : rg.getVertex(1);
        final CompositeNodeState compositeState = (CompositeNodeState) rg.getObjectValue(compositeAttr, compositeNode);
        assertNotNull(compositeState);
        assertEquals(compositeState.getNumberOfNodes(), 1);
        assertTrue(compositeState.isComposite());
        // Assert that there are no transactions in the graph.
        assertEquals(rg.getTransactionCount(), 0);
    } finally {
        rg.release();
    }
    final WritableGraph wg2 = graph.getWritableGraph("test", true);
    try {
        PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.EXPAND_ALL_COMPOSITES)).executeNow(wg2);
    } finally {
        wg2.commit();
    }
    final ReadableGraph rg2 = graph.getReadableGraph();
    final int expanded_v0;
    try {
        // Assert that there are two nodes, that v2 is the same with a null composite state,
        // and that the other node corresponds to v0 with an expanded composite states
        assertEquals(rg2.getVertexCount(), 2);
        assertEquals(rg2.getStringValue(nameAttr, v2), v2name);
        assertNull(rg2.getObjectValue(compositeAttr, v2));
        final int v2pos = rg2.getVertexPosition(v2);
        expanded_v0 = v2pos != 0 ? rg2.getVertex(0) : rg2.getVertex(1);
        final CompositeNodeState compositeState = (CompositeNodeState) rg2.getObjectValue(compositeAttr, expanded_v0);
        assertNotNull(compositeState);
        assertEquals(compositeState.getNumberOfNodes(), 1);
        assertTrue(compositeState.comprisesAComposite());
        assertEquals(rg2.getStringValue(nameAttr, expanded_v0), v0name);
        // Assert that there are no transactions in the graph
        assertEquals(rg.getTransactionCount(), 0);
    } finally {
        rg2.release();
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) CompositeNodeState(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) Test(org.testng.annotations.Test)

Example 50 with WritableGraph

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

the class DualGraphFlushTester method main.

public static void main(String[] args) throws InterruptedException {
    StoreGraph g = new StoreGraph();
    Graph dg = new DualGraph(g, true);
    dg.setUndoManager(new UndoRedo.Manager());
    WritableGraph wg = dg.getWritableGraph("sdfgsdfg", true);
    final int x = VisualConcept.VertexAttribute.X.ensure(wg);
    final int y = VisualConcept.VertexAttribute.Y.ensure(wg);
    final int z = VisualConcept.VertexAttribute.Z.ensure(wg);
    final int id = VisualConcept.VertexAttribute.IDENTIFIER.ensure(wg);
    for (int i = 0; i < 1000; i++) {
        final int vxId = wg.addVertex();
        wg.setFloatValue(x, vxId, i);
        wg.setFloatValue(y, vxId, i);
        wg.setFloatValue(z, vxId, i);
        wg.setStringValue(id, vxId, String.valueOf(i));
    }
    wg.commit();
    final float[] coordSums = new float[1000];
    GraphChangeListener gcl = (GraphChangeEvent event) -> {
        ReadableGraph rg = dg.getReadableGraph();
        try {
            for (int i = 0; i < 1000; i++) {
                final int vxId = rg.getVertex(i);
                coordSums[i] = rg.getFloatValue(x, vxId) + rg.getFloatValue(y, vxId) + rg.getFloatValue(z, vxId);
            }
        } finally {
            rg.release();
        }
    };
    dg.addGraphChangeListener(gcl);
    long time = System.currentTimeMillis();
    wg = dg.getWritableGraph("test", false);
    for (int i = 0; i < 100000; i++) {
        for (int j = 0; j < 1000; j++) {
            final int vxId = wg.getVertex(j);
            wg.setFloatValue(x, vxId, wg.getFloatValue(x, vxId) + 1);
            wg.setFloatValue(y, vxId, wg.getFloatValue(y, vxId) + 2);
            wg.setFloatValue(z, vxId, wg.getFloatValue(z, vxId) + 3);
        }
        wg = wg.flush(false);
    }
    wg.commit();
    LOGGER.log(Level.INFO, "took: {0}", (System.currentTimeMillis() - time));
    Thread.sleep(3000);
    LOGGER.log(Level.INFO, "{0}", Arrays.toString(coordSums));
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) GraphChangeListener(au.gov.asd.tac.constellation.graph.monitor.GraphChangeListener) UndoRedo(org.openide.awt.UndoRedo) GraphChangeEvent(au.gov.asd.tac.constellation.graph.monitor.GraphChangeEvent) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph)

Aggregations

WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)116 Test (org.testng.annotations.Test)77 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)39 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)37 Graph (au.gov.asd.tac.constellation.graph.Graph)20 ArrayList (java.util.ArrayList)14 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)9 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)8 CompositeNodeState (au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState)8 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)7 BeforeMethod (org.testng.annotations.BeforeMethod)7 GraphElementType (au.gov.asd.tac.constellation.graph.GraphElementType)6 Schema (au.gov.asd.tac.constellation.graph.schema.Schema)6 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)6 Attribute (au.gov.asd.tac.constellation.graph.Attribute)5 GraphAttribute (au.gov.asd.tac.constellation.graph.GraphAttribute)5 CopyToNewGraphPlugin (au.gov.asd.tac.constellation.graph.interaction.plugins.clipboard.CopyToNewGraphPlugin)5 ConstellationColor (au.gov.asd.tac.constellation.utilities.color.ConstellationColor)4 DuplicateKeyException (au.gov.asd.tac.constellation.graph.DuplicateKeyException)3 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)3