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.PositionGenerators; 014 import graphlab.plugins.graphgenerator.core.SimpleGeneratorInterface; 015 import graphlab.plugins.graphgenerator.core.extension.GraphGeneratorExtension; 016 017 import java.awt.*; 018 019 /** 020 * User: Houshmand 021 */ 022 @CommandAttitude(name = "generate_pmn", abbreviation = "_g_pmn", description = "generates Pm*Pn") 023 public class PmnGenerator implements GraphGeneratorExtension, Parametrizable, SimpleGeneratorInterface { 024 @Parameter(name = "M") 025 public static Integer m = 10; 026 @Parameter(name = "N") 027 public static Integer n = 4; 028 GraphModel g; 029 030 public void setWorkingGraph(GraphModel g) { 031 this.g = g; 032 } 033 034 public String getName() { 035 return "Pm * Pn"; 036 } 037 038 public String getDescription() { 039 return "Generate Pm*Pn"; 040 } 041 042 VertexModel[] v; 043 044 public VertexModel[] getVertices() { 045 VertexModel[] ret = new VertexModel[n * m]; 046 for (int i = 0; i < m * n; i++) 047 ret[i] = new VertexModel(); 048 v = ret; 049 return ret; 050 } 051 052 public EdgeModel[] getEdges() { 053 EdgeModel[] ret = new EdgeModel[(n * (m - 1) + m * (n - 1))]; 054 int counter = 0; 055 for (int i = 0; i < n; i++) 056 for (int j = 0; j < m; j++) { 057 if (j < m - 1) { 058 ret[counter] = new EdgeModel(v[i * m + j], v[i * m + j + 1]); 059 counter++; 060 } 061 if (i < n - 1) { 062 ret[counter] = new EdgeModel(v[i * m + j], v[(i + 1) * (m) + j]); 063 counter++; 064 } 065 } 066 return ret; 067 } 068 069 public Point[] getVertexPositions() { 070 int w = 200; 071 int h = 200; 072 Point ret[] = new Point[m * n]; 073 int yoffset = h - 30 * n; 074 for (int i = 0; i < n; i++) { 075 yoffset += 50; 076 Point p[] = PositionGenerators.line(5, h * (i + 1) / (n + 1), w, 0, m); 077 System.arraycopy(p, 0, ret, i * m, m); 078 079 } 080 return ret; 081 } 082 083 public String checkParameters() { 084 return null; 085 } 086 087 public GraphModel generateGraph() { 088 return GraphGenerator.getGraph(false, this); 089 } 090 091 092 /** 093 * generates a Pm*Pn Graph with given parameters 094 */ 095 public static GraphModel generatePmn(int m, int n) { 096 PmnGenerator.m = m; 097 PmnGenerator.n = n; 098 return GraphGenerator.getGraph(false, new PmnGenerator()); 099 } 100 }