Author: cs Date: Sun Mar 18 19:12:20 2012 New Revision: 1302199 URL: http://svn.apache.org/viewvc?rev=1302199&view=rev Log: proof of concept with tests using the new proposed API -- the code compiles but it does not work yet
Modified: commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/CommonsGraph.java commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/AbstractExporter.java commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DefaultNamedExportSelector.java commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DotExporter.java commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/ExportSelctor.java commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/EdgeLabelMapper.java commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/EdgeWeightMapper.java commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/ExportTestCase.java commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/VertexLabelMapper.java Modified: commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/CommonsGraph.java URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/CommonsGraph.java?rev=1302199&r1=1302198&r2=1302199&view=diff ============================================================================== --- commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/CommonsGraph.java (original) +++ commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/CommonsGraph.java Sun Mar 18 19:12:20 2012 @@ -35,7 +35,9 @@ import org.apache.commons.graph.coloring import org.apache.commons.graph.coloring.DefaultColorsBuilder; import org.apache.commons.graph.connectivity.ConnectivityBuilder; import org.apache.commons.graph.connectivity.DefaultConnectivityBuilder; +import org.apache.commons.graph.export.DefaultNamedExportSelector; import org.apache.commons.graph.export.DefaultToStreamBuilder; +import org.apache.commons.graph.export.ExportSelctor; import org.apache.commons.graph.export.ToStreamBuilder; import org.apache.commons.graph.flow.DefaultFlowWeightedEdgesBuilder; import org.apache.commons.graph.flow.FlowWeightedEdgesBuilder; @@ -56,10 +58,10 @@ import org.apache.commons.graph.visit.Vi public final class CommonsGraph<V, E, G extends Graph<V, E>> { - public static <V, E, G extends Graph<V, E>> ToStreamBuilder<V, E> export( G graph ) + public static <V, E, G extends Graph<V, E>> ExportSelctor<V, E> export( G graph ) { graph = checkNotNull( graph, "Null graph can not be exported" ); - return new DefaultToStreamBuilder<V, E>( graph ); + return new DefaultNamedExportSelector<V, E>( graph ); } public static <V, E, G extends UndirectedGraph<V, E>> ColorsBuilder<V, E> coloring( G graph ) Modified: commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/AbstractExporter.java URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/AbstractExporter.java?rev=1302199&r1=1302198&r2=1302199&view=diff ============================================================================== --- commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/AbstractExporter.java (original) +++ commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/AbstractExporter.java Sun Mar 18 19:12:20 2012 @@ -21,7 +21,9 @@ package org.apache.commons.graph.export; import static java.lang.String.format; +import java.io.File; import java.io.IOException; +import java.io.OutputStream; import java.io.Writer; import java.util.Date; import java.util.HashMap; @@ -59,7 +61,41 @@ abstract class AbstractExporter<V, E> this.name = name != null ? name : G; } - protected final Graph<V, E> getGraph() + public AbstractExporter(Graph<V, E> graph) { + this( graph, null ); + } + + public AbstractExporter(Graph<V, E> graph, String name) { + // TODO Auto-generated constructor stub + this.graph = graph; + this.writer = null; + this.vertexProperties = new HashMap<String, Mapper<V, ?>>(); + this.edgeProperties = new HashMap<String, Mapper<E, ?>>(); + this.name = name != null ? name : G; + } + + public abstract <N extends Number> AbstractExporter<V, E> withEdgeWeights(Mapper<E, N> edgeWeights); + + public abstract <N extends Number> AbstractExporter<V, E> withVertexWeights( Mapper<V, N> vertexWeights ); + + public abstract AbstractExporter<V, E> withEdgeLabels( Mapper<E, String> edgeLabels ); + + public abstract AbstractExporter<V, E> withVertexLabels( Mapper<V, String> vertexLabels ); + + public void to( File outputFile ) { + // TODO + } + + public void to( OutputStream outputStream ) { + // TODO + } + + public void to( Writer writer ) { + // TODO + } + + + protected final Graph<V, E> getGraph() { return graph; } Modified: commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DefaultNamedExportSelector.java URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DefaultNamedExportSelector.java?rev=1302199&r1=1302198&r2=1302199&view=diff ============================================================================== --- commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DefaultNamedExportSelector.java (original) +++ commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DefaultNamedExportSelector.java Sun Mar 18 19:12:20 2012 @@ -19,60 +19,43 @@ package org.apache.commons.graph.export; * under the License. */ -import static org.apache.commons.graph.utils.Assertions.*; - -import java.io.Writer; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.Map; - import org.apache.commons.graph.Graph; -import org.apache.commons.graph.Mapper; -final class DefaultNamedExportSelector<V, E> - implements NamedExportSelctor<V, E> +public final class DefaultNamedExportSelector<V, E> + implements ExportSelctor<V, E> { private final Graph<V, E> graph; + + public DefaultNamedExportSelector(Graph<V, E> graph) { + this.graph = graph; + } - private final Writer writer; - - private final Map<String, Mapper<V, ?>> vertexProperties = new HashMap<String, Mapper<V,?>>(); - - private final Map<String, Mapper<E, ?>> edgeProperties = new HashMap<String, Mapper<E,?>>(); - - private final String name; - - public DefaultNamedExportSelector( Graph<V, E> graph, Writer writer ) - { - this( graph, writer, null ); - } - - public DefaultNamedExportSelector( Graph<V, E> graph, Writer writer, String name ) - { - this.graph = graph; - this.writer = writer; - this.name = name; - } - - public void usingDotNotation() + public DotExporter<V, E> usingDotNotation() throws GraphExportException { - new DotExporter<V, E>( graph, writer, vertexProperties, edgeProperties, name ).export(); + return new DotExporter<V, E>( graph ); } + + public DotExporter<V, E> usingDotNotation(String name) + throws GraphExportException + { + return new DotExporter<V, E>( graph, name ); + } - public void usingGraphMLFormat() + public GraphMLExporter<V, E> usingGraphMLFormat() throws GraphExportException { - new GraphMLExporter<V, E>( graph, writer, vertexProperties, edgeProperties, name ).export(); - } - - public ExportSelctor<V, E> withName( String name ) - { - return new DefaultNamedExportSelector<V, E>( graph, writer, name ); + return new GraphMLExporter<V, E>( graph ); } + + public GraphMLExporter<V, E> usingGraphMLFormat(String name) + throws GraphExportException + { + return new GraphMLExporter<V, E>( graph, name ); + } + /* public EdgeMapperSelector<V, E> withEdgeProperty( String name ) { final String checkedName = checkNotNull( name, "Null Edge property not admitted" ); @@ -103,6 +86,7 @@ final class DefaultNamedExportSelector<V } }; - } + }//*/ + } Modified: commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java?rev=1302199&r1=1302198&r2=1302199&view=diff ============================================================================== --- commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java (original) +++ commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java Sun Mar 18 19:12:20 2012 @@ -62,7 +62,7 @@ public final class DefaultToStreamBuilde public ExportSelctor<V, E> to( Writer writer ) { writer = checkNotNull( writer, "Impossibe to export the graph in a null stream" ); - return new DefaultNamedExportSelector<V, E>( graph, writer ); + return new DefaultNamedExportSelector<V, E>( graph ); } } Modified: commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DotExporter.java URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DotExporter.java?rev=1302199&r1=1302198&r2=1302199&view=diff ============================================================================== --- commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DotExporter.java (original) +++ commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/DotExporter.java Sun Mar 18 19:12:20 2012 @@ -21,7 +21,6 @@ package org.apache.commons.graph.export; import java.io.PrintWriter; import java.io.Writer; -import java.util.Collection; import java.util.Map; import java.util.Map.Entry; @@ -55,7 +54,15 @@ final class DotExporter<V, E> super( graph, writer, vertexProperties, edgeProperties, name ); } - private PrintWriter printWriter; + public DotExporter(Graph<V, E> graph) { + super( graph ); + } + + public DotExporter(Graph<V, E> graph, String name) { + super( graph, name ); + } + + private PrintWriter printWriter; private String connector; @@ -163,4 +170,31 @@ final class DotExporter<V, E> } } + @Override + public <N extends Number> DotExporter<V, E> + withEdgeWeights(Mapper<E, N> edgeWeights) { + // TODO Auto-generated method stub + return null; + } + + @Override + public <N extends Number> DotExporter<V, E> withVertexWeights( + Mapper<V, N> vertexWeights) { + // TODO Auto-generated method stub + return null; + } + + @Override + public DotExporter<V, E> withEdgeLabels(Mapper<E, String> edgeLabels) { + // TODO Auto-generated method stub + return null; + } + + @Override + public DotExporter<V, E> withVertexLabels( + Mapper<V, String> vertexLabels) { + // TODO Auto-generated method stub + return null; + } + } Modified: commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/ExportSelctor.java URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/ExportSelctor.java?rev=1302199&r1=1302198&r2=1302199&view=diff ============================================================================== --- commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/ExportSelctor.java (original) +++ commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/ExportSelctor.java Sun Mar 18 19:12:20 2012 @@ -1,7 +1,5 @@ package org.apache.commons.graph.export; -import org.apache.commons.graph.Mapper; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -24,20 +22,26 @@ import org.apache.commons.graph.Mapper; public interface ExportSelctor<V, E> { - VertexMapperSelector<V, E> withVertexProperty( String name ); + //VertexMapperSelector<V, E> withVertexProperty( String name ); - EdgeMapperSelector<V, E> withEdgeProperty( String name ); + //EdgeMapperSelector<V, E> withEdgeProperty( String name ); /** * Export Graphs in <a href="http://en.wikipedia.org/wiki/DOT_language">DOT language</a>. */ - void usingDotNotation() + DotExporter<V, E> usingDotNotation() + throws GraphExportException; + + DotExporter<V, E> usingDotNotation( String name ) throws GraphExportException; /** * Export Graphs in <a href="http://graphml.graphdrawing.org/">GraphML file format</a>. */ - void usingGraphMLFormat() + GraphMLExporter<V, E> usingGraphMLFormat() throws GraphExportException; + + GraphMLExporter<V, E> usingGraphMLFormat( String name ) + throws GraphExportException; } Modified: commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java?rev=1302199&r1=1302198&r2=1302199&view=diff ============================================================================== --- commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java (original) +++ commons/sandbox/graph/branches/exporters-with-mappers/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java Sun Mar 18 19:12:20 2012 @@ -86,7 +86,15 @@ final class GraphMLExporter<V, E> super( graph, writer, vertexProperties, edgeProperties, name ); } - @Override + public GraphMLExporter(Graph<V, E> graph) { + super( graph ); + } + + public GraphMLExporter(Graph<V, E> graph, String name) { + super( graph, name ); + } + + @Override protected void startSerialization() throws Exception { @@ -183,4 +191,31 @@ final class GraphMLExporter<V, E> return STRING; } + @Override + public <N extends Number> GraphMLExporter<V, E> + withEdgeWeights(Mapper<E, N> edgeWeights) { + // TODO Auto-generated method stub + return null; + } + + @Override + public <N extends Number> GraphMLExporter<V, E> withVertexWeights( + Mapper<V, N> vertexWeights) { + // TODO Auto-generated method stub + return null; + } + + @Override + public GraphMLExporter<V, E> withEdgeLabels(Mapper<E, String> edgeLabels) { + // TODO Auto-generated method stub + return null; + } + + @Override + public GraphMLExporter<V, E> withVertexLabels( + Mapper<V, String> vertexLabels) { + // TODO Auto-generated method stub + return null; + } + } Modified: commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/EdgeLabelMapper.java URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/EdgeLabelMapper.java?rev=1302199&r1=1302198&r2=1302199&view=diff ============================================================================== --- commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/EdgeLabelMapper.java (original) +++ commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/EdgeLabelMapper.java Sun Mar 18 19:12:20 2012 @@ -26,7 +26,6 @@ public final class EdgeLabelMapper implements Mapper<BaseLabeledWeightedEdge<Double>, String> { - @Override public String map( BaseLabeledWeightedEdge<Double> input ) { return input.getLabel(); Modified: commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/EdgeWeightMapper.java URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/EdgeWeightMapper.java?rev=1302199&r1=1302198&r2=1302199&view=diff ============================================================================== --- commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/EdgeWeightMapper.java (original) +++ commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/EdgeWeightMapper.java Sun Mar 18 19:12:20 2012 @@ -26,7 +26,6 @@ public final class EdgeWeightMapper implements Mapper<BaseLabeledWeightedEdge<Double>, Double> { - @Override public Double map( BaseLabeledWeightedEdge<Double> input ) { return input.getWeight(); Modified: commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/ExportTestCase.java URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/ExportTestCase.java?rev=1302199&r1=1302198&r2=1302199&view=diff ============================================================================== --- commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/ExportTestCase.java (original) +++ commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/ExportTestCase.java Sun Mar 18 19:12:20 2012 @@ -22,7 +22,6 @@ package org.apache.commons.graph.export; import static org.apache.commons.graph.CommonsGraph.export; import static org.apache.commons.graph.CommonsGraph.newUndirectedMutableGraph; -import org.apache.commons.graph.Mapper; import org.apache.commons.graph.builder.AbstractGraphConnection; import org.apache.commons.graph.model.BaseLabeledVertex; import org.apache.commons.graph.model.BaseLabeledWeightedEdge; @@ -69,11 +68,13 @@ public class ExportTestCase { public void shouldPrintDotFormat() throws Exception { - export( actual ).to( System.out ) - .withVertexProperty( "label" ).expandedBy( new VertexLabelMapper() ) - .withEdgeProperty( "label" ).expandedBy( new EdgeLabelMapper() ) - .withEdgeProperty( "weight" ).expandedBy( new EdgeWeightMapper() ) - .usingDotNotation(); + + export( actual ).usingDotNotation() + .withVertexLabels( new VertexLabelMapper() ) + .withEdgeWeights( new EdgeWeightMapper() ) + .withEdgeLabels( new EdgeLabelMapper() ) + .to( System.out ); + } @Test @@ -81,18 +82,20 @@ public class ExportTestCase { public void shouldPrintGraphML() throws Exception { - export( actual ).to( System.out ) - .withVertexProperty( "label" ).expandedBy( new VertexLabelMapper() ) - .withEdgeProperty( "label" ).expandedBy( new EdgeLabelMapper() ) - .withEdgeProperty( "weight" ).expandedBy( new EdgeWeightMapper() ) - .usingGraphMLFormat(); + + export( actual ).usingGraphMLFormat() + .withVertexLabels( new VertexLabelMapper() ) + .withEdgeWeights( new EdgeWeightMapper() ) + .withEdgeLabels( new EdgeLabelMapper() ) + .to( System.out ); + } @Test public void shouldPrintGraphMLFormat() throws Exception { - export( actual ).to( System.out ).usingGraphMLFormat(); + export( actual ).usingGraphMLFormat().to( System.out ); } } Modified: commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/VertexLabelMapper.java URL: http://svn.apache.org/viewvc/commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/VertexLabelMapper.java?rev=1302199&r1=1302198&r2=1302199&view=diff ============================================================================== --- commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/VertexLabelMapper.java (original) +++ commons/sandbox/graph/branches/exporters-with-mappers/src/test/java/org/apache/commons/graph/export/VertexLabelMapper.java Sun Mar 18 19:12:20 2012 @@ -26,7 +26,6 @@ public final class VertexLabelMapper implements Mapper<BaseLabeledVertex, String> { - @Override public String map( BaseLabeledVertex input ) { return input.getLabel();