Search in sources :

Example 26 with ReadableGraph

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

the class CompareGraphPluginNGTest method testReadWithDuplicateGraphScenario.

@Test
public void testReadWithDuplicateGraphScenario() throws InterruptedException {
    int vx0, vx1, vx2, tx0, tx1;
    int identifierAttribute, typeAttribute, uniqueIdAttribute;
    final Schema schema = SchemaFactoryUtilities.getSchemaFactory(AnalyticSchemaFactory.ANALYTIC_SCHEMA_ID).createSchema();
    final StoreGraph originalGraph = new StoreGraph(schema);
    identifierAttribute = VisualConcept.VertexAttribute.IDENTIFIER.ensure(originalGraph);
    // typeAttribute = AnalyticConcept.VertexAttribute.TYPE.ensure(originalGraph);
    uniqueIdAttribute = VisualConcept.TransactionAttribute.IDENTIFIER.ensure(originalGraph);
    originalGraph.setPrimaryKey(GraphElementType.VERTEX, identifierAttribute);
    originalGraph.setPrimaryKey(GraphElementType.TRANSACTION, uniqueIdAttribute);
    vx0 = originalGraph.addVertex();
    vx1 = originalGraph.addVertex();
    vx2 = originalGraph.addVertex();
    tx0 = originalGraph.addTransaction(vx0, vx1, true);
    tx1 = originalGraph.addTransaction(vx1, vx2, true);
    originalGraph.setStringValue(identifierAttribute, vx0, "vx0");
    originalGraph.setStringValue(identifierAttribute, vx1, "vx1");
    originalGraph.setStringValue(identifierAttribute, vx2, "vx2");
    Graph compareGraph;
    GraphRecordStore compareAll;
    try {
        final Plugin copyGraphPlugin = PluginRegistry.get(InteractiveGraphPluginRegistry.COPY_TO_NEW_GRAPH);
        final PluginParameters copyParams = copyGraphPlugin.createParameters();
        copyParams.getParameters().get(CopyToNewGraphPlugin.COPY_ALL_PARAMETER_ID).setBooleanValue(true);
        PluginExecution.withPlugin(copyGraphPlugin).withParameters(copyParams).executeNow((GraphReadMethods) originalGraph);
        compareGraph = (Graph) copyParams.getParameters().get(CopyToNewGraphPlugin.NEW_GRAPH_OUTPUT_PARAMETER_ID).getObjectValue();
    } catch (PluginException ex) {
        compareGraph = null;
        Assert.fail(ex.getLocalizedMessage());
    }
    final WritableGraph wg = compareGraph.getWritableGraph("remove a node", true);
    try {
        Assert.assertEquals(wg.getVertexCount(), 3);
        Assert.assertEquals(wg.getTransactionCount(), 2);
        wg.removeVertex(vx1);
        Assert.assertEquals(wg.getVertexCount(), 2);
        Assert.assertEquals(wg.getTransactionCount(), 0);
    } finally {
        wg.commit();
    }
    ReadableGraph rg = compareGraph.getReadableGraph();
    try {
        compareAll = GraphRecordStoreUtilities.getAll(rg, false, true);
    } finally {
        rg.release();
    }
    final GraphRecordStore originalAll = GraphRecordStoreUtilities.getAll(originalGraph, false, true);
    final Set<String> vertexPrimaryKeys = PrimaryKeyUtilities.getPrimaryKeyNames(originalGraph, GraphElementType.VERTEX);
    final Set<String> transactionPrimaryKeys = PrimaryKeyUtilities.getPrimaryKeyNames(originalGraph, GraphElementType.TRANSACTION);
    final List<String> ignoreVertexAttributes = new ArrayList<>();
    final List<String> ignoreTransactionAttributes = new ArrayList<>();
    ignoreVertexAttributes.add("[id]");
    ignoreTransactionAttributes.add("[id]");
    // debug
    System.out.println("originalAll ==>\n" + originalAll.toStringVerbose());
    System.out.println("compareAll ==>\n" + compareAll.toStringVerbose());
    final CompareGraphPlugin instance = new CompareGraphPlugin();
    Graph finalGraph = null;
    GraphRecordStore changes = new GraphRecordStore();
    try {
        changes = instance.compareGraphs("", originalAll, compareAll, vertexPrimaryKeys, transactionPrimaryKeys, ignoreVertexAttributes, ignoreTransactionAttributes, ADDED_COLOUR, REMOVED_COLOUR, CHANGED_COLOUR, UNCHANGED_COLOUR);
        System.out.println("changes ==>\n" + changes.toStringVerbose());
        assertEquals(changes.size(), 5);
        finalGraph = instance.createComparisonGraph(new DualGraph(originalGraph, true), changes);
    } catch (InterruptedException | PluginException ex) {
        Assert.fail(ex.getLocalizedMessage());
    }
    changes.reset();
    changes.next();
    assertEquals(changes.get(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.IDENTIFIER), "vx0");
    assertEquals(changes.get(GraphRecordStoreUtilities.SOURCE + CompareGraphPlugin.COMPARE_ATTRIBUTE), CompareGraphPlugin.UNCHANGED);
    changes.next();
    assertEquals(changes.get(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.IDENTIFIER), "vx1");
    assertEquals(changes.get(GraphRecordStoreUtilities.SOURCE + CompareGraphPlugin.COMPARE_ATTRIBUTE), CompareGraphPlugin.REMOVED);
    changes.next();
    assertEquals(changes.get(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.IDENTIFIER), "vx2");
    assertEquals(changes.get(GraphRecordStoreUtilities.SOURCE + CompareGraphPlugin.COMPARE_ATTRIBUTE), CompareGraphPlugin.UNCHANGED);
    changes.next();
    assertEquals(changes.get(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.IDENTIFIER), "vx0");
    assertEquals(changes.get(GraphRecordStoreUtilities.DESTINATION + VisualConcept.VertexAttribute.IDENTIFIER), "vx1");
    assertEquals(changes.get(GraphRecordStoreUtilities.TRANSACTION + CompareGraphPlugin.COMPARE_ATTRIBUTE), CompareGraphPlugin.REMOVED);
    changes.next();
    assertEquals(changes.get(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.IDENTIFIER), "vx1");
    assertEquals(changes.get(GraphRecordStoreUtilities.DESTINATION + VisualConcept.VertexAttribute.IDENTIFIER), "vx2");
    assertEquals(changes.get(GraphRecordStoreUtilities.TRANSACTION + CompareGraphPlugin.COMPARE_ATTRIBUTE), CompareGraphPlugin.REMOVED);
    rg = finalGraph.getReadableGraph();
    try {
        int vxCount = rg.getVertexCount();
        int txCount = rg.getTransactionCount();
        assertEquals(vxCount, 3);
        assertEquals(txCount, 2);
    } finally {
        rg.release();
    }
    try {
        SaveGraphUtilities.saveGraphToTemporaryDirectory(originalGraph, "originalGraph");
        SaveGraphUtilities.saveGraphToTemporaryDirectory(compareGraph, "compareGraph", true);
        SaveGraphUtilities.saveGraphToTemporaryDirectory(finalGraph, "finalGraph", true);
    } catch (IOException | InterruptedException ex) {
        Assert.fail(ex.getLocalizedMessage());
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Schema(au.gov.asd.tac.constellation.graph.schema.Schema) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) 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) Graph(au.gov.asd.tac.constellation.graph.Graph) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) 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 27 with ReadableGraph

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

