This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
acl_internal.drl
380 lines (323 loc) · 14.2 KB
/
acl_internal.drl
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Copyright 2011 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
Message content processing.
Determines the actions necessary to handle a message
The actions will be executed on a target session, as decided by the routing rules
*/
package org.drools.mas;
import org.drools.mas.ACLMessage;
import org.drools.mas.util.helper.ResponseContent;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.command.CommandFactory;
import org.drools.command.BatchExecutionCommand;
import org.drools.grid.remote.command.AsyncBatchExecutionCommandImpl;
import org.drools.runtime.rule.QueryResults;
import org.drools.mas.Act;
import org.drools.mas.body.content.*;
import org.drools.mas.body.acts.*;
import org.drools.mas.util.MessageContentHelper;
import org.drools.mas.util.MessageContentFactory;
import org.drools.command.runtime.rule.QueryCommand;
import java.util.List;
import java.util.ArrayList;
import org.drools.mas.util.LoggerHelper;
import org.drools.grid.helper.GridHelper;
import org.drools.grid.GridNode;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.mas.util.helper.SessionLocator;
import org.drools.grid.remote.command.*;
import org.drools.command.*;
//rule "Response content inserted"
// when
// $r: ResponseContent()
// then
// LoggerHelper.debug("CONTENT = "+$r);
//end
rule "Invoke"
when
$msg : ACLMessage( $msgId : id )
$dst : Destination( $msgId , $sessionId ; )
$cmd : MessageCommands( $msgId, $batch ; )
SessionLocator($nodeId: nodeId, sessionId == $sessionId)
then
LoggerHelper.debug(" ### INTERNAL: INVOKE - EXECUTING msg commands for "+ $msgId + " in " + $sessionId);
if (LoggerHelper.isDebugEnabled()) {
LoggerHelper.debug(" ### INTERNAL: INVOKE - EXECUTING msg commands for "+ $msgId + " in " + $sessionId);
LoggerHelper.debug(" ### INTERNAL: \t commands " + $cmd);
}
retract( $cmd );
StatefulKnowledgeSession kSession = GridHelper.getStatefulKnowledgeSession( grid, $nodeId, $sessionId, true );
if ( LoggerHelper.isDebugEnabled() ) {
LoggerHelper.debug( " ### INTERNAL RULES: Trying to execute in = " + kSession );
}
Object retVal = null;
LoggerHelper.debug( " ### INTERNAL RULES: Trying to execute in = " + kSession + " - batch: " + $batch );
try {
org.drools.runtime.impl.ExecutionResultImpl result = (org.drools.runtime.impl.ExecutionResultImpl) kSession.execute( $batch );
if( ! ( $batch instanceof AsyncBatchExecutionCommandImpl ) ) {
if ( LoggerHelper.isDebugEnabled() ) {
LoggerHelper.debug( " ### INTERNAL RULES: Executing Synchronous Batch, waiting for results ... " );
}
retVal = result.getValue( "$out_" + $msgId );
if ( LoggerHelper.isDebugEnabled() ) {
LoggerHelper.debug( " ### INTERNAL RULES: Remote Execution return = " + retVal );
}
ResponseContent response = new ResponseContent( $nodeId, $sessionId, $msgId, retVal );
insert( response );
} else {
if ( LoggerHelper.isDebugEnabled() ) {
LoggerHelper.debug( " ### INTERNAL RULES: Executing Asynchronous Batch, results will come later ... " );
}
}
} catch ( Exception e ) {
e.printStackTrace();
if ( LoggerHelper.isDebugEnabled() ) {
LoggerHelper.debug(" ### INTERNAL RULES: Remote Execution fails = " + e.getMessage() );
}
insert( new PerformativeFail( $msgId, e.getMessage() ) );
}
end
rule "Invoke_OneShot"
salience 1
when
$msg : ACLMessage( $msgId : id )
$dst : Destination( $msgId , $sessionId ; )
$cmd : OneShotMessageCommand( $msgId, $act ; )
SessionLocator( $nodeId: nodeId, sessionId == $sessionId )
then
if ( LoggerHelper.isDebugEnabled() ) {
LoggerHelper.debug( " ### INTERNAL: EXECUTING msg command ONE for "+ $msgId + " in " + $sessionId );
LoggerHelper.debug( " ### INTERNAL: \t commands " + $cmd );
}
retract( $cmd );
StatefulKnowledgeSession kSession = GridHelper.getStatefulKnowledgeSession( grid, $nodeId, $sessionId, true );
kSession.execute( $act );
kSession.execute( CommandFactory.newFireAllRules() );
end
// Process Performatives
rule "Process_Inform"
when
$msg : ACLMessage( $msgId : id, performative == Act.INFORM )
Inform( $info : proposition ) from $msg.body
$dst : Destination( $msgId ; )
then
if (LoggerHelper.isTraceEnabled()) {
LoggerHelper.trace(" ### INTERNAL: GENERATING COMMANDS FOR INFORM " + $msgId );
}
List list = new ArrayList(2);
//list.add(CommandFactory.newInsert( $info.getData(), "$out_"+$msgId ));
list.add( CommandFactory.newInsert( $info.getData() ) );
list.add( CommandFactory.newFireAllRules() );
AsyncBatchExecutionCommandImpl batch = new AsyncBatchExecutionCommandImpl( list );
insert( new MessageCommands( $msgId, batch ) );
end
rule "Process_Confirm"
when
$msg : ACLMessage( $msgId : id, performative == Act.CONFIRM )
Confirm( $info : proposition ) from $msg.body
$dst : Destination( $msgId ; )
then
if (LoggerHelper.isTraceEnabled()) {
LoggerHelper.trace(" ### INTERNAL: GENERATING COMMANDS FOR CONFIRM " + $msgId );
}
List list = new ArrayList(2);
//list.add(CommandFactory.newInsert( $info.getData(), "$out_"+$msgId ));
list.add( CommandFactory.newInsert( $info.getData() ) );
list.add( CommandFactory.newFireAllRules() );
AsyncBatchExecutionCommandImpl batch = new AsyncBatchExecutionCommandImpl( list );
insert( new MessageCommands( $msgId, batch ) );
end
rule "Process_Disconfirm"
when
$msg : ACLMessage( $msgId : id, performative == Act.DISCONFIRM )
Disconfirm( $info : proposition ) from $msg.body
$dst : Destination( $msgId ; )
then
if (LoggerHelper.isTraceEnabled()) {
LoggerHelper.trace(" ### INTERNAL: GENERATING COMMANDS FOR DISCONFIRM " + $msgId );
}
List list = new ArrayList(2);
list.add( new RetractFromObjectCommand( $info.getData() ) );
list.add( CommandFactory.newFireAllRules() );
AsyncBatchExecutionCommandImpl batch = new AsyncBatchExecutionCommandImpl( list );
insert( new MessageCommands( $msgId, batch ) );
end
rule "Process_QueryIf"
when
$msg : ACLMessage( $msgId : id, performative == Act.QUERY_IF )
QueryIf( $info : proposition ) from $msg.body
$dst : Destination( $msgId , $sessionId ; )
SessionLocator( $nodeId: nodeId, sessionId == $sessionId )
then
if ( LoggerHelper.isTraceEnabled() ) {
LoggerHelper.trace(" ### INTERNAL: GENERATING COMMANDS FOR QUERYIF " + $msgId );
}
List list = new ArrayList( 1 );
list.add( new QueryRemoteCommand( "$out_" + $msgId, "queryIf", true, new Object[] { $info.getData() } ) );
BatchExecutionCommand batch = CommandFactory.newBatchExecution( list );
insert( new MessageCommands( $msgId, batch ) );
end
rule "Fix_QueryIf_Results"
salience 9999
no-loop
when
$msg : ACLMessage( $msgId : id, performative == Act.QUERY_IF )
$cont : ResponseContent( $nodeId: nodeId, $sessionId: sessionId, messageId == $msgId, $data : data != null )
QueryResults( size > 0 ) from $data
then
if ( LoggerHelper.isDebugEnabled() ) {
LoggerHelper.debug(" ### INTERNAL: FIXING QUERYIF RESULTS " + $msgId );
LoggerHelper.debug(" ### INTERNAL: FIXING QUERYIF RESULTS " + $nodeId );
LoggerHelper.debug(" ### INTERNAL: FIXING QUERYIF RESULTS " + $sessionId );
}
if( $data instanceof QueryResultsDescriptor ) {
$data = GridHelper.getQueryResultRemoteClient( grid, $nodeId, $sessionId,
( (QueryResultsDescriptor) $data ).getQueryName(), ( (QueryResultsDescriptor) $data ).getQueryId() );
}
modify ( $cont ) {
setData( ( (QueryResults) $data ).iterator().next().get( "$queryIf" ) );
}
end
rule "Process_QueryRef"
when
$msg : ACLMessage( $msgId : id, performative == Act.QUERY_REF )
QueryRef( $query : query ) from $msg.body
$dst : Destination( $msgId , $sessionId ; )
SessionLocator($nodeId: nodeId, sessionId == $sessionId)
then
if (LoggerHelper.isTraceEnabled()) {
LoggerHelper.trace(" ### INTERNAL: GENERATING COMMANDS FOR QUERYREF " + $msgId);
}
List list = new ArrayList(2);
list.add(new QueryRemoteCommand( "$out_"+$msgId, $query.getQueryName() ,true, $query.getArgs().toArray() ));
BatchExecutionCommand batch = CommandFactory.newBatchExecution(list);
insert( new MessageCommands( $msgId, batch ) );
end
rule "Fix_QueryRef_Results"
salience 9999
no-loop
when
$msg : ACLMessage( $msgId : id, performative == Act.QUERY_REF )
QueryRef( $query : query ) from $msg.body
$cont : ResponseContent($nodeId: nodeId, $sessionId: sessionId, messageId == $msgId, $data : data != null )
then
if ( LoggerHelper.isDebugEnabled() ) {
LoggerHelper.debug(" ### INTERNAL: FIXING RESULTS FOR QUERYREF " + $msgId);
LoggerHelper.debug(" ### INTERNAL: FIXING RESULTS FOR QUERYREF " + $nodeId);
LoggerHelper.debug(" ### INTERNAL: FIXING RESULTS FOR QUERYREF " + $sessionId);
LoggerHelper.debug(" ### INTERNAL: FIXING RESULTS FOR QUERYREF " + $data);
}
if( $data instanceof QueryResultsDescriptor ) {
$data = GridHelper.getQueryResultRemoteClient( grid, $nodeId, $sessionId,
( (QueryResultsDescriptor) $data ).getQueryName(), ( (QueryResultsDescriptor) $data ).getQueryId() );
}
modify ( $cont ) {
setData( MessageContentHelper.getQueryReferences( $query, (QueryResults) $data ) );
}
end
rule "Process_Request"
when
$msg : ACLMessage( $msgId : id, performative == Act.REQUEST )
$req : Request( $act : action, $actName : action.actionName ) from $msg.body
$dst : Destination( $msgId , $sessionId ; )
SessionLocator($nodeId: nodeId, sessionId == $sessionId)
then
if (LoggerHelper.isTraceEnabled()) {
LoggerHelper.trace(" ### INTERNAL: GENERATING COMMANDS FOR REQUEST " + $msgId);
}
List list = new ArrayList(3);
list.add(CommandFactory.newInsert( $req.getAction() ));
list.add(CommandFactory.newFireAllRules());
AsyncBatchExecutionCommandImpl batch = new AsyncBatchExecutionCommandImpl(list);
insert( new MessageCommands( $msgId, batch ) );
end
rule "Fix_Request_Results"
salience 9999
no-loop
when
$msg : ACLMessage( $msgId : id, performative == Act.REQUEST )
$req : Request( $act : action, $actName : action.actionName ) from $msg.body
$cont : ResponseContent($nodeId: nodeId, $sessionId: sessionId, messageId == $msgId, $data : data != null )
then
if ( LoggerHelper.isDebugEnabled() ) {
LoggerHelper.debug( " ### INTERNAL: FIXING RESULTS FOR REQUEST " + $msgId );
LoggerHelper.debug( " ### INTERNAL: FIXING RESULTS FOR REQUEST " + $sessionId );
LoggerHelper.debug( " ### INTERNAL: FIXING RESULTS FOR REQUEST " + $nodeId );
LoggerHelper.debug( " ### INTERNAL: FIXING RESULTS FOR REQUEST " + $data );
}
modify ( $cont ) {
setData( MessageContentHelper.getActionReferences( $act, (Map) $data ) );
}
end
rule "Process_Request_When"
when
$msg : ACLMessage( $msgId : id, performative == Act.REQUEST_WHEN )
$req : RequestWhen( $act : action, $actName : action.actionName, $condition : condition.drl ) from $msg.body
$dst : Destination( $msgId , $sessionId ; )
SessionLocator( $nodeId: nodeId, sessionId == $sessionId )
then
if (LoggerHelper.isDebugEnabled()) {
LoggerHelper.debug(" ### INTERNAL: GENERATING COMMANDS FOR REQUEST WHEN " + $msgId + " " + $condition);
}
ActionHolder holder = new ActionHolder( $msgId, $act, $condition, false );
java.util.List list = new ArrayList( 2 );
list.add( CommandFactory.newInsert( holder ) );
list.add( CommandFactory.newFireAllRules() );
AsyncBatchExecutionCommandImpl batch = new AsyncBatchExecutionCommandImpl( list );
insert( new OneShotMessageCommand( $msgId, batch ) );
end
rule "Process_Request_Whenever"
when
$msg : ACLMessage( $msgId : id, performative == Act.REQUEST_WHENEVER )
$req : RequestWhenever( $act : action, $actName : action.actionName, $condition : condition.drl ) from $msg.body
$dst : Destination( $msgId , $sessionId ; )
SessionLocator( $nodeId: nodeId, sessionId == $sessionId )
then
if (LoggerHelper.isTraceEnabled()) {
LoggerHelper.trace(" ### INTERNAL: GENERATING COMMANDS FOR REQUEST WHENEVER " + $msgId + " " + $condition);
}
ActionHolder holder = new ActionHolder( $msgId, $act, $condition, true );
java.util.List list = new ArrayList( 2 );
list.add( CommandFactory.newInsert( holder ) );
list.add( CommandFactory.newFireAllRules() );
AsyncBatchExecutionCommandImpl batch = new AsyncBatchExecutionCommandImpl( list );
insert( new OneShotMessageCommand( $msgId, batch ) );
end
rule "Failed_Results"
salience 9999
no-loop
when
$msg : ACLMessage( $msgId : id, performative == Act.REQUEST || == Act.REQUEST_WHEN || == Act.REQUEST_WHENEVER )
$req : Request( $act : action, $actName : action.actionName ) from $msg.body
$cnt : ResponseContent( messageId == $msgId, $data : data == null, $fault: fault )
ExecutionFault($faultString: faultString) from $fault
not PerformativeFail( messageId == $msgId )
// no exception thrown (so no failure is already in), but still the request did not return anything
then
PerformativeFail fail = new PerformativeFail( $msgId, $actName + " : No result is returned, because: "+$faultString );
if (LoggerHelper.isTraceEnabled()) {
LoggerHelper.trace(" ### INTERNAL: FAILURE -> "+ fail);
}
insert( fail );
end
rule "Clean Destinations"
when
$dst : Destination( $id ; )
not ACLMessage( id == $id )
then
retract( $dst );
end