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