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.EdgeModel; 009 import graphlab.graph.graph.GraphModel; 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.edge.AddEdge; 016 017 /** 018 * @author amir khosrowshahi , Mohammad Ali Rostami 019 * @email ma.rostami@yahoo.com 020 */ 021 public class EdgeCommands { 022 023 BlackBoard bb; 024 private GraphData datas; 025 026 public EdgeCommands(BlackBoard bb) { 027 this.bb = bb; 028 datas = new GraphData(bb); 029 } 030 031 @CommandAttitude(name = "add_edge", abbreviation = "_ae") 032 public void addEdge(@Parameter(name = "first vertex label")String label1 033 , @Parameter(name = "second vertex label")String label2) throws ShellCommandException { 034 try { 035 AddEdge.doJob(((GraphModel) bb.getData(GraphAttrSet.name)), getVertexByLabel(label1), getVertexByLabel(label2)); 036 } catch (NullPointerException e) { 037 throw new ShellCommandException("your entered vertex label doesnt exist"); 038 // Init.run.ext_console.error("your entered vertex label doesnt exist!"); 039 } 040 } 041 042 @CommandAttitude(name = "remove_edge", abbreviation = "_re" 043 , description = "Removes an Edge") 044 public void removeEdge(@Parameter(name = "vertex 1 label :")String label1 045 , @Parameter(name = "vertex 2 label:")String label2) { 046 datas.getGraph().removeEdge(getEdge(label1, label2)); 047 } 048 049 @CommandAttitude(name = "get_edge", abbreviation = "_ge" 050 , description = "return the specific edge") 051 private EdgeModel getEdge(String label1, String label2) { 052 return datas.getGraph().getEdge(getVertexByLabel(label1), getVertexByLabel(label2)); 053 } 054 055 056 @CommandAttitude(name = "set_edge_label", abbreviation = "_sel") 057 public void setEdgeLabel(@Parameter(name = "first vertex label :")String label1 058 , @Parameter(name = "second vertex label :")String label2, 059 @Parameter(name = "new edge label :")String newLabel) throws ShellCommandException { 060 try { 061 getEdge(label1, label2).setLabel(newLabel); 062 } catch (NullPointerException e) { 063 throw new ShellCommandException("your entered a edge label that doesn't exist!"); 064 // Init.run.ext_console.error("your entered a edge label that doesn't exist!"); 065 } 066 } 067 068 @CommandAttitude(name = "set_edge_weight", abbreviation = "_sew") 069 public void setEdgeWeight(@Parameter(name = "first vertex label :")String label1 070 , @Parameter(name = "second vertex label :")String label2, 071 @Parameter(name = "new edge weight :")int newWeight) throws ShellCommandException { 072 try { 073 getEdge(label1, label2).setWeight(newWeight); 074 } catch (NullPointerException e) { 075 throw new ShellCommandException("your entered edge doesn't exist!"); 076 // Init.run.ext_console.error("your entered edge doesn't exist!"); 077 } 078 } 079 080 @CommandAttitude(name = "get_edge_label", abbreviation = "_gel") 081 public String getEdgeLabel(@Parameter(name = "first vertex label :")String label1 082 , @Parameter(name = "second vertex label :")String label2) throws ShellCommandException { 083 try { 084 //Init.run.ext_console.printlnResult(getEdge(label1, label2).model.getLabel()); 085 return getEdge(label1, label2).getLabel(); 086 087 } catch (NullPointerException e) { 088 throw new ShellCommandException("your entered edge doesn't exist!"); 089 } 090 } 091 092 @CommandAttitude(name = "get_edge_weight", abbreviation = "_gew") 093 public int getEdgeWeight(@Parameter(name = "first vertex label :")String label1 094 , @Parameter(name = "second vertex label :")String label2) throws ShellCommandException { 095 try { 096 //Init.run.ext_console.printlnResult(getEdge(label1, label2).model.getWeight()); 097 return getEdge(label1, label2).getWeight(); 098 } catch (NullPointerException e) { 099 throw new ShellCommandException("your entered edge doesn't exist!"); 100 } 101 } 102 103 VertexModel getVertexByID(String id) { 104 int ID = Integer.parseInt(id); 105 for (VertexModel v : datas.getGraph()) { 106 //Init.run.ext_console.printlnResult(v.getId()); 107 if (v.getId() == ID) 108 return v; 109 } 110 return null; 111 } 112 113 VertexModel getVertexByLabel(String label) { 114 for (VertexModel v : datas.getGraph()) { 115 if (v.getLabel().equals(label)) 116 return v; 117 } 118 return null; 119 } 120 }