View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.concepts;
25  
26  import java.util.*;
27  import zeus.util.*;
28  
29  public class ResourceItem extends Observable {
30     public static final String ALLOCATED = "Reserved";
31     public static final String UNALLOCATED = "Free";
32  
33     public static final int UNCHANGED = 0;
34     public static final int MODIFY    = 1;
35     public static final int DELETE    = 2;
36  
37     protected Fact fact = null;
38     protected Hashtable reservations = new Hashtable();
39  
40     public ResourceItem(Fact fact) {
41        Assert.notNull(fact);
42        this.fact = fact;
43     }
44  
45     public Fact getFact() { return fact; }
46  
47     public synchronized boolean isReserved() {
48        return !reservations.isEmpty();
49     }
50  
51     public synchronized boolean reservationOK(int start, boolean consumed,
52                                               int amount) {
53        return unreservedAmount(start,consumed) >= amount;
54     }
55     public synchronized int unreservedAmount(int start, boolean consumed) {
56        return fact.getNumber() - reservedAmount(start,consumed);
57     }
58     public synchronized int reservedAmount(int start, boolean consumed) {
59        Enumeration enum = reservations.elements();
60        int reserved = 0;
61        Entry e;
62        while( enum.hasMoreElements() ) {
63           e = (Entry)enum.nextElement();
64           if ( consumed ) {
65              if ( e.consumed || e.start > start )
66                 reserved += e.amount;
67           }
68           else {
69              if ( e.consumed && e.start <= start )
70                 reserved += e.amount;
71           }
72        }
73        return reserved;
74     }
75  
76     public int getReservedAmount(Observer observer) {
77        Entry e = (Entry) reservations.get(observer);
78        return (e == null) ? 0 : e.amount;
79     }
80     public synchronized void changeReservedAmount(Observer o, int amount) {
81        Entry e = (Entry) reservations.get(o);
82        Assert.notNull(e);
83        Assert.notFalse(e.amount >= amount);
84        e.amount = amount;
85     }
86     public synchronized void cancelReservation(Observer observer) {
87        deleteObserver(observer);
88        Entry e = (Entry) reservations.remove(observer);
89     }
90     public synchronized boolean executeNow(Observer observer, int now) {
91        boolean execute_now = true;
92        Entry e = (Entry) reservations.get(observer);
93        if ( !e.consumed )
94           return true;
95        else {
96           reservations.remove(observer);
97           boolean status = reservationOK(now,e.consumed,e.amount);
98           reservations.put(observer,e);
99           return status;
100       }
101    }
102    public synchronized boolean newStartTime(Observer observer, int start) {
103       Entry e = (Entry) reservations.remove(observer);
104       if ( reserve(observer,start,e.consumed,e.amount) )
105          return true;
106       else {
107          reservations.put(observer,e);
108          return false;
109       }
110    }
111 
112    public synchronized boolean reserve(Observer observer, int start,
113                                        boolean consumed, int amount) {
114 
115       if ( !reservationOK(start,consumed,amount) ) return false;
116 
117       Entry e = new Entry(start,consumed,amount);
118       // Assert.notFalse(reservations.put(observer,e) == null);
119       Entry xe = (Entry) reservations.put(observer,e);
120       if ( xe != null ) {
121          System.err.println("ResourceItem reserve: multiple entries " +
122                             "for same observer " + observer);
123          System.err.println("Previous = " + xe);
124          System.err.println("Current = " + e);
125       }
126 
127       addObserver(observer);
128       return true;
129    }
130 
131    public void deleted() {
132       Core.DEBUG(3,"Notifying observers of deletion...\n" + fact.pprint());
133       setChanged();
134       notifyObservers("deleted");
135    }
136 
137    public int consumed(Observer observer) {
138       deleteObserver(observer);
139       int no = fact.getNumber();
140       Entry e = (Entry) reservations.remove(observer);
141       int status;
142       if ( !e.consumed )         status = UNCHANGED;
143       else if ( e.amount < no )  status = MODIFY;
144       else                       status = DELETE;
145 
146       if ( status == MODIFY ) fact.setNumber(no-e.amount);
147 
148       return status;
149    }
150    protected class Entry {
151       public int start;
152       public boolean consumed;
153       public int amount;
154 
155       public Entry(int start, boolean consumed, int amount) {
156          this.start = start;
157          this.consumed = consumed;
158          this.amount = amount;
159       }
160       public String toString() {
161          return( "(" +
162                   ":start " + start + " " +
163                   ":consumed " + consumed + " " +
164                   ":amount " + amount +
165                  ")"
166                );
167       }
168    }
169 }