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   * @(#)d2.java 1.03b
23   */
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 d2 extends Node {
36    // ST 050500 1.03bB node description due to CVB
37     private String node_desc = "do/find protocols";
38     
39     
40     public final String getDesc()
41        {return node_desc;}
42     
43     
44     public final void setDesc(String node_desc) 
45        {this.node_desc = node_desc;}
46     // ST 050500 1.03bE
47     
48     protected static final double DELTA_TIME = 0.25;
49  
50     public d2() {
51        super("d2");
52     }
53  
54     // memory useful for backtracking
55  
56     protected Graph[] local_graph = null;
57     protected LocalDStruct[] local_dstruct = null;
58     protected int reply_needed = 0;
59  
60     protected int exec() {
61        Engine engine = context.Engine();
62        MsgHandler handler = context.MsgHandler();
63        ProtocolDb protocolDb = context.ProtocolDb();
64  
65        DStruct ds = (DStruct)input;
66        output = input;
67  
68        Goal g = (Goal) ds.goal.elementAt(0);
69        double ct = g.getConfirmTime().getTime();
70        timeout = ct-DELTA_TIME;
71  
72        if ( !Misc.isZero(ds.gs.timeout) )
73           timeout = Math.min(timeout,context.now() + ds.gs.timeout);
74  
75        Time t = new Time(timeout);
76        for(int i = 0; i < ds.goal.size(); i++ ) {
77           g = (Goal) ds.goal.elementAt(i);
78           g.setReplyTime(t);
79        }
80  
81        msg_wait_key = context.newId("ProtocolInitiator");
82  
83        // Get applicable protocol & strategy for this fact/agent combination
84        Fact fact = g.getFact();
85        Vector info = protocolDb.getProtocols(fact,Misc.stringArray(ds.agents),
86           ProtocolInfo.INITIATOR);
87  
88        if ( info.isEmpty() ) return FAIL; // should we query facilitators here!?
89  
90        // Identify all independent agents in info
91        // next, select the first ProtocolDbResult for each identified agent
92        // next create local_graph for each agent then launch graphs
93  
94        HSet agents = new HSet();
95        ProtocolDbResult result;
96        for(int i = 0; i < info.size(); i++ ) {
97           result = (ProtocolDbResult)info.elementAt(i);
98           agents.add(result.agent);
99        }
100 
101       ProtocolDbResult[] protocols = new ProtocolDbResult[agents.size()];
102       Enumeration enum = agents.elements();
103       String agent;
104       for(int j = 0; enum.hasMoreElements(); j++ ) {
105          agent = (String)enum.nextElement();
106          for(int i = 0; i < info.size(); i++ ) {
107             result = (ProtocolDbResult)info.elementAt(i);
108             if ( result.agent.equals(agent) ) {
109                protocols[j] = result;
110                break;
111             }
112          }
113       }
114 
115       MessagePattern pattern = null;
116       MessageAction action = null;
117 
118       local_graph = new Graph[protocols.length];
119       local_dstruct = new LocalDStruct[protocols.length];
120 
121       for(int i = 0; i < local_graph.length; i++ ) {
122          local_graph[i] = createGraph(protocols[i].protocol);
123          if ( local_graph[i] != null ) {
124             local_dstruct[i] = new LocalDStruct(protocols[i].agent,ds);
125             local_dstruct[i].any = protocols[i];
126             local_dstruct[i].key = context.newId("local_dstruct");
127 
128             pattern = new MessagePatternImpl();
129             pattern.setConstraint("in-reply-to",local_dstruct[i].key);
130             // added execute_once to try and stop buildup of message rules - but it screwed
131             // the negotiation!!! Then tried Execute_multi... 
132             action = new MessageActionImpl(engine,"continue_dialogue");
133             handler.addRule(new MessageRuleImpl(local_dstruct[i].key,pattern,action));
134             local_graph[i].run(engine,this,local_dstruct[i],msg_wait_key);
135             reply_needed++;
136          }
137          else
138             local_dstruct[i] = null;
139       }
140       return (reply_needed > 0) ? WAIT : FAIL;
141    }
142 
143    protected int continue_exec() {
144       Core.DEBUG(3,getDescription() + " continue_exec");
145       DStruct ds = (DStruct)input;
146       output = input;
147       
148       int transaction_completed = 0;
149 
150       for(int i = 0; i < local_graph.length; i++ ) {
151          if ( local_graph[i] != null ) {
152             switch( local_graph[i].getState() ) {
153             case Graph.DONE:
154                  if ( !ds.results.contains(local_dstruct[i].result) )
155                     ds.results.addElement(local_dstruct[i].result);
156                  transaction_completed++;
157   	         break;
158             case Graph.FAILED:
159                  transaction_completed++;
160   	         break;
161             }
162          }
163       }
164 
165       Core.DEBUG(3,getDescription() + " No Needed: " + reply_needed +
166          "  completed: " + transaction_completed);
167 
168       if ( reply_needed - transaction_completed == 0 ) return OK;
169       if ( timeout > context.now() )
170          return WAIT;
171       else
172          return OK;
173    }
174 
175    protected void reset() {
176       // reset any state changed by exec()
177       DStruct ds = (DStruct)input;
178       ds.results.removeAllElements();
179    }
180 }