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  package zeus.actors.graphs;
25  
26  import java.util.*;
27  import zeus.util.*;
28  import zeus.concepts.*;
29  import zeus.actors.*;
30  import zeus.actors.rtn.*;
31  import zeus.actors.rtn.util.*;
32  
33  public class se2 extends Node {
34     public se2() {
35        super("se2");
36     }
37  
38     // memory useful for backtracking
39     Graph local_graph = null;
40     GraphStruct gs = null, gs1 = null;
41     DelegationStruct ds = null;
42  
43     protected int exec() {
44        ProtocolDb protocolDb = context.ProtocolDb();
45        Engine engine = context.Engine();
46        MsgHandler handler = context.MsgHandler();
47  
48        Vector in = (Vector)input;
49        gs = (GraphStruct)in.elementAt(0);
50        ds = (DelegationStruct)in.elementAt(1);
51  
52        String[] agents = new String[1];
53        agents[0] = ds.agent;
54  
55        // Get applicable protocol & strategy for this fact/agent combination
56        Fact fact = ((DataRec)gs.any).getFact();
57        Vector info = protocolDb.getProtocols(fact,agents,
58           ProtocolInfo.RESPONDENT);
59  
60        if ( info.isEmpty() ) return FAIL;
61  
62        // Compute latest time when seller wants commodity sold
63        Goal g0 = (Goal) gs.goal.elementAt(0);
64        double t = (double)g0.getEndTime();
65  
66        // Compute latest time when buyer wants commodity bought
67        Goal g = (Goal) ds.goals.elementAt(0);
68        timeout = g.getReplyTime().getTime();
69  
70        // take minimum
71        timeout = Math.min(t,timeout);
72  
73        msg_wait_key = context.newId("ProtocolRespondent");
74  
75        ProtocolDbResult result = (ProtocolDbResult)info.elementAt(0);
76  
77        // set cost of goal to seller's net cost
78        g.setCost(((DataRec)gs.any).getCost());
79        // set reservation price for item to seller's defined value
80        result.parameters.put("reservation.price",Double.toString(g0.getCost()));
81  
82        gs1 = new GraphStruct(ds.agent,g,ds.key);
83        gs1.any = result;
84  
85        local_graph = createGraph(result.protocol);
86        if ( local_graph == null ) return FAIL;
87  
88        String[] pattern = { "in-reply-to", gs1.key };
89        // added exectute once to stop the accumulation of rules
90        handler.addRule(new MessageRuleImpl(gs1.key, pattern, engine,
91                        "continue_dialogue"));
92  
93        local_graph.run(engine,this,gs1,msg_wait_key);
94        return WAIT;
95     }
96  
97     protected int continue_exec() {
98        MsgHandler handler = context.MsgHandler();
99  
100       switch( local_graph.getState() ) {
101          case Graph.DONE:
102               gs1.goal = gs1.confirmed_goal;
103               gs1.confirmed_goal = null;
104               gs1.any = gs.any;
105               output = gs1;
106 	      return OK;
107 
108 	 case Graph.FAILED:
109               handler.removeRule(gs1.key);
110 	      return FAIL;
111 
112 	 default:
113 	      if ( timeout > context.now() )
114 	         return WAIT;
115 	      else {
116                  handler.removeRule(gs1.key);
117 	         return FAIL;
118               }
119       }
120    }
121 
122    protected void reset() {
123    }
124 }