4.13. Constraints

A constraint is a boolean expression that is evaluated when the element with the constraint is executed. The workflow depends on the result of the evaluation, that is true or false.

There are two types of constraints:

  • Code constraints, which are defined in Java or MVEL, and have access to the data in the working memory, including the global and process variables.

    Example 4.4. Java Code Constraint

    return person.getAge() > 20;

    Example 4.5. MVEL Code Constraint

    return person.age > 20;
  • Rule constraints, which are defined in the form of DRL rule conditions. They have access to data in the working memory, including the global variables. However, they cannot access the variables in the process directly, but through the process instance. To retrieve the reference of the parent process instance, use the processInstance variable of the type WorkflowProcessInstance. Note that you need to insert the process instance into the session and update it if necessary, for example, using Java code or an on-entry, on-exit, or explicit action in your process.

    Example 4.6. Rule Constraint with Process Variable Assignment

    import org.kie.api.runtime.process.ProcessInstance;
    import org.kie.api.runtime.process.WorkflowProcessInstance;
    ...
    processInstance : WorkflowProcessInstance()
    Person( name == ( processInstance.getVariable("name") ) )

    This rule constraint retrieves the process variable name.

Red Hat JBoss BPM Suite includes a script editor for Java expressions. The constrain condition allows code constraints for scripts in Java as demonstrated by the editor below.

Figure 4.22. Script Editor

Script editor for BPM Suite 6.3 constraints.

When a Java script cannot be represented by the editor, the following alert appears:

6087