1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 Sv extends Node {
34 public Sv() {
35 super("Sv");
36 }
37
38
39 private Vector previous_external = null;
40 private Vector previous_goals = null;
41 private Vector previous_selection = null;
42
43 protected int exec() {
44 Engine engine = context.Engine();
45 DelegationStruct ds;
46 Goal g, g1;
47
48 Object[] data = (Object[]) input;
49
50
51 GraphStruct gs = ((DStruct)data[0]).gs;
52 gs.d_results = new Vector();
53 for(int i = 0; i < data.length; i++ ) {
54 for(int j = 0; j < ((DStruct)data[i]).results.size(); j++ )
55 gs.d_results.addElement(((DStruct)data[i]).results.elementAt(j));
56 }
57
58 Core.DEBUG(2,"Sv Previous gs " + gs);
59
60 previous_goals = Misc.copyVector(gs.goal);
61
62 BindResults b = bind(gs.goal,gs.d_results);
63
64 Core.DEBUG(2,"Sv BindResults " + b);
65
66 if ( !b.ok ) {
67 for(int i = 0; i < gs.d_results.size(); i++ ) {
68 ds = (DelegationStruct) gs.d_results.elementAt(i);
69 engine.continue_dialogue(ds.key,ds.agent,"reject-proposal",ds.goals);
70 }
71 return FAIL;
72 }
73
74 previous_external = gs.external;
75 previous_selection = Misc.copyVector(gs.selection);
76
77 gs.external = b.external;
78 gs.selection = Misc.union(gs.selection,b.selection);
79
80
81 for(int i = 0; i < b.rejection.size(); i++ ) {
82 ds = (DelegationStruct)b.rejection.elementAt(i);
83 engine.continue_dialogue(ds.key,ds.agent,"reject-proposal",ds.goals);
84 }
85
86 Core.DEBUG(2,"Sv Current gs " + gs);
87
88 output = gs;
89 return OK;
90 }
91 protected void reset() {
92
93 }
94
95 protected BindResults bind(Vector goals, Vector input) {
96 Core.DEBUG(2,"Entering Sv-bind...");
97
98 Goal g0, g1;
99 Fact f0, f1;
100 Vector[] reduced = new Vector[goals.size()];
101 Bindings bindings = new Bindings(context.whoami());
102 BindResults result = new BindResults();
103 result.ok = false;
104
105 for(int i = 0; i < goals.size(); i++ ) {
106 g0 = (Goal)goals.elementAt(i);
107 reduced[i] = sortFeasible(g0.getId(),input);
108 if ( reduced[i].isEmpty() )
109 result.unavailable.addElement(g0);
110 }
111
112 if ( !result.unavailable.isEmpty() )
113 return result;
114
115 Object[] data;
116 PlanRecord rec;
117 DelegationStruct[] ds = new DelegationStruct[goals.size()];
118 Selector selector = new Selector(reduced);
119 boolean found = false;
120
121 while( !result.ok && selector.hasMoreElements() ) {
122 data = (Object[]) selector.nextElement();
123 bindings.clear();
124 result.ok = true;
125 for(int i = 0; i < data.length; i++ ) {
126 ds[i] = (DelegationStruct)data[i];
127
128 g1 = (Goal)ds[i].goals.elementAt(0);
129 g0 = (Goal)goals.elementAt(i);
130 f0 = g0.getFact();
131 f1 = g1.getFact();
132 result.ok = f1.unifiesWith(f0,bindings);
133 if ( !result.ok )
134 break;
135 }
136
137 Core.DEBUG(2,"Sv Current selection ... ");
138 Core.DEBUG(2,ds);
139
140 if ( result.ok ) {
141 for( int i = 0; i < data.length; i++ ) {
142 ds[i] = (DelegationStruct)data[i];
143
144 result.selection.addElement(ds[i]);
145 for(int j = 0; j < reduced[i].size(); j++ )
146 if ( reduced[i].elementAt(j) != ds[i] )
147 result.rejection.addElement(reduced[i].elementAt(j));
148 }
149 }
150 }
151
152 if ( !result.ok ) {
153
154
155 for( int i = 0; i < goals.size(); i++ ) {
156 g0 = (Goal)goals.elementAt(i);
157 result.unavailable.addElement(g0);
158 }
159 }
160
161 Core.DEBUG(2,"++++++ Sv-Bind goals\n" + goals);
162 Core.DEBUG(2,"++++++ Sv-Bind bindings\n" + bindings);
163 Core.DEBUG(2,"++++++ Sv-Bind result\n" + result + "\n" );
164
165 return result;
166 }
167
168 protected Vector sortFeasible(String gid, Vector input) {
169 Core.DEBUG(2,"Sv-sortFeasible input " + gid + "\n" + input);
170
171 Goal g0, g1;
172 Object obj;
173 DelegationStruct ds;
174 Vector reduced = new Vector();
175 for( int i = 0; i < input.size(); i++ ) {
176 ds = (DelegationStruct)input.elementAt(i);
177
178 g0 = (Goal)ds.goals.elementAt(0);
179 if ( gid.equals(g0.getId()) )
180 reduced.addElement(ds);
181 }
182 Core.DEBUG(2,"Sv-sortFeasible reduced " + gid + "\n" + reduced);
183 boolean changed = true;
184 while( changed ) {
185 changed = false;
186 for( int i = 0; i < reduced.size()-1; i++ ) {
187
188 ds = (DelegationStruct)reduced.elementAt(i);
189 g0 = (Goal)ds.goals.elementAt(0);
190 ds = (DelegationStruct)reduced.elementAt(i+1);
191 g1 = (Goal)ds.goals.elementAt(0);
192 if ( g0.getCost() > g1.getCost() ) {
193 obj = reduced.elementAt(i);
194 reduced.setElementAt(reduced.elementAt(i+1),i);
195 reduced.setElementAt(obj,i+1);
196 changed = true;
197 }
198 }
199 }
200 Core.DEBUG(2,"Sv-sortFeasible results " + gid + "\n" + reduced);
201 return reduced;
202 }
203 }