This version of the product has reached End of Life.
A flexible tool, you can use Mule Expression Language (MEL) to evaluate many kinds of expressions, including constants, simple operands, operations with multiple values, or method calls on objects. You can also use MEL to perform regex or XPath3 functions. Thus, though the syntax is consistent, the expressions you write or come across in Mule applications may take many different forms, depending on their purpose. This document aims to offer insight into the basic syntax rules for MEL so you can write, or parse the functionality of MEL expressions.
For further details on MEL syntax, see the full Mule Expression Language Reference material.
This document assumes that you have read Mule Expression Language MEL and have studied the Mule message structure.
Essentially, you use MEL expressions in combination with other things such as operands and operators to dynamically return a value. Mule evaluates the expression at runtime, and the message processor within which the MEL expression resides uses the result to act upon the message payload, properties or variables. For example, you can use an expression inside a Set Payload message processor to extract a username from an inboundProperty on a message, then set it as the payload. See example below.
A few definitions before diving into syntax rules.
In Mule, an expression is a string, enclosed with #[ ] . At runtime, Mule uses MEL (as the default expression evaluator) to determine the value of the expression. Mule supports other types of expressions as well (which must also be enclosed in #[ ] ), they are used to evaluate your expression if it can’t be recognized as MEL.
MEL expressions follow a Java-like syntax. Overall, the basic operators and operands are conventional and predictable (for example, 2 + 2 == 4 returns true ). The simplest kind of expression is the one that provides access to information from the message (for example, message.id ). The Examples in a section below offers some insight into how you can use MEL expressions to access information, perform comparisons, and invoke methods.
It’s important to note that MEL is an expression language, not a programming language. Thus, an expression in Mule:
The term Context Object forms the first part of the simplest form of a MEL expression; the second part is the Field (see image below, left). Use a Variable in a MEL expression to access information contained within a Flow Variable or Session Variable on your Mule message. Itself a top-level identifier in MEL, a variable does not require that you define a context object in an expression (see second image below).
The tables below list the Context Objects and Variables available for use in Mule, and the corresponding Fields you can use with them.
server
The operating system on which the message processor is running.
dateTime
env
fileSeparator
host
ip
locale
javaVendor
javaVersion
nanoSeconds
osArch
osName
osVersion
systemProperties
timeZone
tmpDir
userDir
userHome
userName
mule
The Mule instance on which the application is running.
clusterId
home
nodeId
version
application
The user application within which the current flow is deployed.
encoding
name
registry
standalone
workDir
message
The package (payload, attachments, properties) that the message processor is processing.
id
rootId
correlationId
correlationSequence
correlationGroupSize
replyTo
dataType
payload
inboundProperties
inboundAttachments
outboundProperties
outboundAttachments
flowVars
A variable set on the message; flow variables persist only within the flow in which they were created.
sessionVars
A session variable set on the message; session variables persist across flows within an application.
Shortcut for payloadMule accepts the expression #[payload] as a shortcut for #[message.payload] . This shortcut only applies with the payload field.
For a full list of syntax rules, see full MEL reference material.
Always bounds an expression.
Simple expressions
The simplest type of expression, these consist of just a context object and a field, or simply a variable. Provides access to information from the message including payload, properties, and variables.
Multiline expressions
#[calendar = Calendar.getInstance();
message.payload = new org.mule.el.datetime.DateTime(calendar);]
You can include several lines in a single expression, each must end with a ;
Operators
Performs operations in expressions. Can be unary, comparison, logical, bitwise, arithmetic, and more.
Boolean expressions
Produces Boolean values.
Bean Property Access
Access information from bean.
Method invocations
Calls a method, then performs it on an object according to the parameter (if any) specified within the parentheses. Method calls always follow the syntax: object.method()
Assignments
Evaluates to assign a value. The example at left resolves dynamically to set the payload to sample .
Literals
'expression'
255
null
Strings, numbers, Boolean values, types, and nulls.
xpath and regex
xpath3 and regex provide ways of extracting context information that doesn’t already exist as a single value.
Wildcards
Matches a value (the message payload, by default) against a wildcard pattern, these use the meta-characters '?' to represent any single character and '*' for a repetition of any character. It’s case sensitive by default.
Further, you can use expressions inline to create lists, maps and arrays. Learn more about accessing maps, lists and arrays from within a MEL expression.
Inline list
Evaluates to produce a list.
Inline map
[key1 : value1, key2: value2, …]
Evaluates to produce a map.
Inline array
Evaluates to produce an array.
There is really no such thing as a single typical MEL expression. That said, a few example expressions can help illustrate how MEL expressions resolve. As the following table of examples demonstrates, the values that MEL expressions return can be numerical values, logical values ( true or false ), strings, or virtually any data type. MEL expressions can also perform operations, invoke methods, and execute functions. Explore all the possibilities by consulting the complete syntax reference. Access full examples that illustrate how to use MEL expressions in applications.
This expression evaluates to 4.
This expression uses an operator to perform a comparison. It evaluates to true.
This expression references a context object in MEL ( message , app , mule , and server ). The value of this expression is the message.
This expression accesses the ID field of the message context object. The value of this expression is the unique message id that Mule automatically assigns to the message.
This expression accesses an object within the field (payload) associated with the context object (message). If the object is a map item whose key is 'firstname' then this expression evaluates to the value associated with the key 'firstname'. If the object is a bean, the property is returned.
Same as above, but in this case – provided the field is a list – the expression returns the value of the 5th item in the list. (4 refers to the 5th item because the first item in the list is the 0 item.)
This expression calls the get() method and performs it on the object, according to the parameter (if any) specified within the parentheses.
If you are configuring a field that supports expressions and need help with syntax, you can access MEL suggestions by one of two methods.
Note that the autocomplete functionality described here works in the Visual Editor only. Although Studio’s XML tab does offer some autocomplete options, the suggestions there are limited by Eclipse and are not based on DataSense or Mule Expression Language. |