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.commandline.commands;
005    
006    import Jama.Matrix;
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.graph.ui.GTabbedGraphPane;
012    import graphlab.library.algorithms.goperators.EdgeInduced;
013    import graphlab.library.algorithms.goperators.GraphUnion;
014    import graphlab.library.algorithms.goperators.VertexInduced;
015    import graphlab.library.algorithms.goperators.product.GCartesianProduct;
016    import graphlab.library.algorithms.goperators.product.GPopularProduct;
017    import graphlab.platform.core.BlackBoard;
018    import graphlab.platform.lang.CommandAttitude;
019    import graphlab.platform.parameter.Parameter;
020    import graphlab.plugins.graphgenerator.core.PositionGenerators;
021    import graphlab.plugins.main.GraphData;
022    import org.xml.sax.SAXException;
023    
024    import javax.xml.parsers.ParserConfigurationException;
025    import java.awt.*;
026    import java.io.File;
027    import java.io.IOException;
028    import java.util.HashSet;
029    
030    /**
031     * @author Mohammad Ali Rostami
032     * @email ma.rostami@yahoo.com
033     */
034    
035    public class GraphCommands {
036        BlackBoard bb;
037    
038        public GraphCommands(BlackBoard bb) {
039            this.bb = bb;
040            datas = new GraphData(bb);
041        }
042    
043        GraphData datas;
044    
045    //    @CommandAttitude(name = "matlab" , abbreviation = "_ml"
046    //            , description = "")
047    //    public String matlab(String command) {
048    //        String parsed;
049    ////        return ConnectorReportExtension(parsed);
050    //    }
051    
052        @CommandAttitude(name = "current_graph", abbreviation = "_cg"
053                , description = "the matrix related to the graph")
054        public GraphModel getCurrentGraph() {
055            return datas.getGraph();
056        }
057    
058    
059        @CommandAttitude(name = "matrix", abbreviation = "_mat"
060                , description = "the matrix related to the graph")
061        public Matrix graph2Matrix() {
062            return datas.getGraph().getAdjacencyMatrix();
063        }
064    
065        @CommandAttitude(name = "weighted_matrix", abbreviation = "_wmat"
066                , description = "the weighted matrix related to the graph (with MatLab matrix format)")
067        public String weightMatrix() {
068            GraphModel g = datas.getGraph();
069            String ret = "";
070            for (VertexModel v : g) {
071                for (VertexModel w : g) {
072                    EdgeModel e = g.getEdge(w, v);
073                    ret += " " + (e == null ? "0" : e.getWeight()) + " ,";
074                }
075                ret = ret.substring(0, ret.length() - 1);
076                ret += ";";
077            }
078            if (g.getVerticesCount() > 0)
079                ret = ret.substring(0, ret.length() - 1);
080            return "[" + ret + "]";
081        }
082    
083        @CommandAttitude(name = "matlab_matrix", abbreviation = "_mt"
084                , description = "the weighted matrix related to the graph (with MatLab matrix format)")
085        public String matlabMatrix() {
086            GraphModel g = datas.getGraph();
087            String ret = "";
088            for (VertexModel v : g) {
089                for (VertexModel w : g) {
090                    EdgeModel e = g.getEdge(w, v);
091                    ret += " " + (e == null ? "0" : "1") + " ,";
092                }
093                ret = ret.substring(0, ret.length() - 1);
094                ret += ";";
095            }
096    
097            if (g.getVerticesCount() > 0)
098                ret = ret.substring(0, ret.length() - 1);
099            return "[" + ret + "]";
100        }
101    
102    
103        @CommandAttitude(name = "add_tab", abbreviation = "_at"
104                , description = "Adds a new Tab to GUI")
105        public void addTab() {
106            datas.core.addTab();
107        }
108    
109        @CommandAttitude(name = "show_graph", abbreviation = "_sg"
110                , description = "Shows the given graph in a new TAB")
111        public void showGraph(@Parameter(name = "graph")GraphModel g) {
112            datas.core.addTabNoGUI(g.isDirected(), bb);
113            datas.getGraph().addSubGraph(g, new Rectangle (100,100, 500,500));
114    
115        }
116    
117        @CommandAttitude(name = "clear_graph", abbreviation = "_cg"
118                , description = "Clears the graph")
119        public void clearGraph() {
120            datas.core.clearGraph();
121        }
122    
123        @CommandAttitude(name = "close_tab", abbreviation = "_ct"
124                , description = "closes the selected tab from GUI")
125        public void closeTab() {
126            datas.core.closeTab();
127        }
128    
129        @CommandAttitude(name = "cut", abbreviation = "_c"
130                , description = "Cuts the selected data to clipboard")
131        public void cutToClipboard() throws ShellCommandException {
132            if (!(datas.select.isSelectionEmpty())) {
133                datas.core.cutToClipboard(datas.select.getSelected());
134            } else {
135                throw new ShellCommandException("Nothing has been selected.");
136            }
137        }
138    
139        @CommandAttitude(name = "paste", abbreviation = "_p"
140                , description = "Pastes from the clipboard")
141        public void pasteFromClipboard() {
142            datas.core.pasteFromClipboard();
143        }
144    
145        @CommandAttitude(name = "redo", abbreviation = "_r"
146                , description = "Redos the last action")
147        public void redo() {
148            datas.core.redo();
149        }
150    
151        @CommandAttitude(name = "reset_graph", abbreviation = "_rg"
152                , description = "Resets the Graph")
153        public void resetGraph() {
154            datas.core.resetGraph();
155        }
156    
157        @CommandAttitude(name = "undo", abbreviation = "_u"
158                , description = "Undos the last performed action")
159        public void undo() {
160            datas.core.undo();
161        }
162    
163    //    @CommandAttitude(name = "matlab" , abbreviation = "_ml"
164    //            , description = "")
165    //    public String matlab(@Parameter(name = "matlab_command") String matlab_command) {
166    //        return
167    //    }
168    
169    //    @CommandAttitude(name = "zoom_in", abbreviation = "_zi"
170    //            , description = "zooms the board in")
171    //    public void zoomIn() {
172    //        datas.core.zoomIn();
173    //    }                                \
174    //
175    //    @CommandAttitude(name = "zoom_out", abbreviation = "_zo"
176    //            , description = "zooms the board out")
177    //    public void zoomOut() {
178    //        datas.core.zoomOut();
179    //    }
180    
181        @CommandAttitude(name = "copy_selected", abbreviation = "_c"
182                , description = "Copies the selected data to clipboard")
183        public void copyToClipboard() throws ShellCommandException {
184            if (!(datas.select.isSelectionEmpty())) {
185                datas.core.copyToClipboard(datas.select.getSelected());
186            } else {
187                throw new ShellCommandException("Nothing has been selected.");
188            }
189        }
190    
191        @CommandAttitude(name = "induced", abbreviation = "_induce", description = "Vertex Induced subgraph of given vertices")
192        public GraphModel induced(@Parameter(name = "graph")GraphModel g
193                , @Parameter(name = "vertices")Object[] c) {
194            GTabbedGraphPane gtp = bb.getData(GTabbedGraphPane.NAME);
195            resetGraph();
196            HashSet hs = new HashSet();
197    
198            for (Object vm : c) {
199                hs.add(getVertexById((Integer) vm, g));
200            }
201            GraphModel gm = (GraphModel) VertexInduced.induced(g, hs);
202            gm.setDirected(g.isDirected());
203            gtp.addGraph(gm);
204            return gm;
205        }
206    
207        public VertexModel getVertexById(int id, GraphModel g) {
208            for (VertexModel v : g)
209                if (v.getId() == id)
210                    return v;
211            return null;
212        }
213    
214        @CommandAttitude(name = "edge_induced", abbreviation = "_e_induce", description = "Edge Induced subgraph of selected edges")
215        public GraphModel edge_induced(@Parameter(name = "graph")GraphModel g) {
216            GTabbedGraphPane gtp = bb.getData(GTabbedGraphPane.NAME);
217            HashSet hs = datas.select.getSelectedEdges();
218            resetGraph();
219            GraphModel gm = (GraphModel) EdgeInduced.edgeInduced(g, hs);
220            gm.setDirected(g.isDirected());
221            gtp.addGraph(gm);
222            return gm;
223        }
224    
225        @CommandAttitude(name = "gjoin", abbreviation = "_jn", description = "Joins two graphs")
226        public void gjoin(@Parameter(name = "first_graph")GraphModel g1,
227                          @Parameter(name = "second_graph")GraphModel g2) {
228            GTabbedGraphPane gtp = bb.getData(GTabbedGraphPane.NAME);
229            GraphModel graphModel = (GraphModel) GraphUnion.union(g1, g2);
230            graphModel.setDirected(g1.isDirected());
231            gtp.addGraph(graphModel);
232        }
233    
234    
235        @CommandAttitude(name = "gunion", abbreviation = "_un", description = "Creates the union of two given graphs")
236        public GraphModel gunion(@Parameter(name = "first_graph")GraphModel g1
237                , @Parameter(name = "second_graph")GraphModel g2) {
238            GTabbedGraphPane gtp = bb.getData(GTabbedGraphPane.NAME);
239            GraphModel graphModel = (GraphModel) GraphUnion.union(g1, g2);
240            graphModel.setDirected(g1.isDirected());
241            gtp.addGraph(graphModel);
242            return graphModel;
243        }
244    
245    //    @CommandAttitude(name = "gcomplement", abbreviation = "_gc", description = "complement")
246    //    public GraphModel gcomplement(@Parameter(name = "graph_name") GraphModel g1_name) {
247    //        GTabbedGraphPane gtp = bb.getData(GTabbedGraphPane.NAME);
248    //        GraphModel g1 = null;
249    //        for (Component component : gtp.getTabedPane().getComponents()) {
250    //            if (!(component instanceof JGraph))
251    //                continue;
252    //
253    //            GraphModel graph = ((JGraph) component).getGraph();
254    //            if (graph.getLabel().equals(g1_name))
255    //                g1 = graph;
256    //        }
257    //        g1 = g1_name;
258    ////        GraphModel graphModel = GComplement.complement((GraphModel)g1);
259    ////        graphModel.setDirected(g1.isDirected());
260    ////        gtp.addGraph(graphModel);
261    //        return g1;
262    //    }
263    
264    //    @CommandAttitude(name = "help_window", abbreviation = "_hw"
265    //            , description = "Shows the help window")
266    //    public void showHelpWindow() {
267    //        datas.help.showHelpWindow();
268    //    }
269    
270        @CommandAttitude(name = "cartesian_product", abbreviation = "_cproduct", description = "Computes and shows the cartesian product of given graphs")
271        public void cartesian_product(@Parameter(name = "first_graph")GraphModel g1
272                , @Parameter(name = "second_graph")GraphModel g2) {
273            GTabbedGraphPane gtp = bb.getData(GTabbedGraphPane.NAME);
274            GCartesianProduct p = new GCartesianProduct();
275            GraphModel graphModel = (GraphModel) p.multiply(g1, g2);
276            graphModel.setDirected(g1.isDirected());
277            int n = graphModel.getVerticesCount();
278            Point ps[] = PositionGenerators.circle(250, 300, 300, n);
279            int count = 0;
280            for (VertexModel v : graphModel) {
281                v.setLocation(new GraphPoint(ps[count].x, ps[count].y));
282                count++;
283            }
284            gtp.addGraph(graphModel);
285        }
286    
287        @CommandAttitude(name = "product", abbreviation = "_product", description = "Computes and shows Popular product of given graphs")
288        public void product(@Parameter(name = "first_graph")GraphModel g1
289                , @Parameter(name = "second_graph")GraphModel g2) {
290            GTabbedGraphPane gtp = bb.getData(GTabbedGraphPane.NAME);
291            GPopularProduct p = new GPopularProduct();
292            GraphModel graphModel = (GraphModel) p.multiply(g1, g2);
293            graphModel.setDirected(g1.isDirected());
294            int n = graphModel.getVerticesCount();
295            Point ps[] = PositionGenerators.circle(200, 300, 300, n);
296            int count = 0;
297            for (VertexModel v : graphModel) {
298                v.setLocation(new GraphPoint(ps[count].x, ps[count].y));
299                count++;
300            }
301            gtp.addGraph(graphModel);
302        }
303    
304    //    @CommandAttitude(name = "preview_file", abbreviation = "_pf"
305    //            , description = "Previews the given filename")
306    //    public void showPreview(@Parameter(name = "filename:") String fileName) throws ShellCommandException {
307    //        try {
308    //            datas.preview.showPreview(fileName);
309    //        } catch (Exception e) {
310    //            throw new ShellCommandException("File does not exist of corrupted");
311    //        }
312    //
313    //    }
314    
315        //
316    
317        @CommandAttitude(name = "load_graphml", abbreviation = "_lg"
318                , description = "loads a graph from a GrapmML file")
319        public void loadGraphML(@Parameter(name = "filename")String fileName) {
320            try {
321                datas.saveLoad.loadGraphML(new File(fileName));
322            } catch (IOException e) {
323                e.printStackTrace();
324            } catch (ParserConfigurationException e) {
325                e.printStackTrace();
326            } catch (SAXException e) {
327                e.printStackTrace();
328            }
329        }
330    
331        VertexModel getVertexByID(String id) {
332            int ID = Integer.parseInt(id);
333            for (VertexModel v : datas.getGraph()) {
334                if (v.getId() == ID)
335                    return v;
336            }
337            return null;
338        }
339    
340        VertexModel getVertexByLabel(String label) {
341            for (VertexModel v : datas.getGraph()) {
342                if (v.getLabel().equals(label))
343                    return v;
344            }
345            return null;
346        }
347    }
348    
349