use of au.gov.asd.tac.constellation.graph.ReadableGraph in project constellation by constellation-app.
the class FindNGTest method findSingleNodeCaseSensitiveNoFindTest.
@Test
public void findSingleNodeCaseSensitiveNoFindTest() throws InterruptedException, PluginException {
ArrayList<FindRule> rules = new ArrayList<>();
ReadableGraph rg = graph.getReadableGraph();
try {
// setup find criteria / rules
HashMap<String, Object> values = new HashMap<>();
values.put("string_content", "Name1");
values.put("string_case_sensitive", true);
values.put("string_use_list", false);
FindRule rule1 = new FindRule(FindTypeOperators.Type.STRING, new GraphAttribute(rg, rg.getAttribute(GraphElementType.VERTEX, vNameAttr)), FindTypeOperators.Operator.IS, values);
rules.add(rule1);
// perform find search
// need to create temporary GraphNode and skip the RemoteInit portion of the initialisation
GraphNode aGraphNode = new GraphNode(graph, null, new TopComponent(), null);
final AdvancedFindPlugin queryPlugin = new AdvancedFindPlugin(GraphElementType.VERTEX, rules, false);
PluginExecution.withPlugin(queryPlugin).executeNow(graph);
final List<FindResult> results = queryPlugin.getResults();
// validate results
assertEquals("result size", 0, results.size());
} finally {
rg.release();
}
}
use of au.gov.asd.tac.constellation.graph.ReadableGraph in project constellation by constellation-app.
the class GraphSavingNGTest method writeGraphTest.
@Test
public void writeGraphTest() throws Exception {
final String name = "tmp1";
File graphFile = File.createTempFile(name, ".star");
final ReadableGraph rg = graph.getReadableGraph();
try {
GraphJsonWriter writer = new GraphJsonWriter();
writer.writeGraphToZip(rg, graphFile.getPath(), new TextIoProgress(false));
} finally {
rg.release();
}
assertTrue("file created", graphFile.exists());
graphFile.delete();
}
use of au.gov.asd.tac.constellation.graph.ReadableGraph in project constellation by constellation-app.
the class IONGTest method attributeMergerSaveTest.
/**
* Tests that saving and loading a Constellation file preserves the
* attribute mergers specified on each attribute, both in the null case, and
* the non-null case.
*
* @throws IOException
* @throws InterruptedException
* @throws GraphParseException
*/
@Test
public void attributeMergerSaveTest() throws IOException, InterruptedException, GraphParseException {
final StoreGraph storeGraph = new StoreGraph();
storeGraph.addAttribute(GraphElementType.VERTEX, StringAttributeDescription.ATTRIBUTE_NAME, "defaultMergerAttribute", null, null, GraphAttributeMerger.getDefault().getId());
storeGraph.addAttribute(GraphElementType.VERTEX, StringAttributeDescription.ATTRIBUTE_NAME, "customMergerAttribute", null, null, ConcatenatedSetGraphAttributeMerger.ID);
storeGraph.addAttribute(GraphElementType.VERTEX, StringAttributeDescription.ATTRIBUTE_NAME, "noMergerAttribute", null, null, null);
final GraphJsonWriter writer = new GraphJsonWriter();
final ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.writeGraphToStream(storeGraph, out, false, Arrays.asList(GraphElementType.GRAPH, GraphElementType.VERTEX, GraphElementType.TRANSACTION, GraphElementType.META));
final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
GraphJsonReader reader = new GraphJsonReader();
final Graph graph = reader.readGraph("test", in, -1, null);
ReadableGraph rg = graph.getReadableGraph();
try {
final int defaultMergerAttributeId = rg.getAttribute(GraphElementType.VERTEX, "defaultMergerAttribute");
assert rg.getAttributeMerger(defaultMergerAttributeId) == GraphAttributeMerger.getDefault();
final int customMergerAttributeId = rg.getAttribute(GraphElementType.VERTEX, "customMergerAttribute");
assert rg.getAttributeMerger(customMergerAttributeId) == GraphAttributeMerger.getMergers().get(ConcatenatedSetGraphAttributeMerger.ID);
final int noMergerAttributeId = rg.getAttribute(GraphElementType.VERTEX, "noMergerAttribute");
assert rg.getAttributeMerger(noMergerAttributeId) == null;
} finally {
rg.release();
}
}
use of au.gov.asd.tac.constellation.graph.ReadableGraph in project constellation by constellation-app.
the class IONGTest method saveLoadNonNullDefault.
/**
* Saving and loading an attribute with a non-null default value.
*/
@Test
public void saveLoadNonNullDefault() {
final String name = UUID.randomUUID().toString();
File graphFile = null;
try {
graphFile = File.createTempFile(name, ".star");
} catch (IOException ex) {
Assert.fail("Create file", ex);
}
final String nameAttrLabel = "name";
final String nndAttrLabel = "nnd";
final String defaultValue = "A non-null value";
try {
final StoreGraph graph = new StoreGraph();
final int nameAttrId = graph.addAttribute(GraphElementType.VERTEX, StringAttributeDescription.ATTRIBUTE_NAME, nameAttrLabel, nameAttrLabel, "", null);
final int nndAttrId = graph.addAttribute(GraphElementType.VERTEX, StringAttributeDescription.ATTRIBUTE_NAME, nndAttrLabel, "All nulls", defaultValue, null);
final int v0 = graph.addVertex();
Assert.assertEquals(graph.getStringValue(nameAttrId, v0), "");
Assert.assertNotNull(graph.getStringValue(nndAttrId, v0));
Assert.assertEquals(graph.getStringValue(nndAttrId, v0), defaultValue);
final int v1 = graph.addVertex();
graph.setStringValue(nameAttrId, v1, "V1");
graph.setStringValue(nndAttrId, v1, null);
Assert.assertNull(graph.getStringValue(nndAttrId, v1));
GraphJsonWriter writer = new GraphJsonWriter();
writer.writeGraphToZip(graph, graphFile.getPath(), new TextIoProgress(false));
} catch (IOException ex) {
Assert.fail("Graph write", ex);
}
try {
final Graph newGraph = new GraphJsonReader().readGraphZip(graphFile, new TextIoProgress(false));
final ReadableGraph rg = newGraph.getReadableGraph();
final int nameAttrId = rg.getAttribute(GraphElementType.VERTEX, nameAttrLabel);
final int nattrId = rg.getAttribute(GraphElementType.VERTEX, nndAttrLabel);
final int v0 = rg.getVertex(0);
Assert.assertEquals(rg.getStringValue(nameAttrId, v0), "");
final String val0 = rg.getStringValue(nattrId, v0);
Assert.assertEquals(val0, defaultValue);
final int v1 = rg.getVertex(1);
Assert.assertEquals(rg.getStringValue(nameAttrId, v1), "V1");
final String nval1 = rg.getStringValue(nattrId, v1);
Assert.assertNull(nval1, "Expecting the default non-null value");
} catch (IOException ex) {
Assert.fail("Graph read", ex);
} catch (GraphParseException ex) {
Assert.fail("Graph parse", ex);
} finally {
graphFile.delete();
}
}
use of au.gov.asd.tac.constellation.graph.ReadableGraph 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());
}
}
Aggregations