LeftHandSide.java
001 /*
002  *  LeftHandSide.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: LeftHandSide.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015  */
016 
017 
018 package gate.jape;
019 
020 import java.io.Serializable;
021 import java.util.*;
022 
023 import gate.AnnotationSet;
024 import gate.Document;
025 import gate.util.Strings;
026 
027 
028 /**
029   * The LHS of a CPSL rule. The pattern part. Has a ConstraintGroup and
030   * binding information that associates labels with ComplexPatternElements.
031   * Provides the Matcher interface.
032   */
033 public class LeftHandSide implements JapeConstants, Serializable
034 {
035 
036   /** Debug flag */
037   private static final boolean DEBUG = false;
038 
039   /** The constraint group making up this LHS. */
040   private ConstraintGroup constraintGroup;
041 
042   /** Mapping of binding names to ComplexPatternElements */
043   private HashMap bindingTable;
044 
045   /** Construction from a ConstraintGroup */
046   public LeftHandSide(ConstraintGroup constraintGroup) {
047     this.constraintGroup = constraintGroup;
048     bindingTable = new HashMap();
049   // construction from ConstraintGroup
050 
051   /** Add a binding record. */
052   public void addBinding(
053     String bindingName,
054     ComplexPatternElement binding,
055     HashSet bindingNameSet,
056     boolean macroRef
057   throws JapeException {
058     if(bindingTable.get(bindingName!= null)
059       throw new JapeException(
060         "LeftHandSide.addBinding: " + bindingName + " already bound"
061       );
062     bindingTable.put(bindingName, binding);
063     bindingNameSet.add(bindingName);
064 
065     // if it was a macro ref, we need to recursively set up bindings
066     // in any CPEs that this one contains
067     if(macroRef) {
068       for(Iterator i = binding.getCPEs(); i.hasNext()) {
069         binding = (ComplexPatternElementi.next();
070         bindingName = binding.getBindingName();
071         if(bindingName == null// not all CPEs have binding names
072           continue;
073         if(bindingTable.get(bindingName!= null)
074           throw new JapeException(
075             "LeftHandSide.addBinding: " + bindingName + " already bound"
076           );
077         bindingTable.put(bindingName, binding);
078         bindingNameSet.add(bindingName);
079       // for each binding
080     // macroRef
081 
082   // addBinding
083 
084   /** Finish: replace dynamic data structures with Java arrays; called
085     * after parsing.
086     */
087   public void finish() {
088     constraintGroup.finish();
089   // finish
090 
091   /** Create a string representation of the object. */
092   public String toString() { return toString("")}
093 
094   /** Create a string representation of the object. */
095   public String toString(String pad) {
096     String newline = Strings.getNl();
097     String newPad = Strings.addPadding(pad, INDENT_PADDING);
098 
099     StringBuffer buf = new StringBuffer(pad +
100       /*"LHS: hasMatched(" + hasMatched + ")*/ 
101       "; constraintGroup(" + newline +
102       constraintGroup.toString(newPad+ newline + pad +
103       "); bindingTable(" + newline + pad
104     );
105 
106     for(Iterator i = bindingTable.keySet().iterator(); i.hasNext()) {
107       String bName = ((Stringi.next());
108       ComplexPatternElement cpe = ((ComplexPatternElement)
109                                         bindingTable.get(bName));
110       buf.append(
111         pad + "bT.bn(" + bName + "), cpe.bn(" + cpe.getBindingName() ")"
112       );
113     }
114 
115     buf.append(newline + pad + ") LHS." + newline);
116 
117     return buf.toString();
118   // toString
119 
120   /** Get the constraint group */
121   public ConstraintGroup getConstraintGroup(){
122     return constraintGroup;
123   }
124 
125 // class LeftHandSide
126 
127 
128 // $Log$
129 // Revision 1.10  2005/01/11 13:51:36  ian
130 // Updating copyrights to 1998-2005 in preparation for v3.0
131 //
132 // Revision 1.9  2004/07/21 17:10:08  akshay
133 // Changed copyright from 1998-2001 to 1998-2004
134 //
135 // Revision 1.8  2004/03/25 13:01:13  valyt
136 // Imports optimisation throughout the Java sources
137 // (to get rid of annoying warnings in Eclipse)
138 //
139 // Revision 1.7  2001/09/12 11:59:33  kalina
140 // Changed the old JAPE stuff to use the new Collections API,
141 // instead of com.objectspace stuff. Will eliminate that library
142 // completely very soon! Just one class left to re-implement,
143 //
144 // ParseCPSL.jj changed accordingly. All tested and no smoke.
145 //
146 // Revision 1.6  2000/11/08 16:35:03  hamish
147 // formatting
148 //
149 // Revision 1.5  2000/10/16 16:44:33  oana
150 // Changed the comment of DEBUG variable
151 //
152 // Revision 1.4  2000/10/10 15:36:36  oana
153 // Changed System.out in Out and System.err in Err;
154 // Added the DEBUG variable seted on false;
155 // Added in the header the licence;
156 //
157 // Revision 1.3  2000/05/02 16:54:26  hamish
158 // comment
159 //
160 // Revision 1.2  2000/04/14 18:02:46  valyt
161 // Added some gate.fsm classes
162 // added some accessor function in old jape classes
163 //
164 // Revision 1.1  2000/02/23 13:46:08  hamish
165 // added
166 //
167 // Revision 1.1.1.1  1999/02/03 16:23:01  hamish
168 // added gate2
169 //
170 // Revision 1.14  1998/11/01 21:21:37  hamish
171 // use Java arrays in transduction where possible
172 //
173 // Revision 1.13  1998/10/30 15:31:07  kalina
174 // Made small changes to make compile under 1.2 and 1.1.x
175 //
176 // Revision 1.12  1998/10/01 16:06:32  hamish
177 // new appelt transduction style, replacing buggy version
178 //
179 // Revision 1.11  1998/09/21 16:19:49  hamish
180 // cope with CPEs with no binding
181 //
182 // Revision 1.10  1998/09/17 16:48:32  hamish
183 // added macro defs and macro refs on LHS
184 //
185 // Revision 1.9  1998/08/19 20:21:39  hamish
186 // new RHS assignment expression stuff added
187 //
188 // Revision 1.8  1998/08/18 12:43:07  hamish
189 // fixed SPT bug, not advancing newPosition
190 //
191 // Revision 1.7  1998/08/12 19:05:45  hamish
192 // fixed multi-part CG bug; set reset to real reset and fixed multi-doc bug
193 //
194 // Revision 1.6  1998/08/12 15:39:37  hamish
195 // added padding toString methods
196 //
197 // Revision 1.5  1998/08/03 19:51:22  hamish
198 // rollback added
199 //
200 // Revision 1.4  1998/07/31 13:12:20  mks
201 // done RHS stuff, not tested
202 //
203 // Revision 1.3  1998/07/30 11:05:19  mks
204 // more jape
205 //
206 // Revision 1.2  1998/07/29 11:06:59  hamish
207 // first compiling version
208 //
209 // Revision 1.1.1.1  1998/07/28 16:37:46  hamish
210 // gate2 lives