1   /*
2    *  Constraint.java - transducer class
3    *
4    *  Copyright (c) 1998-2004, 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: Constraint.java,v 1.10 2004/07/21 17:10:07 akshay Exp $
14   */
15  
16  
17  package gate.jape;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  
22  import gate.FeatureMap;
23  import gate.util.SimpleFeatureMapImpl;
24  import gate.util.Strings;
25  
26  
27  /**
28    * An individual annotation/attribute/value expression. It doesn't extend
29    * PatternElement, even though it has to "match", because a set of
30    * Constraint must be applied together in order to avoid doing separate
31    * selectAnnotations calls for each one.
32    */
33  public class Constraint
34  implements JapeConstants, java.io.Serializable, Cloneable
35  {
36    /** Debug flag */
37    private static final boolean DEBUG = false;
38  
39    /** Construction from annot type string */
40    public Constraint(String annotType) {
41      this.annotType = annotType;
42      attrs1 = new SimpleFeatureMapImpl();
43    } // Construction from annot type
44  
45    /** Construction from annot type and attribute sequence */
46    public Constraint(String annotType, FeatureMap attrs) {
47      this.annotType = annotType;
48      this.attrs1 = attrs;
49    } // Construction from annot type and attribute sequence
50  
51    /** Construction from annot type and array of attributes */
52    public Constraint(String annotType, ArrayList attrsArray) {
53      this.annotType = annotType;
54      attrs1 = new SimpleFeatureMapImpl();
55      for ( Iterator i = attrsArray.iterator(); i.hasNext(); )
56        attrs1.put(((JdmAttribute) i.next()).getName(),
57                                            ((JdmAttribute) i.next()).getValue());
58    } // Construction from annot type and array of attributes
59  
60    /** The type of annnotation we're looking for. */
61    private String annotType;
62  
63    /** Are we negated? */
64    private boolean negated = false;
65  
66    /** Set negation. */
67    public void negate() { negated = true; }
68  
69    /** Access to negation flag. */
70    public boolean isNegated() { return negated; }
71  
72    /** Get the type of annnotation we're looking for. */
73    public String getAnnotType() { return annotType; }
74  
75    /** The attributes that must be present on the matched annotation. */
76    private FeatureMap attrs1;
77  
78    /** The attributes array that must be present on the matched annotation. */
79    private JdmAttribute[] attrs2;
80  
81    /** Get the attributes that must be present on the matched annotation. */
82    public FeatureMap getAttributeSeq() { return attrs1; }
83  
84    /** Get the attributes that must be present on the matched annotation. */
85    public JdmAttribute[] getAttributeArray() { return attrs2; }
86  
87    /** Add an attribute. */
88    public void addAttribute(JdmAttribute attr) {
89      attrs1.put(attr.getName(), attr.getValue());
90    } // addAttribute
91  
92    /** Create and add an attribute. */
93    public void addAttribute(String name, Object value) {
94      attrs1.put(name, value);
95    } // addAttribute
96  
97    /** Need cloning for processing of macro references. See comments on
98      * <CODE>PatternElement.clone()</CODE>
99      */
100   public Object clone() {
101     Constraint newC = null;
102     try {
103       newC = (Constraint) super.clone();
104     } catch(CloneNotSupportedException e) {
105       throw(new InternalError(e.toString()));
106     }
107     newC.annotType = annotType;
108     newC.attrs1 = (FeatureMap) ((SimpleFeatureMapImpl) attrs1).clone();
109 
110     /* Enumeration e = attrs1.getElements();
111        while(e.hasMoreElements())
112          newC.attrs1.addAll(new JdmAttribute((JdmAttribute) e.nextElement()));
113        newC.negated = negated;
114     */
115     return newC;
116   } // clone
117 
118 
119  /** Finish: replace dynamic data structures with Java arrays; called
120     * after parsing.
121     */
122   public void finish() {
123     /*
124     if(attrs1 == null || attrs1.size() == 0) {
125       attrs2 = new JdmAttribute[0];
126       attrs1 = null;
127       return;
128     }
129     int attrsLen = attrs1.size();
130     attrs2 = new JdmAttribute[attrsLen];
131 
132     int i = 0;
133     //for(Enumeration e = attrs1.getElements(); e.hasMoreElements(); i++) {
134     //  attrs2[i] = (JdmAttribute) e.nextElement();
135     //}
136     Iterator iter = attrs1.keySet().iterator();
137     while(iter.hasNext()) {
138       String name = (String) iter.next();
139       Object value = attrs1.get(name);
140       attrs2[i++] = new JdmAttribute(name, value);
141     }
142     attrs1 = null;
143     */
144   } // finish
145 
146   /** Create a string representation of the object. */
147   public String toString() { return toString(""); }
148 
149   /** Create a string representation of the object. */
150   public String toString(String pad) {
151     String newline = Strings.getNl();
152 
153     StringBuffer buf = new StringBuffer(
154       pad + "Constraint: annotType(" + annotType + "); attrs(" + newline + pad
155     );
156 
157     // constraints
158     /*
159     for(int i=0; i<attrs.length(); i++)
160       buf.append(" " + attrs.nth(i));
161     for (Enumeration e = attrs.getElements(); e.hasMoreElements(); )
162       buf.append(" " + ((JdmAttribute) e.nextElement() ).toString());
163     buf.append(newline + pad + ") Constraint." + newline);
164      */
165     // constraints
166     if(attrs1 == null) {
167       for(int i=0; i<attrs2.length; i++)
168         buf.append(" " + attrs2[i]);
169     } else {
170       //for (Enumeration e = attrs1.getElements(); e.hasMoreElements(); )
171       //  buf.append(" " + ((JdmAttribute) e.nextElement() ).toString());
172       buf.append(attrs1.toString());
173     }
174     buf.append(newline + pad + ") Constraint." + newline);
175 
176     return buf.toString();
177   } // toString
178 
179   public String shortDesc() {
180     String res = annotType + "(";
181     if(attrs1 == null) {
182       for(int i=0; i<attrs2.length; i++)
183         res +=" " + attrs2[i];
184     } else {
185       res += attrs1.toString();
186     }
187     res += ")";
188     return res;
189   } // shortDesc
190 
191 } // class Constraint
192 
193 
194 // $Log: Constraint.java,v $
195 // Revision 1.10  2004/07/21 17:10:07  akshay
196 // Changed copyright from 1998-2001 to 1998-2004
197 //
198 // Revision 1.9  2004/03/25 13:01:14  valyt
199 // Imports optimisation throughout the Java sources
200 // (to get rid of annoying warnings in Eclipse)
201 //
202 // Revision 1.8  2001/09/13 12:09:49  kalina
203 // Removed completely the use of jgl.objectspace.Array and such.
204 // Instead all sources now use the new Collections, typically ArrayList.
205 // I ran the tests and I ran some documents and compared with keys.
206 // JAPE seems to work well (that's where it all was). If there are problems
207 // maybe look at those new structures first.
208 //
209 // Revision 1.7  2000/11/08 16:35:02  hamish
210 // formatting
211 //
212 // Revision 1.6  2000/10/26 10:45:30  oana
213 // Modified in the code style
214 //
215 // Revision 1.5  2000/10/16 16:44:33  oana
216 // Changed the comment of DEBUG variable
217 //
218 // Revision 1.4  2000/10/10 15:36:35  oana
219 // Changed System.out in Out and System.err in Err;
220 // Added the DEBUG variable seted on false;
221 // Added in the header the licence;
222 //
223 // Revision 1.3  2000/05/25 16:10:41  valyt
224 // JapeGUI is working
225 //
226 // Revision 1.2  2000/04/20 13:26:41  valyt
227 // Added the graph_drawing library.
228 // Creating of the NFSM and DFSM now works.
229 //
230 // Revision 1.1  2000/02/23 13:46:05  hamish
231 // added
232 //
233 // Revision 1.1.1.1  1999/02/03 16:23:01  hamish
234 // added gate2
235 //
236 // Revision 1.8  1998/11/05 13:36:30  kalina
237 // moved to use array of JdmAttributes for selectNextAnnotation instead of a sequence
238 //
239 // Revision 1.7  1998/11/01 22:35:56  kalina
240 // attribute seq hashtable mod
241 //
242 // Revision 1.6  1998/09/23 12:48:02  hamish
243 // negation added; noncontiguous BPEs disallowed
244 //
245 // Revision 1.5  1998/08/12 15:39:34  hamish
246 // added padding toString methods
247 //
248 // Revision 1.4  1998/07/31 13:12:14  mks
249 // done RHS stuff, not tested
250 //
251 // Revision 1.3  1998/07/30 11:05:15  mks
252 // more jape
253 //
254 // Revision 1.2  1998/07/29 11:06:55  hamish
255 // first compiling version
256 //
257 // Revision 1.1.1.1  1998/07/28 16:37:46  hamish
258 // gate2 lives
259