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 se2 extends Node {
34 public se2() {
35 super("se2");
36 }
37
38
39 Graph local_graph = null;
40 GraphStruct gs = null, gs1 = null;
41 DelegationStruct ds = null;
42
43 protected int exec() {
44 ProtocolDb protocolDb = context.ProtocolDb();
45 Engine engine = context.Engine();
46 MsgHandler handler = context.MsgHandler();
47
48 Vector in = (Vector)input;
49 gs = (GraphStruct)in.elementAt(0);
50 ds = (DelegationStruct)in.elementAt(1);
51
52 String[] agents = new String[1];
53 agents[0] = ds.agent;
54
55
56 Fact fact = ((DataRec)gs.any).getFact();
57 Vector info = protocolDb.getProtocols(fact,agents,
58 ProtocolInfo.RESPONDENT);
59
60 if ( info.isEmpty() ) return FAIL;
61
62
63 Goal g0 = (Goal) gs.goal.elementAt(0);
64 double t = (double)g0.getEndTime();
65
66
67 Goal g = (Goal) ds.goals.elementAt(0);
68 timeout = g.getReplyTime().getTime();
69
70
71 timeout = Math.min(t,timeout);
72
73 msg_wait_key = context.newId("ProtocolRespondent");
74
75 ProtocolDbResult result = (ProtocolDbResult)info.elementAt(0);
76
77
78 g.setCost(((DataRec)gs.any).getCost());
79
80 result.parameters.put("reservation.price",Double.toString(g0.getCost()));
81
82 gs1 = new GraphStruct(ds.agent,g,ds.key);
83 gs1.any = result;
84
85 local_graph = createGraph(result.protocol);
86 if ( local_graph == null ) return FAIL;
87
88 String[] pattern = { "in-reply-to", gs1.key };
89
90 handler.addRule(new MessageRuleImpl(gs1.key, pattern, engine,
91 "continue_dialogue"));
92
93 local_graph.run(engine,this,gs1,msg_wait_key);
94 return WAIT;
95 }
96
97 protected int continue_exec() {
98 MsgHandler handler = context.MsgHandler();
99
100 switch( local_graph.getState() ) {
101 case Graph.DONE:
102 gs1.goal = gs1.confirmed_goal;
103 gs1.confirmed_goal = null;
104 gs1.any = gs.any;
105 output = gs1;
106 return OK;
107
108 case Graph.FAILED:
109 handler.removeRule(gs1.key);
110 return FAIL;
111
112 default:
113 if ( timeout > context.now() )
114 return WAIT;
115 else {
116 handler.removeRule(gs1.key);
117 return FAIL;
118 }
119 }
120 }
121
122 protected void reset() {
123 }
124 }