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.platform.StaticUtils;
012    import graphlab.plugins.main.saveload.SaveLoadPluginMethods;
013    import graphlab.plugins.main.saveload.core.GraphIOException;
014    import graphlab.plugins.main.saveload.core.extension.GraphReaderExtension;
015    
016    import java.io.File;
017    import java.io.FileReader;
018    import java.io.IOException;
019    import static java.lang.Integer.parseInt;
020    import java.util.ArrayList;
021    import java.util.Scanner;
022    
023    /**
024     * sample.simplegraph :<br>
025     * graph:<br>
026     * directed<br>
027     * label g0<br>
028     * begin vertices:<br>
029     * vertex 0:<br>
030     * label v1<br>
031     * location 120,23<br>
032     * color 3<br>
033     * vertex 1:<br>
034     * label second vertex<br>
035     * color 1<br>
036     * vertex 2:<br>
037     * vertex 3:<br>
038     * label 4<br>
039     * begin edges:<br>
040     * 0 -> 1<br>
041     * label my edge<br>
042     * color 12<br>
043     * weight -2<br>
044     * 3 -> 0<br>
045     * label edge 2<br>
046     * 2 -> 1<br>
047     *
048     * @author Azin Azadi
049     */
050    public class LoadSimpleGraph implements GraphReaderExtension {
051    
052        public boolean accepts(File file) {
053            return SaveLoadPluginMethods.getExtension(file).equals(getExtension());
054        }
055    
056        public String getName() {
057            return "Simple Graph";
058        }
059    
060        public String getExtension() {
061            return "simplegraph";
062        }
063    
064        public GraphModel read(File file) throws GraphIOException {
065            try {
066                FileReader in = new FileReader(file);
067    //            BufferedReader sc = new BufferedReader(in);
068                Scanner sc = new Scanner(file);
069                String _, s = "";
070                String l = sc.nextLine();
071                if (!l.equals("graph:"))
072                    throw new GraphIOException("Incorrect Format(in the first line)");
073                l = sc.nextLine();
074                GraphModel g;
075                if (l.equals("directed"))
076                    g = new GraphModel(true);
077                else
078                    g = new GraphModel(false);
079                sc.next();
080                g.setLabel(sc.nextLine());
081    
082                //Read Vertices
083                sc.nextLine();
084    
085                l = sc.next();
086                ArrayList<VertexModel> V = new ArrayList<VertexModel>();
087                VertexModel curv = new VertexModel();
088                while (!l.equals("begin")) {    //begin edges
089                    if (l.equals("vertex")) {
090                        String s1 = sc.next();
091                        int i = parseInt(s1.substring(0, s1.length() - 1));
092                        final GraphPoint zeropoint = new GraphPoint(0, 0);
093                        curv.setLocation(new GraphPoint(400 * Math.random(), 400 * Math.random()));
094                        V.add(curv = new VertexModel());
095                    } else if (l.equals("label")) {
096                        curv.setLabel(sc.nextLine());
097                    } else if (l.equals("location")) {
098                        curv.setLocation((GraphPoint) StaticUtils.fromString(GraphPoint.class.getName(), sc.nextLine()));
099                    } else if (l.equals("color")) {
100                        curv.setColor(parseInt(sc.next()));
101                    }
102                    l = sc.next();
103                }
104    
105                g.insertVertices(V);
106                //Read Edges
107                sc.nextLine();
108                VertexModel v1 = V.get(parseInt(sc.next()));
109                sc.next();
110                VertexModel v2 = V.get(parseInt(sc.next()));
111                ArrayList<EdgeModel> E = new ArrayList<EdgeModel>();
112                EdgeModel cure = new EdgeModel(v1, v2);
113                E.add(cure);
114                while (sc.hasNext()) {
115                    l = sc.next();
116                    if (l.equals("label")) {
117                        cure.setLabel(sc.nextLine());
118                    } else if (l.equals("color")) {
119                        cure.setColor(parseInt(sc.next()));
120                    } else if (l.equals("weight")) {
121                        cure.setWeight(parseInt(sc.next()));
122                    } else {
123                        sc.next();
124                        cure = new EdgeModel(V.get(parseInt(l)), V.get(parseInt(sc.next())));
125                        E.add(cure);
126                    }
127                }
128    
129                g.insertEdges(E);
130                return g;
131            } catch (IOException e) {
132                throw new GraphIOException(e.getMessage());
133            }
134        }
135    
136        public String getDescription() {
137            return "Simple Graph File Format";
138        }
139    }