SearchPR.java
001 /*
002  *  SearchPR.java
003  *
004  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
005  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006  *
007  *  This file is part of GATE (see http://gate.ac.uk/), and is free
008  *  software, licenced under the GNU Library General Public License,
009  *  Version 2, June 1991 (in the distribution as file licence.html,
010  *  and also available at http://gate.ac.uk/gate/licence.html).
011  *
012  *  Rosen Marinov, 19/Apr/2002
013  *
014  */
015 
016 package gate.creole.ir;
017 
018 import java.util.List;
019 
020 import javax.swing.JOptionPane;
021 
022 import gate.ProcessingResource;
023 import gate.Resource;
024 import gate.creole.*;
025 import gate.gui.MainFrame;
026 import gate.Gate;
027 
028 
029 public class SearchPR extends AbstractProcessingResource
030                       implements ProcessingResource{
031 
032   private IndexedCorpus corpus = null;
033   private String query  = null;
034   private String searcherClassName = null;
035   private QueryResultList resultList = null;
036   private int limit = -1;
037   private List fieldNames = null;
038 
039   private Search searcher = null;
040 
041   /** Constructor of the class*/
042   public SearchPR(){
043   }
044 
045    /** Initialise this resource, and return it. */
046   public Resource init() throws ResourceInstantiationException {
047     Resource result = super.init();
048     return result;
049   }
050 
051   /**
052    * Reinitialises the processing resource. After calling this method the
053    * resource should be in the state it is after calling init.
054    * If the resource depends on external resources (such as rules files) then
055    * the resource will re-read those resources. If the data used to create
056    * the resource has changed since the resource has been created then the
057    * resource will change too after calling reInit().
058   */
059   public void reInit() throws ResourceInstantiationException {
060     init();
061   }
062 
063   /**
064    * This method runs the coreferencer. It assumes that all the needed parameters
065    * are set. If they are not, an exception will be fired.
066    */
067   public void execute() throws ExecutionException {
068     if corpus == null){
069       throw new ExecutionException("Corpus is not initialized");
070     }
071     if query == null){
072       throw new ExecutionException("Query is not initialized");
073     }
074     if searcher == null){
075       throw new ExecutionException("Searcher is not initialized");
076     }
077 
078     /* Niraj */
079     // we need to check if this is the corpus with the specified feature
080     String val = (Stringcorpus.getFeatures().get(gate.creole.ir.lucene.LuceneIndexManager.CORPUS_INDEX_FEATURE);
081     if(!gate.creole.ir.lucene.LuceneIndexManager.CORPUS_INDEX_FEATURE_VALUE.equals(val)) {
082       throw new ExecutionException("This corpus was not indexed by the specified IR");
083     }
084     /* End */
085 
086 
087     try {
088       if (((IndexedCorpuscorpus).getIndexManager() == null){
089         MainFrame.unlockGUI();
090         JOptionPane.showMessageDialog(MainFrame.getInstance()"Corpus is not indexed!\n"
091                                     +"Please index first this corpus!",
092                        "Search Processing", JOptionPane.WARNING_MESSAGE);
093         return;
094       }
095 
096       fireProgressChanged(0);
097       resultList = null;
098       searcher.setCorpus((IndexedCorpuscorpus);
099       resultList = searcher.search(query, limit, fieldNames);
100       fireProcessFinished();
101     }
102 
103     catch (SearchException ie) {
104       throw new ExecutionException(ie.getMessage());
105     }
106     catch (IndexException ie) {
107       throw new ExecutionException(ie.getMessage());
108     }
109   }
110 
111   public void setCorpus(IndexedCorpus corpus) {
112     this.corpus = corpus;
113   }
114 
115   public IndexedCorpus getCorpus() {
116     return this.corpus;
117   }
118 
119   public void setQuery(String query) {
120     this.query = query;
121   }
122 
123   public String getQuery() {
124     return this.query;
125   }
126 
127   public void setSearcherClassName(String name){
128     this.searcherClassName = name;
129     try {
130       // load searcher class through GATE classloader
131       searcher = (Search)
132         Class.forName(searcherClassName, true, Gate.getClassLoader())
133         .newInstance();
134     }
135     catch(Exception e){
136       e.printStackTrace();
137     }
138   }
139 
140   public String getSearcherClassName(){
141 
142     return this.searcher.getClass().getName();
143   }
144 
145   public void setLimit(Integer limit){
146     this.limit = limit.intValue();
147   }
148 
149   public Integer getLimit(){
150     return new Integer(this.limit);
151   }
152 
153   public void setFieldNames(List fieldNames){
154     this.fieldNames = fieldNames;
155   }
156 
157   public List getFieldNames(){
158     return this.fieldNames;
159   }
160 
161   public QueryResultList getResult(){
162     return resultList;
163   }
164 
165   public void setResult(QueryResultList qr){
166     throw new UnsupportedOperationException();
167   }
168 
169 }