1   /*
2    *  Transducer.java - transducer class
3    *
4    *  Copyright (c) 1998-2005, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Hamish Cunningham, 24/07/98
12   *
13   *  $Id: Transducer.java,v 1.30 2005/03/30 16:03:23 julien Exp $
14   */
15  
16  
17  package gate.jape;
18  
19  import java.io.Serializable;
20  import java.net.URL;
21  import java.util.Vector;
22  
23  import gate.AnnotationSet;
24  import gate.Document;
25  import gate.creole.ExecutionException;
26  import gate.creole.ontology.Ontology;
27  import gate.event.ProgressListener;
28  import gate.event.StatusListener;
29  
30  
31  /**
32    * Represents a single or multiphase transducer.
33    */
34  public abstract class Transducer implements Serializable
35  {
36    /** Debug flag */
37    private static final boolean DEBUG = false;
38  
39    /** Name of this transducer. */
40    protected String name;
41  
42    protected Ontology ontology = null;
43  
44    /** Get the phase name of this transducer */
45    public String getName() { return name; }
46  
47    /** Transduce a document.  */
48    public abstract void transduce(Document doc, AnnotationSet inputAS,
49                                   AnnotationSet outputAS)
50                                   throws JapeException, ExecutionException;
51  
52    /** Finish: replace dynamic data structures with Java arrays; called
53      * after parsing.
54      */
55    public abstract void finish();
56  
57    /** Clean up (delete action class files, for e.g.). */
58    public abstract void cleanUp();
59  
60    /** Create a string representation of the object with padding. */
61    public abstract String toString(String pad);
62  
63  
64    /**
65     * Checks whether this PR has been interrupted since the last time its
66     * {@link #transduce(Document, AnnotationSet, AnnotationSet)} method was called.
67     */
68    public synchronized boolean isInterrupted(){
69      return interrupted;
70    }
71  
72    /**
73     * Notifies this PR that it should stop its execution as soon as possible.
74     */
75    public synchronized void interrupt(){
76      interrupted = true;
77    }
78  
79    protected boolean interrupted = false;
80  
81  
82    public void setBaseURL(java.net.URL newBaseURL) {
83      baseURL = newBaseURL;
84    }
85    public java.net.URL getBaseURL() {
86      return baseURL;
87    }
88    public synchronized void removeProgressListener(ProgressListener l) {
89      if (progressListeners != null && progressListeners.contains(l)) {
90        Vector v = (Vector) progressListeners.clone();
91        v.removeElement(l);
92        progressListeners = v;
93      }
94    }
95    public synchronized void addProgressListener(ProgressListener l) {
96      Vector v = progressListeners == null ? new Vector(2) : (Vector) progressListeners.clone();
97      if (!v.contains(l)) {
98        v.addElement(l);
99        progressListeners = v;
100     }
101   }
102 
103   public void setDebugMode(boolean debugMode) {
104     this.debugMode = debugMode;
105   }
106   public boolean isDebugMode() {
107     return debugMode;
108   }
109 
110   /**
111    * Switch used to enable printing debug messages
112    */
113   private boolean debugMode = false;
114 
115   public void setMatchGroupMode(boolean mode) {
116     matchGroupMode = mode;
117   }
118   
119   public boolean isMatchGroupMode() {
120     return matchGroupMode;
121   }
122 
123   /** Switch used to enable multiple LHS matching  in case of pattern coverage
124    * over one and same span with different annotation groups */
125   private boolean matchGroupMode = false;
126 
127   private URL baseURL;
128 
129   private transient Vector progressListeners;
130   private transient Vector statusListeners;
131 
132   /**
133    * Switch used to activate the JAPE debugger
134    */
135   protected boolean enableDebugging;
136 
137   /**
138    * This property affects the Appelt style of rules application.
139    * If true then the longest match will be fired otherwise the shortest will
140    * be used. By default it is true.
141    */
142   protected void fireProgressChanged(int e) {
143     if (progressListeners != null  && !progressListeners.isEmpty()) {
144       Vector listeners = progressListeners;
145       int count = listeners.size();
146       for (int i = 0; i < count; i++) {
147         ((ProgressListener) listeners.elementAt(i)).progressChanged(e);
148       }
149     }
150   }
151   protected void fireProcessFinished() {
152     if (progressListeners != null) {
153       Vector listeners = progressListeners;
154       int count = listeners.size();
155       for (int i = 0; i < count; i++) {
156         ((ProgressListener) listeners.elementAt(i)).processFinished();
157       }
158     }
159   }
160   public synchronized void removeStatusListener(StatusListener l) {
161     if (statusListeners != null && statusListeners.contains(l)) {
162       Vector v = (Vector) statusListeners.clone();
163       v.removeElement(l);
164       statusListeners = v;
165     }
166   }
167   public synchronized void addStatusListener(StatusListener l) {
168     Vector v = statusListeners == null ? new Vector(2) : (Vector) statusListeners.clone();
169     if (!v.contains(l)) {
170       v.addElement(l);
171       statusListeners = v;
172     }
173   }
174   protected void fireStatusChanged(String e) {
175     if (statusListeners != null) {
176       Vector listeners = statusListeners;
177       int count = listeners.size();
178       for (int i = 0; i < count; i++) {
179         ((StatusListener) listeners.elementAt(i)).statusChanged(e);
180       }
181     }
182   }
183 
184   /**
185    * Gets the ontology used by this transducer;
186    * @return an {@link gate.creole.ontology.Ontology} value;
187    */
188   public Ontology getOntology() {
189     return ontology;
190   }
191 
192   /**
193    * Sets the ontology used by this transducer;
194    * @param ontology an {@link gate.creole.ontology.Ontology} value;
195    */
196   public void setOntology(Ontology ontology) {
197     this.ontology = ontology;
198   }
199 
200   public boolean isEnableDebugging() {
201     return enableDebugging;
202   }
203 
204   public void setEnableDebugging(boolean enableDebugging) {
205     this.enableDebugging = enableDebugging;
206   }
207 
208   //ProcessProgressReporter implementation ends here
209 
210 } // class Transducer
211 
212 
213 
214