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   * @(#)se1.java 1.03b
25   */
26  
27  package zeus.actors.graphs;
28  
29  import java.util.*;
30  import zeus.util.*;
31  import zeus.concepts.*;
32  import zeus.actors.*;
33  import zeus.actors.rtn.*;
34  import zeus.actors.rtn.util.*;
35  
36  public class se1 extends Node {
37  /***
38     Purpose: for a predefined fraction of the period (end_time - now)
39     advertise good for sale to facilitator
40     Wait until contacted by buyer, then proceed with negotiation
41  */
42  
43  
44     // LL 040500 1.03bB
45        private String node_desc = "continue buying";
46        public  final String getDesc()                 {return node_desc;};
47        public  final void   setDesc(String node_desc) {this.node_desc = node_desc;};
48     // LL 040500 1.03bE
49  
50     public se1() {
51        super("se1");
52     }
53  
54     // local memory
55     static final double FRACTION = 0.90;
56  
57     protected int exec() {
58        // if no facilitators are listed, then FAIL
59        // (alternatively, could be made to utilise the nameserver and directly
60        // contact agents, i.e. proactive selling)
61        debug ("1"); 
62        if ( context.facilitators().isEmpty() ) return FAIL;
63        debug ("2");  
64        Engine engine = context.Engine();
65        debug ("3"); 
66        MailBox mbox = context.MailBox();
67        debug ("4"); 
68        GraphStruct gs = (GraphStruct)input;
69        debug ("5"); 
70        // assume gs.goal has one element only
71        Goal goal = (Goal)gs.goal.elementAt(0);
72        debug ("6"); 
73        Fact fact = ((DataRec)gs.any).getFact();
74        debug ("7"); 
75        double t = (double)goal.getEndTime();
76        debug ("8");
77        double now = context.now();
78        debug ("9");
79        if ( t <= now ) return FAIL;
80        debug ("10");   
81        // advertise to facilitators
82        timeout = now + FRACTION*(t-now);
83        debug ("11"); 
84        AbilitySpec a = new AbilitySpec(fact,0,0);
85        debug ("12"); 
86        Performative msg;
87        msg_wait_key = context.newId();
88  debug ("13"); 
89        for(int i = 0; i < context.facilitators().size(); i++ ) {
90           msg = new Performative("inform");
91           msg.setReceiver((String)context.facilitators().elementAt(i));
92           msg.setContent("my_abilities " + a);
93           mbox.sendMsg(msg);
94        }
95        debug ("14"); 
96        engine.addItemForSale(msg_wait_key,fact);
97        debug ("15a"); 
98        return WAIT;
99     }
100 
101    protected int continue_exec() {
102       Engine engine = context.Engine();
103       Core.DEBUG(2,"se1 continue_exec called");
104     debug ("15"); 
105       if ( context.now() > timeout ) {
106         debug ("16"); 
107          engine.removeItemForSale(msg_wait_key);
108          debug("17"); 
109          Core.DEBUG(2,"se1 failing now > timeout");
110          debug ("18"); 
111          return FAIL;
112       }
113     debug ("19"); 
114       GraphStruct gs = (GraphStruct)input;
115     debug ("20"); 
116       DelegationStruct ds;
117       debug ("21"); 
118       String buyers_key = engine.getBuyersKey(msg_wait_key);
119       if ( buyers_key != null && 
120            (ds = engine.replyReceived(buyers_key)) != null ) {
121          output = new Vector();
122          ((Vector)output).addElement(gs);
123          ((Vector)output).addElement(ds);
124          Core.DEBUG(2,"se1 returning success");
125          return OK;
126       }
127       return WAIT;
128    }
129 
130    protected void reset() {
131    }
132    
133    
134    public void debug (String str) { 
135     System.out.println("se1>>" + str);
136    }
137 }