1   /*
2    *  Transducer.java - transducer class
3    *
4    *  Copyright (c) 1998-2001, 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.22 2002/05/14 09:43:17 valyt Exp $
14   */
15  
16  
17  package gate.jape;
18  
19  import java.util.*;
20  import java.net.*;
21  import java.io.*;
22  
23  import gate.annotation.*;
24  import gate.util.*;
25  import gate.event.*;
26  import gate.creole.*;
27  import gate.creole.ontology.Ontology;
28  import gate.*;
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 lsat time its
66     * {@link execute()} 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  private boolean debugMode = false;
111 
112 
113 
114   private URL baseURL;
115 
116   private transient Vector progressListeners;
117   private transient Vector statusListeners;
118   /**
119    * This property affects the Appelt style of rules application.
120    * If true then the longest match will be fired otherwise the shortest will
121    * be used. By default it is true.
122    */
123   protected void fireProgressChanged(int e) {
124     if (progressListeners != null || progressListeners.isEmpty()) {
125       Vector listeners = progressListeners;
126       int count = listeners.size();
127       for (int i = 0; i < count; i++) {
128         ((ProgressListener) listeners.elementAt(i)).progressChanged(e);
129       }
130     }
131   }
132   protected void fireProcessFinished() {
133     if (progressListeners != null) {
134       Vector listeners = progressListeners;
135       int count = listeners.size();
136       for (int i = 0; i < count; i++) {
137         ((ProgressListener) listeners.elementAt(i)).processFinished();
138       }
139     }
140   }
141   public synchronized void removeStatusListener(StatusListener l) {
142     if (statusListeners != null && statusListeners.contains(l)) {
143       Vector v = (Vector) statusListeners.clone();
144       v.removeElement(l);
145       statusListeners = v;
146     }
147   }
148   public synchronized void addStatusListener(StatusListener l) {
149     Vector v = statusListeners == null ? new Vector(2) : (Vector) statusListeners.clone();
150     if (!v.contains(l)) {
151       v.addElement(l);
152       statusListeners = v;
153     }
154   }
155   protected void fireStatusChanged(String e) {
156     if (statusListeners != null) {
157       Vector listeners = statusListeners;
158       int count = listeners.size();
159       for (int i = 0; i < count; i++) {
160         ((StatusListener) listeners.elementAt(i)).statusChanged(e);
161       }
162     }
163   }
164 
165   /**
166    * Gets the ontology used by this transducer;
167    * @return an {@link gate.creole.ontology.Ontology} value;
168    */
169   public Ontology getOntology() {
170     return ontology;
171   }
172 
173   /**
174    * Sets the ontology used by this transducer;
175    * @param ontology an {@link gate.creole.ontology.Ontology} value;
176    */
177   public void setOntology(Ontology ontology) {
178     this.ontology = ontology;
179   }
180 
181   //ProcessProgressReporter implementation ends here
182 
183 } // class Transducer
184 
185 
186 
187