1
13
14 package gate.creole;
15
16 import java.util.*;
17
18 import gate.*;
19 import gate.event.ControllerEvent;
20 import gate.util.Err;
21
22
30 public class ConditionalSerialController extends SerialController
31 implements ConditionalController{
32
33 public ConditionalSerialController(){
34 strategiesList = new ArrayList();
35 }
36
37 public Collection getRunningStrategies(){
38 return Collections.unmodifiableList(strategiesList);
39 }
40
41
47 public void add(int index, ProcessingResource pr){
48 if(pr instanceof LanguageAnalyser){
49 strategiesList.add(index,
50 new AnalyserRunningStrategy((LanguageAnalyser)pr,
51 RunningStrategy.RUN_ALWAYS,
52 null, null));
53 }else{
54 strategiesList.add(index, new RunningStrategy.RunAlwaysStrategy(pr));
55 }
56 super.add(index, pr);
57 }
58
59
63 public void add(ProcessingResource pr){
64 if(pr instanceof LanguageAnalyser){
65 strategiesList.add(new AnalyserRunningStrategy((LanguageAnalyser)pr,
66 RunningStrategy.RUN_ALWAYS,
67 null, null));
68 }else{
69 strategiesList.add(new RunningStrategy.RunAlwaysStrategy(pr));
70 }
71 super.add(pr);
72 }
73
74 public ProcessingResource remove(int index){
75 ProcessingResource aPr = super.remove (index);
76 strategiesList.remove(index);
77 fireResourceRemoved(new ControllerEvent(this,
78 ControllerEvent.RESOURCE_REMOVED, aPr));
79 return aPr;
80 }
81
82 public boolean remove(ProcessingResource pr){
83 int index = prList.indexOf(pr);
84 if(index != -1){
85 prList.remove(index);
86 strategiesList.remove(index);
87 fireResourceRemoved(new ControllerEvent(this,
88 ControllerEvent.RESOURCE_REMOVED, pr));
89 return true;
90 }
91 return false;
92 }
93
94 public void setRunningStrategy(int index, AnalyserRunningStrategy strategy){
95 strategiesList.set(index, strategy);
96 }
97
98
108 public void setRunningStrategies(Collection strategies){
109 strategiesList.clear();
110 Iterator stratIter = strategies.iterator();
111 while(stratIter.hasNext()) strategiesList.add(stratIter.next());
112 }
113
114
117 protected void runComponent(int componentIndex) throws ExecutionException{
118 ProcessingResource currentPR = (ProcessingResource)
119 prList.get(componentIndex);
120
121 FeatureMap listeners = Factory.newFeatureMap();
123 listeners.put("gate.event.StatusListener", sListener);
124 int componentProgress = 100 / prList.size();
125 listeners.put("gate.event.ProgressListener",
126 new IntervalProgressListener(
127 componentIndex * componentProgress,
128 (componentIndex +1) * componentProgress)
129 );
130
131 try{
133 AbstractResource.setResourceListeners(currentPR, listeners);
134 }catch(Exception e){
135 Err.prln("Could not set listeners for " + currentPR.getClass().getName() +
137 "\n" + e.toString() + "\n...nothing to lose any sleep over.");
138 }
139
140
141 if(((RunningStrategy)strategiesList.get(componentIndex)).shouldRun()){
143 currentPR.execute();
144 }
145
146
147 try{
149 AbstractResource.removeResourceListeners(currentPR, listeners);
150 }catch(Exception e){
151 Err.prln("Could not clear listeners for " +
153 currentPR.getClass().getName() +
154 "\n" + e.toString() + "\n...nothing to lose any sleep over.");
155 }
156 }
158
161 public void cleanup(){
162 super.cleanup();
163 strategiesList.clear();
164 }
165
166
167
170 protected List strategiesList;
171 }