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.plugins.commandline.commands;
006    
007    import graphlab.graph.atributeset.GraphAttrSet;
008    import graphlab.graph.graph.GraphModel;
009    import graphlab.graph.graph.GraphPoint;
010    import graphlab.graph.graph.VertexModel;
011    import graphlab.platform.core.BlackBoard;
012    import graphlab.platform.lang.CommandAttitude;
013    import graphlab.platform.parameter.Parameter;
014    import graphlab.plugins.main.GraphData;
015    import graphlab.plugins.main.core.actions.vertex.AddVertex;
016    
017    import java.util.Iterator;
018    import java.util.Vector;
019    
020    /**
021     * @author amir khosrowshahi , Mohammad Ali Rostami
022     * @email ma.rostami@yahoo.com
023     */
024    public class VertexCommands {
025    
026        BlackBoard bb;
027        private GraphData datas;
028    
029        public VertexCommands(BlackBoard bb) {
030            this.bb = bb;
031            datas = new GraphData(bb);
032        }
033    
034    
035        @CommandAttitude(name = "set_label", abbreviation = "_sl"
036                , description = "Changes the label of a vertex")
037        public void setLabel(@Parameter(name = "vertex label:")String label
038                , @Parameter(name = "new vertex label:")String newlabel) {
039            VertexModel v = getVertexByLabel(label);
040    
041            v.setLabel(newlabel);
042        }
043    
044        @CommandAttitude(name = "vertex_iterator", abbreviation = "_v_i"
045                , description = "get a iterator on verteices of graph")
046        public Iterator<VertexModel> getVertexIterator() {
047            return datas.getGraph().iterator();
048        }
049    
050        @CommandAttitude(name = "is_selected", abbreviation = "_is"
051                , description = "shows the vertex is selected or not")
052        public Boolean isSelected(@Parameter(name = "vertex label:")String label) {
053            VertexModel v = getVertexByLabel(label);
054    //        //Init.run.ext_console.println(v.isSelected(), Init.run.ext_console.getResultColor());
055            return v.isSelected();
056        }
057    
058    
059        @CommandAttitude(name = "replace", abbreviation = "_r"
060                , description = "Replaces the given vertex by a new position")
061        public void replace(@Parameter(name = "vertex label:")String label
062                , @Parameter(name = "new X position:")int x,
063                  @Parameter(name = "new Y position:")int y) {
064            VertexModel v = getVertexByLabel(label);
065            if (x == 0) {
066                x = (int) v.getLocation().x;
067            }
068            if (y == 0) {
069                y = (int) v.getLocation().y;
070            }
071            v.setLocation(new GraphPoint(x, y));
072        }
073    
074        @CommandAttitude(name = "add_vertex", abbreviation = "_av", description = "adds a vertex")
075        public void addVertex(@Parameter(name = "x positon")int x
076                , @Parameter(name = "y positon")int y) {
077            try {
078                AddVertex.doJob((GraphModel) bb.getData(GraphAttrSet.name), x, y);
079            }
080            catch (Exception e) {
081                e.printStackTrace();
082            }
083        }
084    
085        @CommandAttitude(name = "select_vertex", abbreviation = "_sv")
086        public void selectVertex(@Parameter(name = "vertex label :")String label) throws ShellCommandException {
087            VertexModel v = getVertexByLabel(label);
088            Vector<VertexModel> vertices = new Vector<VertexModel>();
089            vertices.add(v);
090    
091            if (v != null) datas.select.setSelectedVertices(vertices);
092            else throw new ShellCommandException("your entered vertex label doesnt exist!");
093        }
094    
095        @CommandAttitude(name = "remove_vertex", abbreviation = "_rv"
096                , description = "Removes a Vertex")
097        public void removeVertex(@Parameter(name = "vertex label :")String label) {
098            datas.getGraph().removeVertex(getVertexByLabel(label));
099        }
100    
101        VertexModel getVertexByID(String id) {
102            int ID = Integer.parseInt(id);
103            for (VertexModel v : datas.getGraph()) {
104                //Init.run.ext_console.printlnResult(v.getId());
105                if (v.getId() == ID)
106                    return v;
107            }
108            return null;
109        }
110    
111        VertexModel getVertexByLabel(String label) {
112            for (VertexModel v : datas.getGraph()) {
113                if (v.getLabel().equals(label))
114                    return v;
115            }
116            return null;
117        }
118    }