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      /* Niraj */
77      // we need to check if this is the corpus with the specified feature
78      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      /* End */
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 }