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   * @(#)b4.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 b4 extends Node {
36      
37      // ST 050500 1.03bB node description due to CVB
38     private String node_desc = "do/accept & reject";
39     
40     
41     public final String getDesc()
42        {return node_desc;}
43     
44     
45     public final void setDesc(String node_desc) 
46        {this.node_desc = node_desc;}
47     // ST 050500 1.03bE
48     
49     
50     public b4() {
51        super("b4");
52     }
53  
54     // memory useful for backtracking
55  
56     protected Vector select(Vector goals, Vector input) {
57        Core.DEBUG(3,"select input " + goals + "\n" + input);
58        Goal g, g0, g1;
59        Object obj;
60        DelegationStruct ds;
61        String gid;
62        Vector reduced = new Vector();
63        Vector selection = new Vector();
64  
65        for(int j = 0; j < goals.size(); j++ ) {
66           g = (Goal)goals.elementAt(j);
67           gid = g.getId();
68           reduced.removeAllElements();
69  
70           for(int i = 0; i < input.size(); i++ ) {
71              ds = (DelegationStruct)input.elementAt(i);
72              // assumes only one goal in goals field
73              g0 = (Goal)ds.goals.elementAt(0);
74              if ( gid.equals(g0.getId()) )
75                 reduced.addElement(ds);
76           }
77           Core.DEBUG(3,"select reduced " + gid + "\n" + reduced);
78           boolean changed = true;
79           while( changed ) {
80              changed = false;
81              for(int i = 0; i < reduced.size()-1; i++ ) {
82                 // assumes only one goal in goals field
83                 ds = (DelegationStruct)reduced.elementAt(i);
84                 g0 = (Goal)ds.goals.elementAt(0);
85                 ds = (DelegationStruct)reduced.elementAt(i+1);
86                 g1 = (Goal)ds.goals.elementAt(0);
87                 if ( g0.getCost() > g1.getCost() ) {
88                    obj = reduced.elementAt(i);
89                    reduced.setElementAt(reduced.elementAt(i+1),i);
90                    reduced.setElementAt(obj,i+1);
91                    changed = true;
92                 }
93              }
94           }
95           if ( !reduced.isEmpty() )
96  	    selection.addElement(reduced.elementAt(0));
97        }
98        Core.DEBUG(3,"select results\n" + selection);
99        return selection;
100    }
101 
102 
103    protected int exec() {
104       Engine engine = context.Engine();
105 
106       // first perform join
107       Object[] data = (Object[]) input;
108       GraphStruct gs = ((DStruct)data[0]).gs;
109       gs.d_results = new Vector();
110       for(int i = 0; i < data.length; i++ ) {
111          for(int j = 0; j < ((DStruct)data[i]).results.size(); j++ )
112             gs.d_results.addElement(((DStruct)data[i]).results.elementAt(j));
113       }
114 
115       Core.DEBUG(2,"b4 Input gs " + gs);
116 
117       Vector selection = select(gs.goal,gs.d_results);
118       Vector rejection = Misc.difference(gs.d_results,selection);
119       gs.selection = Misc.union(gs.selection,selection);
120 
121       Core.DEBUG(2,"b4 Results " + selection);
122 
123       DelegationStruct ds;
124       // send reject message to rejected children
125       for(int i = 0; i < rejection.size(); i++ ) {
126          ds = (DelegationStruct) rejection.elementAt(i);
127          engine.continue_dialogue(ds.key,ds.agent,"reject-proposal",ds.goals);
128       }
129 
130       Core.DEBUG(2,"b4 Current gs " + gs);
131 
132       if ( gs.selection.isEmpty() ) return FAIL;
133 
134       // send confirm message to selected children
135       for(int i = 0; i < gs.selection.size(); i++ ) {
136          ds = (DelegationStruct)gs.selection.elementAt(i);
137          engine.continue_dialogue(ds.key,ds.agent,"accept-proposal",ds.goals);
138       }
139 
140       // Now prepare for post-contract phase
141       // for now assume gs.goal & ds.goals have one element only
142       Goal g;
143       AuditTable audit = engine.getAuditTable();
144 
145       for(int i = 0; i < gs.selection.size(); i++ ) {
146          ds = (DelegationStruct)gs.selection.elementAt(i);
147          g = (Goal)ds.goals.elementAt(0);
148 
149          audit.add(g,ds.key,g.getCost(),false,false,ds.agent,
150                    context.whoami(),g.getEndTime());
151       }
152 
153       Core.DEBUG(2,"*** B4 AuditTable State ***");
154       Core.DEBUG(2,audit);
155 
156       output = gs;
157       return OK;
158    }
159    protected void reset() {
160       // reset any state changed by exec()
161       Object[] data = (Object[]) input;
162       GraphStruct gs = ((DStruct)data[0]).gs;
163       gs.d_results = null;
164    }
165 }