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 /***
36 this is a node in the contract net protocol that is the default co-ordination
37 mechanism of zeus.
38 */
39 public class b2 extends Node {
40 protected static final double DELTA_TIME = 0.25;
41
42
43
44 private String node_desc = "do/find protocols";
45
46
47 public final String getDesc()
48 {return node_desc;}
49
50
51 public final void setDesc(String node_desc)
52 {this.node_desc = node_desc;}
53
54
55
56 public b2() {
57 super("b2");
58 }
59
60
61
62 protected Graph[] local_graph = null;
63 protected LocalDStruct[] local_dstruct = null;
64 protected int reply_needed = 0;
65
66 protected int exec() {
67 Engine engine = context.Engine();
68 MsgHandler handler = context.MsgHandler();
69 ProtocolDb protocolDb = context.ProtocolDb();
70
71 DStruct ds = (DStruct)input;
72 output = input;
73
74 Goal g = (Goal)ds.goal.elementAt(0);
75 double ct = g.getConfirmTime().getTime();
76 timeout = ct-DELTA_TIME;
77
78 Core.DEBUG(3,getDescription() + " Pre-timeout = " + timeout);
79 Core.DEBUG(3,getDescription() + " ds.gs.timeout = " + ds.gs.timeout);
80 if ( !Misc.isZero(ds.gs.timeout) )
81 timeout = Math.min(timeout,context.now() + ds.gs.timeout);
82
83 Core.DEBUG(3,getDescription() + " Post-timeout = " + timeout);
84
85 Time t = new Time(timeout);
86 for(int i = 0; i < ds.goal.size(); i++ ) {
87 g = (Goal) ds.goal.elementAt(i);
88 g.setReplyTime(t);
89 }
90
91 msg_wait_key = context.newId("ProtocolInitiator");
92
93
94 Fact fact = g.getFact();
95 Vector info = protocolDb.getProtocols(fact,Misc.stringArray(ds.agents),
96 ProtocolInfo.INITIATOR);
97
98 if ( info.isEmpty() ) return FAIL;
99
100
101
102
103
104 HSet agents = new HSet();
105 ProtocolDbResult result;
106 for(int i = 0; i < info.size(); i++ ) {
107 result = (ProtocolDbResult)info.elementAt(i);
108 agents.add(result.agent);
109 }
110
111 String r_price = Double.toString(g.getCost());
112 ProtocolDbResult[] protocols = new ProtocolDbResult[agents.size()];
113 Enumeration enum = agents.elements();
114 String agent;
115 for(int j = 0; enum.hasMoreElements(); j++ ) {
116 agent = (String)enum.nextElement();
117 for(int i = 0; i < info.size(); i++ ) {
118 result = (ProtocolDbResult)info.elementAt(i);
119 if ( result.agent.equals(agent) ) {
120 protocols[j] = result;
121 protocols[j].parameters.put("reservation.price",r_price);
122 break;
123 }
124 }
125 }
126
127 MessagePattern pattern = null;
128 MessageAction action = null;
129
130 local_graph = new Graph[protocols.length];
131 local_dstruct = new LocalDStruct[protocols.length];
132
133 for(int i = 0; i < local_graph.length; i++ ) {
134 local_graph[i] = createGraph(protocols[i].protocol);
135 if ( local_graph[i] != null ) {
136 local_dstruct[i] = new LocalDStruct(protocols[i].agent,ds);
137 local_dstruct[i].any = protocols[i];
138 local_dstruct[i].key = context.newId("local_dstruct");
139
140 pattern = new MessagePatternImpl();
141 pattern.setConstraint("in-reply-to",local_dstruct[i].key);
142 action = new MessageActionImpl(engine,"continue_dialogue");
143 handler.addRule(new MessageRuleImpl(local_dstruct[i].key,pattern,action));
144
145 local_graph[i].run(engine,this,local_dstruct[i],msg_wait_key);
146 reply_needed++;
147 }
148 else
149 local_dstruct[i] = null;
150 }
151 return (reply_needed > 0) ? WAIT : FAIL;
152 }
153
154 protected int continue_exec() {
155 DStruct ds = (DStruct)input;
156 output = input;
157
158 int transaction_completed = 0;
159
160 for(int i = 0; i < local_graph.length; i++ ) {
161 if ( local_graph[i] != null ) {
162 switch( local_graph[i].getState() ) {
163 case Graph.DONE:
164 if ( !ds.results.contains(local_dstruct[i].result) )
165 ds.results.addElement(local_dstruct[i].result);
166 transaction_completed++;
167 break;
168 case Graph.FAILED:
169 transaction_completed++;
170 break;
171 }
172 }
173 }
174
175 Core.DEBUG(3,getDescription() + "No Needed: " + reply_needed +
176 " completed: " + transaction_completed);
177
178 if ( reply_needed - transaction_completed == 0 ) return OK;
179 if ( timeout > context.now() )
180 return WAIT;
181 else
182 return OK;
183 }
184
185 protected void reset() {
186
187 DStruct ds = (DStruct)input;
188 ds.results.removeAllElements();
189 }
190 }