001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 005 package graphlab.extensions.io; 006 007 import graphlab.graph.graph.EdgeModel; 008 import graphlab.graph.graph.GraphModel; 009 import graphlab.graph.graph.GraphPoint; 010 import graphlab.graph.graph.VertexModel; 011 import graphlab.plugins.main.saveload.core.GraphIOException; 012 import graphlab.plugins.main.saveload.core.extension.GraphWriterExtension; 013 014 import java.io.*; 015 import java.util.Iterator; 016 017 public class SaveSimpleGraph implements GraphWriterExtension { 018 019 public String getName() { 020 return "Simple Graph"; 021 } 022 023 public String getExtension() { 024 return "simplegraph"; 025 } 026 027 public void write(File file, GraphModel graph) throws GraphIOException { 028 try { 029 PrintWriter o = new PrintWriter(new BufferedWriter(new FileWriter(file))); 030 o.println("graph:"); 031 if (graph.isDirected()) 032 o.println("directed"); 033 else 034 o.println("undirected"); 035 o.println("label " + graph.getLabel()); 036 037 //output vertices 038 o.println("begin vertices:"); 039 for (VertexModel v : graph) { 040 o.println("vertex " + v.getId() + ":"); 041 o.println("label " + v.getLabel()); 042 GraphPoint p = v.getLocation(); 043 o.println("location " + p.getX() + "," + p.getY()); 044 o.println("color " + v.getColor()); 045 } 046 047 //output edges 048 o.println("begin edges"); 049 for (Iterator<EdgeModel> ie = graph.edgeIterator(); ie.hasNext();) { 050 EdgeModel e = ie.next(); 051 o.println(e.source.getId() + " -> " + e.target.getId()); 052 o.println("label " + e.getLabel()); 053 o.println("color " + e.getColor()); 054 o.println("weight " + e.getWeight()); 055 } 056 o.close(); 057 } catch (IOException e) { 058 throw new GraphIOException(e.getMessage()); 059 } 060 } 061 062 public String getDescription() { 063 return "Simple Graph File Format"; 064 } 065 }