1   /*
2    *  Copyright (c) 1998-2001, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Valentin Tablan 08/10/2001
10   *
11   *  $Id: SerialAnalyserController.java,v 1.13 2003/03/31 05:05:51 valyt Exp $
12   *
13   */
14  
15  package gate.creole;
16  
17  import gate.*;
18  import gate.util.*;
19  
20  import java.util.*;
21  
22  /**
23   * This class implements a SerialController that only contains
24   * {@link gate.LanguageAnalyser}s.
25   * It has a {@link gate.Corpus} and its execute method runs all the analysers in
26   * turn over each of the documents in the corpus.
27   */
28  public class SerialAnalyserController extends SerialController
29                implements CorpusController{
30  
31    public gate.Corpus getCorpus() {
32      return corpus;
33    }
34  
35    public void setCorpus(gate.Corpus corpus) {
36      this.corpus = corpus;
37    }
38  
39    /** Run the Processing Resources in sequence. */
40    public void execute() throws ExecutionException{
41      interrupted = false;
42      if(corpus == null) throw new ExecutionException(
43        "(SerialAnalyserController) \"" + getName() + "\":\n" +
44        "The corpus supplied for execution was null!");
45      //iterate through the documents in the corpus
46      for(int i = 0; i < corpus.size(); i++){
47        if(isInterrupted()) throw new ExecutionInterruptedException(
48          "The execution of the " + getName() +
49          " application has been abruptly interrupted!");
50  
51        boolean docWasLoaded = corpus.isDocumentLoaded(i);
52        Document doc = (Document)corpus.get(i);
53        //run the system over this document
54        //set the doc and corpus
55        for(int j = 0; j < prList.size(); j++){
56          ((LanguageAnalyser)prList.get(j)).setDocument(doc);
57          ((LanguageAnalyser)prList.get(j)).setCorpus(corpus);
58        }
59  
60  //      try{
61        super.execute();
62  //      }catch(Exception e){
63  //        e.printStackTrace(Err.getPrintWriter());
64  //      }
65  
66        //unset the doc and corpus
67        for(int j = 0; j < prList.size(); j++){
68          ((LanguageAnalyser)prList.get(j)).setDocument(null);
69          ((LanguageAnalyser)prList.get(j)).setCorpus(null);
70        }
71  
72        if(!docWasLoaded){
73          //trigger saving
74          corpus.unloadDocument(doc);
75          //close the previoulsy unloaded Doc
76          Factory.deleteResource(doc);
77        }
78      }
79    }
80  
81    /**
82     * Overidden from {@link SerialController} to only allow
83     * {@link LanguageAnalyser}s as components.
84     */
85    public void add(ProcessingResource pr){
86      if(pr instanceof LanguageAnalyser){
87        super.add(pr);
88      }else{
89        throw new GateRuntimeException(getClass().getName() +
90                                       "only accepts " +
91                                       LanguageAnalyser.class.getName() +
92                                       "s as components\n" +
93                                       pr.getClass().getName() +
94                                       " is not!");
95      }
96    }
97    /**
98     * Sets the current document to the memeber PRs
99     */
100   protected void setDocToPrs(Document doc){
101     Iterator prIter = getPRs().iterator();
102     while(prIter.hasNext()){
103       ((LanguageAnalyser)prIter.next()).setDocument(doc);
104     }
105   }
106 
107 
108   /**
109    * Checks whether all the contained PRs have all the required runtime
110    * parameters set. Ignores the corpus and document parameters as these will
111    * be set at run time.
112    *
113    * @return a {@link List} of {@link ProcessingResource}s that have required
114    * parameters with null values if they exist <tt>null</tt> otherwise.
115    * @throw {@link ResourceInstantiationException} if problems occur while
116    * inspecting the parameters for one of the resources. These will normally be
117    * introspection problems and are usually caused by the lack of a parameter
118    * or of the read accessor for a parameter.
119    */
120   public List getOffendingPocessingResources()
121          throws ResourceInstantiationException{
122     //take all the contained PRs
123     ArrayList badPRs = new ArrayList(getPRs());
124     //remove the ones that no parameters problems
125     Iterator prIter = getPRs().iterator();
126     while(prIter.hasNext()){
127       ProcessingResource pr = (ProcessingResource)prIter.next();
128       ResourceData rData = (ResourceData)Gate.getCreoleRegister().
129                                               get(pr.getClass().getName());
130       //this is a list of lists
131       List parameters = rData.getParameterList().getRuntimeParameters();
132       //remove corpus and document
133       List newParameters = new ArrayList();
134       Iterator pDisjIter = parameters.iterator();
135       while(pDisjIter.hasNext()){
136         List aDisjunction = (List)pDisjIter.next();
137         List newDisjunction = new ArrayList(aDisjunction);
138         Iterator internalParIter = newDisjunction.iterator();
139         while(internalParIter.hasNext()){
140           Parameter parameter = (Parameter)internalParIter.next();
141           if(parameter.getName().equals("corpus") ||
142              parameter.getName().equals("document")) internalParIter.remove();
143         }
144         if(!newDisjunction.isEmpty()) newParameters.add(newDisjunction);
145       }
146 
147       if(AbstractResource.checkParameterValues(pr, newParameters)){
148         badPRs.remove(pr);
149       }
150     }
151     return badPRs.isEmpty() ? null : badPRs;
152   }
153 
154 
155   private gate.Corpus corpus;
156 }