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