use of au.gov.asd.tac.constellation.graph.processing.GraphRecordStore in project constellation by constellation-app.
the class CompareGraphPluginNGTest method testReadWithDuplicateGraphScenarioInReverse.
// @Test(expectedExceptions = DuplicateKeyException.class)
@Test
public void testReadWithDuplicateGraphScenarioInReverse() throws InterruptedException {
int vx0, vx1, vx2, tx0, tx1;
int identifierAttribute, vertexTypeAttribute, uniqueIdAttribute, transactionTypeAttribute, transactionDateTimeAttribute;
final Schema schema = SchemaFactoryUtilities.getSchemaFactory(AnalyticSchemaFactory.ANALYTIC_SCHEMA_ID).createSchema();
final StoreGraph originalGraph = new StoreGraph(schema);
identifierAttribute = VisualConcept.VertexAttribute.IDENTIFIER.ensure(originalGraph);
vertexTypeAttribute = AnalyticConcept.VertexAttribute.TYPE.ensure(originalGraph);
uniqueIdAttribute = VisualConcept.TransactionAttribute.IDENTIFIER.ensure(originalGraph);
transactionTypeAttribute = AnalyticConcept.TransactionAttribute.TYPE.ensure(originalGraph);
transactionDateTimeAttribute = TemporalConcept.TransactionAttribute.DATETIME.ensure(originalGraph);
originalGraph.setPrimaryKey(GraphElementType.VERTEX, identifierAttribute, vertexTypeAttribute);
originalGraph.setPrimaryKey(GraphElementType.TRANSACTION, uniqueIdAttribute, transactionTypeAttribute, transactionDateTimeAttribute);
originalGraph.validateKeys();
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, "Vertex #0");
originalGraph.setStringValue(identifierAttribute, vx1, "Vertex #1");
// mimic creating nodes on visual schema which will create a DuplicateKeyException - i.e. this is a known issue
originalGraph.setStringValue(identifierAttribute, vx2, "Vertex #2");
originalGraph.setStringValue(vertexTypeAttribute, vx0, "Unknown");
originalGraph.setStringValue(vertexTypeAttribute, vx1, "Unknown");
originalGraph.setStringValue(vertexTypeAttribute, vx2, "Unknown");
Graph compareGraph;
GraphRecordStore compareAll;
try {
final Plugin copyGraphPlugin = PluginRegistry.get(InteractiveGraphPluginRegistry.COPY_TO_NEW_GRAPH);
final PluginParameters copyGraphParams = copyGraphPlugin.createParameters();
PluginExecution.withPlugin(copyGraphPlugin).withParameters(copyGraphParams).executeNow((GraphReadMethods) originalGraph);
compareGraph = (Graph) copyGraphParams.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);
wg.removeVertex(vx1);
Assert.assertEquals(wg.getVertexCount(), 2);
} finally {
wg.commit();
}
final ReadableGraph rg = compareGraph.getReadableGraph();
try {
compareAll = GraphRecordStoreUtilities.getAll(rg, false, true);
} finally {
rg.release();
}
final GraphRecordStore originalAll = GraphRecordStoreUtilities.getAll(originalGraph, false, true);
Set<String> vertexPrimaryKeys = null;
Set<String> transactionPrimaryKeys = null;
final ReadableGraph rg2 = compareGraph.getReadableGraph();
try {
vertexPrimaryKeys = PrimaryKeyUtilities.getPrimaryKeyNames(rg2, GraphElementType.VERTEX);
transactionPrimaryKeys = PrimaryKeyUtilities.getPrimaryKeyNames(rg2, GraphElementType.TRANSACTION);
} finally {
rg2.release();
}
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("", compareAll, originalAll, vertexPrimaryKeys, transactionPrimaryKeys, ignoreVertexAttributes, ignoreTransactionAttributes, ADDED_COLOUR, REMOVED_COLOUR, CHANGED_COLOUR, UNCHANGED_COLOUR);
System.out.println("changes ==>\n" + changes.toStringVerbose());
// assertEquals(changes.size(), 3);
finalGraph = instance.createComparisonGraph(compareGraph, changes);
} catch (InterruptedException | PluginException ex) {
Assert.fail(ex.getLocalizedMessage());
}
// }
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.processing.GraphRecordStore 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.processing.GraphRecordStore in project constellation by constellation-app.
the class CreateCompositeFromSelectionPlugin method edit.
@Override
public void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
final int selectedAttr = VisualConcept.VertexAttribute.SELECTED.get(graph);
if (selectedAttr != Graph.NOT_FOUND) {
final Set<Integer> selectedVerts = new HashSet<>();
for (int i = 0; i < graph.getVertexCount(); i++) {
final int vxId = graph.getVertex(i);
if (graph.getBooleanValue(selectedAttr, vxId)) {
selectedVerts.add(vxId);
}
}
if (selectedVerts.size() > 1) {
final int compositeStateAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.ensure(graph);
final int uniqueIdAttr = VisualConcept.TransactionAttribute.IDENTIFIER.get(graph);
final int identifierAttr = VisualConcept.VertexAttribute.IDENTIFIER.get(graph);
// We first destroy any composites which are selected or expanded and have constituents selected.
// For any composites, we also add any expanded ids and remove the destroyed composite id from the list of selected ids.
final Set<Integer> addedVerts = new HashSet<>();
final Set<Integer> removedVerts = new HashSet<>();
selectedVerts.forEach(id -> {
final List<Integer> resultingVerts = CompositeUtilities.destroyComposite(graph, compositeStateAttr, uniqueIdAttr, id);
if (!resultingVerts.isEmpty()) {
removedVerts.add(id);
addedVerts.addAll(resultingVerts);
}
});
// NOTE:: Remove before adding, because of id reuse!
selectedVerts.removeAll(removedVerts);
selectedVerts.addAll(addedVerts);
final String compositeIdentifier = String.format("%s + %d more...", graph.getStringValue(identifierAttr, selectedVerts.iterator().next()), selectedVerts.size() - 1);
String copyId = "";
for (int primarykeyAttr : graph.getPrimaryKey(GraphElementType.VERTEX)) {
final String val = graph.getStringValue(primarykeyAttr, selectedVerts.iterator().next());
copyId += graph.getAttributeName(primarykeyAttr) + "<" + StringUtils.defaultString(val) + ">";
}
final String compositeId = copyId;
// Make a record store representing the new composite that is about to be added
RecordStore newCompositeStore = new GraphRecordStore();
newCompositeStore.add();
newCompositeStore.set(GraphRecordStoreUtilities.SOURCE + GraphRecordStoreUtilities.ID, compositeId);
newCompositeStore.set(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.IDENTIFIER, compositeIdentifier);
newCompositeStore.set(GraphRecordStoreUtilities.SOURCE + AnalyticConcept.VertexAttribute.TYPE, SchemaVertexTypeUtilities.getDefaultType());
// Construct and set an expanded composite node state for each of the nodes that will constitute the composite.
selectedVerts.forEach(id -> {
ExpandedCompositeNodeState expandedState = new ExpandedCompositeNodeState(newCompositeStore, compositeId, true, selectedVerts.size());
graph.setObjectValue(compositeStateAttr, id, new CompositeNodeState(id, expandedState));
});
// Create the composite by calling contract on the first node's expanded composite state.
((CompositeNodeState) graph.getObjectValue(compositeStateAttr, selectedVerts.iterator().next())).expandedState.contract(graph);
}
}
PluginExecution.withPlugin(VisualSchemaPluginRegistry.COMPLETE_SCHEMA).executeNow(graph);
}
use of au.gov.asd.tac.constellation.graph.processing.GraphRecordStore in project constellation by constellation-app.
the class AddRecordStore method callService.
@Override
public void callService(final PluginParameters parameters, final InputStream in, final OutputStream out) throws IOException {
final String graphId = parameters.getStringValue(GRAPH_ID_PARAMETER_ID);
final boolean completeWithSchema = parameters.getBooleanValue(COMPLETE_PARAMETER_ID);
final String arrange = parameters.getStringValue(ARRANGE_PARAMETER_ID);
final boolean resetView = parameters.getBooleanValue(RESET_PARAMETER_ID);
final RecordStore rs = new GraphRecordStore();
final ObjectMapper mapper = new ObjectMapper();
final JsonNode json = mapper.readTree(in);
final Graph graph = graphId == null ? RestUtilities.getActiveGraph() : GraphNode.getGraph(graphId);
if (graph == null) {
throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, "No graph with id " + graphId);
}
// (We ignore the index array.)
if (!json.hasNonNull(COLUMNS) || !json.get(COLUMNS).isArray()) {
throw new RestServiceException("Could not find columns object containing column names");
}
if (!json.hasNonNull("data") || !json.get("data").isArray()) {
throw new RestServiceException("Could not find data object containing data rows");
}
final ArrayNode columns = (ArrayNode) json.get(COLUMNS);
final String[] headers = new String[columns.size()];
for (int i = 0; i < headers.length; i++) {
headers[i] = columns.get(i).asText();
}
final ArrayNode data = (ArrayNode) json.get("data");
for (final Iterator<JsonNode> i = data.elements(); i.hasNext(); ) {
final ArrayNode jrow = (ArrayNode) i.next();
rs.add();
boolean txFound = false;
boolean txSourceFound = false;
for (int ix = 0; ix < headers.length; ix++) {
final String h = headers[ix];
final JsonNode jn = jrow.get(ix);
if (!jn.isNull()) {
if (jn.getNodeType() == JsonNodeType.ARRAY) {
rs.set(h, RestServiceUtilities.toList((ArrayNode) jn));
} else {
rs.set(h, jn.asText());
}
}
txFound |= h.startsWith(GraphRecordStoreUtilities.TRANSACTION);
txSourceFound |= TX_SOURCE.equals(h);
}
if (txFound && !txSourceFound) {
rs.set(TX_SOURCE, API_SOURCE);
}
}
addToGraph(graph, rs, completeWithSchema, arrange, resetView);
}
use of au.gov.asd.tac.constellation.graph.processing.GraphRecordStore in project constellation by constellation-app.
the class CompositeUtilities method makeComposite.
/**
* Make a composite node by specifying a lead node id and a set of the
* comprising node id's
*
* @param graph The graph
* @param comprisingIds A Set of the comprising node id's to be composited
* @param leaderId The lead node id to contain the composited nodes
*/
public static void makeComposite(final GraphWriteMethods graph, final Set<Integer> comprisingIds, final int leaderId) {
final int compositeStateAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.ensure(graph);
final int uniqueIdAttr = VisualConcept.TransactionAttribute.IDENTIFIER.get(graph);
// We first destroy any composites or composite constituents about to be composited.
// For any composites, we also add any expanded ids and remove the destroyed composite id from the list of selected ids.
final Set<Integer> addedVerts = new HashSet<>();
final Set<Integer> removedVerts = new HashSet<>();
comprisingIds.forEach(vxId -> {
final List<Integer> resultingVerts = CompositeUtilities.destroyComposite(graph, compositeStateAttr, uniqueIdAttr, vxId);
if (!resultingVerts.isEmpty()) {
removedVerts.add(vxId);
addedVerts.addAll(resultingVerts);
}
});
// NOTE:: Remove before adding, because of id reuse!
comprisingIds.removeAll(removedVerts);
comprisingIds.addAll(addedVerts);
// Make a record store representing the new composite that is about to be added
final String[] compositeId = new String[1];
final RecordStore newCompositeStore = new GraphRecordStore();
GraphRecordStoreUtilities.copySpecifiedVertex(graph, newCompositeStore, leaderId, compositeId);
// Construct and set an expanded composite node state for each of the nodes that will constitute the composite.
comprisingIds.forEach(vxId -> {
final ExpandedCompositeNodeState expandedState = new ExpandedCompositeNodeState(newCompositeStore, compositeId[0], true, comprisingIds.size());
graph.setObjectValue(compositeStateAttr, vxId, new CompositeNodeState(vxId, expandedState));
});
// Create the composite by calling contract on the first node's expanded composite state.
((CompositeNodeState) graph.getObjectValue(compositeStateAttr, comprisingIds.iterator().next())).expandedState.contract(graph);
}
Aggregations