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.main.select; 005 006 import graphlab.graph.atributeset.GraphAttrSet; 007 import graphlab.graph.graph.EdgeModel; 008 import graphlab.graph.graph.GraphModel; 009 import graphlab.graph.graph.SubGraph; 010 import graphlab.graph.graph.VertexModel; 011 import graphlab.platform.core.BlackBoard; 012 import graphlab.platform.plugin.PluginMethods; 013 014 import java.util.Collection; 015 import java.util.HashSet; 016 import java.util.Vector; 017 018 /** 019 * @author azin azadi 020 */ 021 public class SelectPluginMethods implements PluginMethods { 022 private BlackBoard b; 023 024 public SelectPluginMethods(BlackBoard b) { 025 this.b = b; 026 } 027 028 /** 029 * @return returns the selected Vertices ans Edges of the graph 030 */ 031 public SubGraph getSelected() { 032 return b.getData(Select.EVENT_KEY); 033 } 034 035 public void setSelected(SubGraph sd) { 036 b.setData(Select.EVENT_KEY, sd); 037 } 038 039 public void setSelected(Collection<VertexModel> selectedVertices, Collection<EdgeModel> selectedEdges) { 040 SubGraph sd = new SubGraph(); 041 sd.vertices = new HashSet<VertexModel>(selectedVertices); 042 sd.edges = new HashSet<EdgeModel>(selectedEdges); 043 b.setData(Select.EVENT_KEY, sd); 044 } 045 046 public HashSet<VertexModel> getSelectedVertices() { 047 SubGraph selected = getSelected(); 048 if (selected == null) 049 return new HashSet<VertexModel>(); 050 return selected.vertices; 051 } 052 053 public void setSelectedVertices(Collection<VertexModel> selectedVertices) { 054 SubGraph sd = new SubGraph(); 055 sd.vertices = new HashSet<VertexModel>(selectedVertices); 056 b.setData(Select.EVENT_KEY, sd); 057 } 058 059 public HashSet<EdgeModel> getSelectedEdges() { 060 return getSelected().edges; 061 } 062 063 public void setSelectedEdges(Collection<EdgeModel> selectedEdges) { 064 SubGraph sd = new SubGraph(); 065 sd.edges = new HashSet<EdgeModel>(selectedEdges); 066 b.setData(Select.EVENT_KEY, sd); 067 } 068 069 public boolean isSelectionEmpty() { 070 return getSelectedEdges().size() == 0 && getSelectedVertices().size() == 0; 071 } 072 073 public void clearSelection() { 074 setSelected(new SubGraph()); 075 } 076 077 //selection modification methods 078 // private void shrinkSelection() { 079 // SubGraph selection = getSelected(); 080 // Vector<VertexModel> toSelect = new Vector<VertexModel>(); 081 // 082 // for (VertexModel v : selection.vertices) { 083 // if (selection.getNeighbors(v).size() > 1) 084 // toSelect.add(v); 085 // } 086 // setSelectedVertices(toSelect); 087 // 088 // } 089 090 /** 091 * adds any vertex in graph which is adjacent to at list one vertex in selected vertices 092 * 093 * @param g 094 */ 095 public void expandSelection() { 096 GraphModel g = b.getData(GraphAttrSet.name); 097 HashSet<VertexModel> sV = getSelectedVertices(); 098 Vector<VertexModel> toSelect = new Vector<VertexModel>(); 099 100 for (VertexModel v : sV) { 101 for (VertexModel nv : g.getNeighbors(v)) 102 toSelect.add(nv); 103 } 104 toSelect.addAll(sV); 105 setSelectedVertices(toSelect); 106 } 107 108 }