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   * @(#)s6.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 s6 extends Node {
36  // NOTE: for housekeeping reasons, this node always returns true;
37  
38     // ST 050500 1.03bB node description due to CVB
39     private String node_desc = "do/find protocol";
40     
41     
42     public final String getDesc()
43        {return node_desc;}
44     
45     
46     public final void setDesc(String node_desc) 
47        {this.node_desc = node_desc;}
48     // ST 050500 1.03bE
49     
50     
51     public s6() {
52        super("s6");
53     }
54  
55  
56  
57  
58     // memory useful for backtracking
59     protected static final double DELTA_TIME = 0.25;
60     private Graph local_graph = null;
61  
62     protected int exec() {
63        Planner planner = context.Planner();
64        ProtocolDb protocolDb = context.ProtocolDb();
65        Engine engine = context.Engine();
66        MsgHandler handler = context.MsgHandler();
67  
68        GraphStruct gs = (GraphStruct)input;
69        output = input;
70        gs.goal = planner.bind(gs.goal);
71        debug("1"); 
72        Goal g = (Goal) gs.goal.elementAt(0);   
73        debug("2"); 
74        timeout = g.getConfirmTime().getTime() + DELTA_TIME;
75        debug("3"); 
76        msg_wait_key = context.newId("ProtocolRespondent");
77        debug("4"); 
78        String[] agents = new String[1];
79        agents[0] = gs.agent;
80      
81        // Get applicable protocol & strategy for this fact/agent combination
82        Fact fact = g.getFact();
83         debug("5"); 
84        Vector info = protocolDb.getProtocols(fact,agents,ProtocolInfo.RESPONDENT);
85          debug("6"); 
86        if ( info.isEmpty() ) return OK; // really fail
87          debug("7"); 
88        ProtocolDbResult result = (ProtocolDbResult)info.elementAt(0);
89        gs.any = result;
90          debug("8"); 
91        local_graph = createGraph(result.protocol);
92        if ( local_graph == null ) return OK; // really fail
93          debug("9"); 
94        String[] pattern = { "in-reply-to", gs.key };
95        // added execute_once to stop the accumulation of rules
96        handler.addRule(new MessageRuleImpl(gs.key, pattern, engine, "continue_dialogue"));
97          debug("10"); 
98        local_graph.run(engine,this,gs,msg_wait_key);
99          debug("11"); 
100       return WAIT;
101    }
102    
103 
104    protected int continue_exec() {
105       Core.DEBUG(2,getDescription() + " continue_exec");
106       Planner planner = context.Planner();
107          debug("12"); 
108       MsgHandler handler = context.MsgHandler();
109        debug("13"); 
110       GraphStruct gs = (GraphStruct)input;
111         debug("14"); 
112       output = input;
113         debug("15"); 
114       switch( local_graph.getState() ) {
115          case Graph.DONE:
116                debug("16"); 
117               planner.goalConfirmed(gs.goal,gs.confirmed_goal,gs.selection);
118               gs.goal = gs.confirmed_goal;
119                debug("17"); 
120               gs.confirmed_goal = null;
121               Core.DEBUG(2,getDescription() + " continue_exec OK");
122 	      return OK; 
123 	 case Graph.FAILED:
124               Core.DEBUG(2,getDescription() + " continue_exec FAILED 1");
125 	      return OK; // really fail
126 
127 	 default:
128 	      if ( timeout > context.now() ) {
129                  Core.DEBUG(2,getDescription() + " continue_exec WAIT");
130 	         return WAIT;
131               }
132 	      else {
133                  Core.DEBUG(2,getDescription() + " continue_exec FAILED 2");
134 	         return OK; // really fail
135               }
136       }
137    }
138 
139     
140     private void debug (String str) { 
141        //    System.out.println ("s6>>" + str); 
142     }
143     
144     
145    protected void reset() {
146    }
147 }