|
LeftHandSide |
|
1 /* 2 * LeftHandSide.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: LeftHandSide.java,v 1.7 2001/09/12 11:59:33 kalina Exp $ 14 */ 15 16 17 package gate.jape; 18 19 import java.util.Enumeration; 20 import java.io.Serializable; 21 import java.util.*; 22 import gate.annotation.*; 23 import gate.util.*; 24 import gate.*; 25 26 27 /** 28 * The LHS of a CPSL rule. The pattern part. Has a ConstraintGroup and 29 * binding information that associates labels with ComplexPatternElements. 30 * Provides the Matcher interface. 31 */ 32 public class LeftHandSide implements Matcher, JapeConstants, Serializable 33 { 34 35 /** Debug flag */ 36 private static final boolean DEBUG = false; 37 38 /** The constraint group making up this LHS. */ 39 private ConstraintGroup constraintGroup; 40 41 /** Mapping of binding names to ComplexPatternElements */ 42 private HashMap bindingTable; 43 44 /** Flag for whether our last match was successful or not. */ 45 private boolean hasMatched = false; 46 47 /** Construction from a ConstraintGroup */ 48 public LeftHandSide(ConstraintGroup constraintGroup) { 49 this.constraintGroup = constraintGroup; 50 bindingTable = new HashMap(); 51 hasMatched = false; 52 } // construction from ConstraintGroup 53 54 /** Add a binding record. */ 55 public void addBinding( 56 String bindingName, 57 ComplexPatternElement binding, 58 HashSet bindingNameSet, 59 boolean macroRef 60 ) throws JapeException { 61 if(bindingTable.get(bindingName) != null) 62 throw new JapeException( 63 "LeftHandSide.addBinding: " + bindingName + " already bound" 64 ); 65 bindingTable.put(bindingName, binding); 66 bindingNameSet.add(bindingName); 67 68 // if it was a macro ref, we need to recursively set up bindings 69 // in any CPEs that this one contains 70 if(macroRef) { 71 for(Iterator i = binding.getCPEs(); i.hasNext(); ) { 72 binding = (ComplexPatternElement) i.next(); 73 bindingName = binding.getBindingName(); 74 if(bindingName == null) // not all CPEs have binding names 75 continue; 76 if(bindingTable.get(bindingName) != null) 77 throw new JapeException( 78 "LeftHandSide.addBinding: " + bindingName + " already bound" 79 ); 80 bindingTable.put(bindingName, binding); 81 bindingNameSet.add(bindingName); 82 } // for each binding 83 } // macroRef 84 85 } // addBinding 86 87 /** Finish: replace dynamic data structures with Java arrays; called 88 * after parsing. 89 */ 90 public void finish() { 91 constraintGroup.finish(); 92 } // finish 93 94 /** Get annotations via a binding name. */ 95 public AnnotationSet getBoundAnnots(String bindingName) { 96 ComplexPatternElement pat = 97 (ComplexPatternElement) bindingTable.get(bindingName); 98 if(pat == null) return null; 99 return pat.getMatchedAnnots(); 100 } // getBoundAnnots 101 102 /** For debugging only. 103 * Return a set of all annotations matched by the LHS during the 104 * last call to matches. (May be null.) 105 */ 106 AnnotationSet getMatchedAnnots() { 107 return constraintGroup.getMatchedAnnots(); 108 } // getMatchedAnnots 109 110 /** Clear the matched annotations cached in pattern elements. */ 111 public void reset() { 112 constraintGroup.reset(); 113 hasMatched = false; 114 } // reset 115 116 /** Was the last match successful? */ 117 public boolean hasMatched() { return hasMatched; } 118 119 /** Does the LHS match the document at this position? */ 120 public boolean matches( 121 Document doc, int position, MutableInteger newPosition 122 ) { 123 boolean status = constraintGroup.matches(doc, position, newPosition); 124 //Debug.pr(this, "LHS: status(" + status + "); this: " + this.toString()); 125 126 if(! status) { // purge caches of matched annotations 127 constraintGroup.reset(); 128 hasMatched = false; 129 } else { 130 hasMatched = true; 131 } 132 return status; 133 } // matches 134 135 /** Create a string representation of the object. */ 136 public String toString() { return toString(""); } 137 138 /** Create a string representation of the object. */ 139 public String toString(String pad) { 140 String newline = Strings.getNl(); 141 String newPad = Strings.addPadding(pad, INDENT_PADDING); 142 143 StringBuffer buf = new StringBuffer(pad + 144 "LHS: hasMatched(" + hasMatched + "); constraintGroup(" + newline + 145 constraintGroup.toString(newPad) + newline + pad + 146 "); bindingTable(" + newline + pad 147 ); 148 149 for(Iterator i = bindingTable.keySet().iterator(); i.hasNext(); ) { 150 String bName = ((String) i.next()); 151 ComplexPatternElement cpe = ((ComplexPatternElement) 152 bindingTable.get(bName)); 153 buf.append( 154 pad + "bT.bn(" + bName + "), cpe.bn(" + cpe.getBindingName() + ")" 155 ); 156 } 157 158 buf.append(newline + pad + ") LHS." + newline); 159 160 return buf.toString(); 161 } // toString 162 163 /** Get the constraint group */ 164 public ConstraintGroup getConstraintGroup(){ 165 return constraintGroup; 166 } 167 168 } // class LeftHandSide 169 170 171 // $Log: LeftHandSide.java,v $ 172 // Revision 1.7 2001/09/12 11:59:33 kalina 173 // Changed the old JAPE stuff to use the new Collections API, 174 // instead of com.objectspace stuff. Will eliminate that library 175 // completely very soon! Just one class left to re-implement, 176 // 177 // ParseCPSL.jj changed accordingly. All tested and no smoke. 178 // 179 // Revision 1.6 2000/11/08 16:35:03 hamish 180 // formatting 181 // 182 // Revision 1.5 2000/10/16 16:44:33 oana 183 // Changed the comment of DEBUG variable 184 // 185 // Revision 1.4 2000/10/10 15:36:36 oana 186 // Changed System.out in Out and System.err in Err; 187 // Added the DEBUG variable seted on false; 188 // Added in the header the licence; 189 // 190 // Revision 1.3 2000/05/02 16:54:26 hamish 191 // comment 192 // 193 // Revision 1.2 2000/04/14 18:02:46 valyt 194 // Added some gate.fsm classes 195 // added some accessor function in old jape classes 196 // 197 // Revision 1.1 2000/02/23 13:46:08 hamish 198 // added 199 // 200 // Revision 1.1.1.1 1999/02/03 16:23:01 hamish 201 // added gate2 202 // 203 // Revision 1.14 1998/11/01 21:21:37 hamish 204 // use Java arrays in transduction where possible 205 // 206 // Revision 1.13 1998/10/30 15:31:07 kalina 207 // Made small changes to make compile under 1.2 and 1.1.x 208 // 209 // Revision 1.12 1998/10/01 16:06:32 hamish 210 // new appelt transduction style, replacing buggy version 211 // 212 // Revision 1.11 1998/09/21 16:19:49 hamish 213 // cope with CPEs with no binding 214 // 215 // Revision 1.10 1998/09/17 16:48:32 hamish 216 // added macro defs and macro refs on LHS 217 // 218 // Revision 1.9 1998/08/19 20:21:39 hamish 219 // new RHS assignment expression stuff added 220 // 221 // Revision 1.8 1998/08/18 12:43:07 hamish 222 // fixed SPT bug, not advancing newPosition 223 // 224 // Revision 1.7 1998/08/12 19:05:45 hamish 225 // fixed multi-part CG bug; set reset to real reset and fixed multi-doc bug 226 // 227 // Revision 1.6 1998/08/12 15:39:37 hamish 228 // added padding toString methods 229 // 230 // Revision 1.5 1998/08/03 19:51:22 hamish 231 // rollback added 232 // 233 // Revision 1.4 1998/07/31 13:12:20 mks 234 // done RHS stuff, not tested 235 // 236 // Revision 1.3 1998/07/30 11:05:19 mks 237 // more jape 238 // 239 // Revision 1.2 1998/07/29 11:06:59 hamish 240 // first compiling version 241 // 242 // Revision 1.1.1.1 1998/07/28 16:37:46 hamish 243 // gate2 lives 244
|
LeftHandSide |
|