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 b1 extends Node {
36 /***
37 Purpose: for a predefined fraction of the period (confirm_time - now)
38 subscribe to the facilitator to stream list of all agents with required
39 ability: collect set of agents received
40 */
41
42
43 private String node_desc = "do/find sellers; do\find service";
44
45
46 public final String getDesc()
47 {return node_desc;}
48
49
50 public final void setDesc(String node_desc)
51 {this.node_desc = node_desc;}
52
53
54
55 public b1() {
56 super("b1");
57 }
58
59
60 static final double FRACTION = 0.50;
61 boolean proceed_immediately;
62
63 protected int exec() {
64
65 OrganisationDb db = context.OrganisationDb();
66
67 OntologyDb ontology = context.OntologyDb();
68 Engine engine = context.Engine();
69 MailBox mbox = context.MailBox();
70
71 DStruct ds = (DStruct)input;
72
73 debug ("1");
74 Goal goal = (Goal)ds.goal.elementAt(0);
75 String type = goal.getFactType();
76 double t = goal.getConfirmTime().getTime();
77 double now = context.now();
78 debug ("2");
79 if ( now >= t ) return FAIL;
80
81
82
83
84 if ( context.facilitators().isEmpty() ) {
85 if ( !ds.ignore.contains(context.whoami()) )
86 ds.ignore.addElement(context.whoami());
87 ds.agents = db.anyAgent(goal,ds.ignore);
88 ds.ignore = Misc.union(ds.agents,ds.ignore);
89
90 Core.DEBUG(2,"b1 ds.agents = " + ds.agents);
91
92 output = input;
93 return ds.agents.isEmpty() ? FAIL : OK;
94 }
95 debug ("3");
96
97
98 timeout = now + FRACTION*(t-now);
99 AbilitySpec a = new AbilitySpec(ontology.getFact(Fact.VARIABLE,type),0,0);
100
101 Performative msg;
102 msg_wait_key = context.newId();
103 String[] pattern = { "type", "inform", "in-reply-to", msg_wait_key };
104
105 proceed_immediately = ds.gs.any != null && ((Boolean)ds.gs.any).booleanValue();
106 debug("4");
107 String msg_type;
108 debug("4a");
109 if ( proceed_immediately ) {
110 debug("4b");
111 context.MsgHandler().addRule(new MessageRuleImpl(msg_wait_key, pattern,
112 MessageActionImpl.EXECUTE_ONCE,engine,"agentWithAbilityFound")
113 );
114 msg_type = "query-ref";
115 debug("4c");
116 }
117 else {
118 debug("4d");
119 context.MsgHandler().addRule(new MessageRuleImpl(msg_wait_key, pattern,
120 engine,"agentWithAbilityFound")
121 );
122 msg_type = "subscribe";
123 debug("4e");
124 }
125 debug ("5");
126 for(int i = 0; i < context.facilitators().size(); i++ ) {
127 debug ("5a");
128 msg = new Performative(msg_type);
129 debug ("5b");
130 msg.setReceiver((String)context.facilitators().elementAt(i));
131 debug ("5c");
132 msg.setContent("has_ability " + a);
133 debug ("5d");
134 msg.setReplyWith(msg_wait_key);
135 debug ("5e");
136 mbox.sendMsg(msg);
137 debug ("5f");
138 }
139 Thread.yield();
140 return WAIT;
141 }
142
143 protected int continue_exec() {
144 MailBox mbox = context.MailBox();
145 OrganisationDb db = context.OrganisationDb();
146 debug ("6");
147 if ( proceed_immediately || context.now() > timeout ) {
148
149 if ( !proceed_immediately ) {
150 debug ("6a");
151 context.MsgHandler().removeRule(msg_wait_key);
152 debug("7");
153 Performative msg;
154 for(int i = 0; i < context.facilitators().size(); i++ ) {
155 msg = new Performative("cancel");
156 msg.setReceiver((String)context.facilitators().elementAt(i));
157 msg.setContent("has_ability " + msg_wait_key);
158 mbox.sendMsg(msg);
159 }
160 }
161 debug ("8a");
162 DStruct ds = (DStruct)input;
163
164 Goal goal = (Goal)ds.goal.elementAt(0);
165 ds.agents = db.anyAgent(goal,ds.ignore);
166 ds.ignore = Misc.union(ds.agents,ds.ignore);
167 debug ("8b");
168 Core.DEBUG(2,"b1 ds.agents = " + ds.agents);
169
170 output = input;
171 if (ds.agents.isEmpty () ) {
172 debug ("9");
173 Thread.yield();
174 return FAIL;
175 }
176 else {
177 debug ("10");
178 return OK;
179 }
180
181
182 }
183 return WAIT;
184 }
185
186 protected void reset() {
187 }
188
189 private void debug (String str) {
190
191 }
192 }