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.11 2002/05/10 19:24:42 kalina 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        corpus.unloadDocument(doc);
73        if(!docWasLoaded) Factory.deleteResource(doc);
74      }
75    }
76  
77    /**
78     * Overidden from {@link SerialController} to only allow
79     * {@link LanguageAnalyser}s as components.
80     */
81    public void add(ProcessingResource pr){
82      if(pr instanceof LanguageAnalyser){
83        super.add(pr);
84      }else{
85        throw new GateRuntimeException(getClass().getName() +
86                                       "only accepts " +
87                                       LanguageAnalyser.class.getName() +
88                                       "s as components\n" +
89                                       pr.getClass().getName() +
90                                       " is not!");
91      }
92    }
93    /**
94     * Sets the current document to the memeber PRs
95     */
96    protected void setDocToPrs(Document doc){
97      Iterator prIter = getPRs().iterator();
98      while(prIter.hasNext()){
99        ((LanguageAnalyser)prIter.next()).setDocument(doc);
100     }
101   }
102 
103 
104   /**
105    * Checks whether all the contained PRs have all the required runtime
106    * parameters set. Ignores the corpus and document parameters as these will
107    * be set at run time.
108    *
109    * @return a {@link List} of {@link ProcessingResource}s that have required
110    * parameters with null values if they exist <tt>null</tt> otherwise.
111    * @throw {@link ResourceInstantiationException} if problems occur while
112    * inspecting the parameters for one of the resources. These will normally be
113    * introspection problems and are usually caused by the lack of a parameter
114    * or of the read accessor for a parameter.
115    */
116   public List getOffendingPocessingResources()
117          throws ResourceInstantiationException{
118     //take all the contained PRs
119     ArrayList badPRs = new ArrayList(getPRs());
120     //remove the ones that no parameters problems
121     Iterator prIter = getPRs().iterator();
122     while(prIter.hasNext()){
123       ProcessingResource pr = (ProcessingResource)prIter.next();
124       ResourceData rData = (ResourceData)Gate.getCreoleRegister().
125                                               get(pr.getClass().getName());
126       //this is a list of lists
127       List parameters = rData.getParameterList().getRuntimeParameters();
128       //remove corpus and document
129       List newParameters = new ArrayList();
130       Iterator pDisjIter = parameters.iterator();
131       while(pDisjIter.hasNext()){
132         List aDisjunction = (List)pDisjIter.next();
133         List newDisjunction = new ArrayList(aDisjunction);
134         Iterator internalParIter = newDisjunction.iterator();
135         while(internalParIter.hasNext()){
136           Parameter parameter = (Parameter)internalParIter.next();
137           if(parameter.getName().equals("corpus") ||
138              parameter.getName().equals("document")) internalParIter.remove();
139         }
140         if(!newDisjunction.isEmpty()) newParameters.add(newDisjunction);
141       }
142 
143       if(AbstractResource.checkParameterValues(pr, newParameters)){
144         badPRs.remove(pr);
145       }
146     }
147     return badPRs.isEmpty() ? null : badPRs;
148   }
149 
150 
151   private gate.Corpus corpus;
152 }