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   * @(#)b1.java 1.03b
24   */
25  
26  package zeus.actors.graphs;
27  
28  import java.util.*;
29  import zeus.util.*;
30  import zeus.concepts.*;
31  import zeus.actors.*;
32  import zeus.actors.rtn.*;
33  import zeus.actors.rtn.util.*;
34  
35  public class b1 extends Node {
36  /***
37     Purpose: for a predefined fraction of the period (confirm_time - now)
38     subscribe to the facilitator to stream list of all agents with required
39     ability: collect set of agents received
40  */
41  
42  // ST 050500 1.03bB node description due to CVB
43     private String node_desc = "do/find sellers; do\find service";
44     
45     
46     public final String getDesc()
47        {return node_desc;}
48     
49     
50     public final void setDesc(String node_desc) 
51        {this.node_desc = node_desc;}
52     // ST 050500 1.03bE
53     
54  
55     public b1() {
56        super("b1");
57     }
58  
59     // local memory
60     static final double FRACTION = 0.50;
61     boolean proceed_immediately;
62  
63     protected int exec() {
64       
65        OrganisationDb db = context.OrganisationDb();
66        
67        OntologyDb ontology = context.OntologyDb();
68        Engine engine = context.Engine();
69        MailBox mbox = context.MailBox();
70  
71        DStruct ds = (DStruct)input;
72        // assume ds.goal has one element only
73         debug ("1"); 
74        Goal goal = (Goal)ds.goal.elementAt(0);
75        String type = goal.getFactType();
76        double t = goal.getConfirmTime().getTime();
77        double now = context.now();
78        debug ("2"); 
79        if ( now >= t ) return FAIL;
80  
81        // if no facilitators are listed, then directly query organisationDb
82        // and return
83  
84        if ( context.facilitators().isEmpty() ) {
85           if ( !ds.ignore.contains(context.whoami()) )
86              ds.ignore.addElement(context.whoami());
87           ds.agents = db.anyAgent(goal,ds.ignore);
88           ds.ignore = Misc.union(ds.agents,ds.ignore);
89  
90           Core.DEBUG(2,"b1 ds.agents = " + ds.agents);
91  
92           output = input;
93           return ds.agents.isEmpty() ? FAIL : OK;
94        }
95        debug ("3"); 
96        // otherwise
97        // subscribe to facilitators send list of agents with required ability
98        timeout = now + FRACTION*(t-now);
99        AbilitySpec a = new AbilitySpec(ontology.getFact(Fact.VARIABLE,type),0,0);
100 
101       Performative msg;
102       msg_wait_key = context.newId();
103       String[] pattern = { "type", "inform", "in-reply-to", msg_wait_key };
104 
105       proceed_immediately = ds.gs.any != null && ((Boolean)ds.gs.any).booleanValue();
106       debug("4"); 
107       String msg_type;
108        debug("4a"); 
109       if ( proceed_immediately ) {
110            debug("4b"); 
111           context.MsgHandler().addRule(new MessageRuleImpl(msg_wait_key, pattern,
112             MessageActionImpl.EXECUTE_ONCE,engine,"agentWithAbilityFound")
113           );
114           msg_type = "query-ref";
115            debug("4c"); 
116       }
117       else {
118            debug("4d"); 
119          context.MsgHandler().addRule(new MessageRuleImpl(msg_wait_key, pattern,
120             engine,"agentWithAbilityFound")
121          );
122           msg_type = "subscribe";
123            debug("4e"); 
124       }
125       debug ("5"); 
126       for(int i = 0; i < context.facilitators().size(); i++ ) {
127            debug ("5a"); 
128          msg = new Performative(msg_type);
129           debug ("5b"); 
130          msg.setReceiver((String)context.facilitators().elementAt(i));
131           debug ("5c"); 
132          msg.setContent("has_ability " + a);
133           debug ("5d"); 
134          msg.setReplyWith(msg_wait_key);
135           debug ("5e"); 
136          mbox.sendMsg(msg);
137           debug ("5f"); 
138       }
139       Thread.yield(); 
140       return WAIT;
141    }
142 
143    protected int continue_exec() {
144       MailBox mbox = context.MailBox();
145       OrganisationDb db = context.OrganisationDb();
146       debug ("6"); 
147       if ( proceed_immediately || context.now() > timeout ) {
148 
149          if ( !proceed_immediately ) {
150             debug ("6a"); 
151             context.MsgHandler().removeRule(msg_wait_key);
152             debug("7"); 
153             Performative msg;
154             for(int i = 0; i < context.facilitators().size(); i++ ) {
155                msg = new Performative("cancel");
156                msg.setReceiver((String)context.facilitators().elementAt(i));
157                msg.setContent("has_ability " + msg_wait_key);
158                mbox.sendMsg(msg);
159             }
160          }
161          debug ("8a"); 
162          DStruct ds = (DStruct)input;
163          // assume ds.goal has one element only
164          Goal goal = (Goal)ds.goal.elementAt(0);
165          ds.agents = db.anyAgent(goal,ds.ignore);
166          ds.ignore = Misc.union(ds.agents,ds.ignore);
167          debug ("8b"); 
168          Core.DEBUG(2,"b1 ds.agents = " + ds.agents);
169 
170          output = input;
171          if (ds.agents.isEmpty () ) { 
172                 debug ("9"); 
173                 Thread.yield(); 
174                 return FAIL; 
175          }
176          else {
177             debug ("10");
178             return OK; 
179          }
180             
181          //return ds.agents.isEmpty() ? FAIL : OK;
182       }
183       return WAIT;
184    }
185 
186    protected void reset() {
187    }
188    
189    private void debug (String str) { 
190        // System.out.println("b1>> " + str); 
191    }
192 }