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   * @(#)ZeusAgent.java 1.00
26   */
27  
28  package zeus.agents;
29  
30  import java.awt.*;
31  import java.util.*;
32  import java.io.*;
33  
34  import zeus.util.*;
35  import zeus.concepts.*;
36  import zeus.actors.*;
37  import zeus.actors.event.*;
38  import zeus.actors.rtn.Engine;
39  import zeus.rete.ReteEngine;
40  
41  
42  /***
43   * This class implements the core functionality of all Zeus Task Agents.
44   * Every task agent implementation created by the Zeus toolkit includes a call
45   * to the ZeusAgent constructor, which creates and initialises all the agent's
46   * internal components and stores references to them in its {@link AgentContext}. <p>
47   *
48   * This class is of particular interest to developers because it provides
49   * methods that allow the agent state to be interrogated and changed, as well
50   * listing all the listener classes that can be added to monitor the agent's
51   * state. <p>
52   */
53  
54  public class ZeusAgent extends BasicAgent
55  {
56    /*** Version 1.01 constructor - the componentmask parameter will prevent the
57        creation of unnecessary agent components */
58    public ZeusAgent(String name, String ontology_file, Vector nameservers,
59                     int planner_width, int planner_length, 
60                     boolean hasTasks, boolean hasRules)
61    {
62      super(SystemProps.getProperty("agent.names.agent"),name,nameservers);
63  
64      OntologyDb db = new OntologyDb(context.GenSym());
65      context.set(db);
66  
67      int status = db.openFile(new File(ontology_file));
68      if ( (status & OntologyDb.ERROR_MASK) != 0 ) {
69         System.err.println("File I/O Error: " + db.getError());
70        // System.exit(0);
71      }
72      else if ( (status & OntologyDb.WARNING_MASK) != 0 ) {
73         System.err.println("Warning: " + db.getWarning());
74      }
75  
76      // Create Agent Components
77      new OrganisationDb(context);
78      new ResourceDb(context);
79      new Engine(context);
80      new ProtocolDb(context);
81      
82      // changed by Jaron (v1.04) to fix uninitialised component bug
83      new Planner(context,planner_width,planner_length);
84      new TaskDb(context);
85      new ExecutionMonitor(context);
86  
87      if (hasTasks)
88      {
89        // strictly speaking if there are no tasks, the Planner, TaskDb
90        // and ExecutionMonitor don't need to exist. However, some internal
91        // code does still reference these components whether the agent
92        // has tasks or not - so this selective initialisation has been removed
93      }
94  
95      if (hasRules)
96      {
97        // agent has rules - create rule engine
98        ReteEngine rete = new ReteEngine(context);
99      }
100   }
101 
102   /*** v1.00 Constructor, provided for backwards compatibility */
103   public ZeusAgent(String name, String ont_file, Vector nameservers, int plan_width, int plan_length)
104   {
105     this(name, ont_file, nameservers, plan_width, plan_length, true, true);
106   }
107 
108 
109   public void addProtocol(Vector Protocols) {
110     context.ProtocolDb().addProtocol(Protocols);
111   }
112 
113   public void addProtocol(ProtocolInfo aProtocol) {
114     context.ProtocolDb().addProtocol(aProtocol);
115   }
116 
117   public void addTask(Vector Tasks) {
118     for(int i = 0; i < Tasks.size(); i++ )
119        addTask((AbstractTask)Tasks.elementAt(i));
120   }
121 
122   public void addTask(AbstractTask aTask) {
123     switch(aTask.getType()) {
124        case AbstractTask.PRIMITIVE:
125        case AbstractTask.SUMMARY:
126             context.TaskDb().add((Task)aTask);
127             break;
128        case AbstractTask.BEHAVIOUR:
129             context.ReteEngine().add((ReteKB)aTask);
130             break;
131        case AbstractTask.SCRIPT:
132             break;
133     }
134   }
135 
136   public void addFact(Vector Facts) {
137     context.ResourceDb().add(Facts);
138   }
139 
140    /*** Provides the agent with a new resource fact */
141   public void addFact(Fact aFact) {
142     context.ResourceDb().add(aFact);
143   }
144 
145   public void addAbility(Vector AbilityItems) {
146     context.OrganisationDb().add(AbilityItems);
147   }
148 
149   public void addAbility(AbilityDbItem anAbilityItem ) {
150     context.OrganisationDb().add(anAbilityItem);
151   }
152 
153   public void addRelation(Vector Relations ) {
154     context.OrganisationDb().addRelation(Relations);
155   }
156 
157   public void addRelation(String agent, String aRelation ) {
158     context.OrganisationDb().addRelation(agent,aRelation);
159   }
160 
161   /*** Adds an achievement goal to the agent's co-ordination engine, this
162       will alter the agent's behaviour as it attempts to satisfy the goal */
163   public void achieve(Goal g) {
164     context.Engine().achieve(g);
165   }
166 
167   /*** Instructs the agent to achieve a specified goal */
168   public void achieve(Goal g, String key) {
169     context.Engine().achieve(g,key);
170   }
171 
172   /*** Instructs the agent to acquire the fact referred to in the goal */
173   public void buy(Goal g) {
174     context.Engine().buy(g);
175   }
176 
177   /*** Instructs the agent to sell the fact referred to in the goal */
178   public void sell(Goal g) {
179     context.Engine().sell(g);
180   }
181 
182   /*** Adds a new set of rules to the agent's rule base */
183   public void addRulebase(ReteKB kb) {
184     context.ReteEngine().add(kb);
185   }
186 
187   public void addAbilityMonitor(AbilityMonitor monitor, long type) {
188      context.OrganisationDb().addAbilityMonitor(monitor,type);
189   }
190   public void removeAbilityMonitor(AbilityMonitor monitor, long type) {
191      context.OrganisationDb().removeAbilityMonitor(monitor,type);
192   }
193   public void addRelationMonitor(RelationMonitor monitor, long type) {
194      context.OrganisationDb().addRelationMonitor(monitor,type);
195   }
196   public void removeRelationMonitor(RelationMonitor monitor, long type) {
197      context.OrganisationDb().removeRelationMonitor(monitor,type);
198   }
199   public void addClockMonitor(ClockMonitor monitor, long type) {
200      context.ExecutionMonitor().addClockMonitor(monitor,type);
201   }
202   public void removeClockMonitor(ClockMonitor monitor, long type) {
203      context.ExecutionMonitor().removeClockMonitor(monitor,type);
204   }
205   public void addPlanningMonitor(PlanningMonitor monitor, long type) {
206      context.Planner().addPlanningMonitor(monitor,type);
207   }
208   public void removePlanningMonitor(PlanningMonitor monitor, long type) {
209      context.Planner().removePlanningMonitor(monitor,type);
210   }
211   public void addFactMonitor(FactMonitor monitor, long type) {
212      context.ResourceDb().addFactMonitor(monitor,type);
213   }
214   public void removeFactMonitor(FactMonitor monitor, long type) {
215      context.ResourceDb().removeFactMonitor(monitor,type);
216   }
217   public void addTaskMonitor(TaskMonitor monitor, long type) {
218      context.TaskDb().addTaskMonitor(monitor,type);
219   }
220   public void removeTaskMonitor(TaskMonitor monitor, long type) {
221      context.TaskDb().removeTaskMonitor(monitor,type);
222   }
223   public void addProtocolMonitor(ProtocolMonitor monitor, long type) {
224      context.ProtocolDb().addProtocolMonitor(monitor,type);
225   }
226   public void removeProtocolMonitor(ProtocolMonitor monitor, long type) {
227      context.ProtocolDb().removeProtocolMonitor(monitor,type);
228   }
229 }