1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 b4 extends Node {
36
37
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
48
49
50 public b4() {
51 super("b4");
52 }
53
54
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
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
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
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
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
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
141
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
161 Object[] data = (Object[]) input;
162 GraphStruct gs = ((DStruct)data[0]).gs;
163 gs.d_results = null;
164 }
165 }