| Constraint |

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

| Constraint |