Mobile SDK > Disconnected mobile application reference > Expression Evaluator reference > Expression evaluation
 
Expression evaluation
The Expression class takes an expression, that is represented as the expression's identifier values, and evaluates the expression. The Expression class can be used to evaluate any expression. For example, the class can evaluate a logical expression (for an If block), or a Categorical expression that returns a category set (for category filtering purposes).
The Expression object constructor takes an array of opcodes that represent the expression. For example, Expression(opcodes).
The opcodes parameter must be an array of objects. Each object must include a Code property that is set to the opcode name. Each object must also include an Operands property, which is an array of operand values. The following example shows the expected parameter, represented as JSON. The example includes the opcodes PushCategorical (has one operand) and Function (has three operands).
[
{
"Code": "PushCategorical",
"Operands": [
"{cat,dog}"
]
},
{
"Code": "Function",
"Operands": [
"ContainsAny",
"1",
"Owns"
]
}
]
The Opcodes correspond to the following mrEvaluate expression.
Owns.ContainsAny({cat, dog})
Expression methods
getIdentifiers()
Returns an Array of objects. Each object has 2 properties (name and value).
evaluate(identifiers)
Returns the expression evaluation results, as an InterviewValue object, with the defined identifier values.
In the constructor, the Expression object builds a JavaScript expression string that is based on the supplied opcodes, and extracts a list of required identifiers. The example JSON is internally converted to the following JavaScript expression:
FunctionLibrary.Contains(Owns, new Categorical('{cat,dog}'))
The process for converting from opcodes to a JavaScript string requires processing the opcodes in sequence, generating the appropriate JavaScript for each opcode, and using a stack to run the opcodes. The following JavaScript example shows the process for a small set of opcodes. After all of the opcodes are processed, a single entry remains on the stack. The entry is the complete JavaScript expression.
function buildExpression(opcodes)
{
var stack = {};
opcodes.forEach(function(opcode) {
if (opcode.Code == 'PushIdentifier')
stack.push(opcode.Operands[0]);
else if (opcode.Code == 'PushNumeric')
stack.push("new InterviewValue(" + opcode.Operands[0] + ")");
else if (opcode.Code == 'PushCategorical')
stack.push("new Categorical('" + opcode.Operands[0] + "')");
else if (opcode.Code == 'OpAdd') {
var arg1 = stack.pop();
var arg2 = stack.pop();
var newStackEntry = arg2;
newStackEntry += ".add(";
newStackEntry += arg1;
newStackEntry += ")";
stack.push(newStackEntry);
}
... and so on...
Using the earlier example expression, getIdentifiers() returns the following JSON example:
[
  { "name": "Owns", "value": null }
]
The example includes an array that contains a single object with a property name of Owns and a value of null.
When the Expression object user assigns the result of getIdentifiers() to an identifiers variable, the following example can be used to assign a value to the identifier:
identifiers[0].value = new Categorical('{dog, rabbit}');
The expression can then be evaluated by using the following example:
var result = expression.evaluate(identifiers);
In this case, the result variable is assigned an InterviewValue object with a type of Boolean and a value of True.
The mobile application controls how the Expression, FunctionLibrary, InterviewValue, and Categorical objects are used.
See also
Expression Evaluator reference