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.VertexModel; 010 import graphlab.platform.Application; 011 import graphlab.platform.StaticUtils; 012 import graphlab.platform.parameter.Parameter; 013 import graphlab.platform.parameter.Parametrizable; 014 import graphlab.plugins.commonplugin.undo.Undoable; 015 import graphlab.plugins.commonplugin.undo.UndoableActionOccuredData; 016 import graphlab.plugins.main.GraphData; 017 import graphlab.plugins.main.core.AlgorithmUtils; 018 import graphlab.plugins.main.extension.GraphActionExtension; 019 020 import java.util.Vector; 021 022 /** 023 * @author Azin Azadi 024 */ 025 026 027 public class GraphPower implements GraphActionExtension, Parametrizable, Undoable { 028 @Parameter 029 public int k = 2; 030 031 Vector<EdgeModel> toInsert = new Vector<EdgeModel>(); 032 Vector<VertexModel> subtree = new Vector<VertexModel>(); 033 034 public String getName() { 035 return "Create Power Graph"; 036 } 037 038 public String getDescription() { 039 return "Create Power graph"; 040 } 041 042 public void action(GraphData graphData) { 043 toInsert.clear(); 044 GraphModel g = graphData.getGraph(); 045 046 AlgorithmUtils.clearVertexMarks(g); 047 for (VertexModel v : g) { 048 subtree.clear(); 049 aStar(v, v, k, g); 050 for (VertexModel vv : subtree) 051 vv.setMark(false); 052 } 053 g.insertEdges(toInsert); 054 055 //undo log operation 056 //todo: make a copy of toInsert 057 final UndoableActionOccuredData uaod = new UndoableActionOccuredData(this); 058 uaod.properties.put("edges", toInsert); 059 uaod.properties.put("graph", g); 060 graphData.core.addUndoData(uaod); 061 } 062 063 void aStar(VertexModel root, VertexModel v, int k, GraphModel g) { 064 if (k == 0) 065 return; 066 v.setMark(true); 067 subtree.add(v); 068 069 for (VertexModel vv : g.getNeighbors(v)) { 070 if (!vv.getMark()) { 071 toInsert.add(new EdgeModel(root, vv)); 072 aStar(root, vv, k - 1, g); 073 } 074 } 075 } 076 077 public String checkParameters() { 078 toInsert = new Vector<EdgeModel>(); 079 subtree = new Vector<VertexModel>(); 080 return (k < 2 ? "K must be larger than 1" : null); 081 } 082 083 public static void main(String[] args) { 084 Application.main(args); 085 086 StaticUtils.loadSingleExtension(GraphPower.class); 087 } 088 089 public void undo(UndoableActionOccuredData uaod) { 090 final Vector<EdgeModel> tI = (Vector<EdgeModel>) uaod.properties.get("edges"); 091 final GraphModel g = (GraphModel) uaod.properties.get("graph"); 092 for (EdgeModel em:tI) 093 g.removeEdge(em); 094 } 095 096 public void redo(UndoableActionOccuredData uaod) { 097 final Vector<EdgeModel> tI = (Vector<EdgeModel>) uaod.properties.get("edges"); 098 final GraphModel g = (GraphModel) uaod.properties.get("graph"); 099 g.insertEdges(tI); 100 } 101 } 102 103