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   * Change Log
23   * ----------
24   * Simon 28/0/00 - added a task external field, so that the agent writer can
25   * check to see if an external is specified for this task, and then insert
26   * a line of code to call it if required
27   * Also added three methods to check, get and set the value of external
28   */
29  
30  
31  package zeus.concepts;
32  
33  import java.util.*;
34  import zeus.util.*;
35  import zeus.concepts.fn.*;
36  
37  /***
38   * PrimativeTask is the representation that the Agent Generator uses
39   * to store task specifications and write them into Java using the {@see TaskWriter}
40   */
41  public class PrimitiveTask extends Task {
42      
43      protected Vector consumed = new Vector();
44      protected Vector produced = new Vector();
45      protected Vector ordering = new Vector();
46      
47      public transient Vector _produced = null;
48      
49      protected int active_effect = -1;
50      
51      public PrimitiveTask() {
52          type = PRIMITIVE;
53      }
54      
55      public PrimitiveTask(String name, ValueFunction time, ValueFunction cost,
56      Fact[] produced, Fact[] consumed,
57      LogicalFn[] constraints, Ordering[] ordering) {
58          this();
59          //  System.out.println("init 1 : "  +name);
60          setName(name);
61          setTimeFn(time);
62          setCostFn(cost);
63          setPostconditions(produced); // consumed?
64          setPreconditions(consumed);
65          setConstraints(constraints);
66          setOrdering(ordering);
67      }
68      
69      
70      public PrimitiveTask(String name, String time, String cost,
71      Fact[] produced, Fact[] consumed,
72      LogicalFn[] constraints, Ordering[] ordering) {
73          this();
74          //      System.out.println("init 2 : "  +name);
75          
76          setName(name);
77          setTimeFn(time);
78          setCostFn(cost);
79          setPostconditions(produced);
80          setPreconditions(consumed);
81          setConstraints(constraints);
82          setOrdering(ordering);
83      }
84      
85      
86      
87      public PrimitiveTask(String name, ValueFunction time, ValueFunction cost,
88      Vector produced, Vector consumed,
89      Vector constraints, Vector ordering) {
90          this();
91          //      System.out.println("init 3 : "  +name);
92          
93          setName(name);
94          setTimeFn(time);
95          setCostFn(cost);
96          setPostconditions(produced);
97          setPreconditions(consumed);
98          setConstraints(constraints);
99          setOrdering(ordering);
100     }
101     
102     
103     
104     public PrimitiveTask(PrimitiveTask task) {
105         this();
106         //      System.out.println("init 4 : "  +name);
107         
108         name = task.getName();
109         cost = task.getCostFn();
110         time = task.getTimeFn();
111         setPostconditions( task.getPostconditions() );
112         setPreconditions( task.getPreconditions() );
113         setConstraints( task.getConstraints() );
114         setOrdering( task.getOrdering() );
115         setActiveEffect( task.getActiveEffectPos() );
116     }
117     
118     //added for 1.04 by Simon
119     protected String external = null;
120     
121     public boolean hasExternal() {
122         return (external != null);
123     }
124     
125     
126     public String getExternal()  {
127         return (external);
128     }
129     
130     
131     public void setExternal(String external) {
132         this.external = external;
133     }
134     
135     
136     // end add 1.04
137     
138     
139     
140     public void setPostconditions( Vector List ) {
141         produced.removeAllElements();
142         for(int i = 0; i < List.size(); i++ )
143             produced.addElement(new Fact((Fact)List.elementAt(i)));
144     }
145     
146     
147     
148     public void setPostconditions( Fact[] List ) {
149         produced.removeAllElements();
150         for(int i = 0; i < List.length; i++ )
151             produced.addElement(new Fact(List[i]));
152     }
153     
154     public void setPreconditions( Vector List ) {
155         consumed.removeAllElements();
156         for(int i = 0; i < List.size(); i++ )
157             consumed.addElement(new Fact((Fact)List.elementAt(i)));
158     }
159     
160     public void setPreconditions( Fact[] List ) {
161         consumed.removeAllElements();
162         for(int i = 0; i < List.length; i++ )
163             consumed.addElement(new Fact(List[i]));
164     }
165     
166     public Fact[] getPostconditions() {
167         Fact[] out = new Fact[produced.size()];
168         for(int i = 0; i < produced.size(); i++)
169             out[i] = new Fact((Fact)produced.elementAt(i));
170         return out;
171     }
172     
173     public Fact[] getOriginalPostconditions() {
174         if ( _produced == null ) return getPostconditions();
175         
176         Fact[] out = new Fact[_produced.size()];
177         for(int i = 0; i < _produced.size(); i++)
178             out[i] = new Fact((Fact)_produced.elementAt(i));
179         return out;
180     }
181     
182     public void preprocess() {
183         Fact f;
184         boolean status = false;
185         for(int i = 0; !status && i < consumed.size(); i++ ) {
186             f = (Fact)consumed.elementAt(i);
187             status = f.isReplaced();
188         }
189         if ( !status ) return;
190         
191         _produced = Misc.copyVector(produced);
192         
193         for(int i = 0; i < consumed.size(); i++ ) {
194             f = (Fact)consumed.elementAt(i);
195             if ( f.isReplaced() )
196                 produced.addElement(f);
197         }
198     }
199     
200     public int countPreconditions() {
201         return consumed.size();
202     }
203     
204     
205     public int countPostconditions() {
206         return produced.size();
207     }
208     
209     public int[] numPreconditions() {
210         int[] array = new int[consumed.size()];
211         
212         for(int i = 0; i < consumed.size(); i++)
213             array[i] = ((Fact)consumed.elementAt(i)).getNumber();
214         return array;
215     }
216     
217     
218     public int[] numPostconditions() {
219         int[] array = new int[produced.size()];
220         
221         for(int i = 0; i < produced.size(); i++)
222             array[i] = ((Fact)produced.elementAt(i)).getNumber();
223         return array;
224     }
225     
226     
227     public Fact[] getPreconditions() {
228         Fact[] out = new Fact[consumed.size()];
229         
230         for(int i = 0; i < consumed.size(); i++)
231             out[i] = new Fact((Fact)consumed.elementAt(i));
232         return out;
233     }
234     
235     
236     public Fact getPrecondition(String fid) {
237         Fact f;
238         for(int i = 0; i < consumed.size(); i++) {
239             f = (Fact)consumed.elementAt(i);
240             if ( fid.equals(f.getId()) )
241                 return f;
242         }
243         return null;
244     }
245     
246     
247     public Fact getPostcondition(String fid) {
248         Fact f;
249         for(int i = 0; i < produced.size(); i++) {
250             f = (Fact)produced.elementAt(i);
251             if ( fid.equals(f.getId()) )
252                 return f;
253         }
254         return null;
255     }
256     
257     
258     public Fact getPrecondition(int pos) {
259         return (Fact)consumed.elementAt(pos);
260     }
261     public Fact getPostcondition(int pos) {
262         return (Fact)produced.elementAt(pos);
263     }
264     
265     public int getConsumedPos(Fact fact) {
266         String fid = fact.getId();
267         Fact f;
268         for(int i = 0; i < consumed.size(); i++) {
269             f = (Fact)consumed.elementAt(i);
270             if ( fid.equals(f.getId()) )
271                 return i;
272         }
273         Assert.notNull(null);
274         return -1;
275     }
276     public int getProducedPos(Fact fact) {
277         String fid = fact.getId();
278         Fact f;
279         for(int i = 0; i < produced.size(); i++) {
280             f = (Fact)produced.elementAt(i);
281             if ( fid.equals(f.getId()) )
282                 return i;
283         }
284         Assert.notNull(null);
285         return -1;
286     }
287     
288     public void relaxNumberFields() {
289         Fact f1;
290         ValueFunction fn;
291         for(int i = 0; i < consumed.size(); i++ ) {
292             f1 = (Fact)consumed.elementAt(i);
293             if ( f1.isa(OntologyDb.ENTITY) ) {
294                 fn = f1.getFn(OntologyDb.NUMBER);
295                 if ( fn.getID() == ValueFunction.INT )
296                     f1.setValue(OntologyDb.NUMBER,f1.newVar());
297             }
298         }
299         for(int i = 0; i < produced.size(); i++ ) {
300             f1 = (Fact)produced.elementAt(i);
301             if ( f1.isa(OntologyDb.ENTITY) ) {
302                 fn = f1.getFn(OntologyDb.NUMBER);
303                 if ( fn.getID() == ValueFunction.INT )
304                     f1.setValue(OntologyDb.NUMBER,f1.newVar());
305             }
306         }
307     }
308     
309     public void setOrdering( Vector List ) {
310         ordering.removeAllElements();
311         for( int i = 0; i < List.size(); i++ )
312             ordering.addElement(new Ordering((Ordering)List.elementAt(i)));
313     }
314     
315     public void setOrdering( Ordering[] List ) {
316         ordering.removeAllElements();
317         for( int i = 0; i < List.length; i++ )
318             ordering.addElement(new Ordering(List[i]));
319     }
320     
321     public Ordering[] getOrdering() {
322         Ordering[] out = new Ordering[ordering.size()];
323         
324         for(int i = 0; i < ordering.size(); i++)
325             out[i] = new Ordering((Ordering)ordering.elementAt(i));
326         return out;
327     }
328     
329     public boolean resolve(Bindings bindings) {
330         boolean status = true;
331         ResolutionContext context = getContext();
332         
333         time = time.resolve(context,bindings);
334         if ( time == null ) return false;
335         
336         cost = cost.resolve(context,bindings);
337         if ( cost == null ) return false;
338         
339         for(int i = 0; status && i < consumed.size(); i++ )
340             status &= ((Fact)consumed.elementAt(i)).resolve(context,bindings);
341         
342         if ( !status ) return status;
343         
344         for(int i = 0; status && i < produced.size(); i++ )
345             status &= ((Fact)produced.elementAt(i)).resolve(context,bindings);
346         
347         return status;
348     }
349     
350     public ResolutionContext getContext() {
351         if ( resolution_context != null ) return resolution_context;
352         
353         resolution_context = new ResolutionContext();
354         resolution_context.add(produced);
355         resolution_context.add(consumed);
356         return resolution_context;
357     }
358     
359     public boolean applyConstraints(Bindings bindings) {
360         Bindings local = new Bindings(bindings);
361         if ( !super.applyConstraints(local) ) return false;
362         return resolve(local) && bindings.add(local);
363     }
364     
365     public Fact[][] orderPreconditions() {
366         if ( ordering.isEmpty() ) {
367             Fact[][] result = new Fact[1][consumed.size()];
368             for(int i = 0; i < consumed.size(); i++ )
369                 result[0][i] = (Fact)consumed.elementAt(i);
370             return result;
371         }
372         Vector list = new Vector();
373         Vector curr = new Vector();
374         Vector next = new Vector();
375         Vector cs_set = new Vector();
376         Ordering cs;
377         Fact f1;
378         String fid;
379         
380         
381         for(int i = 0; i < consumed.size(); i++ )
382             curr.addElement(consumed.elementAt(i));
383         
384         for(int i = 0; i < ordering.size(); i++ )
385             cs_set.addElement(ordering.elementAt(i));
386         
387         while( !cs_set.isEmpty() ) {
388             for(int i = 0; i < curr.size(); i++ ) {
389                 f1 = (Fact)curr.elementAt(i);
390                 fid = f1.getId();
391                 for( int j = 0; j < cs_set.size(); j++ ) {
392                     cs = (Ordering)cs_set.elementAt(j);
393                     if ( fid.equals(cs.getRHS()) ) {
394                         next.addElement(f1);
395                         curr.removeElementAt(i--);
396                         break;
397                     }
398                 }
399             }
400             for(int i = 0; i < curr.size(); i++ ) {
401                 f1 = (Fact)curr.elementAt(i);
402                 fid = f1.getId();
403                 for( int j = 0; j < cs_set.size(); j++ ) {
404                     cs = (Ordering)cs_set.elementAt(j);
405                     if ( fid.equals(cs.getLHS()) ) {
406                         cs_set.removeElementAt(j--);
407                     }
408                 }
409             }
410             list.addElement(curr);
411             curr = next;
412             next = new Vector();
413         }
414         if ( !curr.isEmpty() ) list.addElement(curr);
415         
416         Fact[][] result = new Fact[list.size()][];
417         for(int i = 0; i < list.size(); i++ ) {
418             curr = (Vector) list.elementAt(i);
419             result[i] = new Fact[curr.size()];
420             for( int j = 0; j < curr.size(); j++ )
421                 result[i][j] = (Fact)curr.elementAt(j);
422         }
423         return result;
424     }
425     
426     
427     public String toString() {
428         String s = "(:" + TaskTypes[type] + " " + name + " ";
429         
430         s += ":time (" + time + ") ";
431         s += ":cost (" + cost + ") ";
432         
433         if ( !consumed.isEmpty() ) {
434             s += ":consumed_facts (";
435             for(int i = 0; i < consumed.size(); i++ )
436                 s += consumed.elementAt(i);
437             s += ") ";
438         }
439         if ( !produced.isEmpty() ) {
440             s += ":produced_facts (";
441             for(int i = 0; i < produced.size(); i++ )
442                 s += produced.elementAt(i);
443             s += ") ";
444         }
445         if ( !constraints.isEmpty() ) {
446             s += ":constraints (";
447             for(int i = 0; i < constraints.size(); i++ )
448                 s += "(" + constraints.elementAt(i) + ")";
449             s += ") ";
450         }
451         if ( !ordering.isEmpty() ) {
452             s += ":ordering (";
453             for(int i = 0; i < ordering.size(); i++ )
454                 s += ordering.elementAt(i);
455             s += ") ";
456         }
457         return s.trim() + ")";
458     }
459     
460     public String pprint(int sp) {
461         String suffix, prefix;
462         String tabs = Misc.spaces(sp);
463         String eol  = "\n" + tabs + " ";
464         
465         String s = "(:" + TaskTypes[type] + " " + name + eol;;
466         
467         s += ":time (" + time + ")" + eol;
468         s += ":cost (" + cost + ")" + eol;
469         
470         if ( !consumed.isEmpty() ) {
471             prefix = ":consumed_facts ";
472             suffix = Misc.spaces(1 + sp + prefix.length());
473             s += prefix + "(";
474             for(int i = 0; i < consumed.size(); i++ )
475                 s += ((Fact)consumed.elementAt(i)).pprint(1+suffix.length()) +
476                 "\n" + suffix + " ";
477             s = s.trim() + "\n" + suffix + ")" + eol;
478         }
479         if ( !produced.isEmpty() ) {
480             prefix = ":produced_facts ";
481             suffix = Misc.spaces(1 + sp + prefix.length());
482             s += prefix + "(";
483             for(int i = 0; i < produced.size(); i++ )
484                 s += ((Fact)produced.elementAt(i)).pprint(1+suffix.length()) +
485                 "\n" + suffix + " ";
486             s = s.trim() + "\n" + suffix + ")" + eol;
487         }
488         if ( !constraints.isEmpty() ) {
489             prefix = ":constraints ";
490             suffix = Misc.spaces(1 + sp + prefix.length());
491             s += prefix + "(" + "\n" + suffix + " ";
492             for(int i = 0; i < constraints.size(); i++ )
493                 s += "(" + constraints.elementAt(i) + ")" +
494                 "\n" + suffix + " ";
495             s = s.trim() + "\n" + suffix + ")" + eol;
496         }
497         if ( !ordering.isEmpty() ) {
498             prefix = ":ordering ";
499             suffix = Misc.spaces(1 + sp + prefix.length());
500             s += prefix + "(";
501             for(int i = 0; i < ordering.size(); i++ )
502                 s += ((Ordering)ordering.elementAt(i)).pprint(1+suffix.length()) +
503                 "\n" + suffix + " ";
504             s = s.trim() + "\n" + suffix + ")" + eol;
505         }
506         return tabs + s.trim() + "\n" + tabs + ")";
507     }
508     
509     public boolean isValid() {
510         return true;
511     }
512     
513     public void setActiveEffect(int j) {
514         Assert.notFalse((j >= -1) && (j < produced.size()));
515         active_effect = j;
516     }
517     public Fact getActiveEffect() {
518         return getPostcondition(active_effect);
519     }
520     public int getActiveEffectPos() {
521         return active_effect;
522     }
523     
524     public AbstractTask duplicate(DuplicationTable table) {
525         // System.out.println("duplicating");
526         Fact[]       Xconsumed = new Fact[consumed.size()];
527         Fact[]       Xproduced = new Fact[produced.size()];
528         LogicalFn[]  Xconstraints = new LogicalFn[constraints.size()];
529         Ordering[]   Xordering = new Ordering[ordering.size()];
530         
531         ValueFunction Xtime = time.duplicate(table);
532         ValueFunction Xcost = cost.duplicate(table);
533         
534         for(int i = 0; i < consumed.size(); i++ )
535             Xconsumed[i] = ((Fact)consumed.elementAt(i)).duplicate(table);
536         
537         for(int i = 0; i < produced.size(); i++ )
538             Xproduced[i] = ((Fact)produced.elementAt(i)).duplicate(table);
539         
540         for(int i = 0; i < constraints.size(); i++ )
541             Xconstraints[i] = (LogicalFn)((LogicalFn)constraints.elementAt(i)).duplicate(table);
542         
543         for(int i = 0; i < ordering.size(); i++ )
544             Xordering[i] = ((Ordering)ordering.elementAt(i)).duplicate(table);
545         
546         PrimitiveTask task = new PrimitiveTask(name,Xtime,Xcost,Xproduced,
547         Xconsumed,Xconstraints,Xordering);
548         
549         task.setActiveEffect(active_effect);
550         
551         return task;
552     }
553     
554    
555     
556 }