1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }