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 Lesser General Public License (LGPL): http://www.gnu.org/licenses/
004    
005    package graphlab.extensions.actions;
006    
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.plugins.main.GraphData;
012    import graphlab.plugins.main.extension.GraphActionExtension;
013    
014    import java.util.Iterator;
015    
016    
017    /**
018     * Creates a line graph from the current graph and shows it in a new tab
019     *
020     * @author Mohammad Ali Rostami
021     * @author Azin Azadi
022     */
023    public class LineGraph implements GraphActionExtension {
024    
025        public void action(GraphData graphData) {
026    
027            GraphModel g1 = graphData.getGraph();
028            GraphModel g2 = new GraphModel(false);//
029            VertexModel vg1[] = g1.getVertexArray();
030    
031            for (EdgeModel e : g1.getEdges()) {
032                VertexModel v = new VertexModel();
033                v.setLabel(e.getLabel());
034                GraphPoint loc = new GraphPoint(e.source.getLocation());
035                loc.add(e.target.getLocation());
036                loc.multiply(0.5);
037                loc.add(e.getCurveControlPoint());
038                v.setLocation(loc);
039                e.getProp().obj = v;
040                g2.insertVertex(v);
041            }
042            for (VertexModel v : g1) {
043                Iterator<EdgeModel> ie = g1.lightEdgeIterator(v);
044    
045                while (ie.hasNext()) {
046                    EdgeModel e = ie.next();
047                    Iterator<EdgeModel> ie2 = g1.lightEdgeIterator(v);
048                    while (ie2.hasNext()) {
049                        EdgeModel e2 = ie2.next();
050                        if (e != e2) {
051                            EdgeModel ne = new EdgeModel((VertexModel) e.getProp().obj, (VertexModel) e2.getProp().obj);
052                            g2.insertEdge(ne);
053                        }
054                    }
055                }
056            }
057            graphData.core.showGraph(g2);
058        }
059    
060        public String getName() {
061            return "LineGraph";
062        }
063    
064        public String getDescription() {
065            return "Makes a graph including the edges of original graph as vertices ";
066        }
067    
068    }