Search in sources :

Example 1 with LexicographicalElementComparator

use of com.tinkerpop.blueprints.util.io.LexicographicalElementComparator in project blueprints by tinkerpop.

the class GraphMLWriter method outputGraph.

/**
     * Write the data in a Graph to a GraphML OutputStream.
     *
     * @param graphMLOutputStream the GraphML OutputStream to write the Graph data to
     * @throws IOException thrown if there is an error generating the GraphML data
     */
public void outputGraph(final OutputStream graphMLOutputStream) throws IOException {
    if (null == vertexKeyTypes || null == edgeKeyTypes) {
        Map<String, String> vertexKeyTypes = new HashMap<String, String>();
        Map<String, String> edgeKeyTypes = new HashMap<String, String>();
        for (Vertex vertex : graph.getVertices()) {
            for (String key : vertex.getPropertyKeys()) {
                if (!vertexKeyTypes.containsKey(key)) {
                    vertexKeyTypes.put(key, GraphMLWriter.getStringType(vertex.getProperty(key)));
                }
            }
            for (Edge edge : vertex.getEdges(Direction.OUT)) {
                for (String key : edge.getPropertyKeys()) {
                    if (!edgeKeyTypes.containsKey(key)) {
                        edgeKeyTypes.put(key, GraphMLWriter.getStringType(edge.getProperty(key)));
                    }
                }
            }
        }
        if (null == this.vertexKeyTypes) {
            this.vertexKeyTypes = vertexKeyTypes;
        }
        if (null == this.edgeKeyTypes) {
            this.edgeKeyTypes = edgeKeyTypes;
        }
    }
    // will live with the edge data itself (which won't validate against the graphml schema)
    if (null != this.edgeLabelKey && null != this.edgeKeyTypes && null == this.edgeKeyTypes.get(this.edgeLabelKey))
        this.edgeKeyTypes.put(this.edgeLabelKey, GraphMLTokens.STRING);
    final XMLOutputFactory inputFactory = XMLOutputFactory.newInstance();
    try {
        XMLStreamWriter writer = inputFactory.createXMLStreamWriter(graphMLOutputStream, "UTF8");
        if (normalize) {
            writer = new GraphMLWriterHelper.IndentingXMLStreamWriter(writer);
            ((GraphMLWriterHelper.IndentingXMLStreamWriter) writer).setIndentStep("    ");
        }
        writer.writeStartDocument();
        writer.writeStartElement(GraphMLTokens.GRAPHML);
        writer.writeAttribute(GraphMLTokens.XMLNS, GraphMLTokens.GRAPHML_XMLNS);
        //XML Schema instance namespace definition (xsi)
        writer.writeAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + GraphMLTokens.XML_SCHEMA_NAMESPACE_TAG, XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
        //XML Schema location
        writer.writeAttribute(GraphMLTokens.XML_SCHEMA_NAMESPACE_TAG + ":" + GraphMLTokens.XML_SCHEMA_LOCATION_ATTRIBUTE, GraphMLTokens.GRAPHML_XMLNS + " " + (this.xmlSchemaLocation == null ? GraphMLTokens.DEFAULT_GRAPHML_SCHEMA_LOCATION : this.xmlSchemaLocation));
        // <key id="weight" for="edge" attr.name="weight" attr.type="float"/>
        Collection<String> keyset;
        if (normalize) {
            keyset = new ArrayList<String>();
            keyset.addAll(vertexKeyTypes.keySet());
            Collections.sort((List<String>) keyset);
        } else {
            keyset = vertexKeyTypes.keySet();
        }
        for (String key : keyset) {
            writer.writeStartElement(GraphMLTokens.KEY);
            writer.writeAttribute(GraphMLTokens.ID, key);
            writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.NODE);
            writer.writeAttribute(GraphMLTokens.ATTR_NAME, key);
            writer.writeAttribute(GraphMLTokens.ATTR_TYPE, vertexKeyTypes.get(key));
            writer.writeEndElement();
        }
        if (normalize) {
            keyset = new ArrayList<String>();
            keyset.addAll(edgeKeyTypes.keySet());
            Collections.sort((List<String>) keyset);
        } else {
            keyset = edgeKeyTypes.keySet();
        }
        for (String key : keyset) {
            writer.writeStartElement(GraphMLTokens.KEY);
            writer.writeAttribute(GraphMLTokens.ID, key);
            writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.EDGE);
            writer.writeAttribute(GraphMLTokens.ATTR_NAME, key);
            writer.writeAttribute(GraphMLTokens.ATTR_TYPE, edgeKeyTypes.get(key));
            writer.writeEndElement();
        }
        writer.writeStartElement(GraphMLTokens.GRAPH);
        writer.writeAttribute(GraphMLTokens.ID, GraphMLTokens.G);
        writer.writeAttribute(GraphMLTokens.EDGEDEFAULT, GraphMLTokens.DIRECTED);
        Iterable<Vertex> vertices;
        if (normalize) {
            vertices = new ArrayList<Vertex>();
            for (Vertex v : graph.getVertices()) {
                ((Collection<Vertex>) vertices).add(v);
            }
            Collections.sort((List<Vertex>) vertices, new LexicographicalElementComparator());
        } else {
            vertices = graph.getVertices();
        }
        for (Vertex vertex : vertices) {
            writer.writeStartElement(GraphMLTokens.NODE);
            writer.writeAttribute(GraphMLTokens.ID, vertex.getId().toString());
            Collection<String> keys;
            if (normalize) {
                keys = new ArrayList<String>();
                keys.addAll(vertex.getPropertyKeys());
                Collections.sort((List<String>) keys);
            } else {
                keys = vertex.getPropertyKeys();
            }
            for (String key : keys) {
                writer.writeStartElement(GraphMLTokens.DATA);
                writer.writeAttribute(GraphMLTokens.KEY, key);
                Object value = vertex.getProperty(key);
                if (null != value) {
                    writer.writeCharacters(value.toString());
                }
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }
        if (normalize) {
            List<Edge> edges = new ArrayList<Edge>();
            for (Vertex vertex : graph.getVertices()) {
                for (Edge edge : vertex.getEdges(Direction.OUT)) {
                    edges.add(edge);
                }
            }
            Collections.sort(edges, new LexicographicalElementComparator());
            for (Edge edge : edges) {
                writer.writeStartElement(GraphMLTokens.EDGE);
                writer.writeAttribute(GraphMLTokens.ID, edge.getId().toString());
                writer.writeAttribute(GraphMLTokens.SOURCE, edge.getVertex(Direction.OUT).getId().toString());
                writer.writeAttribute(GraphMLTokens.TARGET, edge.getVertex(Direction.IN).getId().toString());
                if (this.edgeLabelKey == null) {
                    // this will not comply with the graphml schema but is here so that the label is not
                    // mixed up with properties.
                    writer.writeAttribute(GraphMLTokens.LABEL, edge.getLabel());
                } else {
                    writer.writeStartElement(GraphMLTokens.DATA);
                    writer.writeAttribute(GraphMLTokens.KEY, this.edgeLabelKey);
                    writer.writeCharacters(edge.getLabel());
                    writer.writeEndElement();
                }
                final List<String> keys = new ArrayList<String>();
                keys.addAll(edge.getPropertyKeys());
                Collections.sort(keys);
                for (String key : keys) {
                    writer.writeStartElement(GraphMLTokens.DATA);
                    writer.writeAttribute(GraphMLTokens.KEY, key);
                    Object value = edge.getProperty(key);
                    if (null != value) {
                        writer.writeCharacters(value.toString());
                    }
                    writer.writeEndElement();
                }
                writer.writeEndElement();
            }
        } else {
            for (Vertex vertex : graph.getVertices()) {
                for (Edge edge : vertex.getEdges(Direction.OUT)) {
                    writer.writeStartElement(GraphMLTokens.EDGE);
                    writer.writeAttribute(GraphMLTokens.ID, edge.getId().toString());
                    writer.writeAttribute(GraphMLTokens.SOURCE, edge.getVertex(Direction.OUT).getId().toString());
                    writer.writeAttribute(GraphMLTokens.TARGET, edge.getVertex(Direction.IN).getId().toString());
                    writer.writeAttribute(GraphMLTokens.LABEL, edge.getLabel());
                    for (String key : edge.getPropertyKeys()) {
                        writer.writeStartElement(GraphMLTokens.DATA);
                        writer.writeAttribute(GraphMLTokens.KEY, key);
                        Object value = edge.getProperty(key);
                        if (null != value) {
                            writer.writeCharacters(value.toString());
                        }
                        writer.writeEndElement();
                    }
                    writer.writeEndElement();
                }
            }
        }
        // graph
        writer.writeEndElement();
        // graphml
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
        writer.close();
    } catch (XMLStreamException xse) {
        throw new IOException(xse);
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Collection(java.util.Collection) LexicographicalElementComparator(com.tinkerpop.blueprints.util.io.LexicographicalElementComparator) Edge(com.tinkerpop.blueprints.Edge)

Example 2 with LexicographicalElementComparator

use of com.tinkerpop.blueprints.util.io.LexicographicalElementComparator in project blueprints by tinkerpop.

the class GraphSONWriter method edges.

private Iterable<Edge> edges(boolean normalize) {
    Iterable<Edge> edges;
    if (normalize) {
        edges = new ArrayList<Edge>();
        for (Edge v : graph.getEdges()) {
            ((Collection<Edge>) edges).add(v);
        }
        Collections.sort((List<Edge>) edges, new LexicographicalElementComparator());
    } else {
        edges = graph.getEdges();
    }
    return edges;
}
Also used : Collection(java.util.Collection) LexicographicalElementComparator(com.tinkerpop.blueprints.util.io.LexicographicalElementComparator) Edge(com.tinkerpop.blueprints.Edge)

Example 3 with LexicographicalElementComparator

use of com.tinkerpop.blueprints.util.io.LexicographicalElementComparator in project blueprints by tinkerpop.

the class GMLWriter method outputGraph.

/**
     * Write the data in a Graph to a GML OutputStream.
     *
     * @param gMLOutputStream the GML OutputStream to write the Graph data to
     * @throws IOException thrown if there is an error generating the GML data
     */
public void outputGraph(final OutputStream gMLOutputStream) throws IOException {
    // ISO 8859-1 as specified in the GML documentation
    final Writer writer = new BufferedWriter(new OutputStreamWriter(gMLOutputStream, Charset.forName("ISO-8859-1")));
    final List<Vertex> vertices = new ArrayList<Vertex>();
    final List<Edge> edges = new ArrayList<Edge>();
    populateLists(vertices, edges);
    if (normalize) {
        final LexicographicalElementComparator comparator = new LexicographicalElementComparator();
        Collections.sort(vertices, comparator);
        Collections.sort(edges, comparator);
    }
    writeGraph(writer, vertices, edges);
    // just flush, don't close...allow the underlying stream to stay open and let the calling function close it
    writer.flush();
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) ArrayList(java.util.ArrayList) LexicographicalElementComparator(com.tinkerpop.blueprints.util.io.LexicographicalElementComparator) OutputStreamWriter(java.io.OutputStreamWriter) Edge(com.tinkerpop.blueprints.Edge) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 4 with LexicographicalElementComparator

use of com.tinkerpop.blueprints.util.io.LexicographicalElementComparator in project blueprints by tinkerpop.

the class GraphSONWriter method vertices.

private Iterable<Vertex> vertices(boolean normalize) {
    Iterable<Vertex> vertices;
    if (normalize) {
        vertices = new ArrayList<Vertex>();
        for (Vertex v : graph.getVertices()) {
            ((Collection<Vertex>) vertices).add(v);
        }
        Collections.sort((List<Vertex>) vertices, new LexicographicalElementComparator());
    } else {
        vertices = graph.getVertices();
    }
    return vertices;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Collection(java.util.Collection) LexicographicalElementComparator(com.tinkerpop.blueprints.util.io.LexicographicalElementComparator)

Aggregations

LexicographicalElementComparator (com.tinkerpop.blueprints.util.io.LexicographicalElementComparator)4 Edge (com.tinkerpop.blueprints.Edge)3 Vertex (com.tinkerpop.blueprints.Vertex)3 Collection (java.util.Collection)3 ArrayList (java.util.ArrayList)2 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 HashMap (java.util.HashMap)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1