the class ListeningGraphMonitor method graphChanged.

@Override
public void graphChanged(final GraphChangeEvent event) {
    if (VERBOSE) {
        LOGGER.log(Level.INFO, "Graph Monitor: graphChanged()");
    }
    final GraphChangeEvent latestEvent = event.getLatest();
    if (latestEvent.getId() > lastEvent) {
        lastEvent = latestEvent.getId();
        ReadableGraph rg = latestEvent.getGraph().getReadableGraph();
        try {
            update(rg, true);
        } finally {
            rg.release();
        }
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph)

Example 28 with ReadableGraph

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

the class PrimaryKeyUnitNGTest method rollbackTest.

@Test
public void rollbackTest() {
    try {
        Graph graph = new DualGraph(null);
        int vName, tName;
        int source, destination;
        int transaction1, transaction2, transaction3;
        WritableGraph wg = graph.getWritableGraph("Set Up Graph", true);
        try {
            vName = wg.addAttribute(GraphElementType.VERTEX, StringAttributeDescription.ATTRIBUTE_NAME, "name", "name", null, null);
            tName = wg.addAttribute(GraphElementType.TRANSACTION, StringAttributeDescription.ATTRIBUTE_NAME, "name", "name", null, null);
            wg.setPrimaryKey(GraphElementType.VERTEX, vName);
            wg.setPrimaryKey(GraphElementType.TRANSACTION, tName);
            source = wg.addVertex();
            wg.setStringValue(vName, source, "Source");
            destination = wg.addVertex();
            wg.setStringValue(vName, destination, "Destination");
            transaction1 = wg.addTransaction(source, destination, true);
            wg.setStringValue(tName, transaction1, "transaction");
        } finally {
            wg.commit();
        }
        try {
            wg = graph.getWritableGraph("Add Duplicate Transactions", true);
            try {
                transaction2 = wg.addTransaction(source, destination, true);
                wg.setStringValue(transaction2, tName, "transaction");
                transaction3 = wg.addTransaction(source, destination, true);
                wg.setStringValue(transaction3, tName, "transaction");
            } finally {
                wg.commit();
            }
        } catch (DuplicateKeyException ex) {
        }
        wg = graph.getWritableGraph("Add Unique Transaction", true);
        try {
            transaction2 = wg.addTransaction(source, destination, true);
            wg.setStringValue(transaction2, tName, "transaction2");
        } finally {
            wg.commit();
        }
        ReadableGraph rg = graph.getReadableGraph();
        try {
            assert rg.getTransactionCount() == 2;
        } finally {
            rg.release();
        }
    } catch (InterruptedException ex) {
        Exceptions.printStackTrace(ex);
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) DuplicateKeyException(au.gov.asd.tac.constellation.graph.DuplicateKeyException) Test(org.testng.annotations.Test)

Example 29 with ReadableGraph

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

the class SaveTemplatePlugin method execute.

@Override
public void execute(final PluginGraphs graphs, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    final Graph graph = graphs.getGraph();
    ReadableGraph rg = graph.getReadableGraph();
    try {
        saveTemplate(rg, parameters.getStringValue(TEMPLATE_NAME_PARAMETER_ID));
        parameters.getParameters().get(TEMPLATE_NAME_PARAMETER_ID).storeRecentValue();
    } finally {
        rg.release();
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph)

Example 30 with ReadableGraph

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

the class UpdateNGTest method multiRelease.

@Test
public void multiRelease() {
    final DualGraph g = new DualGraph(null);
    final ReadableGraph wg = g.getReadableGraph();
    wg.release();
    try {
        wg.release();
    } catch (IllegalMonitorStateException ex) {
        System.out.printf("%s\n", ex);
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Test(org.testng.annotations.Test)

Aggregations

ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)167 Test (org.testng.annotations.Test)62 Graph (au.gov.asd.tac.constellation.graph.Graph)57 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)43 ArrayList (java.util.ArrayList)40 GraphAttribute (au.gov.asd.tac.constellation.graph.GraphAttribute)26 HashMap (java.util.HashMap)16 Attribute (au.gov.asd.tac.constellation.graph.Attribute)14 GraphElementType (au.gov.asd.tac.constellation.graph.GraphElementType)12 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)12 GraphNode (au.gov.asd.tac.constellation.graph.node.GraphNode)12 TableViewState (au.gov.asd.tac.constellation.views.tableview.state.TableViewState)12 HashSet (java.util.HashSet)12 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)11 List (java.util.List)10 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)9 PluginParameter (au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)9 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)9 FindRule (au.gov.asd.tac.constellation.views.find.advanced.FindRule)9 File (java.io.File)9