Transducer.java
001 /*
002  *  Transducer.java - transducer class
003  *
004  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
005  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006  *
007  *  This file is part of GATE (see http://gate.ac.uk/), and is free
008  *  software, licenced under the GNU Library General Public License,
009  *  Version 2, June 1991 (in the distribution as file licence.html,
010  *  and also available at http://gate.ac.uk/gate/licence.html).
011  *
012  *  Hamish Cunningham, 24/07/98
013  *
014  *  $Id: Transducer.java 13280 2010-12-08 15:09:18Z markagreenwood $
015  */
016 
017 
018 package gate.jape;
019 
020 import gate.Controller;
021 import java.io.Serializable;
022 import java.net.URL;
023 import java.util.HashMap;
024 import java.util.Map;
025 import java.util.Vector;
026 
027 import gate.AnnotationSet;
028 import gate.Document;
029 import gate.creole.ExecutionException;
030 import gate.creole.ontology.Ontology;
031 import gate.event.ProgressListener;
032 import gate.event.StatusListener;
033 import gate.util.Benchmarkable;
034 
035 
036 /**
037   * Represents a single or multiphase transducer.
038   */
039 public abstract class Transducer implements Serializable, Benchmarkable
040 {
041   /** Debug flag */
042   private static final boolean DEBUG = false;
043 
044   /** Name of this transducer. */
045   protected String name;
046 
047   protected Ontology ontology = null;
048   
049   /**
050    * Shared featureMap
051    */
052   protected Map benchmarkFeatures = new HashMap();
053 
054   /**
055    * Benchmark ID of this transducer.
056    */
057   protected String benchmarkID;
058 
059   /** Get the phase name of this transducer */
060   public String getName() { return name; }
061 
062   /**
063    * Gets the benchmark ID of this transducer.
064    */
065   public String getBenchmarkId() {
066     if(benchmarkID == null) {
067       return getName();
068     }
069     else {
070       return benchmarkID;
071     }
072   }
073 
074   /**
075    * Set the benchmark ID for this transducer.
076    */
077   public void setBenchmarkId(String benchmarkId) {
078     this.benchmarkID = benchmarkId;
079   }
080 
081   /** Transduce a document.  */
082   public abstract void transduce(Document doc, AnnotationSet inputAS,
083                                  AnnotationSet outputAS)
084                                  throws JapeException, ExecutionException;
085 
086   /** Finish: replace dynamic data structures with Java arrays; called
087     * after parsing.
088     */
089   public abstract void finish();
090 
091   /** Clean up (delete action class files, for e.g.). */
092   public abstract void cleanUp();
093 
094   /** Create a string representation of the object with padding. */
095   public abstract String toString(String pad);
096 
097 
098   /**
099    * Checks whether this PR has been interrupted since the last time its
100    {@link #transduce(Document, AnnotationSet, AnnotationSet)} method was called.
101    */
102   public synchronized boolean isInterrupted(){
103     return interrupted;
104   }
105 
106   /**
107    * Notifies this PR that it should stop its execution as soon as possible.
108    */
109   public synchronized void interrupt(){
110     interrupted = true;
111   }
112 
113   protected boolean interrupted = false;
114 
115 
116   public void setBaseURL(java.net.URL newBaseURL) {
117     baseURL = newBaseURL;
118   }
119   public java.net.URL getBaseURL() {
120     return baseURL;
121   }
122   public synchronized void removeProgressListener(ProgressListener l) {
123     if (progressListeners != null && progressListeners.contains(l)) {
124       Vector v = (VectorprogressListeners.clone();
125       v.removeElement(l);
126       progressListeners = v;
127     }
128   }
129   public synchronized void addProgressListener(ProgressListener l) {
130     Vector v = progressListeners == null new Vector(2(VectorprogressListeners.clone();
131     if (!v.contains(l)) {
132       v.addElement(l);
133       progressListeners = v;
134     }
135   }
136 
137   public void setDebugMode(boolean debugMode) {
138     this.debugMode = debugMode;
139   }
140   public boolean isDebugMode() {
141     return debugMode;
142   }
143 
144   /**
145    * Switch used to enable printing debug messages
146    */
147   private boolean debugMode = false;
148 
149   public void setMatchGroupMode(boolean mode) {
150     matchGroupMode = mode;
151   }
152   
153   public boolean isMatchGroupMode() {
154     return matchGroupMode;
155   }
156 
157   /** Switch used to enable multiple LHS matching  in case of pattern coverage
158    * over one and same span with different annotation groups */
159   private boolean matchGroupMode = false;
160 
161   private URL baseURL;
162 
163   private transient Vector progressListeners;
164   private transient Vector statusListeners;
165 
166   /**
167    * Switch used to activate the JAPE debugger
168    */
169   protected boolean enableDebugging;
170 
171   /**
172    * This property affects the Appelt style of rules application.
173    * If true then the longest match will be fired otherwise the shortest will
174    * be used. By default it is true.
175    */
176   protected void fireProgressChanged(int e) {
177     if (progressListeners != null  && !progressListeners.isEmpty()) {
178       Vector listeners = progressListeners;
179       int count = listeners.size();
180       for (int i = 0; i < count; i++) {
181         ((ProgressListenerlisteners.elementAt(i)).progressChanged(e);
182       }
183     }
184   }
185   protected void fireProcessFinished() {
186     if (progressListeners != null) {
187       Vector listeners = progressListeners;
188       int count = listeners.size();
189       for (int i = 0; i < count; i++) {
190         ((ProgressListenerlisteners.elementAt(i)).processFinished();
191       }
192     }
193   }
194   public synchronized void removeStatusListener(StatusListener l) {
195     if (statusListeners != null && statusListeners.contains(l)) {
196       Vector v = (VectorstatusListeners.clone();
197       v.removeElement(l);
198       statusListeners = v;
199     }
200   }
201   public synchronized void addStatusListener(StatusListener l) {
202     Vector v = statusListeners == null new Vector(2(VectorstatusListeners.clone();
203     if (!v.contains(l)) {
204       v.addElement(l);
205       statusListeners = v;
206     }
207   }
208   protected void fireStatusChanged(String e) {
209     if (statusListeners != null) {
210       Vector listeners = statusListeners;
211       int count = listeners.size();
212       for (int i = 0; i < count; i++) {
213         ((StatusListenerlisteners.elementAt(i)).statusChanged(e);
214       }
215     }
216   }
217 
218   /**
219    * Gets the ontology used by this transducer;
220    @return an {@link gate.creole.ontology.Ontology} value;
221    */
222   public Ontology getOntology() {
223     return ontology;
224   }
225 
226   /**
227    * Sets the ontology used by this transducer;
228    @param ontology an {@link gate.creole.ontology.Ontology} value;
229    */
230   public void setOntology(Ontology ontology) {
231     this.ontology = ontology;
232   }
233 
234   public boolean isEnableDebugging() {
235     return enableDebugging;
236   }
237 
238   public void setEnableDebugging(boolean enableDebugging) {
239     this.enableDebugging = enableDebugging;
240   }
241 
242   //ProcessProgressReporter implementation ends here
243   
244   protected ActionContext actionContext;
245   public void setActionContext(ActionContext ac) {
246     actionContext = ac;
247   }
248 
249   void runControllerExecutionStartedBlock(ActionContext ac, Controller c, Ontology othrows ExecutionException { }
250   void runControllerExecutionFinishedBlock(ActionContext ac, Controller c, Ontology othrows ExecutionException { }
251   void runControllerExecutionAbortedBlock(ActionContext ac, Controller c, Throwable t, Ontology othrows ExecutionException { }
252  
253 
254 
255 // class Transducer
256 
257