Thanks @StephenOTT
For who may need, here is my solution to evaluate the gateway nodes and get the next possible task…
I dont know if it will work for every scenario, but worked for my process flow.
public List<UserTask> getNextTasks(String processDefinitionId, String taskDefinitionKey, Map<String, Object> vars) {
BpmnModelInstance modelInstance = repositoryService.getBpmnModelInstance(processDefinitionId);
ModelElementInstance instance = modelInstance.getModelElementById(taskDefinitionKey);
FlowNode flowNode = (FlowNode)instance;
return getOutgoingTask(flowNode, vars);
}
private List<UserTask> getOutgoingTask(FlowNode node, Map<String, Object> vars) {
List<UserTask> possibleTasks = new ArrayList<>();
for(SequenceFlow sf: node.getOutgoing()) {
if (sf.getName() != null) {
LOGGER.info("Entering flow node {}", sf.getName());
}
boolean next = true;
if (sf.getConditionExpression() != null) {
ExpressionFactory factory = new ExpressionFactoryImpl();
SimpleContext context = new SimpleContext(new SimpleResolver());
LOGGER.info("Generating map vars {}", vars.toString());
for (Map.Entry<String, Object> v : vars.entrySet()) {
if(v.getValue() instanceof Boolean) {
factory.createValueExpression(context, "${"+ v.getKey() +"}", Boolean.class).setValue(context, v.getValue());
}else if(v.getValue() instanceof java.util.Date) {
factory.createValueExpression(context, "${"+ v.getKey() +"}", java.util.Date.class).setValue(context, v.getValue());
}else {
factory.createValueExpression(context, "${"+ v.getKey() +"}", String.class).setValue(context, v.getValue());
}
}
ValueExpression expr1 = factory.createValueExpression(context, sf.getConditionExpression().getTextContent(), boolean.class);
next = (Boolean)expr1.getValue(context);
LOGGER.info("Evaluating expression {} to result {}", sf.getConditionExpression().getTextContent(), expr1.getValue(context));
}
if (next && sf.getTarget() != null) {
if (sf.getTarget() instanceof UserTask) {
LOGGER.info("Found next user task {}", sf.getTarget().getName());
possibleTasks.add((UserTask)sf.getTarget());
break;
}
possibleTasks.addAll(getOutgoing(sf.getTarget(), vars));
}
}
return possibleTasks;
}