1
14
15 package gate.creole;
16
17 import java.util.*;
18
19 import gate.*;
20 import gate.event.CreoleEvent;
21 import gate.util.*;
22
23
29 public class SerialAnalyserController extends SerialController
30 implements CorpusController{
31
32
33 private static final boolean DEBUG = false;
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
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 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 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 if (DEBUG)
66 Out.pr("SerialAnalyserController processing doc=" + doc.getName()+ "...");
67 super.execute();
68 if (DEBUG)
69 Out.prln("done.");
70
74 for(int j = 0; j < prList.size(); j++){
76 ((LanguageAnalyser)prList.get(j)).setDocument(null);
77 ((LanguageAnalyser)prList.get(j)).setCorpus(null);
78 }
79
80 if(!docWasLoaded){
81 corpus.unloadDocument(doc);
83 Factory.deleteResource(doc);
85 }
86 }
87 }
88
89
93 public void add(ProcessingResource pr){
94 if(pr instanceof LanguageAnalyser){
95 super.add(pr);
96 }else{
97 throw new GateRuntimeException(getClass().getName() +
98 "only accepts " +
99 LanguageAnalyser.class.getName() +
100 "s as components\n" +
101 pr.getClass().getName() +
102 " is not!");
103 }
104 }
105
108 protected void setDocToPrs(Document doc){
109 Iterator prIter = getPRs().iterator();
110 while(prIter.hasNext()){
111 ((LanguageAnalyser)prIter.next()).setDocument(doc);
112 }
113 }
114
115
116
128 public List getOffendingPocessingResources()
129 throws ResourceInstantiationException{
130 ArrayList badPRs = new ArrayList(getPRs());
132 Iterator prIter = getPRs().iterator();
134 while(prIter.hasNext()){
135 ProcessingResource pr = (ProcessingResource)prIter.next();
136 ResourceData rData = (ResourceData)Gate.getCreoleRegister().
137 get(pr.getClass().getName());
138 List parameters = rData.getParameterList().getRuntimeParameters();
140 List newParameters = new ArrayList();
142 Iterator pDisjIter = parameters.iterator();
143 while(pDisjIter.hasNext()){
144 List aDisjunction = (List)pDisjIter.next();
145 List newDisjunction = new ArrayList(aDisjunction);
146 Iterator internalParIter = newDisjunction.iterator();
147 while(internalParIter.hasNext()){
148 Parameter parameter = (Parameter)internalParIter.next();
149 if(parameter.getName().equals("corpus") ||
150 parameter.getName().equals("document")) internalParIter.remove();
151 }
152 if(!newDisjunction.isEmpty()) newParameters.add(newDisjunction);
153 }
154
155 if(AbstractResource.checkParameterValues(pr, newParameters)){
156 badPRs.remove(pr);
157 }
158 }
159 return badPRs.isEmpty() ? null : badPRs;
160 }
161
162
163 private gate.Corpus corpus;
164
165
168 public void resourceUnloaded(CreoleEvent e) {
169 super.resourceUnloaded(e);
170 if(e.getResource() == corpus){
171 setCorpus(null);
172 }
173 }
174 }