🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators scripteval
Created by Hans.karlsen on 2022-03-30 · Last edited by Vale.buyondo on 2026-02-24.

ScriptEval is an operator on a model object (the context) that lets you have expressions as data and evaluate them.

You must state the valid Return type.

let info=self.ScriptEvalCheck(false,Double, self.SomeString) in
(
  vSomeStringResult:=(info='ok').casetruefalse(self.ScriptEval(false,Double, self.SomeString).asstring,info)
)

The above code will check if the self.SomeString value is a valid expression in the context of self and whether it returns a double - if so, the result will be converted to string and assigned to vSomeStringResult. If it's not a valid expression, the problem with the expression will be in vSomeStringResult.

Using the default model:

0.2222+self.SomeInt+self.SomeDateTime.Ticks

Usage Example

1. Model Structure

To implement this, two specific attributes were added to the Order class in the Class Diagram:

  • DynamicFormula (String): A derived attribute that holds the OCL "code" as text. In our case, it was set to 'self.Total * 0.10.ToDecimal'.
  • FormulaResult (String): A persistent attribute used to store and display the final outcome of the script evaluation.

2. The Implementation (Executable Action Language)

The logic was placed in a ViewModel Action (a button labelled "SCRIPT EVAL CHECK") using the following EAL script:

Object Constraint Language

-- 1. Check if the script is valid and returns a Double (Numeric)
let validationInfo = self.ScriptEvalCheck(false, Decimal, self.DynamicFormula) in
(
  -- 2. If valid, 'validationInfo' returns 'ok'
  if validationInfo = 'ok' then
    -- Evaluate the script and store result as string
    self.FormulaResult := self.ScriptEval(false, Decimal, self.DynamicFormula).asstring
  else
    -- If invalid, store the error message (e.g., 'Syntax error at...')
    self.FormulaResult := validationInfo
  endif
)

3. Verification & Troubleshooting Steps

During the implementation, two distinct states were visualized in the Turnkey UI:

A. The Syntax Error State

When the formula used mixed types (a Decimal Total multiplied by a Double 0.10), the ScriptEvalCheck successfully caught the error.

  • Visual Result: The FormulaResult field displayed: "Syntax error: 11: There is no version of * that takes these parameter types...".
  • Significance: This demonstrated that the system handles bad code gracefully without crashing.

B. The Success State

After correcting the formula to 'self.Total * 0.10.ToDecimal', the types were aligned.

  • Input Data: Order.Total was set to 50.00.
  • Visual Result: Upon clicking the action, FormulaResult updated to show 5.0.

4. Key Takeaways

  • Assignment is the Goal: The primary purpose of this OCL block is not just to "return" a value, but to perform an assignment (:=) to the FormulaResult attribute.
  • User Feedback: By storing the result of ScriptEvalCheck in a visible UI field, the developer or end-user receives immediate debugging information.
  • Type Alignment: Constants in dynamic strings (like 0.10) must often be explicitly cast (e.g., .ToDecimal) to match the attributes they interact with.