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    package graphlab.plugins.graphgenerator.generators;
005    
006    import graphlab.graph.graph.EdgeModel;
007    import graphlab.graph.graph.GraphModel;
008    import graphlab.graph.graph.VertexModel;
009    import graphlab.platform.lang.CommandAttitude;
010    import graphlab.platform.parameter.Parameter;
011    import graphlab.platform.parameter.Parametrizable;
012    import graphlab.plugins.graphgenerator.GraphGenerator;
013    import graphlab.plugins.graphgenerator.core.SimpleGeneratorInterface;
014    import graphlab.plugins.graphgenerator.core.extension.GraphGeneratorExtension;
015    
016    import java.awt.*;
017    
018    /**
019     * User: root
020     */
021    @CommandAttitude(name = "generate_random", abbreviation = "_g_rand")
022    public class RandomGenerator implements GraphGeneratorExtension, Parametrizable, SimpleGeneratorInterface {
023        GraphModel g;
024        @Parameter(name = "Vertices", description = "Num of Vertices")
025        public static Integer numOfVertices = 30;
026        @Parameter(name = "Edges", description = "Num of Edges")
027        public static Integer numOfEdges = 80;
028    
029        public void setWorkingGraph(GraphModel g) {
030            this.g = g;
031        }
032    
033        public String getName() {
034            return "Random Graph";
035        }
036    
037        public String getDescription() {
038            return "Generates a random graph with N Vertices and E Edges";
039        }
040    
041        VertexModel v[];
042    
043        public VertexModel[] getVertices() {
044            VertexModel[] ret = new VertexModel[numOfVertices];
045            for (int i = 0; i < numOfVertices; i++)
046                ret[i] = new VertexModel();
047            v = ret;
048            return ret;
049        }
050    
051        public EdgeModel[] getEdges() {
052            EdgeModel[] ret = new EdgeModel[numOfEdges];
053            for (int i = 0; i < numOfEdges; i++)
054                ret[i] = randomEdge();
055            return ret;
056        }
057    
058        private EdgeModel randomEdge() {
059            VertexModel v1 = randomVertex();
060            VertexModel v2 = randomVertex();
061            //System.out.println(v1.getID()+","+v2.getID());
062            if (v1 != v2)
063                return new EdgeModel(v1, v2);
064            else
065                return randomEdge();
066        }
067    
068        private VertexModel randomVertex() {
069            return v[(int) (Math.random() * numOfVertices)];
070        }
071    
072        public Point[] getVertexPositions() {
073            Point[] ret = new Point[numOfVertices];
074            int w = 100;
075            int h = 100;
076            for (int i = 0; i < numOfVertices; i++) {
077                int x = (int) (Math.random() * w);
078                int y = (int) (Math.random() * h);
079                ret[i] = new Point(x, y);
080            }
081            return ret;
082        }
083    
084        public String checkParameters() {
085            return null;
086        }
087    
088        public GraphModel generateGraph() {
089            return GraphGenerator.getGraph(false, this);
090        }
091    
092        /**
093         * generates a Random Graph with given parameters
094         */
095        public static GraphModel generateRandomGraph(int numOfVertices, int numOfEdges) {
096            RandomGenerator.numOfVertices = numOfVertices;
097            RandomGenerator.numOfEdges = numOfEdges;
098            return GraphGenerator.getGraph(false, new RandomGenerator());
099        }
100    
101    }