001 /*
002 * AbstractProcessingResource.java
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, 10/Nov/2000
013 *
014 * $Id: AbstractProcessingResource.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015 */
016
017 package gate.creole;
018
019 import java.util.Vector;
020
021 import gate.ProcessingResource;
022 import gate.Resource;
023 import gate.event.ProgressListener;
024 import gate.event.StatusListener;
025
026 /** A convenience implementation of ProcessingResource with some default
027 * code.
028 */
029 abstract public class AbstractProcessingResource
030 extends AbstractResource implements ProcessingResource, ANNIEConstants
031 {
032 /** Initialise this resource, and return it. */
033 public Resource init() throws ResourceInstantiationException {
034 return this;
035 } // init()
036
037 /** Run the resource. It doesn't make sense not to override
038 * this in subclasses so the default implementation signals an
039 * exception.
040 */
041 public void execute() throws ExecutionException{
042 throw new ExecutionException(
043 "Resource " + getClass() + " hasn't overriden the execute() method"
044 );
045 } // execute()
046
047 /**
048 * Reinitialises the processing resource. After calling this method the
049 * resource should be in the state it is after calling init.
050 * If the resource depends on external resources (such as rules files) then
051 * the resource will re-read those resources. If the data used to create
052 * the resource has changed since the resource has been created then the
053 * resource will change too after calling reInit().
054 * The implementation in this class simply calls {@link #init()}. This
055 * functionality must be overriden by derived classes as necessary.
056 */
057 public void reInit() throws ResourceInstantiationException{
058 init();
059 } // reInit()
060
061 /** should clear all internal data of the resource. Does nothing now */
062 public void cleanup() {
063 }
064
065 /**
066 * Checks whether this PR has been interrupted since the last time its
067 * {@link #execute()} method was called.
068 */
069 public synchronized boolean isInterrupted(){
070 return interrupted;
071 }
072
073 /**
074 * Notifies this PR that it should stop its execution as soon as possible.
075 */
076 public synchronized void interrupt(){
077 interrupted = true;
078 }
079
080
081 /**
082 * Removes a {@link gate.event.StatusListener} from the list of listeners for
083 * this processing resource
084 */
085 public synchronized void removeStatusListener(StatusListener l) {
086 if (statusListeners != null && statusListeners.contains(l)) {
087 Vector v = (Vector) statusListeners.clone();
088 v.removeElement(l);
089 statusListeners = v;
090 }
091 }
092
093 /**
094 * Adds a {@link gate.event.StatusListener} to the list of listeners for
095 * this processing resource
096 */
097 public synchronized void addStatusListener(StatusListener l) {
098 Vector v = statusListeners == null ? new Vector(2) : (Vector) statusListeners.clone();
099 if (!v.contains(l)) {
100 v.addElement(l);
101 statusListeners = v;
102 }
103 }
104
105 /**
106 * Notifies all the {@link gate.event.StatusListener}s of a change of status.
107 * @param e the message describing the status change
108 */
109 protected void fireStatusChanged(String e) {
110 if (statusListeners != null) {
111 Vector listeners = statusListeners;
112 int count = listeners.size();
113 for (int i = 0; i < count; i++) {
114 ((StatusListener) listeners.elementAt(i)).statusChanged(e);
115 }
116 }
117 }
118
119 /**
120 * Adds a {@link gate.event.ProgressListener} to the list of listeners for
121 * this processing resource.
122 */
123 public synchronized void addProgressListener(ProgressListener l) {
124 Vector v = progressListeners == null ? new Vector(2) : (Vector) progressListeners.clone();
125 if (!v.contains(l)) {
126 v.addElement(l);
127 progressListeners = v;
128 }
129 }
130
131 /**
132 * Removes a {@link gate.event.ProgressListener} from the list of listeners
133 * for this processing resource.
134 */
135 public synchronized void removeProgressListener(ProgressListener l) {
136 if (progressListeners != null && progressListeners.contains(l)) {
137 Vector v = (Vector) progressListeners.clone();
138 v.removeElement(l);
139 progressListeners = v;
140 }
141 }
142
143 /**
144 * Notifies all the {@link gate.event.ProgressListener}s of a progress change
145 * event.
146 * @param e the new value of execution completion
147 */
148 protected void fireProgressChanged(int e) {
149 if (progressListeners != null) {
150 Vector listeners = progressListeners;
151 int count = listeners.size();
152 for (int i = 0; i < count; i++) {
153 ((ProgressListener) listeners.elementAt(i)).progressChanged(e);
154 }
155 }
156 }
157
158 /**
159 * Notifies all the {@link gate.event.ProgressListener}s of a progress
160 * finished.
161 */
162 protected void fireProcessFinished() {
163 if (progressListeners != null) {
164 Vector listeners = progressListeners;
165 int count = listeners.size();
166 for (int i = 0; i < count; i++) {
167 ((ProgressListener) listeners.elementAt(i)).processFinished();
168 }
169 }
170 }
171
172 /**
173 * A progress listener used to convert a 0..100 interval into a smaller one
174 */
175 protected class IntervalProgressListener implements ProgressListener{
176 public IntervalProgressListener(int start, int end){
177 this.start = start;
178 this.end = end;
179 }
180 public void progressChanged(int i){
181 fireProgressChanged(start + (end - start) * i / 100);
182 }
183
184 public void processFinished(){
185 fireProgressChanged(end);
186 }
187
188 int start;
189 int end;
190 }//IntervalProgressListener
191
192 /**
193 * A simple status listener used to forward the events upstream.
194 */
195 protected class InternalStatusListener implements StatusListener{
196 public void statusChanged(String message){
197 fireStatusChanged(message);
198 }
199 }//InternalStatusListener
200
201 /**
202 * The list of {@link gate.event.StatusListener}s registered with this
203 * resource
204 */
205 private transient Vector statusListeners;
206
207 /**
208 * The list of {@link gate.event.ProgressListener}s registered with this
209 * resource
210 */
211 private transient Vector progressListeners;
212
213 protected boolean interrupted = false;
214 } // class AbstractProcessingResource
|