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  /*
25   * @(#)Goal.java 1.00
26   */
27  
28  package zeus.concepts;
29  
30  import java.lang.reflect.*;
31  import java.util.*;
32  import zeus.util.*;
33  
34  /***
35   * The Goal class is an important agent-level conceptual data structure.
36   * Goals describe objectives, namely the achievement of the {@link Fact} object
37   * that it contains, subject to the constraints that are its other variables. <p>
38   *
39   * This class contains many methods that allow the attributes of goal objects
40   * to be set and modified, although typically developers will only call one of 
41   * the constructors.
42   */
43  
44  public class Goal
45  {
46     public static final boolean DISCRETE = true;
47     public static final boolean CONTINUOUS = false;
48     public static final int     DEFAULT_PRIORITY = 1;
49     public static final int     MIN_PRIORITY = 1;
50     public static final int     MAX_PRIORITY = 10;
51  
52     /*** The fact description whose achievement is the objective of the goal */
53     protected Fact     fact = null;
54  
55     protected int      start_time = -1;
56     protected int      end_time = -1;
57     protected double   cost = 0;
58     protected int      invocations = 0;
59     protected boolean  type = DISCRETE;
60     protected int      priority = DEFAULT_PRIORITY;
61     protected String   id = null;
62     protected String   image = null;
63     protected String   desired_by;
64     protected String   root_id = null;
65     protected String[] media = null;
66     protected Time     replyTime = null;
67     protected Time     confirmTime = null;
68     protected String   user_data_type = null;
69     protected Object   user_data = null;
70  
71     protected ResolutionContext context = null;
72     protected SuppliedDb given = null;
73     protected Vector   producer_records = null;
74     protected Vector   consumer_records = null;
75  
76     public Goal(boolean type, String id, Fact fact, String desired_by) {
77        this.type = type;
78        this.id = id;
79        this.fact = new Fact(fact);
80        this.desired_by = desired_by;
81     }
82  
83     public Goal(String id, Fact fact, int end_time, double cost,
84                 String agent, double confirm ) {
85        this.type = DISCRETE;
86        this.id = id;
87        this.fact = new Fact( fact );
88        this.end_time = end_time;
89        this.cost = cost;
90        this.desired_by = agent;
91        this.confirmTime = new Time(confirm);
92     }
93  
94     public Goal(String id, Fact fact, int time, double cost, int priority,
95                 String agent, Time replyTime, Time confirmTime ) {
96        this.type = DISCRETE;
97        this.id = id;
98        this.fact = new Fact( fact );
99        this.end_time = time;
100       this.cost = cost;
101       this.priority = priority;
102       this.desired_by = agent;
103       this.replyTime = replyTime;
104       this.confirmTime = confirmTime;
105    }
106 
107    public Goal(String id, Fact fact, int startTime, int endTime,
108                double cost, int priority, int invocations,
109                String agent, Time replyTime, Time confirmTime) {
110       this.type = CONTINUOUS;
111       this.id = id;
112       this.fact = new Fact( fact );
113       this.end_time = endTime;
114       this.start_time = startTime;
115       this.cost = cost;
116       this.priority = priority;
117       this.invocations = invocations;
118       this.desired_by = agent;
119       this.replyTime = replyTime;
120       this.confirmTime = confirmTime;
121    }
122 
123 
124    public Goal(Goal goal) {
125       if ( goal.getType().equals("discrete") )
126          type = DISCRETE;
127       else
128          type = CONTINUOUS;
129 
130       id = goal.getId();
131       fact = goal.getFact();
132       end_time = goal.getEndTime();
133 
134       if ( type == CONTINUOUS ) {
135          start_time = goal.getStartTime();
136          invocations = goal.getInvocations();
137       }
138 
139       cost = goal.getCost();
140       priority = goal.getPriority();
141       desired_by = goal.getDesiredBy();
142       root_id = goal.getRootId();
143       media = goal.getTargetMedia();
144       replyTime = goal.getReplyTime();
145       confirmTime = goal.getConfirmTime();
146       image = goal.getImage();
147 
148       user_data_type = goal.getUserDataType();
149       user_data = goal.getUserData();
150       context = goal.getResolutionContext();
151       given = goal.getSuppliedDb();
152       producer_records = goal.getProducerRecords();
153       consumer_records = goal.getConsumerRecords();
154    }
155 
156    public Goal duplicate(String name, GenSym genSym) {
157       DuplicationTable table = new DuplicationTable(name,genSym);
158       return duplicate(table);
159    }
160    public Goal duplicate(DuplicationTable table) {
161       Goal g;
162       if ( type == DISCRETE )
163          g = new Goal(id, fact.duplicate(table), end_time, cost,
164                       priority, desired_by, replyTime, confirmTime);
165       else
166          g = new Goal(id, fact.duplicate(table), start_time, end_time, cost,
167                       priority, invocations, desired_by, replyTime,
168                       confirmTime);
169 
170       g.setRootId(this.getRootId());
171       g.setImage(this.getImage());
172       g.setTargetMedia(this.getTargetMedia());
173 
174       g.setUserDataType(this.getUserDataType());
175       g.setUserData(this.getUserData());
176       g.setProducerRecords(this.getProducerRecords());
177       g.setConsumerRecords(this.getConsumerRecords());
178 
179       if ( context != null )
180          g.setResolutionContext(context.duplicate(table));
181 
182       if ( given != null )
183          g.setSuppliedDb(given.duplicate(table));
184 
185       return g;
186    }
187 
188    public String getType() {
189       if ( type ) return "discrete";
190       else return "continuous";
191    }
192 
193    public boolean whichType()       { return type; }
194    public void    setId(String id)  { Assert.notNull(id); this.id = id; }
195    public String  getId()           { return id; }
196    public String  getImage()        { return image; }
197    public Fact    getFact()         { return new Fact(fact); }
198    public String  getFactType()     { return fact.getType(); }
199    public String  getFactId()       { return fact.getId(); }
200  
201    public int     getEndTime()      { return end_time; }
202    public double  getCost()         { return cost; }
203    public int     getPriority()     { return priority; }
204    public String  getDesiredBy()    { return desired_by; }
205 
206    public String  getUserDataType() { return user_data_type; }
207    public Object  getUserData()     { return user_data; }
208 
209    public boolean isDiscrete()      { return type == DISCRETE; }
210    public boolean isContinuous()    { return type == CONTINUOUS; }
211 
212    public void setProducerRecords(Vector records) {
213       this.producer_records = records;
214    }
215 
216    public void setConsumerRecords(Vector records) {
217       this.consumer_records = records;
218    }
219 
220    public void addProducer(String supply_ref, String use_ref, String comms_key,
221                            String producer, String producer_id,
222                            String consumer, String consumer_id) {
223       ProducerRecord entry = new ProducerRecord(supply_ref, use_ref, comms_key,
224          producer, producer_id, consumer, consumer_id);
225       addProducer(entry);
226    }
227 
228    public void addProducer(ProducerRecord entry) {
229       if ( producer_records == null )
230          producer_records = new Vector();
231       producer_records.addElement(entry);
232    }
233 
234    public void addConsumer(String producer, String producer_id,
235                            String consumer, String consumer_id,
236                            String use_ref, String comms_key,
237                            int start, int amount, boolean consumed) {
238       ConsumerRecord entry = new ConsumerRecord(producer, producer_id,
239          consumer, consumer_id, use_ref, comms_key, start, amount, consumed);
240       addConsumer(entry);
241    }
242 
243    public void addConsumer(ConsumerRecord entry) {
244       if ( consumer_records == null )
245          consumer_records = new Vector();
246       consumer_records.addElement(entry);
247    }
248 
249    public void setSuppliedDb(SuppliedDb given) {
250       this.given = given;
251    }
252 
253    public void setResolutionContext(ResolutionContext context) {
254       this.context = context;
255    }
256 
257    public void setUserDataType(String data_type) {
258       this.user_data_type = data_type;
259    }
260 
261    public void setUserData(Object data) {
262       this.user_data = data;
263    }
264 
265    public void setImage(String image) {
266       this.image = image;
267    }
268 
269    public void setFact(Fact fact)  {
270       Assert.notNull(fact);
271       this.fact = fact;
272    }
273 
274    public void setFactType(String type)  {
275       fact.setType(type);
276    }
277 
278    public void setStartTime(int time) {
279       start_time = time;
280    }
281 
282    public void setInvocations(int invocations) {
283       // discrete goal --> no # invocations
284       Assert.notFalse( type == CONTINUOUS );
285       Assert.notFalse( invocations >= 0 );
286       this.invocations = invocations;
287    }
288 
289    public void setEndTime(int time) {
290       end_time = time;
291    }
292 
293    public void setCost(double cost) {
294       Assert.notFalse( cost >= 0 );
295       this.cost = cost;
296    }
297 
298    public void setPriority(int priority) {
299       Assert.notFalse( priority >= MIN_PRIORITY && priority <= MAX_PRIORITY );
300       this.priority = priority;
301    }
302    public void setDesiredBy(String person) {
303       Assert.notNull(person);
304       desired_by = person;
305    }
306 
307    public Vector getProducerRecords() {
308       return producer_records;
309    }
310 
311    public Vector getConsumerRecords() {
312       return consumer_records;
313    }
314 
315    public SuppliedDb getSuppliedDb() {
316       return given;
317    }
318 
319    public ResolutionContext getResolutionContext() {
320       return context;
321    }
322 
323    public int getStartTime() {
324       return start_time;
325    }
326 
327    public int getInvocations()  {
328       // discrete goal --> no # invocations
329       Assert.notFalse( type == CONTINUOUS );
330       return invocations;
331    }
332 
333    public String getRootId() {
334       if ( root_id == null ) return id;
335       else return root_id;
336    }
337 
338    public void setRootId( String rootId ) {
339       Assert.notNull(rootId);
340       this.root_id = rootId;
341    }
342 
343    public void setTargetMedia(String[] media) {
344       this.media = media;
345    }
346 
347    public void setReplyTime(Time value) {
348       replyTime = new Time(value);
349    }
350 
351    public void setConfirmTime(Time value) {
352       confirmTime = new Time(value);
353    }
354 
355    public void setReplyTime(double value) {
356       replyTime = new Time(value);
357    }
358 
359    public void setConfirmTime(double value) {
360       confirmTime = new Time(value);
361    }
362 
363    public Time getReplyTime() {
364       return replyTime;
365    }
366 
367    public Time getConfirmTime() {
368       return confirmTime;
369    }
370 
371    public String[] getTargetMedia() {
372       return media;
373    }
374 
375    public AbilitySpec getAbility() {
376       return new AbilitySpec("goal_generated",fact, 0, 0);
377    }
378 
379    public boolean constrain(Bindings bindings) {
380       return fact.resolve(bindings);
381    }
382 
383    public boolean equals(Goal goal ) {
384       return id.equals( goal.getId() ) &&
385              fact.getId().equals( goal.getFact().getId() ) &&
386              getType().equals( goal.getType() );
387    }
388 
389  public String toSL() {
390      // String s = new String("(action (inform " + desired_by +"((iota "+fact.toSL() ) ;
391      // s+= "( achieve " + fact.toSL() + "(goal_id ("+id +"))))))";
392        // (action (inform Agent ((iota ?goal_instance (achieve ?goal_instance)))))
393 
394         String slType = new String ();
395         if (DISCRETE) slType = "I";
396         if (CONTINUOUS) slType = "PG";
397         if (DISCRETE && CONTINUOUS) slType = "U";
398         if (!DISCRETE && !CONTINUOUS) slType = "U";
399 	
400 	String s = new String ("( " + slType + " " +desired_by + " ("+id+" "+ fact.toSL());
401 	s+= "(parameters :start_time "+start_time + " :end_time "+ end_time;
402         s+= " :cost " + cost +" :priority " + priority;
403 	s+=")))";
404 	// (PG Agent (goal_idID (goal_istance)))
405 
406       return s.trim() ;
407    }
408 
409    public String toString() {
410       //System.out.println("Goal ==\n"+this.toSL());
411       String s = new String("(");
412 
413       s += ":id "   + id + " ";
414       s += ":desired_by " + desired_by + " ";
415       s += ":type " + type + " ";
416       s += ":fact " + fact.toString() + " ";
417 
418       if ( image != null )
419          s += ":image "   + image + " ";
420 
421       if ( type == CONTINUOUS )
422          s += ":start_time " + start_time + " ";
423 
424       s += ":end_time " + end_time + " ";
425       s += ":cost " + cost + " ";
426       s += ":priority " + priority + " ";
427 
428       if ( type == CONTINUOUS )
429          s += ":invocations " + invocations + " ";
430 
431       if ( root_id != null )
432          s += ":root_id " + root_id + " ";
433 
434       if ( media != null ) {
435          s += ":media (";
436          for( int i = 0; i < media.length; i++ )
437             s += "\"" + media[i] + "\"" + " ";
438          s = s.trim() + ")";
439       }
440       if ( replyTime != null )
441          s += ":reply_time " + replyTime + " ";
442 
443       if ( confirmTime != null )
444          s += ":confirm_time " + confirmTime + " ";
445 
446       if ( user_data_type != null ) {
447          s += ":user_data_type \"" + user_data_type + "\" ";
448 
449          if ( user_data != null )
450             s += ":user_data " + Misc.OPAQUE_CHAR + user_data + Misc.OPAQUE_CHAR + " ";
451       }
452 
453       if ( context != null )
454          s += ":context (" + context + ") ";
455 
456       if ( given != null )
457          s += ":given (" + given + ") ";
458 
459       if ( producer_records != null && !producer_records.isEmpty() ) {
460          s += ":producer_records (";
461          for(int i = 0; i < producer_records.size(); i++ )
462             s += producer_records.elementAt(i);
463          s += ") ";
464       }
465 
466       if ( consumer_records != null && !consumer_records.isEmpty() ) {
467          s += ":consumer_records (";
468          for(int i = 0; i < consumer_records.size(); i++ )
469             s += consumer_records.elementAt(i);
470          s += ") ";
471       }
472 
473       return s.trim() + ")";
474    }
475 }