|
ConditionalSerialAnalyserController |
|
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: ConditionalSerialAnalyserController.java,v 1.2 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 * This is a copy of the {@link SerialAnalyserController}, the only difference 28 * being that it inherits from {@link ConditionalSerialController} rather than 29 * from {@link SerialController} which makes it a <b>conditional</b> serial 30 * analyser controller. 31 */ 32 public class ConditionalSerialAnalyserController 33 extends ConditionalSerialController implements CorpusController { 34 35 public gate.Corpus getCorpus() { 36 return corpus; 37 } 38 39 public void setCorpus(gate.Corpus corpus) { 40 this.corpus = corpus; 41 } 42 43 /** Run the Processing Resources in sequence. */ 44 public void execute() throws ExecutionException{ 45 interrupted = false; 46 if(corpus == null) throw new ExecutionException( 47 "(SerialAnalyserController) \"" + getName() + "\":\n" + 48 "The corpus supplied for execution was null!"); 49 //iterate through the documents in the corpus 50 for(int i = 0; i < corpus.size(); i++){ 51 if(isInterrupted()) throw new ExecutionInterruptedException( 52 "The execution of the " + getName() + 53 " application has been abruptly interrupted!"); 54 55 boolean docWasLoaded = corpus.isDocumentLoaded(i); 56 Document doc = (Document)corpus.get(i); 57 //run the system over this document 58 //set the doc and corpus 59 for(int j = 0; j < prList.size(); j++){ 60 ((LanguageAnalyser)prList.get(j)).setDocument(doc); 61 ((LanguageAnalyser)prList.get(j)).setCorpus(corpus); 62 } 63 64 try{ 65 super.execute(); 66 }catch(Exception e){ 67 e.printStackTrace(Err.getPrintWriter()); 68 } 69 70 //unset the doc and corpus 71 for(int j = 0; j < prList.size(); j++){ 72 ((LanguageAnalyser)prList.get(j)).setDocument(null); 73 ((LanguageAnalyser)prList.get(j)).setCorpus(null); 74 } 75 76 corpus.unloadDocument(doc); 77 if(!docWasLoaded) Factory.deleteResource(doc); 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 }
|
ConditionalSerialAnalyserController |
|