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
27 package zeus.actors.graphs;
28
29 import java.util.*;
30 import zeus.util.*;
31 import zeus.concepts.*;
32 import zeus.actors.*;
33 import zeus.actors.rtn.*;
34 import zeus.actors.rtn.util.*;
35
36 public class se1 extends Node {
37 /***
38 Purpose: for a predefined fraction of the period (end_time - now)
39 advertise good for sale to facilitator
40 Wait until contacted by buyer, then proceed with negotiation
41 */
42
43
44
45 private String node_desc = "continue buying";
46 public final String getDesc() {return node_desc;};
47 public final void setDesc(String node_desc) {this.node_desc = node_desc;};
48
49
50 public se1() {
51 super("se1");
52 }
53
54
55 static final double FRACTION = 0.90;
56
57 protected int exec() {
58
59
60
61 debug ("1");
62 if ( context.facilitators().isEmpty() ) return FAIL;
63 debug ("2");
64 Engine engine = context.Engine();
65 debug ("3");
66 MailBox mbox = context.MailBox();
67 debug ("4");
68 GraphStruct gs = (GraphStruct)input;
69 debug ("5");
70
71 Goal goal = (Goal)gs.goal.elementAt(0);
72 debug ("6");
73 Fact fact = ((DataRec)gs.any).getFact();
74 debug ("7");
75 double t = (double)goal.getEndTime();
76 debug ("8");
77 double now = context.now();
78 debug ("9");
79 if ( t <= now ) return FAIL;
80 debug ("10");
81
82 timeout = now + FRACTION*(t-now);
83 debug ("11");
84 AbilitySpec a = new AbilitySpec(fact,0,0);
85 debug ("12");
86 Performative msg;
87 msg_wait_key = context.newId();
88 debug ("13");
89 for(int i = 0; i < context.facilitators().size(); i++ ) {
90 msg = new Performative("inform");
91 msg.setReceiver((String)context.facilitators().elementAt(i));
92 msg.setContent("my_abilities " + a);
93 mbox.sendMsg(msg);
94 }
95 debug ("14");
96 engine.addItemForSale(msg_wait_key,fact);
97 debug ("15a");
98 return WAIT;
99 }
100
101 protected int continue_exec() {
102 Engine engine = context.Engine();
103 Core.DEBUG(2,"se1 continue_exec called");
104 debug ("15");
105 if ( context.now() > timeout ) {
106 debug ("16");
107 engine.removeItemForSale(msg_wait_key);
108 debug("17");
109 Core.DEBUG(2,"se1 failing now > timeout");
110 debug ("18");
111 return FAIL;
112 }
113 debug ("19");
114 GraphStruct gs = (GraphStruct)input;
115 debug ("20");
116 DelegationStruct ds;
117 debug ("21");
118 String buyers_key = engine.getBuyersKey(msg_wait_key);
119 if ( buyers_key != null &&
120 (ds = engine.replyReceived(buyers_key)) != null ) {
121 output = new Vector();
122 ((Vector)output).addElement(gs);
123 ((Vector)output).addElement(ds);
124 Core.DEBUG(2,"se1 returning success");
125 return OK;
126 }
127 return WAIT;
128 }
129
130 protected void reset() {
131 }
132
133
134 public void debug (String str) {
135 System.out.println("se1>>" + str);
136 }
137 }