You can use ScriptEvalCheck in EAL to validate an OCL expression stored as text before you evaluate it with ScriptEval.
What ScriptEvalCheck does
ScriptEvalCheck is an operator on the model object that supplies the context for the expression. It parses the supplied string as OCL and checks that the expression is valid in that object's context and returns the type you require.
The operator does not execute the supplied expression and does not change data. Use it before ScriptEval when an expression is held in an attribute or other string value.
For example, when the context is an Order, an expression can refer to attributes on that order:
0.2222 + self.SomeInt + self.SomeDateTime.TicksSyntax
self.ScriptEvalCheck(IsQuery, ReturnType, Expression)| Argument | Description |
|---|---|
IsQuery
|
Use false for a standard action and true for a read-only evaluation.
|
ReturnType
|
The type that the expression must return, such as Double, Decimal, String, or Integer.
|
Expression
|
The string containing the OCL expression to validate. This can be an attribute, for example self.DynamicFormula, or a string literal.
|
The return value is a string:
okmeans that the expression is valid in the current context and has the requested return type.- Any other value contains information about why validation failed. Store or display that value to give the user feedback.
Validate before evaluating
Use a let expression to keep the validation result, then call ScriptEval only when the result is ok.
let info = self.ScriptEvalCheck(false, Double, self.SomeString) in
(
vSomeStringResult :=
(info = 'ok').casetruefalse(
self.ScriptEval(false, Double, self.SomeString).asstring,
info
)
)In this example, self.SomeString must be a valid OCL expression in the context of self and return a Double. If it does, the evaluated value is converted to a string and assigned to vSomeStringResult. If it does not, vSomeStringResult receives the validation message instead.
Step-by-step pattern
- Put the dynamic OCL expression in a String attribute or other string value.
- Call
ScriptEvalCheckwith the sameIsQueryandReturnTypethat you plan to use for ScriptEval. - Compare the returned string with
'ok'. - Call ScriptEval only in the
okbranch. - In the other branch, assign the returned validation message to a visible result or error attribute.
Example: validate an Order formula
Assume an Order has these attributes:
| Attribute | Type | Purpose |
|---|---|---|
DynamicFormula
|
String | Holds the OCL formula as text. |
FormulaResult
|
String | Receives either the evaluated value or the validation message. |
Use the following EAL in an action on the Order context:
let validationInfo = self.ScriptEvalCheck(false, Decimal, self.DynamicFormula) in
(
if validationInfo = 'ok' then
self.FormulaResult := self.ScriptEval(false, Decimal, self.DynamicFormula).asstring
else
self.FormulaResult := validationInfo
endif
)With self.DynamicFormula set to self.Total * 0.10.ToDecimal and Order.Total set to 50.00, the action assigns 5.0 to FormulaResult.
What validation checks
ScriptEvalCheck verifies the expression before evaluation:
- OCL syntax: the text must be parseable OCL. For example, missing delimiters or an invalid operator expression fail validation.
- Model context: properties and operations referenced through
selfmust be available from the context object. In the Order example,self.Totalmust exist on Order. - Return type: the final expression must match
ReturnType.
Type mismatch example
If Total is Decimal, the formula below can fail because 0.10 is a Double while the requested result is Decimal:
self.Total * 0.10Use an explicit conversion so that the numeric types align:
self.Total * 0.10.ToDecimalOn failure, ScriptEvalCheck returns an error string, such as a message that no applicable version of * accepts the supplied parameter types. Do not depend on the exact wording of that message; test for ok and present the returned text as feedback.
Notes
- ScriptEvalCheck validates OCL stored as data; it is not a replacement for normal OCL editing and validation in MDriven Designer.
- Keep the
ReturnTypein ScriptEvalCheck and ScriptEval identical. A check against one type does not validate evaluation against another type. - Convert the ScriptEval result with
.asstringwhen assigning it to a String result attribute. - For parsing-related material, see Documentation:OCLOperators Parse. For general operator discovery, see Documentation:OCL General Operators.
See also
- Documentation:OCLOperators scripteval
- Documentation:OCLOperators Parse
- Documentation:OCL General Operators
- Documentation:OCL Boolean Operators
[[Category:EAL (Executable Action Language)]]
