1   /*
2    *  PrioritisedRuleList.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, 27/07/98
12   *
13   *  $Id: PrioritisedRuleList.java,v 1.5 2001/09/13 12:09:50 kalina Exp $
14   */
15  
16  
17  package gate.jape;
18  
19  import java.util.*;
20  
21  
22  /**
23    * A list of rules ordered according to priority. May be used for ordering
24    * non-matched rules (in which case the order is based on
25    * priority/position), or matched rules (in which case the order is based
26    * on matched lenght/priority/position). Note that position represents
27    * the idea of order within an input file; it is assumed that this is the
28    * same as the order of addition of the rules to the list, i.e. a rule
29    * added 5th is assumed to occupy 5th place in the file (or other rule
30    * source). This class is based on JGL's DList, which allows for fast
31    * insertion of elements at any point. The highest priority rule is the
32    * first in the list, which may be accessed by <CODE>front()</CODE>.
33    */
34  public class PrioritisedRuleList extends ArrayList implements java.io.Serializable
35  {
36    /** Debug flag */
37    private static final boolean DEBUG = false;
38  
39    /** Adds a rule in order. Used for non-matched rules. Implements the
40      * ordering based on priority/position.
41      */
42    public synchronized void add(Rule newRule) {
43      /* for each rule,
44       *   if it is higher priority, continue;
45       *   else if it is same priority
46       *     if it is higher position, continue;
47       *     else break
48       *   else (it is lower priority) break
49       * insert newRule before current position (which may be finish)
50       */
51      Iterator iterator = this.iterator();
52      int i = 0;
53      for(  ; iterator.hasNext(); i++) {
54        Rule rule = (Rule) iterator.next();
55        int rulePriority =  rule.getPriority();
56        int newRulePriority = newRule.getPriority();
57        int rulePosition =  rule.getPosition();
58        int newRulePosition = newRule.getPosition();
59  
60        if(rulePriority > newRulePriority)
61          continue;
62        else if(rulePriority == newRulePriority) {
63          if(rulePosition < newRulePosition)
64            continue;
65          else
66            break;
67        } else {
68          break;
69        }
70  
71      } // while not hit the end of the rules
72  
73  
74      this.add(i, newRule);
75    } // add(Rule)
76  
77    /** Adds a rule in order. Used for matched rules. Implements the
78      * ordering based on length/priority/position. Length is given as
79      * a parameter.
80      */
81    public synchronized void add(Rule newRule, int newRuleLength) {
82      /* for each rule,
83       *   if it is longer than the new one, continue;
84       *   else if it is the same length
85       *     if it is higher priority, continue;
86       *     else if it is same priority
87       *       if it is higher position, continue;
88       *       else break;
89       *     else (it is lower priority) break;
90       *   else (it is shorter) break;
91       * insert newRule before current position (which may be finish)
92       */
93      Iterator iterator = this.iterator();
94      int i = 0;
95      for(  ; iterator.hasNext(); i++) {
96        Rule rule = (Rule) iterator.next();
97        int rulePriority =  rule.getPriority();
98        int newRulePriority = newRule.getPriority();
99        int rulePosition =  rule.getPosition();
100       int newRulePosition = newRule.getPosition();
101       int ruleLength = rule.getEndPosition() - rule.getStartPosition();
102 
103       if(ruleLength > newRuleLength)
104         continue;
105       else if(ruleLength == newRuleLength) {
106         if(rulePriority > newRulePriority)
107           continue;
108         else if(rulePriority == newRulePriority) {
109           if(rulePosition < newRulePosition)
110             continue;
111           else
112             break;
113         } else {
114           break;
115         }
116       } else {
117         break;
118       }
119 
120     } // while not hit the end of the rules
121 
122     add(i, newRule);
123   } // add(Rule,int)
124 
125 } // class PrioritisedRuleList
126 
127 
128 // $Log: PrioritisedRuleList.java,v $
129 // Revision 1.5  2001/09/13 12:09:50  kalina
130 // Removed completely the use of jgl.objectspace.Array and such.
131 // Instead all sources now use the new Collections, typically ArrayList.
132 // I ran the tests and I ran some documents and compared with keys.
133 // JAPE seems to work well (that's where it all was). If there are problems
134 // maybe look at those new structures first.
135 //
136 // Revision 1.4  2000/11/08 16:35:03  hamish
137 // formatting
138 //
139 // Revision 1.3  2000/10/16 16:44:34  oana
140 // Changed the comment of DEBUG variable
141 //
142 // Revision 1.2  2000/10/10 15:36:36  oana
143 // Changed System.out in Out and System.err in Err;
144 // Added the DEBUG variable seted on false;
145 // Added in the header the licence;
146 //
147 // Revision 1.1  2000/02/23 13:46:10  hamish
148 // added
149 //
150 // Revision 1.1.1.1  1999/02/03 16:23:02  hamish
151 // added gate2
152 //
153 // Revision 1.6  1998/10/01 16:06:34  hamish
154 // new appelt transduction style, replacing buggy version
155 //
156 // Revision 1.5  1998/09/17 10:24:02  hamish
157 // added options support, and Appelt-style rule application
158 //
159 // Revision 1.4  1998/08/12 19:05:47  hamish
160 // fixed multi-part CG bug; set reset to real reset and fixed multi-doc bug
161 //
162 // Revision 1.3  1998/07/30 11:05:24  mks
163 // more jape
164 //
165 // Revision 1.2  1998/07/29 11:07:08  hamish
166 // first compiling version
167 //
168 // Revision 1.1.1.1  1998/07/28 16:37:46  hamish
169 // gate2 lives
170