Search in sources :

Example 1 with AbstractGraphIOProvider

use of au.gov.asd.tac.constellation.graph.attribute.io.AbstractGraphIOProvider 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();
}
Also used : AbstractGraphIOProvider(au.gov.asd.tac.constellation.graph.attribute.io.AbstractGraphIOProvider) Attribute(au.gov.asd.tac.constellation.graph.Attribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Aggregations

Attribute (au.gov.asd.tac.constellation.graph.Attribute)1 GraphAttribute (au.gov.asd.tac.constellation.graph.GraphAttribute)1 AbstractGraphIOProvider (au.gov.asd.tac.constellation.graph.attribute.io.AbstractGraphIOProvider)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1