Rule.java
001 /*
002  *  Rule.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: Rule.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015  */
016 
017 
018 package gate.jape;
019 
020 import gate.AnnotationSet;
021 import gate.Document;
022 import gate.event.ProgressListener;
023 import gate.event.StatusListener;
024 import gate.util.Out;
025 import gate.util.Strings;
026 
027 /**
028   * A CPSL rule. Has an LHS, RHS and a name, and a priority.
029   */
030 public class Rule extends Transducer
031 implements JapeConstants, java.io.Serializable
032 {
033   /** Debug flag */
034   private static final boolean DEBUG = false;
035 
036   /** Construction */
037   public Rule(
038     String name, int position, int priority,
039     LeftHandSide lhs, RightHandSide rhs
040   ) {
041     this.name = name;
042     this.position = position;
043     this.priority = priority;
044     this.lhs = lhs;
045     this.rhs = rhs;
046   // Construction
047 
048   /** The LHS or pattern of the rule. */
049   private LeftHandSide lhs;
050 
051   /** The RHS or action of the rule. */
052   private RightHandSide rhs;
053 
054   /** The priority of the rule. */
055   private int priority;
056 
057   /** Get the rule priority. */
058   public int getPriority() { return priority; }
059 
060   /** The rule's position in sequence (e.g. order in file). */
061   private int position;
062 
063   /** Get the rule's position in sequence (e.g. order in file). */
064   public int getPosition() { return position; }
065 
066   /** If we're pending (have matched), get the position we want to fire in,
067     * else -1.
068     */
069   public int pending() {
070     return pendingPosition;
071   // pending
072 
073   /** If we matched but didn't fire yet, this is our pending position. */
074   private int pendingPosition = -1;
075 
076   /** Flag for end of document during getNextMatch. */
077   private boolean weFinished = false;
078 
079   /** Have we hit the end of the document without matching? */
080   public boolean finished() {
081     return weFinished;
082   // finished
083 
084   /** Finish: replace dynamic data structures with Java arrays; called
085     * after parsing. WARNING:
086     * bad choice of names: this is not related to the weFinished
087     * member or the finished method!
088     */
089   public void finish() {
090     lhs.finish();
091   // finish
092 
093 
094 
095 
096   /** Apply the RHS of this rule (LHS must have been matched first). */
097   public void transduce(Document doc, AnnotationSet inputAS,
098                         AnnotationSet outputASthrows JapeException {
099     // the righthand side does the transduction, using bindings from lhs */
100     if(DEBUGOut.println("applying rule " + name);
101 //    rhs.transduce(doc);
102     /*Debug.pr(
103       this, "Rule.transduce: annotations after transduction: " +
104       doc.selectAnnotations("Name", new FeatureMap()).toString() +
105       Debug.getNl()
106     );*/
107 
108     // clear the caches of matched annotations in the LHS
109 //    reset();
110     //Debug.pr(this, "LHS after reset: " + lhs.toString());
111 
112   // transduce
113 
114 
115   /** For debugging. */
116   // public String getName() { return name; }
117 
118   /** Clean up (delete action class files, for e.g.). */
119   public void cleanUp() {
120     RightHandSide.cleanUp();
121   // cleanUp
122 
123 
124   /** Create a string representation of the object. */
125   public String toString() { return toString("")}
126 
127   /** Create a string representation of the object. */
128   public String toString(String pad) {
129     String newline = Strings.getNl();
130     String newPad = Strings.addPadding(pad, INDENT_PADDING);
131 
132     StringBuffer buf = new StringBuffer(
133       pad + "Rule: name(" + name + "); position(" + position + "); priority(" +
134       priority + "); pendingPosition(" + pendingPosition + "); " +
135       "weFinished(" + weFinished + "); lhs(" + newline +
136       lhs.toString(newPad+ newline + pad + "); rhs(" + newline +
137       rhs.toString(newPad+ newline + pad + ");"
138     );
139 
140     buf.append(newline + pad + ") Rule." + newline);
141 
142     return buf.toString();
143   // toString
144 
145   //needed by FSM
146   public LeftHandSide getLHS(){
147     return lhs;
148   }
149   public RightHandSide getRHS(){
150     return rhs;
151   }
152 
153   //StatusReporter VOID Implementation
154   public void addStatusListener(StatusListener listener){}
155   public void removeStatusListener(StatusListener listener){}
156 
157   //ProcessProgressReporter VOID implementation
158   public void addProcessProgressListener(ProgressListener listener){}
159   public void removeProcessProgressListener(ProgressListener listener){}
160   //ProcessProgressReporter implementation ends here
161 
162 // class Rule
163 
164 
165 // $Log$
166 // Revision 1.17  2005/01/11 13:51:36  ian
167 // Updating copyrights to 1998-2005 in preparation for v3.0
168 //
169 // Revision 1.16  2004/07/21 17:10:08  akshay
170 // Changed copyright from 1998-2001 to 1998-2004
171 //
172 // Revision 1.15  2004/03/25 13:23:04  valyt
173 // 
174 // Style: static access to static members
175 //
176 // Revision 1.14  2004/03/25 13:01:14  valyt
177 // Imports optimisation throughout the Java sources
178 // (to get rid of annoying warnings in Eclipse)
179 //
180 // Revision 1.13  2001/09/13 12:09:50  kalina
181 // Removed completely the use of jgl.objectspace.Array and such.
182 // Instead all sources now use the new Collections, typically ArrayList.
183 // I ran the tests and I ran some documents and compared with keys.
184 // JAPE seems to work well (that's where it all was). If there are problems
185 // maybe look at those new structures first.
186 //
187 // Revision 1.12  2001/03/06 20:11:14  valyt
188 //
189 // <b><em><strong>DOCUMENTATION</></></> for most of the GUI classes.
190 //
191 // Cleaned up some obsolete classes
192 //
193 // Revision 1.11  2001/02/20 12:25:49  valyt
194 //
195 // Fixed the Jpae priorities bug
196 //
197 // Revision 1.10  2001/01/21 20:51:31  valyt
198 // Added the DocumentEditor class and the necessary changes to the gate API
199 //
200 // Revision 1.9  2000/11/08 16:35:03  hamish
201 // formatting
202 //
203 // Revision 1.8  2000/10/26 10:45:31  oana
204 // Modified in the code style
205 //
206 // Revision 1.7  2000/10/16 16:44:34  oana
207 // Changed the comment of DEBUG variable
208 //
209 // Revision 1.6  2000/10/10 15:36:37  oana
210 // Changed System.out in Out and System.err in Err;
211 // Added the DEBUG variable seted on false;
212 // Added in the header the licence;
213 //
214 // Revision 1.5  2000/07/04 14:37:39  valyt
215 // Added some support for Jape-ing in a different annotations et than the default one;
216 // Changed the L&F for the JapeGUI to the System default
217 //
218 // Revision 1.4  2000/07/03 21:00:59  valyt
219 // Added StatusBar and ProgressBar support for tokenisation & Jape transduction
220 // (it looks great :) )
221 //
222 // Revision 1.3  2000/05/05 12:51:12  valyt
223 // Got rid of deprecation warnings
224 //
225 // Revision 1.2  2000/04/14 18:02:46  valyt
226 // Added some gate.fsm classes
227 // added some accessor function in old jape classes
228 //
229 // Revision 1.1  2000/02/23 13:46:11  hamish
230 // added
231 //
232 // Revision 1.1.1.1  1999/02/03 16:23:02  hamish
233 // added gate2
234 //
235 // Revision 1.16  1998/11/01 21:21:40  hamish
236 // use Java arrays in transduction where possible
237 //
238 // Revision 1.15  1998/10/30 14:06:46  hamish
239 // added getTransducer
240 //
241 // Revision 1.14  1998/10/29 12:16:13  hamish
242 // changed reset to not do lhs if weFinished  - coz
243 // there should be no state cached if the last try failed
244 //
245 // Revision 1.13  1998/10/01 16:06:37  hamish
246 // new appelt transduction style, replacing buggy version
247 //
248 // Revision 1.12  1998/09/18 13:36:00  hamish
249 // made Transducer a class
250 //
251 // Revision 1.11  1998/08/19 20:21:43  hamish
252 // new RHS assignment expression stuff added
253 //
254 // Revision 1.10  1998/08/12 19:05:48  hamish
255 // fixed multi-part CG bug; set reset to real reset and fixed multi-doc bug
256 //
257 // Revision 1.9  1998/08/12 15:39:43  hamish
258 // added padding toString methods
259 //
260 // Revision 1.8  1998/08/10 14:16:39  hamish
261 // fixed consumeblock bug and added batch.java
262 //
263 // Revision 1.7  1998/08/03 21:44:58  hamish
264 // moved parser classes to gate.jape.parser
265 //
266 // Revision 1.6  1998/08/03 19:51:27  hamish
267 // rollback added
268 //
269 // Revision 1.5  1998/07/31 16:50:19  mks
270 // RHS compilation works; it runs - and falls over...
271 //
272 // Revision 1.4  1998/07/31 13:12:27  mks
273 // done RHS stuff, not tested
274 //
275 // Revision 1.3  1998/07/30 11:05:25  mks
276 // more jape
277 //
278 // Revision 1.2  1998/07/29 11:07:11  hamish
279 // first compiling version
280 //
281 // Revision 1.1.1.1  1998/07/28 16:37:46  hamish
282 // gate2 lives