/
RuleSetNodeInstance.java
485 lines (414 loc) · 20.8 KB
/
RuleSetNodeInstance.java
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/**
* Copyright 2005 Red Hat, Inc. and/or its affiliates.
*
* 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.
*/
package org.jbpm.workflow.instance.node;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.stream.Collectors;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.drools.core.common.InternalAgenda;
import org.drools.core.common.InternalKnowledgeRuntime;
import org.drools.core.util.MVELSafeHelper;
import org.jbpm.process.core.Context;
import org.jbpm.process.core.ContextContainer;
import org.jbpm.process.core.context.exception.ExceptionScope;
import org.jbpm.process.core.context.variable.Variable;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.process.core.datatype.DataType;
import org.jbpm.process.core.impl.DataTransformerRegistry;
import org.jbpm.process.instance.ContextInstance;
import org.jbpm.process.instance.ContextInstanceContainer;
import org.jbpm.process.instance.ProcessInstance;
import org.jbpm.process.instance.context.exception.ExceptionScopeInstance;
import org.jbpm.process.instance.context.variable.VariableScopeInstance;
import org.jbpm.process.instance.impl.ContextInstanceFactory;
import org.jbpm.process.instance.impl.ContextInstanceFactoryRegistry;
import org.jbpm.workflow.core.node.DataAssociation;
import org.jbpm.workflow.core.node.RuleSetNode;
import org.jbpm.workflow.core.node.Transformation;
import org.jbpm.workflow.instance.WorkflowRuntimeException;
import org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory;
import org.kie.api.runtime.KieRuntime;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.process.DataTransformer;
import org.kie.api.runtime.process.EventListener;
import org.kie.api.runtime.process.NodeInstance;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.dmn.api.core.DMNContext;
import org.kie.dmn.api.core.DMNMessage.Severity;
import org.kie.dmn.api.core.DMNModel;
import org.kie.dmn.api.core.DMNResult;
import org.kie.dmn.api.core.DMNRuntime;
import org.kie.internal.runtime.StatefulKnowledgeSession;
import org.mvel2.integration.impl.MapVariableResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Runtime counterpart of a ruleset node.
*
*/
public class RuleSetNodeInstance extends StateBasedNodeInstance implements EventListener, ContextInstanceContainer {
private static final long serialVersionUID = 510l;
private static final Logger logger = LoggerFactory.getLogger(RuleSetNodeInstance.class);
private static final String ACT_AS_WAIT_STATE_PROPERTY = "org.jbpm.rule.task.waitstate";
private Map<String, FactHandle> factHandles = new HashMap<String, FactHandle>();
private String ruleFlowGroup;
// NOTE: ContetxInstances are not persisted as current functionality (exception scope) does not require it
private Map<String, ContextInstance> contextInstances = new HashMap<String, ContextInstance>();
private Map<String, List<ContextInstance>> subContextInstances = new HashMap<String, List<ContextInstance>>();
protected RuleSetNode getRuleSetNode() {
return (RuleSetNode) getNode();
}
public void internalTrigger(final NodeInstance from, String type) {
try {
super.internalTrigger(from, type);
// if node instance was cancelled, abort
if (getNodeInstanceContainer().getNodeInstance(getId()) == null) {
return;
}
if (!org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
throw new IllegalArgumentException("A RuleSetNode only accepts default incoming connections!");
}
RuleSetNode ruleSetNode = getRuleSetNode();
KieRuntime kruntime = getProcessInstance().getKnowledgeRuntime();
Map<String, Object> inputs = evaluateParameters(ruleSetNode);
if (ruleSetNode.isDMN()) {
String namespace = resolveVariable(ruleSetNode.getNamespace());
String model = resolveVariable(ruleSetNode.getModel());
String decision = resolveVariable(ruleSetNode.getDecision());
DMNRuntime runtime = ((KieSession) kruntime).getKieRuntime(DMNRuntime.class);
DMNModel dmnModel = runtime.getModel(namespace, model);
if (dmnModel == null) {
throw new IllegalArgumentException("DMN model '" + model + "' not found with namespace '" + namespace + "'");
}
DMNResult dmnResult = null;
DMNContext context = runtime.newContext();
for (Entry<String, Object> entry : inputs.entrySet()) {
context.set(entry.getKey(), entry.getValue());
}
if (decision != null && !decision.isEmpty()) {
dmnResult = runtime.evaluateDecisionByName(dmnModel, decision, context);
} else {
dmnResult = runtime.evaluateAll(dmnModel, context);
}
if (dmnResult.hasErrors()) {
String errors = dmnResult.getMessages(Severity.ERROR).stream()
.map(message -> message.toString())
.collect(Collectors.joining(", "));
throw new RuntimeException("DMN result errors:: " + errors);
}
processOutputs(dmnResult.getContext().getAll());
triggerCompleted();
} else {
// first set rule flow group
setRuleFlowGroup(resolveRuleFlowGroup(ruleSetNode.getRuleFlowGroup()));
//proceed
for (Entry<String, Object> entry : inputs.entrySet()) {
String inputKey = getRuleFlowGroup() + "_" + getProcessInstance().getId() + "_" + entry.getKey();
factHandles.put(inputKey, kruntime.insert(entry.getValue()));
}
if (actAsWaitState()) {
addRuleSetListener();
((InternalAgenda) getProcessInstance().getKnowledgeRuntime().getAgenda())
.activateRuleFlowGroup(getRuleFlowGroup(), getProcessInstance().getId(), getUniqueId());
} else {
((InternalAgenda) getProcessInstance().getKnowledgeRuntime().getAgenda())
.activateRuleFlowGroup(getRuleFlowGroup(), getProcessInstance().getId(), getUniqueId());
((KieSession) getProcessInstance().getKnowledgeRuntime()).fireAllRules();
removeEventListeners();
retractFacts();
triggerCompleted();
}
}
} catch (Exception e) {
handleException(e);
}
}
private void handleException(Throwable e) {
ExceptionScopeInstance exceptionScopeInstance = getExceptionScopeInstance(e);
if(exceptionScopeInstance != null) {
exceptionScopeInstance.handleException(e.getClass().getName(), e);
} else {
if(ExceptionUtils.getRootCause(e) != null) {
handleException(ExceptionUtils.getRootCause(e));
} else {
throw new WorkflowRuntimeException(this, getProcessInstance(), "Unable to execute Action: " + e.getMessage(), e);
}
}
}
private ExceptionScopeInstance getExceptionScopeInstance(Throwable e) {
return (ExceptionScopeInstance) resolveContextInstance(ExceptionScope.EXCEPTION_SCOPE, e.getClass().getName());
}
public void addEventListeners() {
super.addEventListeners();
addRuleSetListener();
}
private String getRuleSetEventType() {
InternalKnowledgeRuntime kruntime = getProcessInstance().getKnowledgeRuntime();
if (kruntime instanceof StatefulKnowledgeSession) {
return "RuleFlowGroup_" + getRuleFlowGroup() + "_" + ((StatefulKnowledgeSession) kruntime).getIdentifier();
} else {
return "RuleFlowGroup_" + getRuleFlowGroup();
}
}
private void addRuleSetListener() {
getProcessInstance().addEventListener(getRuleSetEventType(), this, true);
}
public void removeEventListeners() {
super.removeEventListeners();
getProcessInstance().removeEventListener(getRuleSetEventType(), this, true);
}
public void cancel() {
super.cancel();
((InternalAgenda) getProcessInstance().getKnowledgeRuntime().getAgenda()).deactivateRuleFlowGroup(getRuleFlowGroup());
}
public void signalEvent(String type, Object event) {
if (getRuleSetEventType().equals(type)) {
removeEventListeners();
retractFacts();
triggerCompleted();
}
}
public void retractFacts() {
Map<String, Object> objects = new HashMap<String, Object>();
KieRuntime kruntime = getProcessInstance().getKnowledgeRuntime();
for (Entry<String, FactHandle> entry : factHandles.entrySet()) {
Object object = ((StatefulKnowledgeSession)kruntime).getObject(entry.getValue());
String key = entry.getKey();
key = key.replaceAll(getRuleFlowGroup()+"_", "");
key = key.replaceAll(getProcessInstance().getId()+"_", "");
objects.put(key , object);
kruntime.delete(entry.getValue());
}
processOutputs(objects);
factHandles.clear();
}
private void processOutputs(Map<String, Object> objects) {
RuleSetNode ruleSetNode = getRuleSetNode();
if (ruleSetNode != null) {
for (Iterator<DataAssociation> iterator = ruleSetNode.getOutAssociations().iterator(); iterator.hasNext(); ) {
DataAssociation association = iterator.next();
if (association.getTransformation() != null) {
Transformation transformation = association.getTransformation();
DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
if (transformer != null) {
Object parameterValue = transformer.transform(transformation.getCompiledExpression(), objects);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance)
resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
if (variableScopeInstance != null && parameterValue != null) {
variableScopeInstance.setVariable(association.getTarget(), parameterValue);
} else {
logger.warn("Could not find variable scope for variable {}", association.getTarget());
logger.warn("Continuing without setting variable.");
}
if (parameterValue != null) {
variableScopeInstance.setVariable(association.getTarget(), parameterValue);
}
}
} else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance)
resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
if (variableScopeInstance != null) {
Object value = objects.get(association.getSources().get(0));
if (value == null) {
try {
value = MVELSafeHelper.getEvaluator().eval(association.getSources().get(0), new MapVariableResolverFactory(objects));
} catch (Throwable t) {
// do nothing
}
}
Variable varDef = variableScopeInstance.getVariableScope().findVariable(association.getTarget());
DataType dataType = varDef.getType();
// exclude java.lang.Object as it is considered unknown type
if (!dataType.getStringType().endsWith("java.lang.Object") && value instanceof String) {
value = dataType.readValue((String) value);
}
variableScopeInstance.setVariable(association.getTarget(), value);
} else {
logger.warn("Could not find variable scope for variable {}", association.getTarget());
}
}
}
}
}
protected Map<String, Object> evaluateParameters(RuleSetNode ruleSetNode) {
Map<String, Object> replacements = new HashMap<String, Object>();
for (Iterator<DataAssociation> iterator = ruleSetNode.getInAssociations().iterator(); iterator.hasNext(); ) {
DataAssociation association = iterator.next();
if (association.getTransformation() != null) {
Transformation transformation = association.getTransformation();
DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
if (transformer != null) {
Object parameterValue = transformer.transform(transformation.getCompiledExpression(), getSourceParameters(association));
if (parameterValue != null) {
replacements.put(association.getTarget(), parameterValue);
}
}
} else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
Object parameterValue = null;
VariableScopeInstance variableScopeInstance = (VariableScopeInstance)
resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getSources().get(0));
if (variableScopeInstance != null) {
parameterValue = variableScopeInstance.getVariable(association.getSources().get(0));
} else {
try {
parameterValue = MVELSafeHelper.getEvaluator().eval(association.getSources().get(0), new NodeInstanceResolverFactory(this));
} catch (Throwable t) {
logger.error("Could not find variable scope for variable {}", association.getSources().get(0));
logger.error("when trying to execute RuleSetNode {}", ruleSetNode.getName());
logger.error("Continuing without setting parameter.");
}
}
if (parameterValue != null) {
replacements.put(association.getTarget(), parameterValue);
}
}
}
for (Map.Entry<String, Object> entry: ruleSetNode.getParameters().entrySet()) {
if (entry.getValue() instanceof String) {
Object value = resolveVariable(entry.getValue());
if (value != null) {
replacements.put(entry.getKey(), value);
}
}
}
return replacements;
}
private Object resolveVariable(Object s) {
if (s instanceof String) {
Matcher matcher = PARAMETER_MATCHER.matcher((String) s);
while (matcher.find()) {
String paramName = matcher.group(1);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance)
resolveContextInstance(VariableScope.VARIABLE_SCOPE, paramName);
if (variableScopeInstance != null) {
Object variableValue = variableScopeInstance.getVariable(paramName);
if (variableValue != null) {
return variableValue;
}
} else {
try {
Object variableValue = MVELSafeHelper.getEvaluator().eval(paramName, new NodeInstanceResolverFactory(this));
if (variableValue != null) {
return variableValue;
}
} catch (Throwable t) {
logger.error("Could not find variable scope for variable {}", paramName);
}
}
}
}
return s;
}
protected Map<String, Object> getSourceParameters(DataAssociation association) {
Map<String, Object> parameters = new HashMap<String, Object>();
for (String sourceParam : association.getSources()) {
Object parameterValue = null;
VariableScopeInstance variableScopeInstance = (VariableScopeInstance)
resolveContextInstance(VariableScope.VARIABLE_SCOPE, sourceParam);
if (variableScopeInstance != null) {
parameterValue = variableScopeInstance.getVariable(sourceParam);
} else {
try {
parameterValue = MVELSafeHelper.getEvaluator().eval(sourceParam, new NodeInstanceResolverFactory(this));
} catch (Throwable t) {
logger.warn("Could not find variable scope for variable {}", sourceParam);
}
}
if (parameterValue != null) {
parameters.put(association.getTarget(), parameterValue);
}
}
return parameters;
}
private String resolveRuleFlowGroup(String origin) {
return (String) resolveVariable(origin);
}
public Map<String, FactHandle> getFactHandles() {
return factHandles;
}
public void setFactHandles(Map<String, FactHandle> factHandles) {
this.factHandles = factHandles;
}
public String getRuleFlowGroup() {
if (ruleFlowGroup == null || ruleFlowGroup.trim().length() == 0) {
ruleFlowGroup = getRuleSetNode().getRuleFlowGroup();
}
return ruleFlowGroup;
}
public void setRuleFlowGroup(String ruleFlowGroup) {
this.ruleFlowGroup = ruleFlowGroup;
}
protected boolean actAsWaitState() {
Object asWaitState = getProcessInstance().getKnowledgeRuntime().getEnvironment().get(ACT_AS_WAIT_STATE_PROPERTY);
if (asWaitState != null) {
return Boolean.parseBoolean(asWaitState.toString());
}
return false;
}
@Override
public List<ContextInstance> getContextInstances(String contextId) {
return this.subContextInstances.get(contextId);
}
@Override
public void addContextInstance(String contextId, ContextInstance contextInstance) {
List<ContextInstance> list = this.subContextInstances.get(contextId);
if (list == null) {
list = new ArrayList<ContextInstance>();
this.subContextInstances.put(contextId, list);
}
list.add(contextInstance);
}
@Override
public void removeContextInstance(String contextId, ContextInstance contextInstance) {
List<ContextInstance> list = this.subContextInstances.get(contextId);
if (list != null) {
list.remove(contextInstance);
}
}
@Override
public ContextInstance getContextInstance(String contextId, long id) {
List<ContextInstance> contextInstances = subContextInstances.get(contextId);
if (contextInstances != null) {
for (ContextInstance contextInstance: contextInstances) {
if (contextInstance.getContextId() == id) {
return contextInstance;
}
}
}
return null;
}
@Override
public ContextInstance getContextInstance(Context context) {
ContextInstanceFactory conf = ContextInstanceFactoryRegistry.INSTANCE.getContextInstanceFactory(context);
if (conf == null) {
throw new IllegalArgumentException("Illegal context type (registry not found): " + context.getClass());
}
ContextInstance contextInstance = (ContextInstance) conf.getContextInstance(context, this, (ProcessInstance) getProcessInstance());
if (contextInstance == null) {
throw new IllegalArgumentException("Illegal context type (instance not found): " + context.getClass());
}
return contextInstance;
}
@Override
public ContextContainer getContextContainer() {
return getRuleSetNode();
}
}