org.jgrapht.experimental.GraphReader Maven / Gradle / Ivy
Go to download
JGraphT is a free Java graph library
that provides mathematical graph-theory objects and algorithms
The newest version!
/* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2007, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -------------------
* LinearGraphGenerator.java
* -------------------
* (C) Copyright 2003-2007, by John V. Sichi and Contributors.
*
* Original Author: John V. Sichi
* Contributor(s): -
*
* $Id: LinearGraphGenerator.java 504 2006-07-03 02:37:26Z perfecthash $
*
* Changes
* -------
* 16-Sep-2003 : Initial revision (JVS);
*
*/
package org.jgrapht.experimental;
import java.io.*;
import java.util.*;
import org.jgrapht.*;
import org.jgrapht.generate.*;
import org.jgrapht.graph.*;
/**
* Generates a linear graph of any size. For a directed graph, the edges are
* oriented from START_VERTEX to END_VERTEX.
*
* @author John V. Sichi
* @since Sep 16, 2003
*/
public class GraphReader
implements GraphGenerator
{
//~ Instance fields --------------------------------------------------------
// ~ Static fields/initializers --------------------------------------------
// ~ Instance fields -------------------------------------------------------
private final BufferedReader _in;
// ~ Constructors ----------------------------------------------------------
//~ Constructors -----------------------------------------------------------
/**
* Construct a new LinearGraphGenerator.
*
* @param size number of vertices to be generated
*
* @throws IllegalArgumentException if the specified size is negative.
*/
public GraphReader(String file)
throws IOException
{
_in = new BufferedReader(new FileReader(file));
}
//~ Methods ----------------------------------------------------------------
// ~ Methods ---------------------------------------------------------------
private List split(String src)
{
final List l = new ArrayList();
final StringTokenizer tok = new StringTokenizer(src);
while (tok.hasMoreTokens()) {
l.add(tok.nextToken());
}
return l;
}
private List skipComments()
{
try {
if (_in.ready()) {
List cols = split(_in.readLine());
while (
cols.isEmpty()
|| cols.get(0).equals("c")
|| cols.get(0).startsWith("%"))
{
cols = split(_in.readLine());
}
return cols;
}
} catch (IOException e) {
}
return null;
}
private int readNodeCount()
{
List cols = skipComments();
if (cols.get(0).equals("p")) {
return Integer.parseInt(cols.get(1));
}
return -1;
}
/**
* {@inheritDoc}
*/
public void generateGraph(
Graph target,
VertexFactory vertexFactory,
Map resultMap)
{
final int size = readNodeCount();
if (resultMap == null) {
resultMap = new HashMap();
}
for (int i = 0; i < size; i++) {
V newVertex = vertexFactory.createVertex();
target.addVertex(newVertex);
resultMap.put(Integer.toString(i + 1), newVertex);
}
List cols = skipComments();
while (cols != null) {
if (cols.get(0).equals("e")) {
target.addEdge(
resultMap.get(cols.get(1)),
resultMap.get(cols.get(2)));
}
cols = skipComments();
}
}
public static void main(String [] args)
throws Exception
{
GraphReader reader =
new GraphReader(
args[0]);
Graph g =
new SimpleGraph(
DefaultEdge.class);
VertexFactory vf = new IntVertexFactory();
reader.generateGraph(g, vf, null);
System.out.println(g);
}
//~ Inner Classes ----------------------------------------------------------
private static final class IntVertexFactory
implements VertexFactory
{
int last = 0;
public Integer createVertex()
{
return last++;
}
}
}
// End GraphReader.java