KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > search > PrefixSearchTupleSet


1 package prefuse.data.search;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.StringTokenizer JavaDoc;
5
6 import prefuse.data.Tuple;
7
8
9 /**
10  * <p>
11  * SearchTupleSet implementation supporting word prefix searches over indexed
12  * Tuple data fields. This class uses a {@link Trie Trie} data structure
13  * to find search results quickly; however, only prefix matches will be
14  * identified as valid search matches. Multi-term search queries will result
15  * in the union of the results for the individual query terms. That is, Tuples
16  * that match any one of the terms will be included in the results.
17  * </p>
18  *
19  * <p>
20  * For more advanced search capabilities, see
21  * {@link KeywordSearchTupleSet} or {@link RegexSearchTupleSet}.
22  * </p>
23  *
24  * @version 1.0
25  * @author <a HREF="http://jheer.org">jeffrey heer</a>
26  * @see prefuse.data.query.SearchQueryBinding
27  */

28 public class PrefixSearchTupleSet extends SearchTupleSet {
29     
30     private Trie m_trie;
31     private Trie.TrieNode m_curNode;
32     private String JavaDoc m_delim = " \t\n\r";
33     private String JavaDoc m_query = "";
34     
35     /**
36      * Creates a new KeywordSearchFocusSet that is not case sensitive.
37      */

38     public PrefixSearchTupleSet() {
39         this(false);
40     }
41     
42     /**
43      * Creates a new KeywordSearchFocusSet with the indicated case sensitivity.
44      * @param caseSensitive true if the search routines should be case
45      * sensitive, false otherwise.
46      */

47     public PrefixSearchTupleSet(boolean caseSensitive) {
48         m_trie = new Trie(caseSensitive);
49     }
50     
51     /**
52      * Returns the delimiter string used to divide data values and
53      * queries into separate words. By default, the value consists
54      * of just whitespace characters.
55      * @return the delimiter string used. This is passed as an argument to a
56      * {@link java.util.StringTokenizer} instance that will tokenize the text.
57      * @see java.util.StringTokenizer
58      */

59     public String JavaDoc getDelimiterString() {
60         return m_delim;
61     }
62     
63     /**
64      * Sets the delimiter string used to divide data values and
65      * queries into separate words. By default, the delimiter consists
66      * of just whitespace characters.
67      * @param delim the delimiter string to use. This is passed as an argument
68      * to a {@link java.util.StringTokenizer} instance that will tokenize the
69      * text.
70      * @see java.util.StringTokenizer
71      */

72     public void setDelimiterString(String JavaDoc delim) {
73         m_delim = delim;
74     }
75     
76     /**
77      * @see prefuse.data.search.SearchTupleSet#getQuery()
78      */

79     public String JavaDoc getQuery() {
80         return m_query;
81     }
82     
83     /**
84      * Searches the indexed Tuple fields for matching string prefixes,
85      * adding the Tuple instances for each search match to this TupleSet.
86      * The query string is first broken up into separate terms, as determined
87      * by the current delimiter string. A search for each term is conducted,
88      * and all matching Tuples are included in the results.
89      * @param query the query string to search for.
90      * @see #setDelimiterString(String)
91      */

92     public void search(String JavaDoc query) {
93         if ( query == null )
94             query = "";
95         
96         if ( query.equals(m_query) )
97             return;
98         
99         Tuple[] rem = clearInternal();
100         m_query = query;
101         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(m_query, m_delim);
102         if ( !st.hasMoreTokens() )
103             m_query = "";
104         while ( st.hasMoreTokens() )
105             prefixSearch(st.nextToken());
106         Tuple[] add = getTupleCount() > 0 ? toArray() : null;
107         fireTupleEvent(add, rem);
108     }
109
110     /**
111      * Issues a prefix search and collects the results
112      */

113     private void prefixSearch(String JavaDoc query) {
114         m_curNode = m_trie.find(query);
115         if ( m_curNode != null ) {
116             Iterator JavaDoc iter = trieIterator();
117             while ( iter.hasNext() )
118                 addInternal((Tuple)iter.next());
119         }
120     }
121     
122     /**
123      * Indexes the given field of the provided Tuple instance.
124      * @see prefuse.data.search.SearchTupleSet#index(prefuse.data.Tuple, java.lang.String)
125      */

126     public void index(Tuple t, String JavaDoc field) {
127         String JavaDoc s;
128         if ( (s=t.getString(field)) == null ) return;
129         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s,m_delim);
130         while ( st.hasMoreTokens() ) {
131             String JavaDoc tok = st.nextToken();
132             addString(tok, t);
133         }
134     }
135     
136     private void addString(String JavaDoc s, Tuple t) {
137         m_trie.addString(s,t);
138     }
139     
140     /**
141      * Returns true, as unidexing is supported by this class.
142      * @see prefuse.data.search.SearchTupleSet#isUnindexSupported()
143      */

144     public boolean isUnindexSupported() {
145         return true;
146     }
147     
148     /**
149      * @see prefuse.data.search.SearchTupleSet#unindex(prefuse.data.Tuple, java.lang.String)
150      */

151     public void unindex(Tuple t, String JavaDoc field) {
152         String JavaDoc s;
153         if ( (s=t.getString(field)) == null ) return;
154         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s,m_delim);
155         while ( st.hasMoreTokens() ) {
156             String JavaDoc tok = st.nextToken();
157             removeString(tok, t);
158         }
159     }
160     
161     /**
162      * Removes all search hits and clears out the index.
163      * @see prefuse.data.tuple.TupleSet#clear()
164      */

165     public void clear() {
166         m_trie = new Trie(m_trie.isCaseSensitive());
167         super.clear();
168     }
169     
170     private void removeString(String JavaDoc s, Tuple t) {
171         m_trie.removeString(s,t);
172     }
173     
174     private Iterator JavaDoc trieIterator() {
175         return m_trie.new TrieIterator(m_curNode);
176     }
177     
178 } // end of class PrefixSearchTupleSet
179
Popular Tags