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   * @(#)ZeusAgentContext.java 1.00
26   */
27  
28  package zeus.actors;
29  
30  import java.util.*;
31  import zeus.util.*;
32  import zeus.concepts.*;
33  import zeus.rete.ReteEngine;
34  import zeus.actors.rtn.*;
35  import zeus.agents.ZeusExternal;
36  import zeus.agents.BasicAgent;
37  import zeus.agents.BasicAgentUI;
38  import zeus.actors.outtrays.*;
39  import zeus.actors.factories.*;
40  import java.net.*;
41  
42  
43  /***
44    Every Zeus Agent possesses one implementation of the AgentContext interface
45    whose role is to hold references to the objects that implement the agent's internal components.
46    <p>
47    
48    ZeusAgentContext is the default implementation that is used for normal agents 
49    generated by the AgentGenerator. All of the components of an agent that we have 
50    implemented so far are expected to be present in an agent of this sort.
51   *Changes
52   *Simon adds whereAmI() to allow service descriptions to be done 22/9/02
53   
54   */
55  
56  public class ZeusAgentContext implements AgentContext
57  {
58    public  boolean share_plan, execute_earliest;
59    public  double  registration_timeout, facilitator_timeout, address_timeout,
60                    accept_timeout, addressbook_refresh, facilitator_refresh,
61                    replan_period;
62  
63  
64    /*** Stores the agent's own list of known Name Servers */
65    protected Vector           nameservers = new Vector();
66  
67    /*** Stores the agent's own list of known Facilitators */
68    protected Vector           facilitators = new Vector();
69    
70  
71    /*** Stores agent identities and the location the agent believes they currenty
72        reside at  */
73    protected AddressBook      addressBook = null;
74  
75    /*** The component that implements the agent's communication mechanism*/
76    protected MailBox          mbox = null;
77  
78    /*** The component that determines what actions are performed on the receipt
79        of messages */
80    protected MsgHandler       msgHandler = null;
81  
82    /*** A rule engine based on the classic <a href="http://www.haley.com/framed/ReteAlgorithm.html" target="alexandria_uri">http://www.haley.com/framed/ReteAlgorithm.html"> Rete Algorithm</a>*/
83    protected ReteEngine       reteEngine = null;
84  
85    /*** The co-ordination engine component, this drives the agent's social and
86        problem solving behaviour */
87    protected Engine           engine = null;
88  
89    /*** This component monitors the state of the tasks executed by the agent */
90    protected ExecutionMonitor monitor = null;
91  
92    /*** This component maintains and enacts the agent's diary of schedules goals */
93    protected Planner          planner = null;
94  
95    /*** This stores the organisational relationships believed by the agent */
96    protected OrganisationDb   organisationDb = null;
97  
98    /*** This stores the definitions of the tasks the agent knows it is capable of
99        performing */
100   protected TaskDb           taskDb = null;
101 
102   /*** This stores the resource objects currently owned by the agent */
103   protected ResourceDb       resourceDb = null;
104 
105   /*** This holds the agents local copy of the concepts that form the 
106       application ontology */
107   protected OntologyDb       ontologyDb = null;
108 
109   /*** A reference to an implementation of wrapper class that connects the
110       agent with an external data source (like a database) */
111   protected ExternalDb       externalDb = null;
112 
113   /*** A reference to the class that implements the agent's external interface,
114       this is typically used to connect GUI front-ends to the agent. */
115   protected ZeusExternal     zeusExternal = null;
116 
117   /*** A reference to a class implementing some shared agent methods */
118   protected BasicAgent       agent = null;
119 
120   /*** A reference to the AgentViewer class that provides a visual depiction 
121       of the state of the agent's components */
122   protected BasicAgentUI     agentUI = null;
123 
124   /*** This stores the protocols and strategies known the agent */
125   protected ProtocolDb       protocolDb = null;
126 
127   /*** The agent's local unique identifier generator */
128   protected GenSym           genSym = null;
129 
130   /*** The agent's local time-keeping model */
131   protected Clock            clock = null;
132 
133   /*** The agent's identity - this should be unique */
134   protected String           name = null;
135 
136   /*** The agent's functional type: one of "Nameserver", "Facilitator", "Visualiser",
137       "DbProxy" or "Agent" */
138   protected String           type = null;
139 
140   /*** Internal variable used by co-ordination engine components */
141   protected Hashtable        queryTable = new Hashtable();
142   
143  /*** 
144     class constructor <p> 
145     @param name is a string containing the name of the agent (ie. "myAgent") which must be 
146     unique in this naming space
147     @param type is the type of the agent - usually something like "ANServer","Facilitator" or 
148     "FIPA_IIOP"<p>
149     If either of these two parameters are null an IllegalArguementException will be thrown.
150     <p> 
151     TO DO: the IllegalArguementException should be part of the method declaration.
152     */
153   public ZeusAgentContext(String name, String type) {
154      Assert.notNull(name);
155      Assert.notNull(type);
156 
157      this.name = name;
158      this.type = type;
159 
160      share_plan           = SystemProps.getState("share.plan",true);
161      execute_earliest     = SystemProps.getState("execute.earliest",true);
162      registration_timeout = SystemProps.getDouble("registration.timeout",3.0);
163      facilitator_timeout  = SystemProps.getDouble("facilitator.timeout",0.5);
164      address_timeout      = SystemProps.getDouble("address.timeout",0.5);
165      accept_timeout       = SystemProps.getDouble("accept.timeout",1.5);
166      addressbook_refresh  = SystemProps.getDouble("addressbook.refresh",5.0);
167      facilitator_refresh  = SystemProps.getDouble("facilitator.refresh",5.0);
168      replan_period        = SystemProps.getDouble("replan.period",2.0);
169      genSym = new GenSym(name,true);
170   }
171   
172 
173   public boolean getSharePlan() {
174         return share_plan;}
175         
176         
177   public boolean getExecuteEarliest() {
178         return execute_earliest; }
179   
180   
181   public double getAddressBookRefresh() { 
182         return addressbook_refresh; }
183         
184         
185   public double getAddressTimeout () {
186         return address_timeout;}   
187         
188         
189   public double getReplanPeriod () {
190         return replan_period; } 
191     
192     
193   public double getRegistrationTimeout() {
194         return registration_timeout; }
195         
196         
197   public double getFacilitatorTimeout() {
198         return facilitator_timeout; } 
199         
200                 
201   public double getAcceptTimeout() { 
202         return accept_timeout; }
203         
204         
205   public double getFacilitatorRefresh() {
206         return facilitator_refresh; }
207 
208 
209   public void setSharePlan(boolean share_plan) { 
210         this.share_plan = share_plan; }
211         
212         
213   public void setExecuteEarliest(boolean execute_earliest) { 
214         this.execute_earliest = execute_earliest; }
215   
216   
217   public void setAddressBookRefresh(double val) { 
218         this.addressbook_refresh = val; }
219         
220         
221   public void setAddressTimeout (double val) {
222         this.address_timeout = val; }
223         
224         
225   public void setReplanPeriod (double val) { 
226         this.replan_period = val; }
227         
228         
229   public void setRegistrationTimeout(double val) {
230         this.registration_timeout = val;}
231         
232         
233   public void setFacilitatorTimeout(double val) {
234         this.facilitator_timeout = val; }
235     
236     
237   public void setAcceptTimeout(double val) { 
238         this.accept_timeout = val; }
239         
240         
241   public void setFacilitatorRefresh(double val) {
242         this.facilitator_refresh = val; } 
243   
244 
245   public void setFacilitators( Vector input ) {
246     Assert.notNull(input);
247     facilitators.removeAllElements();
248     for(int i = 0; i < input.size(); i++ )
249        addFacilitator((String)input.elementAt(i));
250   }
251   
252   
253   public void addFacilitator(String agent) {
254      Assert.notNull(agent);
255      if ( !facilitators.contains(agent) )
256         facilitators.addElement(agent);
257   }
258   
259   
260   public void removeFacilitator(String agent) {
261      Assert.notNull(agent);
262      facilitators.removeElement(agent);
263   }
264 
265 
266   public void setNameservers(Vector input) {
267     Assert.notNull(input);
268     nameservers.removeAllElements();
269     for(int i = 0; i < input.size(); i++ )
270        addNameserver((Address)input.elementAt(i));
271   }
272   
273   
274   public void addNameserver(Address address) {
275      Assert.notNull(address);
276      if ( !nameservers.contains(address) )
277         nameservers.addElement(address);
278   }
279   
280   
281   public void removeNameserver(String address) {
282      Assert.notNull(address);
283      nameservers.removeElement(address);
284   }
285 
286 
287   public void set(AddressBook addressBook) {
288     Assert.notNull(addressBook);
289     this.addressBook = addressBook;
290   }
291 
292 
293   public void set(ProtocolDb protocolDb) {
294      Assert.notNull(protocolDb);
295      this.protocolDb = protocolDb;
296   }
297 
298 
299   public void set(MailBox mbox) {
300     Assert.notNull(mbox);
301     this.mbox = mbox;
302   }
303 
304 
305   public void set(MsgHandler msgHandler) {
306     Assert.notNull(msgHandler);
307     this.msgHandler = msgHandler;
308   }
309 
310 
311   public void set(Engine engine) {
312     Assert.notNull(engine);
313     this.engine = engine;
314   }
315 
316 
317   public void set(ReteEngine reteEngine) {
318     Assert.notNull(reteEngine);
319     this.reteEngine = reteEngine;
320   }
321 
322 
323   public void set(ExecutionMonitor monitor) {
324     Assert.notNull(monitor);
325     this.monitor = monitor;
326   }
327 
328 
329   public void set(Planner planner) {
330     Assert.notNull(planner);
331     this.planner = planner;
332   }
333 
334 
335   public void set(OrganisationDb db) {
336     Assert.notNull(db);
337     this.organisationDb = db;
338   }
339 
340 
341   public void set(TaskDb taskDb) {
342     Assert.notNull(taskDb);
343     this.taskDb = taskDb;
344   }
345 
346 
347   public void set(ResourceDb resourceDb) {
348     Assert.notNull(resourceDb);
349     this.resourceDb = resourceDb;
350   }
351 
352 
353   public void set(OntologyDb ontologyDb) {
354     Assert.notNull(ontologyDb);
355     this.ontologyDb = ontologyDb;
356   }
357 
358 
359   public void set(BasicAgent agent) {
360     Assert.notNull(agent);
361     this.agent = agent;
362   }
363 
364 
365   public void set(BasicAgentUI agentUI) {
366     Assert.notNull(agentUI);
367     this.agentUI = agentUI;
368   }
369 
370 
371   public void set(ZeusExternal zeusExternal) {
372     Assert.notNull(zeusExternal);
373     this.zeusExternal = zeusExternal;
374   }
375 
376 
377   public void set(ExternalDb externalDb) {
378     Assert.notNull(externalDb);
379     this.externalDb = externalDb;
380   }
381 
382 
383   public void set(Clock clock) {
384     Assert.notNull(clock);
385     this.clock = clock;
386   }
387   
388   
389   
390 
391   public double           now()              { return clock != null ? clock.currentTime().getTime() : -1; }
392   public Time             currentTime()      { return clock != null ? clock.currentTime() : null; }
393   public Time             time(long ctm)     { return clock != null ? clock.time(ctm) : null; }
394   public long             getClockStep()     { return clock != null ? clock.getIncrement() : -1; }
395 
396   public String           newId()            { return genSym.newId();  }
397   public String           newId(String tag)  { return genSym.newId(tag);  }
398 
399   public String           whatami()          { return type;  }
400   public String           whoami()           { return name;  }
401 
402    
403   public Hashtable        queryTable()       { return queryTable; }
404   public Vector           facilitators()     { return facilitators; }
405   public Vector           nameservers()      { return nameservers; }
406   public AddressBook      AddressBook()      { return addressBook; }
407   public MailBox          MailBox()          { return mbox; }
408   public MsgHandler       MsgHandler()       { return msgHandler; }
409   public ReteEngine       ReteEngine()       { return reteEngine; }
410   public Engine           Engine()           { return engine; }
411   public ExecutionMonitor ExecutionMonitor() { return monitor; }
412   public Planner          Planner()          { return planner; }
413   public OrganisationDb   OrganisationDb()   { return organisationDb; }
414   public TaskDb           TaskDb()           { return taskDb; }
415   public ResourceDb       ResourceDb()       { return resourceDb; }
416   public OntologyDb       OntologyDb()       { return ontologyDb; }
417   public ExternalDb       ExternalDb()       { return externalDb; }
418   public ProtocolDb       ProtocolDb()       { return protocolDb; }
419   public ZeusExternal     ZeusExternal()     { return zeusExternal; }
420   public BasicAgentUI     AgentUI()          { return agentUI; }
421   public BasicAgent       Agent()            { return agent; }
422   public GenSym           GenSym()           { return genSym; }
423   public Clock            Clock()            { return clock; }
424 
425 
426   public Hashtable        getQueryTable()       { return queryTable; }
427   public Vector           getFacilitators()     { return facilitators; }
428   public Vector           getNameservers()      { return nameservers; }
429   public AddressBook      getAddressBook()      { return addressBook; }
430   public MailBox          getMailBox()          { return mbox; }
431   public MsgHandler       getMsgHandler()       { return msgHandler; }
432   public ReteEngine       getReteEngine()       { return reteEngine; }
433   public Engine           getEngine()           { return engine; }
434   public ExecutionMonitor getExecutionMonitor() { return monitor; }
435   public Planner          getPlanner()          { return planner; }
436   public OrganisationDb   getOrganisationDb()   { return organisationDb; }
437   public TaskDb           getTaskDb()           { return taskDb; }
438   public ResourceDb       getResourceDb()       { return resourceDb; }
439   public OntologyDb       getOntologyDb()       { return ontologyDb; }
440   public ExternalDb       getExternalDb()       { return externalDb; }
441   public ProtocolDb       getProtocolDb()       { return protocolDb; }
442   public ZeusExternal     getZeusExternal()     { return zeusExternal; }
443   public BasicAgentUI     getAgentUI()          { return agentUI; }
444   public BasicAgent       getAgent()            { return agent; }
445   public GenSym           getGenSym()           { return genSym; }
446   public Clock            getClock()            { return clock; }
447 
448 
449   /*** 
450     use the getTransportFactory to get a transportFactory that your agent 
451     can use to get a message transport for a particular address that it 
452     wants to send a message to. <p>
453     Once you have the TransportFactory you will be able to use it to get the specific
454     transport (OutTray) that you need 
455     @see zeus.actors.OutTray
456     @see zeus.actors.service.TransportFactory
457     @see zeus.actors.service.IIOP_Z_HTTP_TransportFactory 
458     */
459   public TransportFactory getTransportFactory () {
460     IIOP_Z_HTTP_TransportFactory fact = new IIOP_Z_HTTP_TransportFactory(); 
461     fact.setContext(this); 
462     return (TransportFactory) fact;
463   }
464   
465   
466   /***
467     return the inTray for this agent 
468     @since 1.1
469     @returns the object responsible for receiving messages
470     */
471   public InTray getInTray() { 
472     return mbox.getInTray(); 
473   }
474 
475   /***  whereAmI should return the deployed address of the agent -
476    * implemented as a TCP/IP address ....
477    * would be better implemented from a config file
478    *this is used to generate service descriptions 
479    */
480   public String whereAmI() {
481       try {
482 
483         InetAddress ip = InetAddress.getLocalHost();
484         String host = ip.getHostAddress();
485         return (host); }
486       catch (Exception e) {
487           return (new String ("host.discovery.failed.here")); 
488       }
489   }  
490   
491 
492   
493 
494 }