You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2011/06/30 23:32:30 UTC
svn commit: r1141729 - in
/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export:
./ DotExporter.java package-info.java
Author: simonetripodi
Date: Thu Jun 30 21:32:30 2011
New Revision: 1141729
URL: http://svn.apache.org/viewvc?rev=1141729&view=rev
Log:
first checkin of DOT language graph exporter
Added:
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java (with props)
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/package-info.java (with props)
Added: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java?rev=1141729&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java (added)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java Thu Jun 30 21:32:30 2011
@@ -0,0 +1,246 @@
+package org.apache.commons.graph.export;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static java.lang.String.format;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.util.Date;
+
+import org.apache.commons.graph.DirectedGraph;
+import org.apache.commons.graph.Edge;
+import org.apache.commons.graph.Graph;
+import org.apache.commons.graph.Labeled;
+import org.apache.commons.graph.Vertex;
+import org.apache.commons.graph.VertexPair;
+import org.apache.commons.graph.WeightedEdge;
+
+/**
+ * Utility methods to export Graphs in <a href="http://en.wikipedia.org/wiki/DOT_language">DOT language</a>.
+ */
+public final class DotExporter
+{
+
+ private static final String GRAPH = "graph";
+
+ private static final String DIGRAPH = "digraph";
+
+ private static final String CONNECTOR = "--";
+
+ private static final String DICONNECTOR = "->";
+
+ /**
+ * Export the input Graph to a {@link File}.
+ *
+ * @param <V> the Graph vertices type
+ * @param <E> the Graph edges type
+ * @param graph the Graph has to be exported
+ * @param outputFile the file where exporting the Graph to
+ * @throws IOException if any error occurs while serializing the input Graph
+ */
+ public static <V extends Vertex, E extends Edge> void dotExport( Graph<V, E> graph, File outputFile )
+ throws IOException
+ {
+ dotExport( graph, null, outputFile );
+ }
+
+ /**
+ * Export the input Graph to a {@link File}.
+ *
+ * @param <V> the Graph vertices type
+ * @param <E> the Graph edges type
+ * @param graph the Graph has to be exported
+ * @param name the Graph name (can be null)
+ * @param outputFile the file where exporting the Graph to
+ * @throws IOException if any error occurs while serializing the input Graph
+ */
+ public static <V extends Vertex, E extends Edge> void dotExport( Graph<V, E> graph, String name,
+ File outputFile )
+ throws IOException
+ {
+ if ( outputFile == null )
+ {
+ throw new IllegalArgumentException( "DOT exporter requires a not null file" );
+ }
+ dotExport( graph, new FileOutputStream( outputFile ) );
+ }
+
+ /**
+ * Export the input Graph to an {@link OutputStream}.
+ *
+ * @param <V> the Graph vertices type
+ * @param <E> the Graph edges type
+ * @param graph the Graph has to be exported
+ * @param outputStream the output stream where exporting the Graph to
+ * @throws IOException if any error occurs while serializing the input Graph
+ */
+ public static <V extends Vertex, E extends Edge> void dotExport( Graph<V, E> graph, OutputStream outputStream )
+ throws IOException
+ {
+ dotExport( graph, null, outputStream );
+ }
+
+ /**
+ * Export the input Graph to an {@link OutputStream}.
+ *
+ * @param <V> the Graph vertices type
+ * @param <E> the Graph edges type
+ * @param graph the Graph has to be exported
+ * @param name the Graph name (can be null)
+ * @param outputStream the output stream where exporting the Graph to
+ * @throws IOException if any error occurs while serializing the input Graph
+ */
+ public static <V extends Vertex, E extends Edge> void dotExport( Graph<V, E> graph, String name,
+ OutputStream outputStream )
+ throws IOException
+ {
+ if ( outputStream == null )
+ {
+ throw new IllegalArgumentException( "DOT exporter requires a not null output stream" );
+ }
+ dotExport( graph, new OutputStreamWriter( outputStream ) );
+ }
+
+ /**
+ * Export the input Graph to an {@link Writer}.
+ *
+ * @param <V> the Graph vertices type
+ * @param <E> the Graph edges type
+ * @param graph the Graph has to be exported
+ * @param writer the writer where exporting the Graph to
+ * @throws IOException if any error occurs while serializing the input Graph
+ */
+ public static <V extends Vertex, E extends Edge> void dotExport( Graph<V, E> graph, Writer writer )
+ throws IOException
+ {
+ dotExport( graph, null, writer );
+ }
+
+ /**
+ * Export the input Graph to an {@link Writer}.
+ *
+ * @param <V> the Graph vertices type
+ * @param <E> the Graph edges type
+ * @param graph the Graph has to be exported
+ * @param name the Graph name (can be null)
+ * @param writer the writer where exporting the Graph to
+ * @throws IOException if any error occurs while serializing the input Graph
+ */
+ public static <V extends Vertex, E extends Edge> void dotExport( Graph<V, E> graph, String name, Writer writer )
+ throws IOException
+ {
+ if ( graph == null )
+ {
+ throw new IllegalArgumentException( "Impossible to export a null Graph to DOT notation" );
+ }
+ if ( writer == null )
+ {
+ throw new IllegalArgumentException( "DOT exporter requires a not null writer" );
+ }
+
+ String graphDeclaration;
+ String connector;
+
+ if ( graph instanceof DirectedGraph )
+ {
+ graphDeclaration = DIGRAPH;
+ connector = DICONNECTOR;
+ }
+ else
+ {
+ graphDeclaration = GRAPH;
+ connector = CONNECTOR;
+ }
+
+ PrintWriter printWriter = new PrintWriter( writer );
+ try
+ {
+ printWriter.format( "# Graph generated by Apache Commons Graph on %s%n", new Date() );
+ printWriter.format( "%s %s {%n", graphDeclaration, name != null ? name : 'G' );
+
+ for ( V vertex : graph.getVertices() )
+ {
+ printWriter.format( " %s [label=\"%s\"];%n",
+ vertex.hashCode(),
+ ( vertex instanceof Labeled) ? ( (Labeled) vertex).getLabel() : vertex.toString() );
+ }
+
+ for ( E edge : graph.getEdges() )
+ {
+ String label = null;
+
+ if ( edge instanceof Labeled )
+ {
+ if ( edge instanceof WeightedEdge )
+ {
+ label =
+ format( "'%s' (w=%s)", ( (Labeled) edge ).getLabel(), ( (WeightedEdge) edge ).getWeight() );
+ }
+ else
+ {
+ label = ( (Labeled) edge ).getLabel();
+ }
+ }
+ else if ( edge instanceof WeightedEdge )
+ {
+ label = String.valueOf( ( (WeightedEdge) edge ).getWeight() );
+ }
+
+ VertexPair<V> vertexPair = graph.getVertices( edge );
+
+ if ( label != null )
+ {
+ printWriter.format( " %s %s %s [label=\"%s\"];%n",
+ vertexPair.getHead().hashCode(),
+ connector,
+ vertexPair.getTail().hashCode(),
+ label );
+ }
+ else
+ {
+ printWriter.format( " %s %s %s",
+ vertexPair.getHead().hashCode(),
+ connector,
+ vertexPair.getTail().hashCode() );
+ }
+ }
+
+ printWriter.write( '}' );
+ }
+ finally
+ {
+ try
+ {
+ writer.close();
+ }
+ catch ( IOException e )
+ {
+ // swallow it
+ }
+ }
+ }
+
+}
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/package-info.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/package-info.java?rev=1141729&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/package-info.java (added)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/package-info.java Thu Jun 30 21:32:30 2011
@@ -0,0 +1,23 @@
+/**
+ * Graph languages exporters implementations.
+ */
+package org.apache.commons.graph.export;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/package-info.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/package-info.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/package-info.java
------------------------------------------------------------------------------
svn:mime-type = text/plain