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.9 2002/03/05 14:07:58 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  
30    public gate.Corpus getCorpus() {
31      return corpus;
32    }
33  
34    public void setCorpus(gate.Corpus corpus) {
35      this.corpus = corpus;
36    }
37  
38    /** Run the Processing Resources in sequence. */
39    public void execute() throws ExecutionException{
40      if(corpus == null) throw new ExecutionException(
41        "(SerialAnalyserController) \"" + getName() + "\":\n" +
42        "The corpus supplied for execution was null!");
43      //iterate through the documents in the corpus
44      for(int i = 0; i < corpus.size(); i++){
45        if(isInterrupted()) throw new ExecutionInterruptedException(
46          "The execution of the " + getName() +
47          " application has been abruptly interrupted!");
48  
49        boolean docWasLoaded = corpus.isDocumentLoaded(i);
50        Document doc = (Document)corpus.get(i);
51        //run the system over this document
52        //set the doc and corpus
53        for(int j = 0; j < prList.size(); j++){
54          ((LanguageAnalyser)prList.get(j)).setDocument(doc);
55          ((LanguageAnalyser)prList.get(j)).setCorpus(corpus);
56        }
57  
58        try{
59          super.execute();
60        }catch(Exception e){
61          e.printStackTrace(Err.getPrintWriter());
62        }
63  
64        //unset the doc and corpus
65        for(int j = 0; j < prList.size(); j++){
66          ((LanguageAnalyser)prList.get(j)).setDocument(null);
67          ((LanguageAnalyser)prList.get(j)).setCorpus(null);
68        }
69  
70        corpus.unloadDocument(doc);
71        if(!docWasLoaded) Factory.deleteResource(doc);
72      }
73    }
74  
75    /**
76     * Overidden from {@link SerialController} to only allow
77     * {@link LanguageAnalyser}s as components.
78     */
79    public void add(ProcessingResource pr){
80      if(pr instanceof LanguageAnalyser){
81        super.add(pr);
82      }else{
83        throw new GateRuntimeException(getClass().getName() +
84                                       "only accepts " +
85                                       LanguageAnalyser.class.getName() +
86                                       "s as components\n" +
87                                       pr.getClass().getName() +
88                                       " is not!");
89      }
90    }
91    /**
92     * Sets the current document to the memeber PRs
93     */
94    protected void setDocToPrs(Document doc){
95      Iterator prIter = getPRs().iterator();
96      while(prIter.hasNext()){
97        ((LanguageAnalyser)prIter.next()).setDocument(doc);
98      }
99    }
100 
101 
102   /**
103    * Checks whether all the contained PRs have all the required runtime
104    * parameters set. Ignores the corpus and document parameters as these will
105    * be set at run time.
106    *
107    * @return a {@link List} of {@link ProcessingResource}s that have required
108    * parameters with null values if they exist <tt>null</tt> otherwise.
109    * @throw {@link ResourceInstantiationException} if problems occur while
110    * inspecting the parameters for one of the resources. These will normally be
111    * introspection problems and are usually caused by the lack of a parameter
112    * or of the read accessor for a parameter.
113    */
114   public List getOffendingPocessingResources()
115          throws ResourceInstantiationException{
116     //take all the contained PRs
117     ArrayList badPRs = new ArrayList(getPRs());
118     //remove the ones that no parameters problems
119     Iterator prIter = getPRs().iterator();
120     while(prIter.hasNext()){
121       ProcessingResource pr = (ProcessingResource)prIter.next();
122       ResourceData rData = (ResourceData)Gate.getCreoleRegister().
123                                               get(pr.getClass().getName());
124       //this is a list of lists
125       List parameters = rData.getParameterList().getRuntimeParameters();
126       //remove corpus and document
127       List newParameters = new ArrayList();
128       Iterator pDisjIter = parameters.iterator();
129       while(pDisjIter.hasNext()){
130         List aDisjunction = (List)pDisjIter.next();
131         List newDisjunction = new ArrayList(aDisjunction);
132         Iterator internalParIter = newDisjunction.iterator();
133         while(internalParIter.hasNext()){
134           Parameter parameter = (Parameter)internalParIter.next();
135           if(parameter.getName().equals("corpus") ||
136              parameter.getName().equals("document")) internalParIter.remove();
137         }
138         if(!newDisjunction.isEmpty()) newParameters.add(newDisjunction);
139       }
140 
141       if(AbstractResource.checkParameterValues(pr, newParameters)){
142         badPRs.remove(pr);
143       }
144     }
145     return badPRs.isEmpty() ? null : badPRs;
146   }
147 
148 
149   private gate.Corpus corpus;
150 }