1   /*
2    *  SearchPR.java
3    *
4    *  Copyright (c) 1998-2005, 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  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    /** Constructor of the class*/
41    public SearchPR(){
42    }
43  
44     /** Initialise this resource, and return it. */
45    public Resource init() throws ResourceInstantiationException {
46      Resource result = super.init();
47      return result;
48    }
49  
50    /**
51     * Reinitialises the processing resource. After calling this method the
52     * resource should be in the state it is after calling init.
53     * If the resource depends on external resources (such as rules files) then
54     * the resource will re-read those resources. If the data used to create
55     * the resource has changed since the resource has been created then the
56     * resource will change too after calling reInit().
57    */
58    public void reInit() throws ResourceInstantiationException {
59      init();
60    }
61  
62    /**
63     * This method runs the coreferencer. It assumes that all the needed parameters
64     * are set. If they are not, an exception will be fired.
65     */
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      /* Niraj */
78      // we need to check if this is the corpus with the specified feature
79      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      /* End */
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       // load searcher class through GATE classloader
130       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