1   /*
2    *  SearchPR.java
3    *
4    *  Copyright (c) 1998-2004, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Rosen Marinov, 19/Apr/2002
12   *
13   */
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    /** Constructor of the class*/
40    public SearchPR(){
41    }
42  
43     /** Initialise this resource, and return it. */
44    public Resource init() throws ResourceInstantiationException {
45      Resource result = super.init();
46      return result;
47    }
48  
49    /**
50     * Reinitialises the processing resource. After calling this method the
51     * resource should be in the state it is after calling init.
52     * If the resource depends on external resources (such as rules files) then
53     * the resource will re-read those resources. If the data used to create
54     * the resource has changed since the resource has been created then the
55     * resource will change too after calling reInit().
56    */
57    public void reInit() throws ResourceInstantiationException {
58      init();
59    }
60  
61    /**
62     * This method runs the coreferencer. It assumes that all the needed parameters
63     * are set. If they are not, an exception will be fired.
64     */
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      try {
77        if (((IndexedCorpus) corpus).getIndexManager() == null){
78          MainFrame.unlockGUI();
79          JOptionPane.showMessageDialog(null, "Corpus is not indexed!\n"
80                                      +"Please index fisrt this corpus!",
81                         "Search Procesing", JOptionPane.WARNING_MESSAGE);
82          return;
83        }
84  
85        fireProgressChanged(0);
86        resultList = null;
87        searcher.setCorpus((IndexedCorpus) corpus);
88        resultList = searcher.search(query, limit, fieldNames);
89        fireProcessFinished();
90      }
91  
92      catch (SearchException ie) {
93        throw new ExecutionException(ie.getMessage());
94      }
95      catch (IndexException ie) {
96        throw new ExecutionException(ie.getMessage());
97      }
98    }
99  
100   public void setCorpus(IndexedCorpus corpus) {
101     this.corpus = corpus;
102   }
103 
104   public IndexedCorpus getCorpus() {
105     return this.corpus;
106   }
107 
108   public void setQuery(String query) {
109     this.query = query;
110   }
111 
112   public String getQuery() {
113     return this.query;
114   }
115 
116   public void setSearcherClassName(String name){
117     this.searcherClassName = name;
118     try {
119       searcher = (Search) Class.forName(searcherClassName).newInstance();
120     }
121     catch(Exception e){
122       e.printStackTrace();
123     }
124   }
125 
126   public String getSearcherClassName(){
127 
128     return this.searcher.getClass().getName();
129   }
130 
131   public void setLimit(Integer limit){
132     this.limit = limit.intValue();
133   }
134 
135   public Integer getLimit(){
136     return new Integer(this.limit);
137   }
138 
139   public void setFieldNames(List fieldNames){
140     this.fieldNames = fieldNames;
141   }
142 
143   public List getFieldNames(){
144     return this.fieldNames;
145   }
146 
147   public QueryResultList getResult(){
148     return resultList;
149   }
150 
151   public void setResult(QueryResultList qr){
152     throw new UnsupportedOperationException();
153   }
154 
155 }