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