PrioritisedRuleList.java
001 /*
002  *  PrioritisedRuleList.java - transducer class
003  *
004  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
005  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006  *
007  *  This file is part of GATE (see http://gate.ac.uk/), and is free
008  *  software, licenced under the GNU Library General Public License,
009  *  Version 2, June 1991 (in the distribution as file licence.html,
010  *  and also available at http://gate.ac.uk/gate/licence.html).
011  *
012  *  Hamish Cunningham, 27/07/98
013  *
014  *  $Id: PrioritisedRuleList.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015  */
016 
017 
018 package gate.jape;
019 
020 import java.util.ArrayList;
021 import java.util.Iterator;
022 
023 
024 /**
025   * A list of rules ordered according to priority. May be used for ordering
026   * non-matched rules (in which case the order is based on
027   * priority/position), or matched rules (in which case the order is based
028   * on matched lenght/priority/position). Note that position represents
029   * the idea of order within an input file; it is assumed that this is the
030   * same as the order of addition of the rules to the list, i.e. a rule
031   * added 5th is assumed to occupy 5th place in the file (or other rule
032   * source). This class is based on JGL's DList, which allows for fast
033   * insertion of elements at any point. The highest priority rule is the
034   * first in the list, which may be accessed by <CODE>front()</CODE>.
035   */
036 public class PrioritisedRuleList extends ArrayList implements java.io.Serializable
037 {
038   /** Debug flag */
039   private static final boolean DEBUG = false;
040 
041   /** Adds a rule in order. Used for non-matched rules. Implements the
042     * ordering based on priority/position.
043     */
044   public synchronized void add(Rule newRule) {
045     /* for each rule,
046      *   if it is higher priority, continue;
047      *   else if it is same priority
048      *     if it is higher position, continue;
049      *     else break
050      *   else (it is lower priority) break
051      * insert newRule before current position (which may be finish)
052      */
053     Iterator iterator = this.iterator();
054     int i = 0;
055     for(  ; iterator.hasNext(); i++) {
056       Rule rule  =  (Ruleiterator.next();
057       int rulePriority =  rule.getPriority();
058       int newRulePriority =  newRule.getPriority();
059       int rulePosition =  rule.getPosition();
060       int newRulePosition =  newRule.getPosition();
061 
062       if(rulePriority > newRulePriority)
063         continue;
064       else if(rulePriority == newRulePriority) {
065         if(rulePosition < newRulePosition)
066           continue;
067         else
068           break;
069       else {
070         break;
071       }
072 
073     // while not hit the end of the rules
074 
075 
076     this.add(i, newRule);
077   // add(Rule)
078 
079 
080 // class PrioritisedRuleList
081 
082 
083 // $Log$
084 // Revision 1.8  2005/01/11 13:51:36  ian
085 // Updating copyrights to 1998-2005 in preparation for v3.0
086 //
087 // Revision 1.7  2004/07/21 17:10:08  akshay
088 // Changed copyright from 1998-2001 to 1998-2004
089 //
090 // Revision 1.6  2004/03/25 13:01:13  valyt
091 // Imports optimisation throughout the Java sources
092 // (to get rid of annoying warnings in Eclipse)
093 //
094 // Revision 1.5  2001/09/13 12:09:50  kalina
095 // Removed completely the use of jgl.objectspace.Array and such.
096 // Instead all sources now use the new Collections, typically ArrayList.
097 // I ran the tests and I ran some documents and compared with keys.
098 // JAPE seems to work well (that's where it all was). If there are problems
099 // maybe look at those new structures first.
100 //
101 // Revision 1.4  2000/11/08 16:35:03  hamish
102 // formatting
103 //
104 // Revision 1.3  2000/10/16 16:44:34  oana
105 // Changed the comment of DEBUG variable
106 //
107 // Revision 1.2  2000/10/10 15:36:36  oana
108 // Changed System.out in Out and System.err in Err;
109 // Added the DEBUG variable seted on false;
110 // Added in the header the licence;
111 //
112 // Revision 1.1  2000/02/23 13:46:10  hamish
113 // added
114 //
115 // Revision 1.1.1.1  1999/02/03 16:23:02  hamish
116 // added gate2
117 //
118 // Revision 1.6  1998/10/01 16:06:34  hamish
119 // new appelt transduction style, replacing buggy version
120 //
121 // Revision 1.5  1998/09/17 10:24:02  hamish
122 // added options support, and Appelt-style rule application
123 //
124 // Revision 1.4  1998/08/12 19:05:47  hamish
125 // fixed multi-part CG bug; set reset to real reset and fixed multi-doc bug
126 //
127 // Revision 1.3  1998/07/30 11:05:24  mks
128 // more jape
129 //
130 // Revision 1.2  1998/07/29 11:07:08  hamish
131 // first compiling version
132 //
133 // Revision 1.1.1.1  1998/07/28 16:37:46  hamish
134 // gate2 lives