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