X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/ff6cb26262ba25fefdf1265628265a75d790ebd6..200986a368bbbbb5df459d43cbc7f5ef3d7678db:/contrib/psg/src/peersim/graph/GraphIO.java diff --git a/contrib/psg/src/peersim/graph/GraphIO.java b/contrib/psg/src/peersim/graph/GraphIO.java new file mode 100644 index 0000000000..9d928d380b --- /dev/null +++ b/contrib/psg/src/peersim/graph/GraphIO.java @@ -0,0 +1,295 @@ +/* + * Copyright (c) 2003-2005 The BISON Project + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2 as + * published by the Free Software Foundation. + * + * This program 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 program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +package peersim.graph; + +import java.util.*; +import java.io.*; + +/** +* Implements static methods to load and write graphs. +*/ +public class GraphIO { +private GraphIO() {} + + +// ================== public static methods ========================= +// ================================================================== + + +/** +* Prints graph in edge list format. Each line contains exactly two +* node IDs separated by whitespace. +*/ +public static void writeEdgeList( Graph g, PrintStream out ) { + + for(int i=0; i it=g.getNeighbours(i).iterator(); + while(it.hasNext()) + { + final int j = it.next(); + if(g.directed()) + out.println(i+" -> "+j+";"); + else if( i<=j ) + out.println(i+" -- "+j+";"); + } + } + + out.println("}"); +} + +// ------------------------------------------------------------------ + +/** +* Saves the given graph to +* the given stream in GML format. +*/ +public static void writeGML( Graph g, PrintStream out ) { + + out.println("graph [ directed "+(g.directed()?"1":"0")); + + for(int i=0; i +*
  • 1: Begins with cacheSize in binary format (int), followed by the +* numberOfNodes (int), and then a continuous series of exactly +* numberOfNodes records, where a record describes a node's +* neighbours and their timestamps. +* A record is a series of exactly cacheSize (int,long) pairs where +* the int is the node id, and the long is the timestamp. +* Node id-s start from 1. Node id 0 means no node and used if the parent +* node has less that cacheSize nodes.
  • +* +* @param file Filename to read +* @param direction If 0, the original directionality is preserved, if 1, +* than each edge is reversed, if 2 then directionality is dropped and the +* returned graph will be undirected. +*/ +public static Graph readNewscastGraph( String file, int direction ) +throws IOException { + + NeighbourListGraph gr = new NeighbourListGraph( direction != 2 ); + FileInputStream fis = new FileInputStream(file); + DataInputStream dis = new DataInputStream(fis); + + dis.readByte(); + dis.readByte(); + dis.readByte(); + + final int MODE = dis.readInt(); + if( MODE != 1 ) throw new IOException("Unknown mode "+MODE); + + final int CACHESIZE = dis.readInt(); + final int GRAPHSIZE = dis.readInt(); + +//System.out.println("header: "+MODE+" "+CACHESIZE+" "+GRAPHSIZE); + + for(int i=1; i<=GRAPHSIZE; ++i) + { + int iind = gr.addNode(i); + + for(int j=0; j