use of au.gov.asd.tac.constellation.graph.GraphAttribute in project constellation by constellation-app.
the class FindNGTest method findBooleanValuesTest.
@Test
public void findBooleanValuesTest() 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("boolean_content", true);
FindRule rule1 = new FindRule(FindTypeOperators.Type.BOOLEAN, new GraphAttribute(rg, vSelAttr), 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", 3, results.size());
assertTrue("node 'name2' found", nodeFound(rg, "name2", results));
assertTrue("node 'name4' found", nodeFound(rg, "name4", results));
assertTrue("node 'name6' found", nodeFound(rg, "name6", results));
} finally {
rg.release();
}
}
use of au.gov.asd.tac.constellation.graph.GraphAttribute in project constellation by constellation-app.
the class FindNGTest method findSingleNodeCaseSensitiveFindTest.
@Test
public void findSingleNodeCaseSensitiveFindTest() 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 gn = 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", 1, results.size());
assertTrue("node 'name1' found", nodeFound(rg, "name1", results));
} finally {
rg.release();
}
}
use of au.gov.asd.tac.constellation.graph.GraphAttribute in project constellation by constellation-app.
the class FindNGTest method findFloatValuesTest.
@Test
public void findFloatValuesTest() 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("float_first_item", 5.1f);
FindRule rule1 = new FindRule(FindTypeOperators.Type.FLOAT, new GraphAttribute(rg, attrX), FindTypeOperators.Operator.GREATER_THAN, 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", 2, results.size());
assertTrue("node 'name6' found", nodeFound(rg, "name6", results));
assertTrue("node 'name7' found", nodeFound(rg, "name7", results));
} finally {
rg.release();
}
}
use of au.gov.asd.tac.constellation.graph.GraphAttribute 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.GraphAttribute in project constellation by constellation-app.
the class GraphJsonWriter method writeElements.
/**
* Write elements of a Graph to JSON.
*
* @param jg The JsonGenerator to use for writing.
* @param graph The graph.
* @param elementType The GraphElementType being written.
* @param writeData If false, write out the attributes but not the data for
* the given element type.
*
* @throws IOException
*/
private void writeElements(final JsonGenerator jg, final GraphReadMethods graph, final GraphElementType elementType, final boolean verbose, final boolean writeData) throws Exception {
final String elementTypeLabel = IoUtilities.getGraphElementTypeString(elementType);
if (progress != null) {
progress.progress("Writing " + elementTypeLabel + " elements...");
}
final AbstractGraphIOProvider[] ioProviders = new AbstractGraphIOProvider[graph.getAttributeCapacity()];
final ArrayList<Attribute> attrs = new ArrayList<>();
for (int position = 0; position < graph.getAttributeCount(elementType); position++) {
final int attrId = graph.getAttribute(elementType, position);
final Attribute attr = new GraphAttribute(graph, attrId);
ioProviders[attrId] = graphIoProviders.get(attr.getAttributeType());
// Don't write non-META object types; we don't know what they are.
if (!"object".equals(attr.getAttributeType()) || elementType == GraphElementType.META) {
attrs.add(attr);
}
}
// Here we go.
jg.writeStartObject();
jg.writeArrayFieldStart(elementTypeLabel);
jg.writeStartObject();
jg.writeArrayFieldStart("attrs");
// Write the attributes.
for (final Attribute attr : attrs) {
jg.writeStartObject();
jg.writeStringField("label", attr.getName());
jg.writeStringField("type", attr.getAttributeType());
if (attr.getDescription() != null) {
jg.writeStringField("descr", attr.getDescription());
}
// actual attribute values inside the attribute descriptions.
if (attr.getDefaultValue() != null && isNumeric(attr)) {
jg.writeNumberField(DEFAULT_FIELD, ((Number) attr.getDefaultValue()).doubleValue());
} else if (attr.getDefaultValue() != null && "boolean".equals(attr.getAttributeType())) {
jg.writeBooleanField(DEFAULT_FIELD, (Boolean) attr.getDefaultValue());
} else if (attr.getDefaultValue() != null) {
jg.writeStringField(DEFAULT_FIELD, attr.getDefaultValue().toString());
} else {
// Do nothing
}
if (attr.getAttributeMerger() != null) {
jg.writeStringField("merger", attr.getAttributeMerger().getId());
}
jg.writeNumberField("mod_count", graph.getValueModificationCounter(attr.getId()));
jg.writeEndObject();
}
jg.writeEndArray();
// Check for optional key.
if (elementType == GraphElementType.VERTEX || elementType == GraphElementType.TRANSACTION) {
final int[] key = graph.getPrimaryKey(elementType);
if (key.length > 0) {
// Write the labels of the key attributes.
jg.writeArrayFieldStart("key");
for (int i = 0; i < key.length; i++) {
final Attribute attr = new GraphAttribute(graph, key[i]);
jg.writeString(attr.getName());
}
jg.writeEndArray();
}
}
jg.writeEndObject();
// Write the main graph data (graph, vertex, transaction).
jg.writeStartObject();
jg.writeArrayFieldStart("data");
if (!writeData) {
// Do nothing
} else if (elementType == GraphElementType.GRAPH || elementType == GraphElementType.META) {
jg.writeStartObject();
for (final Attribute attr : attrs) {
final AbstractGraphIOProvider ioProvider = ioProviders[attr.getId()];
if (ioProvider != null) {
// Get the provider to write its data into an ObjectNode.
// If they didn't write anything, don't write the data to the JSON.
ioProvider.writeObject(attr, 0, jg, graph, byteWriter, verbose);
} else {
final Object value = graph.getObjectValue(attr.getId(), 0);
final String className = value != null ? value.getClass().getName() : "<null>";
final String msg = String.format("No I/O provider found for object type %s, attribute %s", className, attr);
throw new Exception(msg);
}
}
jg.writeEndObject();
} else if (elementType == GraphElementType.VERTEX) {
for (int position = 0; position < graph.getVertexCount(); position++) {
final int vxId = graph.getVertex(position);
jg.writeStartObject();
jg.writeNumberField(GraphFileConstants.VX_ID, vxId);
for (final Attribute attr : attrs) {
final AbstractGraphIOProvider ioProvider = ioProviders[attr.getId()];
if (ioProvider != null) {
// Get the provider to write its data into an ObjectNode.
// If they didn't write anything, don't write the data to the JSON.
ioProvider.writeObject(attr, vxId, jg, graph, byteWriter, verbose);
} else {
throw new Exception("No IO provider found for attribute type: " + attr.getAttributeType());
}
}
jg.writeEndObject();
counter++;
if (counter % REPORT_INTERVAL == 0 && isCancelled) {
return;
} else if (counter % REPORT_INTERVAL == 0 && progress != null) {
progress.progress(counter);
} else {
// Do nothing
}
}
} else if (elementType == GraphElementType.TRANSACTION) {
for (int position = 0; position < graph.getTransactionCount(); position++) {
final int txId = graph.getTransaction(position);
jg.writeStartObject();
jg.writeNumberField(GraphFileConstants.TX_ID, txId);
jg.writeNumberField(GraphFileConstants.SRC, graph.getTransactionSourceVertex(txId));
jg.writeNumberField(GraphFileConstants.DST, graph.getTransactionDestinationVertex(txId));
jg.writeBooleanField(GraphFileConstants.DIR, graph.getTransactionDirection(txId) != Graph.UNDIRECTED);
for (final Attribute attr : attrs) {
final AbstractGraphIOProvider ioProvider = ioProviders[attr.getId()];
if (ioProvider != null) {
// Get the provider to write its data into an ObjectNode.
// If they didn't write anything, don't write the data to the JSON.
ioProvider.writeObject(attr, txId, jg, graph, byteWriter, verbose);
} else {
throw new Exception("No IO provider found for attribute type: " + attr.getAttributeType());
}
}
jg.writeEndObject();
counter++;
if (counter % REPORT_INTERVAL == 0 && isCancelled) {
return;
} else if (counter % REPORT_INTERVAL == 0 && progress != null) {
progress.progress(counter);
}
}
} else {
// Do nothing
}
jg.writeEndArray();
jg.writeEndObject();
jg.writeEndArray();
jg.writeEndObject();
}
Aggregations