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 SuppliedItem {
30     public static final int UNCHANGED = 0;
31     public static final int MODIFY    = 1;
32     public static final int DELETE    = 2;
33  
34     protected Fact fact = null;
35     protected String supplier = null;
36     protected String id = null;
37     protected String link = null;
38     protected Hashtable reservations = new Hashtable();
39  
40     public SuppliedItem(String id, String link, String supplier, Fact fact) {
41        Assert.notNull(id);
42        Assert.notNull(link);
43        Assert.notNull(fact);
44        Assert.notNull(supplier);
45        this.id = id;
46        this.link = link;
47        this.fact = fact;
48        this.supplier = supplier;
49     }
50  
51     public Fact    getFact()     { return fact; }
52     public String  getSupplier() { return supplier; }
53     public String  getId()       { return id; }
54     public String  getLink()     { return link; }
55  
56     public boolean equals(SuppliedItem item) {
57        return id.equals(item.getId());
58     }
59  
60     public synchronized ReservationEntry[] getReservations() {
61        ReservationEntry[] out = new ReservationEntry[reservations.size()];
62        Enumeration enum = reservations.elements();
63        for(int i = 0; enum.hasMoreElements(); i++ )
64           out[i] = (ReservationEntry)enum.nextElement();
65        return out;
66     }
67     public synchronized ReservationEntry[] getReservations(String consumer) {
68        Vector out = new Vector();
69        ReservationEntry entry;
70        Enumeration enum = reservations.elements();
71        for(int i = 0; enum.hasMoreElements(); i++ ) {
72           entry = (ReservationEntry)enum.nextElement();
73           if ( entry.agent.equals(consumer) )
74              out.addElement(entry);
75        }
76        ReservationEntry[] data = new ReservationEntry[out.size()];
77        for(int i = 0; i < out.size(); i++ )
78           data[i] = (ReservationEntry)out.elementAt(i);
79        return data;
80     }
81  
82     public synchronized boolean isReserved() {
83        return !reservations.isEmpty();
84     }
85  
86     public synchronized boolean reservationOK(int start, boolean consumed,
87                                               int amount) {
88        return unreservedAmount(start,consumed) >= amount;
89     }
90     public synchronized int unreservedAmount(int start, boolean consumed) {
91        return fact.getNumber() - reservedAmount(start,consumed);
92     }
93     public synchronized int reservedAmount(int start, boolean consumed) {
94        Enumeration enum = reservations.elements();
95        int reserved = 0;
96        ReservationEntry e;
97        while( enum.hasMoreElements() ) {
98           e = (ReservationEntry)enum.nextElement();
99           if ( consumed ) {
100             if ( e.consumed || e.start > start )
101                reserved += e.amount;
102          }
103          else {
104             if ( e.consumed && e.start <= start )
105                reserved += e.amount;
106          }
107       }
108       return reserved;
109    }
110 
111    public synchronized String[] getReservationId() {
112       String[] data = new String[reservations.size()];
113       Enumeration enum = reservations.elements();
114       ReservationEntry e;
115       for(int i = 0; enum.hasMoreElements(); i++ ) {
116          e = (ReservationEntry)enum.nextElement();
117          data[i] = e.id;
118       }
119       return data;
120    }
121    public synchronized int getEarliestReservationTime() {
122       Enumeration enum = reservations.elements();
123       int time = Integer.MAX_VALUE;
124       ReservationEntry e;
125       while( enum.hasMoreElements() ) {
126          e = (ReservationEntry)enum.nextElement();
127          time = Math.min(time,e.start);
128       }
129       return time;
130    }
131    public boolean containsReservationId(String reservationId) {
132       ReservationEntry e = (ReservationEntry) reservations.get(reservationId);
133       return e != null;
134    }
135    public int getReservationTime(String reservationId) {
136       ReservationEntry e = (ReservationEntry) reservations.get(reservationId);
137       return e.start;
138    }
139    public boolean isReservationConsumed(String reservationId) {
140       ReservationEntry e = (ReservationEntry) reservations.get(reservationId);
141       return e.consumed;
142    }
143    public int getReservedAmount(String reservationId) {
144       ReservationEntry e = (ReservationEntry) reservations.get(reservationId);
145       return (e == null) ? 0 : e.amount;
146    }
147    public String getReservingAgent(String reservationId) {
148       ReservationEntry e = (ReservationEntry) reservations.get(reservationId);
149       return (e == null) ? null : e.agent;
150    }
151    public String getReservationGoalId(String reservationId) {
152       ReservationEntry e = (ReservationEntry) reservations.get(reservationId);
153       return (e == null) ? null : e.goalId;
154    }
155    public String getReservationCommsKey(String reservationId) {
156       ReservationEntry e = (ReservationEntry) reservations.get(reservationId);
157       return (e == null) ? null : e.comms_key;
158    }
159    public int getAmountReservedByAgent(String agentId) {
160       Enumeration enum = reservations.elements();
161       int reserved = 0;
162       ReservationEntry e;
163       while( enum.hasMoreElements() ) {
164          e = (ReservationEntry)enum.nextElement();
165          if ( e.agent.equals(agentId) )
166             reserved += e.amount;
167       }
168       return reserved;
169    }
170    public synchronized void changeReservedAmount(String resrvId, int amount) {
171       ReservationEntry e = (ReservationEntry) reservations.get(resrvId);
172       Assert.notNull(e);
173       Assert.notFalse(e.amount >= amount);
174       e.amount = amount;
175    }
176    public synchronized boolean cancelReservation(String resrvId) {
177       ReservationEntry e = (ReservationEntry) reservations.remove(resrvId);
178       return e != null;
179    }
180    public synchronized boolean executeNow(String resrvId, int now) {
181       boolean execute_now = true;
182       ReservationEntry e = (ReservationEntry) reservations.get(resrvId);
183       if ( !e.consumed )
184          return true;
185       else {
186          reservations.remove(resrvId);
187          boolean status = reservationOK(now,e.consumed,e.amount);
188          reservations.put(resrvId,e);
189          return status;
190       }
191    }
192    public synchronized boolean newStartTime(String resrvId, int start) {
193       ReservationEntry e = (ReservationEntry) reservations.remove(resrvId);
194       if ( reserve(resrvId,start,e.consumed,e.amount,
195                    e.agent,e.goalId,e.comms_key) )
196          return true;
197       else {
198          reservations.put(resrvId,e);
199          return false;
200       }
201    }
202    public synchronized boolean reserve(ReservationEntry e) {
203       return reserve(e.id,e.start,e.consumed,e.amount,e.agent,
204          e.goalId,e.comms_key);
205    }
206 
207    public synchronized boolean reserve(String resrvId, int start,
208                                        boolean consumed, int amount,
209                                        String agent, String goalId,
210                                        String comms_key) {
211 
212       ReservationEntry e = (ReservationEntry)reservations.get(resrvId);
213       if ( e != null ) {
214          Assert.notFalse(e.start == start   && e.consumed == consumed &&
215                          e.amount == amount && e.agent.equals(agent) &&
216                          e.goalId.equals(goalId) &&
217                          e.comms_key.equals(comms_key));
218          return true;
219       }
220 
221       if ( !reservationOK(start,consumed,amount) ) return false;
222 
223       e = new ReservationEntry(resrvId,start,consumed,
224                                amount,agent,goalId,comms_key);
225       // Assert.notFalse(reservations.put(observer,e) == null);
226       ReservationEntry xe = (ReservationEntry) reservations.put(resrvId,e);
227       if ( xe != null ) {
228          System.err.println("SuppliedItem reserve: multiple entries " +
229                             "for same observer " + resrvId);
230          System.err.println("Previous = " + xe);
231          System.err.println("Current = " + e);
232       }
233       return true;
234    }
235 
236    public int consumed(String resrvId) {
237       int no = fact.getNumber();
238       ReservationEntry e = (ReservationEntry) reservations.remove(resrvId);
239       int status;
240       if ( !e.consumed )         status = UNCHANGED;
241       else if ( e.amount < no )  status = MODIFY;
242       else                       status = DELETE;
243 
244       if ( status == MODIFY ) fact.setNumber(no-e.amount);
245       return status;
246    }
247    public String toString() {
248       String out = "(";
249       out += ":id " + id + " " +
250              ":link \"" + link + "\" " +
251              ":fact " + fact + " " +
252              ":supplier " + supplier;
253 
254       Enumeration enum = reservations.elements();
255       if ( enum.hasMoreElements() ) {
256          ReservationEntry  entry;
257          out += " :reservations (";
258          while( enum.hasMoreElements() ) {
259             entry = (ReservationEntry)enum.nextElement();
260             out += entry;
261          }
262          out += ")";
263       }
264       out += ")";
265       return out;
266    }
267    public SuppliedItem duplicate(String name, GenSym genSym) {
268       DuplicationTable table = new DuplicationTable(name,genSym);
269       return duplicate(table);
270    }
271    public SuppliedItem duplicate(DuplicationTable table) {
272       Fact f1 = fact.duplicate(table);
273       SuppliedItem item = new SuppliedItem(id,link,supplier,f1);
274 
275       Enumeration enum = reservations.elements();
276       ReservationEntry  e;
277       while( enum.hasMoreElements() ) {
278          e = (ReservationEntry)enum.nextElement();
279          item.reserve(e.id,e.start,e.consumed,
280                       e.amount,e.agent,e.goalId,e.comms_key);
281       }
282       return item;
283    }
284 }