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.6 2001/11/29 15:36:37 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        boolean docWasLoaded = corpus.isDocumentLoaded(i);
46        Document doc = (Document)corpus.get(i);
47        //run the system over this document
48  
49        //set the doc and corpus
50        for(int j = 0; j < prList.size(); j++){
51          ((LanguageAnalyser)prList.get(j)).setDocument(doc);
52          ((LanguageAnalyser)prList.get(j)).setCorpus(corpus);
53        }
54  
55        super.execute();
56  
57        corpus.unloadDocument(doc);
58        if(!docWasLoaded) Factory.deleteResource(doc);
59      }
60    }
61  
62    /**
63     * Overidden from {@link SerialController} to only allow
64     * {@link LanguageAnalyser}s as components.
65     */
66    public void add(ProcessingResource pr){
67      if(pr instanceof LanguageAnalyser){
68        super.add(pr);
69      }else{
70        throw new GateRuntimeException(getClass().getName() +
71                                       "only accepts " +
72                                       LanguageAnalyser.class.getName() +
73                                       "s as components\n" +
74                                       pr.getClass().getName() +
75                                       " is not!");
76      }
77    }
78    /**
79     * Sets the current document to the memeber PRs
80     */
81    protected void setDocToPrs(Document doc){
82      Iterator prIter = getPRs().iterator();
83      while(prIter.hasNext()){
84        ((LanguageAnalyser)prIter.next()).setDocument(doc);
85      }
86    }
87  
88  
89    /**
90     * Checks whether all the contained PRs have all the required runtime
91     * parameters set. Ignores the corpus and document parameters as these will
92     * be set at run time.
93     *
94     * @return a {@link List} of {@link ProcessingResource}s that have required
95     * parameters with null values if they exist <tt>null</tt> otherwise.
96     * @throw {@link ResourceInstantiationException} if problems occur while
97     * inspecting the parameters for one of the resources. These will normally be
98     * introspection problems and are usually caused by the lack of a parameter
99     * or of the read accessor for a parameter.
100    */
101   public List getOffendingPocessingResources()
102          throws ResourceInstantiationException{
103     //take all the contained PRs
104     ArrayList badPRs = new ArrayList(getPRs());
105     //remove the ones that no parameters problems
106     Iterator prIter = getPRs().iterator();
107     while(prIter.hasNext()){
108       ProcessingResource pr = (ProcessingResource)prIter.next();
109       ResourceData rData = (ResourceData)Gate.getCreoleRegister().
110                                               get(pr.getClass().getName());
111       //this is a list of lists
112       List parameters = rData.getParameterList().getRuntimeParameters();
113       //remove corpus and document
114       List newParameters = new ArrayList();
115       Iterator pDisjIter = parameters.iterator();
116       while(pDisjIter.hasNext()){
117         List aDisjunction = (List)pDisjIter.next();
118         List newDisjunction = new ArrayList(aDisjunction);
119         Iterator internalParIter = newDisjunction.iterator();
120         while(internalParIter.hasNext()){
121           Parameter parameter = (Parameter)internalParIter.next();
122           if(parameter.getName().equals("corpus") ||
123              parameter.getName().equals("document")) internalParIter.remove();
124         }
125         if(!newDisjunction.isEmpty()) newParameters.add(newDisjunction);
126       }
127 
128       if(AbstractResource.checkParameterValues(pr, newParameters)){
129         badPRs.remove(pr);
130       }
131     }
132     return badPRs.isEmpty() ? null : badPRs;
133   }
134 
135 
136   private gate.Corpus corpus;
137 }