|
ConditionalSerialController |
|
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 11 Apr 2002 10 * 11 * $Id: ConditionalSerialController.java,v 1.2 2002/04/24 09:44:49 valyt Exp $ 12 */ 13 14 package gate.creole; 15 16 import java.util.*; 17 import java.io.*; 18 19 import gate.*; 20 import gate.util.*; 21 import gate.creole.*; 22 import gate.event.*; 23 24 /** 25 * Execute a list of PRs serially. For each PR a running strategy is stored 26 * which decides whether the PR will be run always, never or upon a condition 27 * being satisfied. 28 * This controller uses {@link AnalyserRunningStrategy} objects as running 29 * strategies and they only work with {@link LanguageAnalyser}s so the PRs that 30 * are not analysers will get a default "run always" strategy. 31 */ 32 public class ConditionalSerialController extends SerialController 33 implements ConditionalController{ 34 35 public ConditionalSerialController(){ 36 strategiesList = new ArrayList(); 37 } 38 39 public Collection getRunningStrategies(){ 40 return Collections.unmodifiableList(strategiesList); 41 } 42 43 /** 44 * Set a PR at a specified location. 45 * The running strategy defaults to run always. 46 * @param index the position for the PR 47 * @param pr the PR to be set. 48 */ 49 public void add(int index, ProcessingResource pr){ 50 super.add(index, pr); 51 if(pr instanceof LanguageAnalyser){ 52 strategiesList.add(index, 53 new AnalyserRunningStrategy((LanguageAnalyser)pr, 54 RunningStrategy.RUN_ALWAYS, 55 null, null)); 56 }else{ 57 strategiesList.add(index, new RunningStrategy.RunAlwaysStrategy(pr)); 58 } 59 } 60 61 /** 62 * Add a PR to the end of the execution list. 63 * @param pr the PR to be added. 64 */ 65 public void add(ProcessingResource pr){ 66 super.add(pr); 67 if(pr instanceof LanguageAnalyser){ 68 strategiesList.add(new AnalyserRunningStrategy((LanguageAnalyser)pr, 69 RunningStrategy.RUN_ALWAYS, 70 null, null)); 71 }else{ 72 strategiesList.add(new RunningStrategy.RunAlwaysStrategy(pr)); 73 } 74 } 75 76 public ProcessingResource remove(int index){ 77 ProcessingResource aPr = super.remove (index); 78 strategiesList.remove(index); 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 return true; 88 } 89 return false; 90 } 91 92 public void setRunningStrategy(int index, AnalyserRunningStrategy strategy){ 93 strategiesList.set(index, strategy); 94 } 95 96 /** 97 * Populates this controller with the appropiate running strategies from a 98 * collection of running strategies 99 * (optional operation). 100 * 101 * Controllers that are serializable must implement this method needed by GATE 102 * to restore their contents. 103 * @throws UnsupportedOperationException if the <tt>setPRs</tt> method 104 * is not supported by this controller. 105 */ 106 public void setRunningStrategies(Collection strategies){ 107 strategiesList.clear(); 108 Iterator stratIter = strategies.iterator(); 109 while(stratIter.hasNext()) strategiesList.add(stratIter.next()); 110 } 111 112 /** 113 * Executes a {@link ProcessingResource}. 114 */ 115 protected void runComponent(int componentIndex) throws ExecutionException{ 116 ProcessingResource currentPR = (ProcessingResource) 117 prList.get(componentIndex); 118 119 //create the listeners 120 FeatureMap listeners = Factory.newFeatureMap(); 121 listeners.put("gate.event.StatusListener", sListener); 122 int componentProgress = 100 / prList.size(); 123 listeners.put("gate.event.ProgressListener", 124 new IntervalProgressListener( 125 componentIndex * componentProgress, 126 (componentIndex +1) * componentProgress) 127 ); 128 129 //add the listeners 130 try{ 131 AbstractResource.setResourceListeners(currentPR, listeners); 132 }catch(Exception e){ 133 // the listeners setting failed; nothing important 134 Err.prln("Could not set listeners for " + currentPR.getClass().getName() + 135 "\n" + e.toString() + "\n...nothing to lose any sleep over."); 136 } 137 138 139 //run the thing 140 if(((RunningStrategy)strategiesList.get(componentIndex)).shouldRun()){ 141 currentPR.execute(); 142 } 143 144 145 //remove the listeners 146 try{ 147 AbstractResource.removeResourceListeners(currentPR, listeners); 148 }catch(Exception e){ 149 // the listeners removing failed; nothing important 150 Err.prln("Could not clear listeners for " + 151 currentPR.getClass().getName() + 152 "\n" + e.toString() + "\n...nothing to lose any sleep over."); 153 } 154 }//protected void runComponent(int componentIndex) 155 156 /** 157 * Cleans the internal data and prepares this object to be collected 158 */ 159 public void cleanup(){ 160 super.cleanup(); 161 strategiesList.clear(); 162 } 163 164 165 /** 166 * The list of running strategies for the member PRs. 167 */ 168 protected List strategiesList; 169 } // class SerialController 170
|
ConditionalSerialController |
|