Camunda BPM supports Unified Expression Language (EL), specified as part of the JSP 2.1 standard(JSR-245). To do so, it uses the open source JUEL implementation. To get more generalinformation about the usage of Expression Language, please read the official documentation.Especially the provided examples give a good overview of the syntax of expressions.

Within Camunda BPM, EL can be used in many circumstances to evaluate small script-likeexpressions. The following table provides an overview of the BPMN elements which supportusage of EL.

BPMN element EL support
Service Task, Business Rule Task, Send Task, Message Intermediate Throwing Event, Message End Event, Execution Listener and Task Listener Expression language as delegation code
Sequence Flows, Conditional Events Expression language as condition expression
All Tasks, All Events, Transaction, Subprocess and Connector Expression language inside an inputOutput parameter mapping
Different Elements Expression language as the value of an attribute or element
All Flow Nodes, Process Definition Expression language to determine the priority of a job

Usage of Expression Language

Delegation Code

Besides Java code, Camunda BPM also supports the evaluation of expressions as delegation code. Forgeneral information about delegation code, see the correspondingsection.

Two types of expressions are currently supported: camunda:expression andcamunda:delegateExpression.

With camunda:expression it is possible to evaluate a value expression or to invokea method expression. You can use special variables which are available inside an expression orSpring and CDI beans. For more information about variables and Spring, respectively CDI beans,please see the corresponding sections.

  1. <process id="process">
  2. <extensionElements>
  3. <!-- execution listener which uses an expression to set a process variable -->
  4. <camunda:executionListener event="start" expression="${execution.setVariable('test', 'foo')}" />
  5. </extensionElements>
  6. <!-- ... -->
  7. <userTask id="userTask">
  8. <extensionElements>
  9. <!-- task listener which calls a method of a bean with current task as parameter -->
  10. <camunda:taskListener event="complete" expression="${myBean.taskDone(task)}" />
  11. </extensionElements>
  12. </userTask>
  13. <!-- ... -->
  14. <!-- service task which evaluates an expression and saves it in a result variable -->
  15. <serviceTask id="serviceTask"
  16. camunda:expression="${myBean.ready}" camunda:resultVariable="myVar" />
  17. <!-- ... -->
  18. </process>

The attribute camunda:delegateExpression is used for expressions which evaluate to a delegateobject. This delegate object must implement either the JavaDelegate or ActivityBehaviorinterface.

  1. <!-- service task which calls a bean implementing the JavaDelegate interface -->
  2. <serviceTask id="task1" camunda:delegateExpression="${myBean}" />
  3. <!-- service task which calls a method which returns delegate object -->
  4. <serviceTask id="task2" camunda:delegateExpression="${myBean.createDelegate()}" />

Conditions

To use conditional sequence flows or conditional events, expression language is usually used.For conditional sequence flows, a conditionExpression element of a sequence flow has to be used.For conditional events, a condition element of a conditional event has to be used. Both areof the type tFormalExpression. The text content of the element is the expression to be evaluated.

Within the expression, some special variables are available which enable access of the currentcontext. To find more information about the available variables, please see the correspondingsection.

The following example shows usage of expression language as condition of a sequence flow:

  1. <sequenceFlow>
  2. <conditionExpression xsi:type="tFormalExpression">
  3. ${test == 'foo'}
  4. </conditionExpression>
  5. </sequenceFlow>

For usage of expression language on conditional events, see the following example:

  1. <conditionalEventDefinition>
  2. <condition type="tFormalExpression">${var1 == 1}</condition>
  3. </conditionalEventDefinition>

inputOutput Parameters

With the Camunda inputOutput extension element you can map an inputParameter or outputParameterwith expression language.

Inside the expression some special variables are available which enable the access of the currentcontext. To find more information about the available variables please see the correspondingsection.

The following example shows an inputParameter which uses expression language to call a method ofa bean.

  1. <serviceTask id="task" camunda:class="org.camunda.bpm.example.SumDelegate">
  2. <extensionElements>
  3. <camunda:inputOutput>
  4. <camunda:inputParameter name="x">
  5. ${myBean.calculateX()}
  6. </camunda:inputParameter>
  7. </camunda:inputOutput>
  8. </extensionElements>
  9. </serviceTask>

Value

Different BPMN and CMMN elements allow to specify their content or an attribute value by anexpression. Please see the corresponding sections for BPMN and CMMN in the referencesfor more detailed examples.

Availability of Variables and Functions Inside Expression Language

Process Variables

All process variables of the current scope are directly available inside an expression. So aconditional sequence flow can directly check a variable value:

  1. <sequenceFlow>
  2. <conditionExpression xsi:type="tFormalExpression">
  3. ${test == 'start'}
  4. </conditionExpression>
  5. </sequenceFlow>

Internal Context Variables

Depending on the current execution context, special built-in context variables are available whileevaluating expressions:

Variable Java Type Context
execution DelegateExecution Available in a BPMN execution context like a service task, execution listener or sequence flow.
task DelegateTask Available in a task context like a task listener.
caseExecution DelegateCaseExecution Available in a CMMN execution context.
authenticatedUserId String The id of the currently authenticated user. Only returns a value if the id of the currently authenticated user has been set through the corresponding methods of the IdentityService. Otherwise it returns null.

The following example shows an expression which sets the variable test to the currentevent name of an execution listener.

  1. <camunda:executionListener event="start"
  2. expression="${execution.setVariable('test', execution.eventName)}" />

External Context Variables With Spring and CDI

If the process engine is integrated with Spring or CDI, it is possible to access Spring and CDIbeans inside of expressions. Please see the corresponding sections for Spring and CDIfor more information. The following example shows the usage of a bean which implements theJavaDelegate interface as delegateExecution.

  1. <serviceTask id="task1" camunda:delegateExpression="${myBean}" />

With the expression attribute any method of a bean can be called.

  1. <serviceTask id="task2" camunda:expression="${myBean.myMethod(execution)}" />

Internal Context Functions

Special built-in context functions are available while evaluating expressions:

Function Return Type Description
currentUser() String Returns the user id of the currently authenticated user or null no user is authenticated at the moment.
currentUserGroups() List of Strings Returns a list of the group ids of the currently authenticated user or null if no user is authorized at the moment.
now() Date Returns the current date as a Java Date object.
dateTime() DateTime Returns a Joda-Time DateTime object of the current date. Please see the Joda-Time documentation for all available functions.

The following example sets the due date of a user task to the date 3 days after the creationof the task.

  1. <userTask id="theTask" name="Important task" camunda:dueDate="${dateTime().plusDays(3).toDate()}"/>

Built-In Camunda Spin Functions

If the Camunda Spin process engine plugin is activated, the Spin functions S,XML and JSON are also available inside of an expression. See the Data Formats section for a detailed explanation.

  1. <serviceTask id="task" camunda:expression="${XML(xml).attr('test').value()}" resultVariable="test" />

原文: https://docs.camunda.org/manual/7.9/user-guide/process-engine/expression-language/