1
15
16 package gate.creole;
17
18 import java.util.*;
19
20 import gate.*;
21 import gate.event.*;
22 import gate.util.Err;
23 import gate.util.profile.Profiler;
24
25
27 public class SerialController extends AbstractController
28 implements CreoleListener{
29 private final static boolean DEBUG = false;
30
31
32 protected Profiler prof;
33
34 public SerialController(){
35 prList = Collections.synchronizedList(new ArrayList());
36 sListener = new InternalStatusListener();
37
38 if(DEBUG) {
39 prof = new Profiler();
40 prof.enableGCCalling(false);
41 prof.printToSystemOut(true);
42 }
43 }
44
45
49 public Collection getPRs(){
50 return Collections.unmodifiableList(prList);
51 }
52
53
62 public void setPRs(Collection prs){
63 prList.clear();
64 Iterator prIter = prs.iterator();
65 while(prIter.hasNext()) add((ProcessingResource)prIter.next());
66 }
67
68 public void add(int index, ProcessingResource pr){
69 prList.add(index, pr);
70 fireResourceAdded(new ControllerEvent(this,
71 ControllerEvent.RESOURCE_ADDED, pr));
72 }
73
74 public void add(ProcessingResource pr){
75 prList.add(pr);
76 fireResourceAdded(new ControllerEvent(this,
77 ControllerEvent.RESOURCE_ADDED, pr));
78 }
79
80 public ProcessingResource remove(int index){
81 ProcessingResource aPr = (ProcessingResource)prList.remove(index);
82 fireResourceRemoved(new ControllerEvent(this,
83 ControllerEvent.RESOURCE_REMOVED, aPr));
84 return aPr;
85 }
86
87 public boolean remove(ProcessingResource pr){
88 boolean ret = prList.remove(pr);
89 if(ret) fireResourceRemoved(new ControllerEvent(this,
90 ControllerEvent.RESOURCE_REMOVED, pr));
91 return ret;
92 }
93
94 public ProcessingResource set(int index, ProcessingResource pr){
95 return (ProcessingResource)prList.set(index, pr);
96 }
97
98
101 protected void checkParameters() throws ExecutionException{
102 List badPRs;
103 try{
104 badPRs = getOffendingPocessingResources();
105 }catch(ResourceInstantiationException rie){
106 throw new ExecutionException(
107 "Could not check runtime parameters for the processing resources:\n" +
108 rie.toString());
109 }
110 if(badPRs != null && !badPRs.isEmpty()){
111 throw new ExecutionException(
112 "Some of the processing resources in this controller have unset " +
113 "runtime parameters:\n" +
114 badPRs.toString());
115 }
116 }
117
118
119 public void execute() throws ExecutionException{
120 checkParameters();
122
123 if(DEBUG) {
124 prof.initRun("Execute controller [" + getName() + "]");
125 }
126
127 interrupted = false;
129 for (int i = 0; i < prList.size(); i++){
130 if(isInterrupted()) throw new ExecutionInterruptedException(
131 "The execution of the " + getName() +
132 " application has been abruptly interrupted!");
133
134 runComponent(i);
135 if (DEBUG) {
136 prof.checkPoint("~Execute PR ["+((ProcessingResource)
137 prList.get(i)).getName()+"]");
138 }
139 }
140
141 if (DEBUG) {
142 prof.checkPoint("Execute controller [" + getName() + "] finished");
143 }
144
145 }
147
148
151 protected void runComponent(int componentIndex) throws ExecutionException{
152 ProcessingResource currentPR = (ProcessingResource)
153 prList.get(componentIndex);
154
155 FeatureMap listeners = Factory.newFeatureMap();
157 listeners.put("gate.event.StatusListener", sListener);
158 int componentProgress = 100 / prList.size();
159 listeners.put("gate.event.ProgressListener",
160 new IntervalProgressListener(
161 componentIndex * componentProgress,
162 (componentIndex +1) * componentProgress)
163 );
164
165 try{
167 AbstractResource.setResourceListeners(currentPR, listeners);
168 }catch(Exception e){
169 Err.prln("Could not set listeners for " + currentPR.getClass().getName() +
171 "\n" + e.toString() + "\n...nothing to lose any sleep over.");
172 }
173
174 currentPR.execute();
176
177 try{
179 AbstractResource.removeResourceListeners(currentPR, listeners);
180 }catch(Exception e){
181 Err.prln("Could not clear listeners for " +
183 currentPR.getClass().getName() +
184 "\n" + e.toString() + "\n...nothing to lose any sleep over.");
185 }
186 }
188
191 public void cleanup(){
192 Resource res;
194 for(int i=0; i<prList.size(); ++i) {
195 res = (Resource) prList.get(i);
196 Factory.deleteResource(res);
197 } prList.clear();
199 }
200
201
202 protected List prList;
203
204
205 protected StatusListener sListener;
206 public void resourceLoaded(CreoleEvent e) {
207 }
208 public void resourceUnloaded(CreoleEvent e) {
209 if(e.getResource() instanceof ProcessingResource)
211 while(prList.remove(e.getResource()));
212 }
213
214 public void resourceRenamed(Resource resource, String oldName,
215 String newName){
216 }
217
218 public void datastoreOpened(CreoleEvent e) {
219 }
220 public void datastoreCreated(CreoleEvent e) {
221 }
222 public void datastoreClosed(CreoleEvent e) {
223 }
224
225 }