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
FormulaResultfield 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.Totalwas set to50.00. - Visual Result: Upon clicking the action,
FormulaResultupdated to show5.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 theFormulaResultattribute. - User Feedback: By storing the result of
ScriptEvalCheckin 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.
