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 SuppliedDb extends Hashtable {
30     protected OntologyDb ontology;
31  
32     public SuppliedDb(OntologyDb ontology) {
33        Assert.notNull(ontology);
34        this.ontology = ontology;
35     }
36     public SuppliedDb(SuppliedDb db) {
37        this.ontology = db.getOntology();
38        add(db);
39     }
40     OntologyDb getOntology() {
41        return ontology;
42     }
43  
44     public synchronized boolean add(SuppliedDb db) {
45        SuppliedItem item;
46        Enumeration e;
47        Hashtable tb;
48        boolean status = true;
49        Enumeration enum = db.elements();
50  
51        while( status && enum.hasMoreElements() ) {
52           tb = (Hashtable)enum.nextElement();
53           e = tb.elements();
54           while( status && e.hasMoreElements() ) {
55              item = (SuppliedItem)e.nextElement();
56              status &= add(item);
57           }
58        }
59        return status;
60     }
61     public synchronized boolean add(Vector List) {
62        boolean status = true;
63        for(int i = 0; status && i < List.size(); i++ )
64           status &= add((SuppliedItem)List.elementAt(i));
65        return status;
66     }
67     public synchronized boolean add(SuppliedItem[] List) {
68        boolean status = true;
69        for(int i = 0; status && i < List.length; i++ )
70           status &= add(List[i]);
71        return status;
72     }
73     public synchronized boolean add(SuppliedItem item) {
74        Assert.notNull(item);
75  
76        String type = item.getFact().getType();
77        Hashtable table;
78  
79        if ( (table = (Hashtable)this.get(type)) == null ) {
80           table = new Hashtable();
81           this.put(type,table);
82        }
83        SuppliedItem db_item;
84        db_item = (SuppliedItem)table.get(item.getId());
85        if ( db_item == null ) {
86           table.put(item.getId(),item);
87           return true;
88        }
89        else {
90           ReservationEntry[] entry = item.getReservations();
91           boolean status = true;
92           for(int i = 0; status && i < entry.length; i++ )
93              status &= db_item.reserve(entry[i].id,entry[i].start,
94                                        entry[i].consumed,entry[i].amount,
95                                        entry[i].agent,entry[i].goalId,
96                                        entry[i].comms_key);
97           return status;
98        }
99     }
100 
101    public synchronized void del(Vector List) {
102       for(int i = 0; i < List.size(); i++ )
103          del( (SuppliedItem)List.elementAt(i));
104    }
105    public synchronized void del(SuppliedItem[] List) {
106       for(int i = 0; i < List.length; i++ )
107          del(List[i]);
108    }
109    public synchronized void del(SuppliedItem item) {
110       Assert.notNull(item);
111 
112       String type = item.getFact().getType();
113       Hashtable table;
114 
115       if ( (table = (Hashtable)this.get(type)) == null )
116          return;
117 
118       SuppliedItem db_item;
119       Enumeration enum = table.elements();
120       while( enum.hasMoreElements() ) {
121          db_item = (SuppliedItem)enum.nextElement();
122          if ( db_item.equals(item) ) {
123             table.remove(db_item.getId());
124             break;
125          }
126       }
127       if ( table.isEmpty() ) this.remove(type);
128    }
129 
130    public synchronized int findAll(SuppliedRequester rec, Fact fact,
131                                    int precond_position, int required) {
132       Hashtable table;
133       SuppliedItem item;
134       Fact f1, f2;
135 
136       Assert.notFalse(required > 0);
137 
138       if ( (table = (Hashtable)this.get(fact.getType())) == null )
139          return required;
140 
141       int start = rec.getStartTime();
142       boolean consumed = !fact.isReadOnly();
143 
144       Enumeration enum = table.elements();
145       Bindings b = new Bindings();
146       while(required > 0 && enum.hasMoreElements() ) {
147          item = (SuppliedItem)enum.nextElement();
148          f1 = item.getFact();
149          b.clear();
150          int available;
151          if ( (available = item.unreservedAmount(start,consumed)) > 0 &&
152               f1.unifiesWith(fact,b) && rec.applyConstraints(b) ) {
153             Assert.notFalse( rec.setSupplier(precond_position,
154                                              Math.min(available,required),
155                                              item) );
156             required = required - Math.min(available,required);
157 
158             Core.DEBUG(3,"SRDb Required to find Fact:\n" + fact);
159             Core.DEBUG(3,"SRDb Supplied Item:\n" + item +
160                          "\nassigned to rec " + rec);
161          }
162       }
163       return required;
164    }
165 
166 
167    public synchronized void allocateResources(SuppliedRequester rec) {
168       int position, required;
169 
170       PrimitiveTask task = rec.getTask();
171       Fact[][] consumed = task.orderPreconditions();
172       boolean status = true;
173 
174       for(int i = 0; status && i < consumed.length; i++) {
175          for(int j = 0; j < consumed[i].length; j++) {
176             if ( !consumed[i][j].isNegative() ) {
177                position = task.getConsumedPos(consumed[i][j]);
178                required = rec.noRequiredItems(position);
179                if ( required > 0)
180                   status &= (findAll(rec,consumed[i][j],position,required) == 0);
181             }
182          }
183       }
184    }
185    public synchronized int allocateResource(SuppliedRequester rec,
186                                             int position, int amount) {
187       PrimitiveTask task = rec.getTask();
188       Fact fact = task.getPrecondition(position);
189       return findAll(rec,fact,position,amount);
190    }
191 
192    public synchronized Fact evalLocal(Fact fact) {
193       Fact[] answer = all(fact);
194       if ( answer == null || answer.length == 0 ) return null;
195       Fact result = new Fact(answer[0]);
196       for(int i = 1; i < answer.length; i++ )
197          Assert.notFalse( result.disjoin(answer[i]) );
198       return result;
199    }
200 
201    public synchronized Fact[] all(Fact fact) {
202       Hashtable table;
203       SuppliedItem item;
204       Fact f1;
205 
206       if ( (table = (Hashtable)this.get(fact.getType())) == null )
207          return null;
208 
209       Vector answer = new Vector();
210       Bindings b = new Bindings();
211       Enumeration enum = table.elements();
212       while( enum.hasMoreElements() ) {
213          item = (SuppliedItem)enum.nextElement();
214          f1 = item.getFact();
215          b.clear();
216          if ( f1.unifiesWith(fact,b) )
217             answer.addElement(f1);
218       }
219       Fact[] results = new Fact[answer.size()];
220       for(int i = 0; i < answer.size(); i++ )
221          results[i] = (Fact) answer.elementAt(i);
222       return results;
223    }
224 
225    public synchronized Fact any(Fact fact) {
226       Fact[] answer = all(fact);
227       if ( answer == null ) return null;
228       int pos = (int) (Math.random()*answer.length);
229       return answer[pos];
230    }
231 
232    public synchronized boolean contains(Fact fact, int start) {
233       Hashtable table;
234       SuppliedItem item;
235       Fact f1;
236 
237       if ( (table = (Hashtable)this.get(fact.getType())) == null )
238          return false;
239 
240       Bindings b = new Bindings();
241       int required = fact.getNumber();
242       boolean consumed = !fact.isReadOnly();
243       int available;
244       Enumeration enum = table.elements();
245       while( enum.hasMoreElements() && required > 0 ) {
246          item = (SuppliedItem)enum.nextElement();
247          b.clear();
248          f1 = item.getFact();
249          if ( (available = item.unreservedAmount(start,consumed)) > 0 &&
250               f1.unifiesWith(fact,b) ) {
251             required = required - Math.min(available,required);
252          }
253       }
254       return required <= 0;
255    }
256 
257    public boolean cancelReservation(String resrvId) {
258       SuppliedItem item;
259       boolean status = false;
260       Enumeration enum = this.elements();
261       Enumeration e;
262       Hashtable tb;
263       while( !status && enum.hasMoreElements() ) {
264          tb = (Hashtable)enum.nextElement();
265          e = tb.elements();
266          while( !status && e.hasMoreElements() ) {
267             item = (SuppliedItem) e.nextElement();
268             status = item.cancelReservation(resrvId);
269          }
270       }
271       return status;
272    }
273 
274    public synchronized SuppliedItem getSuppliedItem(String itemId) {
275       SuppliedItem item;
276       Enumeration enum = this.elements();
277       Enumeration e;
278       Hashtable tb;
279       while( enum.hasMoreElements() ) {
280          tb = (Hashtable)enum.nextElement();
281          e = tb.elements();
282          while( e.hasMoreElements() ) {
283             item = (SuppliedItem) e.nextElement();
284             if ( item.getId().equals(itemId) )
285                return item;
286          }
287       }
288       return null;
289    }
290 
291    public synchronized boolean isReserved(String itemId) {
292       SuppliedItem item  = getSuppliedItem(itemId);
293       return item.isReserved();
294    }
295 
296    public synchronized ReservationEntry[] getReservations(String producer,
297                                                           String consumer) {
298       Vector data = new Vector();
299       SuppliedItem item;
300       Enumeration enum = this.elements();
301       Enumeration e;
302       Hashtable tb;
303       ReservationEntry[] out;
304       while( enum.hasMoreElements() ) {
305          tb = (Hashtable)enum.nextElement();
306          e = tb.elements();
307          while( e.hasMoreElements() ) {
308             item = (SuppliedItem) e.nextElement();
309             if ( item.getSupplier().equals(producer) ) {
310                out = item.getReservations(consumer);
311                for(int i = 0; i < out.length; i++ )
312                   data.addElement(out[i]);
313             }
314          }
315       }
316       ReservationEntry[] output = new ReservationEntry[data.size()];
317       for(int i = 0; i < data.size(); i++ )
318          output[i] = (ReservationEntry)data.elementAt(i);
319       return output;
320    }
321 
322    public String toString() {
323       String out = "";
324       Enumeration enum = this.elements();
325       Enumeration e;
326       Hashtable tb;
327       while( enum.hasMoreElements() ) {
328          tb = (Hashtable)enum.nextElement();
329          e = tb.elements();
330          while( e.hasMoreElements() ) {
331             out += e.nextElement();
332          }
333       }
334       return out;
335    }
336    public SuppliedDb duplicate(String name, GenSym genSym) {
337       DuplicationTable table = new DuplicationTable(name,genSym);
338       return duplicate(table);
339    }
340    public SuppliedDb duplicate(DuplicationTable table) {
341       SuppliedItem item;
342       SuppliedDb db = new SuppliedDb(ontology);
343 
344       Enumeration enum = this.elements();
345       Enumeration e;
346       Hashtable tb;
347       while( enum.hasMoreElements() ) {
348          tb = (Hashtable)enum.nextElement();
349          e = tb.elements();
350          while( e.hasMoreElements() ) {
351             item = (SuppliedItem)e.nextElement();
352             db.add(item.duplicate(table));
353          }
354       }
355       return db;
356    }
357 }