ComplexPatternElement.java
001 /*
002  *  ComplexPatternElement.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: ComplexPatternElement.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.util.Strings;
023 
024 import java.util.Iterator;
025 
026 
027 /**
028   * A pattern element enclosed in round brackets. Has a
029   * ConstraintGroup, optional Kleene operator and optional binding name.
030   */
031 public class ComplexPatternElement extends PatternElement
032 implements JapeConstants, java.io.Serializable
033 {
034   /** Kleene operator (defaults to none). Other values: KLEENE_STAR (*);
035     * KLEENE_PLUS (+); KLEENE_QUERY (?) */
036   private KleeneOperator kleeneOp = null;
037 
038   /** Binding name (may be null). */
039   private String bindingName = null;
040 
041   /** Get binding name. */
042   public String getBindingName() { return bindingName; }
043 
044   /** Get a list of CPEs that we contain. */
045   protected Iterator<ComplexPatternElement> getCPEs() {
046     return constraintGroup.getCPEs();
047   // getCPEs
048 
049   /** The recursive definition of what pattern elements make up this one. */
050   private ConstraintGroup constraintGroup;
051 
052   /** Construction from ConstraintGroup, Kleene operator type and binding
053     * name. Kleene types are defined in JapeConstants.
054     @deprecated Use {@link #ComplexPatternElement(ConstraintGroup, KleeneOperator.Type, String)} instead.
055     */
056   public ComplexPatternElement(
057     ConstraintGroup constraintGroup,
058     int kleeneOp,
059     String bindingName
060   ) {
061     this(constraintGroup, KleeneOperator.Type.getFromJapeConstant(kleeneOp), bindingName);
062   }
063 
064   public ComplexPatternElement(
065     ConstraintGroup constraintGroup,
066     KleeneOperator.Type kleeneType,
067     String bindingName
068   ) {
069     this(constraintGroup, new KleeneOperator(kleeneType), bindingName);
070   }
071 
072   public ComplexPatternElement(
073           ConstraintGroup constraintGroup,
074           KleeneOperator kleeneOp,
075           String bindingName
076         ) {
077           if (kleeneOp == null)
078             kleeneOp = new KleeneOperator(KleeneOperator.Type.SINGLE);
079           this.constraintGroup = constraintGroup;
080           this.kleeneOp = kleeneOp;
081           this.bindingName = bindingName;
082         }
083 
084   /** Construction from ConstraintGroup, min and max legal occurance limits,
085    * and binding name.
086    */
087   public ComplexPatternElement(
088     ConstraintGroup constraintGroup,
089     int minOccurance, int maxOccurance,
090     String bindingName
091   ) {
092     this.constraintGroup = constraintGroup;
093     this.kleeneOp = new KleeneOperator(minOccurance, maxOccurance);
094     this.bindingName = bindingName;
095   }
096 
097   /** Need cloning for processing of macro references. See comments on
098     <CODE>PatternElement.clone()</CODE>
099     */
100   public Object clone() {
101     ComplexPatternElement newPE = (ComplexPatternElementsuper.clone();
102     newPE.constraintGroup = (ConstraintGroupconstraintGroup.clone();
103     return newPE;
104   // clone
105 
106   /** Finish: replace dynamic data structures with Java arrays; called
107     * after parsing.
108     */
109   public void finish() {
110     constraintGroup.finish();
111   // finish
112 
113   /** Create a string representation of the object. */
114   public String toString() { return toString("")}
115 
116   /** Create a string representation of the object. */
117   public String toString(String pad) {
118     String newline = Strings.getNl();
119 
120     StringBuffer buf = new StringBuffer(
121       pad + "CPE: bindingName(" + bindingName + "); kleeneOp("
122     );
123 
124     if (kleeneOp != null)
125       buf.append(kleeneOp);
126 
127     buf.append(
128       "); constraintGroup(" + newline +
129       constraintGroup.toString(Strings.addPadding(pad, INDENT_PADDING)) +
130       newline + pad + ") CPE." + newline
131     );
132 
133     return buf.toString();
134   // toString
135   //needed by FSM
136 
137   public KleeneOperator getKleeneOp(){ return kleeneOp; };
138 
139   public ConstraintGroup getConstraintGroup(){ return constraintGroup; }
140 
141 // class ComplexPatternElement
142 
143 
144 // $Log$
145 // Revision 1.11  2005/01/11 13:51:36  ian
146 // Updating copyrights to 1998-2005 in preparation for v3.0
147 //
148 // Revision 1.10  2004/07/21 17:10:07  akshay
149 // Changed copyright from 1998-2001 to 1998-2004
150 //
151 // Revision 1.9  2004/03/25 13:01:15  valyt
152 // Imports optimisation throughout the Java sources
153 // (to get rid of annoying warnings in Eclipse)
154 //
155 // Revision 1.8  2001/09/13 12:09:49  kalina
156 // Removed completely the use of jgl.objectspace.Array and such.
157 // Instead all sources now use the new Collections, typically ArrayList.
158 // I ran the tests and I ran some documents and compared with keys.
159 // JAPE seems to work well (that's where it all was). If there are problems
160 // maybe look at those new structures first.
161 //
162 // Revision 1.7  2001/09/12 11:59:33  kalina
163 // Changed the old JAPE stuff to use the new Collections API,
164 // instead of com.objectspace stuff. Will eliminate that library
165 // completely very soon! Just one class left to re-implement,
166 //
167 // ParseCPSL.jj changed accordingly. All tested and no smoke.
168 //
169 // Revision 1.6  2000/11/08 16:35:02  hamish
170 // formatting
171 //
172 // Revision 1.5  2000/10/26 10:45:30  oana
173 // Modified in the code style
174 //
175 // Revision 1.4  2000/10/16 16:44:33  oana
176 // Changed the comment of DEBUG variable
177 //
178 // Revision 1.3  2000/10/10 15:36:35  oana
179 // Changed System.out in Out and System.err in Err;
180 // Added the DEBUG variable seted on false;
181 // Added in the header the licence;
182 //
183 // Revision 1.2  2000/04/14 18:02:46  valyt
184 // Added some gate.fsm classes
185 // added some accessor function in old jape classes
186 //
187 // Revision 1.1  2000/02/23 13:46:05  hamish
188 // added
189 //
190 // Revision 1.1.1.1  1999/02/03 16:23:01  hamish
191 // added gate2
192 //
193 // Revision 1.14  1998/11/13 13:17:16  hamish
194 // merged in the doc length bug fix
195 //
196 // Revision 1.13  1998/11/12 17:47:27  kalina
197 // A bug fixed, wasn't restoring the document length
198 //
199 // Revision 1.12  1998/11/05 13:36:29  kalina
200 // moved to use array of JdmAttributes for selectNextAnnotation instead
201 // of a sequence
202 //
203 // Revision 1.11  1998/11/01 21:21:35  hamish
204 // use Java arrays in transduction where possible
205 //
206 // Revision 1.10  1998/10/06 16:16:09  hamish
207 // negation percolation during constrain add; position advance when none at end
208 //
209 // Revision 1.9  1998/10/01 16:06:29  hamish
210 // new appelt transduction style, replacing buggy version
211 //
212 // Revision 1.8  1998/09/26 09:19:14  hamish
213 // added cloning of PE macros
214 //
215 // Revision 1.7  1998/09/17 16:48:29  hamish
216 // added macro defs and macro refs on LHS
217 //
218 // Revision 1.6  1998/08/12 15:39:32  hamish
219 // added padding toString methods
220 //
221 // Revision 1.5  1998/08/05 21:58:04  hamish
222 // backend works on simple test
223 //
224 // Revision 1.4  1998/08/03 19:51:19  hamish
225 // rollback added
226 //
227 // Revision 1.3  1998/07/30 11:05:14  hamish
228 // more jape
229 //
230 // Revision 1.2  1998/07/29 11:06:54  hamish
231 // first compiling version
232 //
233 // Revision 1.1.1.1  1998/07/28 16:37:46  hamish
234 // gate2 lives