1
14
15 package gate.creole.ir;
16
17 import java.util.List;
18
19 import javax.swing.JOptionPane;
20
21 import gate.ProcessingResource;
22 import gate.Resource;
23 import gate.creole.*;
24 import gate.gui.MainFrame;
25 import gate.Gate;
26
27
28 public class SearchPR extends AbstractProcessingResource
29 implements ProcessingResource{
30
31 private IndexedCorpus corpus = null;
32 private String query = null;
33 private String searcherClassName = null;
34 private QueryResultList resultList = null;
35 private int limit = -1;
36 private List fieldNames = null;
37
38 private Search searcher = null;
39
40
41 public SearchPR(){
42 }
43
44
45 public Resource init() throws ResourceInstantiationException {
46 Resource result = super.init();
47 return result;
48 }
49
50
58 public void reInit() throws ResourceInstantiationException {
59 init();
60 }
61
62
66 public void execute() throws ExecutionException {
67 if ( corpus == null){
68 throw new ExecutionException("Corpus is not initialized");
69 }
70 if ( query == null){
71 throw new ExecutionException("Query is not initialized");
72 }
73 if ( searcher == null){
74 throw new ExecutionException("Searcher is not initialized");
75 }
76
77
78 String val = (String) corpus.getFeatures().get(gate.creole.ir.lucene.LuceneIndexManager.CORPUS_INDEX_FEATURE);
80 if(!val.equals(gate.creole.ir.lucene.LuceneIndexManager.CORPUS_INDEX_FEATURE_VALUE)) {
81 throw new ExecutionException("This corpus was not indexed by the specified IR");
82 }
83
84
85
86 try {
87 if (((IndexedCorpus) corpus).getIndexManager() == null){
88 MainFrame.unlockGUI();
89 JOptionPane.showMessageDialog(null, "Corpus is not indexed!\n"
90 +"Please index fisrt this corpus!",
91 "Search Procesing", JOptionPane.WARNING_MESSAGE);
92 return;
93 }
94
95 fireProgressChanged(0);
96 resultList = null;
97 searcher.setCorpus((IndexedCorpus) corpus);
98 resultList = searcher.search(query, limit, fieldNames);
99 fireProcessFinished();
100 }
101
102 catch (SearchException ie) {
103 throw new ExecutionException(ie.getMessage());
104 }
105 catch (IndexException ie) {
106 throw new ExecutionException(ie.getMessage());
107 }
108 }
109
110 public void setCorpus(IndexedCorpus corpus) {
111 this.corpus = corpus;
112 }
113
114 public IndexedCorpus getCorpus() {
115 return this.corpus;
116 }
117
118 public void setQuery(String query) {
119 this.query = query;
120 }
121
122 public String getQuery() {
123 return this.query;
124 }
125
126 public void setSearcherClassName(String name){
127 this.searcherClassName = name;
128 try {
129 searcher = (Search)
131 Class.forName(searcherClassName, true, Gate.getClassLoader())
132 .newInstance();
133 }
134 catch(Exception e){
135 e.printStackTrace();
136 }
137 }
138
139 public String getSearcherClassName(){
140
141 return this.searcher.getClass().getName();
142 }
143
144 public void setLimit(Integer limit){
145 this.limit = limit.intValue();
146 }
147
148 public Integer getLimit(){
149 return new Integer(this.limit);
150 }
151
152 public void setFieldNames(List fieldNames){
153 this.fieldNames = fieldNames;
154 }
155
156 public List getFieldNames(){
157 return this.fieldNames;
158 }
159
160 public QueryResultList getResult(){
161 return resultList;
162 }
163
164 public void setResult(QueryResultList qr){
165 throw new UnsupportedOperationException();
166 }
167
168 }
169