KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > action > filter > GraphDistanceFilter


1 package prefuse.action.filter;
2
3 import java.util.Iterator JavaDoc;
4
5 import prefuse.Constants;
6 import prefuse.Visualization;
7 import prefuse.action.GroupAction;
8 import prefuse.data.Graph;
9 import prefuse.data.expression.Predicate;
10 import prefuse.data.tuple.TupleSet;
11 import prefuse.data.util.BreadthFirstIterator;
12 import prefuse.data.util.FilterIterator;
13 import prefuse.util.PrefuseLib;
14 import prefuse.visual.VisualItem;
15 import prefuse.visual.expression.InGroupPredicate;
16
17 /**
18  * Filter Action that sets visible all items within a specified graph distance
19  * from a set of focus items; all other items will be set to invisible.
20  *
21  * @author <a HREF="http://jheer.org">jeffrey heer</a>
22  */

23 public class GraphDistanceFilter extends GroupAction {
24
25     protected int m_distance;
26     protected String JavaDoc m_sources;
27     protected Predicate m_groupP;
28     protected BreadthFirstIterator m_bfs;
29    
30     /**
31      * Create a new GraphDistanceFilter that processes the given data group
32      * and uses a graph distance of 1. By default, the
33      * {@link prefuse.Visualization#FOCUS_ITEMS} group will be used as the
34      * source nodes from which to measure the distance.
35      * @param group the group to process. This group should resolve to a
36      * Graph instance, otherwise exceptions will be thrown when this
37      * Action is run.
38      */

39     public GraphDistanceFilter(String JavaDoc group) {
40         this(group, 1);
41     }
42     
43     /**
44      * Create a new GraphDistanceFilter that processes the given data group
45      * and uses the given graph distance. By default, the
46      * {@link prefuse.Visualization#FOCUS_ITEMS} group will be used as the
47      * source nodes from which to measure the distance.
48      * @param group the group to process. This group should resolve to a
49      * Graph instance, otherwise exceptions will be thrown when this
50      * Action is run.
51      * @param distance the graph distance within which items will be
52      * visible.
53      */

54     public GraphDistanceFilter(String JavaDoc group, int distance) {
55         this(group, Visualization.FOCUS_ITEMS, distance);
56     }
57     
58     /**
59      * Create a new GraphDistanceFilter that processes the given data group
60      * and uses the given graph distance.
61      * @param group the group to process. This group should resolve to a
62      * Graph instance, otherwise exceptions will be thrown when this
63      * Action is run.
64      * @param sources the group to use as source nodes for measuring
65      * graph distance.
66      * @param distance the graph distance within which items will be
67      * visible.
68      */

69     public GraphDistanceFilter(String JavaDoc group, String JavaDoc sources, int distance)
70     {
71         super(group);
72         m_sources = sources;
73         m_distance = distance;
74         m_groupP = new InGroupPredicate(
75             PrefuseLib.getGroupName(group, Graph.NODES));
76         m_bfs = new BreadthFirstIterator();
77     }
78     
79     /**
80      * Return the graph distance threshold used by this filter.
81      * @return the graph distance threshold
82      */

83     public int getDistance() {
84         return m_distance;
85     }
86
87     /**
88      * Set the graph distance threshold used by this filter.
89      * @param distance the graph distance threshold to use
90      */

91     public void setDistance(int distance) {
92         m_distance = distance;
93     }
94     
95     /**
96      * Get the name of the group to use as source nodes for measuring
97      * graph distance. These form the roots from which the graph distance
98      * is measured.
99      * @return the source data group
100      */

101     public String JavaDoc getSources() {
102         return m_sources;
103     }
104     
105     /**
106      * Set the name of the group to use as source nodes for measuring
107      * graph distance. These form the roots from which the graph distance
108      * is measured.
109      * @param sources the source data group
110      */

111     public void setSources(String JavaDoc sources) {
112         m_sources = sources;
113     }
114     
115     /**
116      * @see prefuse.action.GroupAction#run(double)
117      */

118     public void run(double frac) {
119         // mark the items
120
Iterator JavaDoc items = m_vis.visibleItems(m_group);
121         while ( items.hasNext() ) {
122             VisualItem item = (VisualItem)items.next();
123             item.setDOI(Constants.MINIMUM_DOI);
124         }
125         
126         // set up the graph traversal
127
TupleSet src = m_vis.getGroup(m_sources);
128         Iterator JavaDoc srcs = new FilterIterator(src.tuples(), m_groupP);
129         m_bfs.init(srcs, m_distance, Constants.NODE_AND_EDGE_TRAVERSAL);
130         
131         // traverse the graph
132
while ( m_bfs.hasNext() ) {
133             VisualItem item = (VisualItem)m_bfs.next();
134             int d = m_bfs.getDepth(item);
135             PrefuseLib.updateVisible(item, true);
136             item.setDOI(-d);
137             item.setExpanded(d < m_distance);
138         }
139         
140         // mark unreached items
141
items = m_vis.visibleItems(m_group);
142         while ( items.hasNext() ) {
143             VisualItem item = (VisualItem)items.next();
144             if ( item.getDOI() == Constants.MINIMUM_DOI ) {
145                 PrefuseLib.updateVisible(item, false);
146                 item.setExpanded(false);
147             }
148         }
149     }
150
151 } // end of class GraphDistanceFilter
152
Popular Tags