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: root
021     */
022    @CommandAttitude(name = "generate_kmn", abbreviation = "_g_kmn", description = "generates a 2partite complete graph")
023    public class KmnGenerator implements GraphGeneratorExtension, Parametrizable, SimpleGeneratorInterface {
024    
025        @Parameter(name = "M")
026        public static Integer m = 3;
027    
028        @Parameter(name = "N")
029        public static Integer n = 3;
030        GraphModel g;
031    
032        public void setWorkingGraph(GraphModel g) {
033            this.g = g;
034        }
035    
036    //    public NotifiableAttributeSet getParameters() {
037    //        PortableNotifiableAttributeSetImpl a=new PortableNotifiableAttributeSetImpl();
038    //        a.put("M",10);
039    //        a.put("N",4);
040    //        return a;
041    //    }
042    //
043    //    public void setParameters(NotifiableAttributeSet parameters) {
044    //        m = Integer.parseInt(""+parameters.getAttributes().get("M"));
045    //        n = Integer.parseInt(""+parameters.getAttributes().get("N"));
046    //    }
047    
048        public String getName() {
049            return "Km,n";
050        }
051    
052        public String getDescription() {
053            return "Generate Km,n";
054        }
055    
056        VertexModel[] v;
057    
058        public VertexModel[] getVertices() {
059            VertexModel[] ret = new VertexModel[n + m];
060            for (int i = 0; i < m + n; i++)
061                ret[i] = new VertexModel();
062            v = ret;
063            return ret;
064        }
065    
066        public EdgeModel[] getEdges() {
067            EdgeModel[] ret = new EdgeModel[m * n];
068            for (int i = 0; i < n; i++)
069                for (int j = 0; j < m; j++) {
070                    ret[i * m + j] = new EdgeModel(v[i], v[n + j]);
071                }
072            return ret;
073        }
074    
075        public Point[] getVertexPositions() {
076            int w = 100;
077            int h = 100;
078            Point ret[] = new Point[m + n];
079            Point np[] = PositionGenerators.line(5, h / 4, w, 0, n);
080            Point mp[] = PositionGenerators.line(5, 3 * h / 4, w, 0, m);
081            System.arraycopy(np, 0, ret, 0, n);
082            System.arraycopy(mp, 0, ret, n, m);
083            return ret;
084        }
085    
086        public String checkParameters() {
087    
088            return null;
089        }
090    
091        public GraphModel generateGraph() {
092            return GraphGenerator.getGraph(false, this);
093        }
094    
095        /**
096         * generates a Km,n Graph with given parameters
097         */
098        public static GraphModel generateKmn(int m, int n) {
099            KmnGenerator.m = m;
100            KmnGenerator.n = n;
101            return GraphGenerator.getGraph(false, new KmnGenerator());
102        }
103    
104    }