Process Decision in Script

Is it possible to leverage the DecisionEngine from within a groovy script in a Task Listener? We have a situation where we’d like to derive several Task variables based on some Process variables when a Task is created, using a decision table.

I’m looking at the documentation for doing this in Java ( Evaluating Decisions using the DMN Engine API) but I can’t determine whether I would need to read the dmn file in as an inputstream if it was included with a deployment in my script.

Would I generally follow the pattern from the User Guide, adapting the java commands to script equivalents?

Hi @Bill_Powell,

if the DMN table is deployed into the engine, you can use the DecisionService to evaluate a decision: https://docs.camunda.org/javadoc/camunda-bpm-platform/7.11/org/camunda/bpm/engine/DecisionService.html#evaluateDecisionTableByKey-java.lang.String-java.util.Map-

Hope this helps, Ingo

Thank you, Ingo. Is it also necessary to initiate the DmnEngine, as described in the docs? Or has that already occurred?

DmnEngine dmnEngine = DmnEngineConfiguration
  .createDefaultDmnEngineConfiguration()
  .buildEngine();

Hi @Bill_Powell,

no, the dmn engine is part of the platform and created at startup. processEngine().getDecisionService() is sufficient.

Hope this helps, Ingo

1 Like

Thanks for your help. Here’s the functioning code, in case anyone is interested. I’m not a java or groovy developer, so I’m winging it here. This is executed within a Sub-Process and I needed to store the decision result variables with the Parent process, hence setVariableByBusinessKey()

def setPriority(taskName) {
    def dmnEngine = task.execution.getProcessEngineServices().getDecisionService()
    def rush = task.execution.getVariable("rush")
    def followUpDate = task.execution.getVariable("followUpDate")
    def clientDueDate = task.execution.getVariable("clientDueDate")
    def dmnKey = "TaskPriority_1"
    def variables = Variables
            .putValue("rush", rush)
            .putValue("task", taskName)
            .putValue("followUpDate", followUpDate)
            .putValue("clientDueDate", clientDueDate)
    result = dmnEngine.evaluateDecisionTableByKey(dmnKey, variables)
    def priorityLevel = result.getSingleResult().get("priorityLevel")
    def priorityDate = result.getSingleResult().get("priorityDate")
    bk = task.execution.processBusinessKey
    setVariableByBusinessKey(bk, "priorityLevel",priorityLevel)
    setVariableByBusinessKey(bk, "priorityDate",priorityDate)
}

def setVariableByBusinessKey(businessKey, processVariable, value){
    runtimeService = task.execution.getProcessEngineServices().getRuntimeService()
    processInstances = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(businessKey).list();
    for (ProcessInstance processInstance: processInstances){
        runtimeService.setVariable(processInstance.getId(), processVariable, value);
    }
}