Certain important constructs

Some constructs are more returning than others as an everyday business developer with MDriven. Your favorite ways to express yourself may be different from mine but these are some of my returning expressions:

let z= Things.allinstances- >select(x|x.someInt>3)->size in

(

If z>4 then

‘There are more than 4

Things with SomeInt>3’

else

‘There are ‘+z.asstring+’

Things with SomeInt>3’

endif

)

I use the “let” construct to assign a result of an expression to a temporary variable. This so I do not need to repeat myself in the testing of z>4 and the z.asstring
Thing.allinstances-

>groupby(x|x.SomeValue)

Groupby , this expression has the type Collection(SomeValue:ValueStore+List:Collection(Thing)) so I get a list of SomeValue and for each a list of the things that use it
Thing.allinstances-

>collect(x|x.SomeValue.UsedB

y.SomeInt->sum,

x.SomeValue.UsedBy-

>collect(y|x, y.Details))

Nested collecting. This expression get the type Collection(Part1:System.Int32+Part2:Collection(Thing:Thing+Det ails:Collection(Detail))) The ability to nest collections is very powerful. In this case I start with all Things – grab the SomeValue valueStore– check what other things has this set via the association MultiValuePick and for these I sum up all SomeValue plus grab the Details. This kind of multi level collect-usage is very handy when summarizing deep object hierarchy’s on different levels
' '.Chars(0) This expression returns System.Char. Since OCL has not literal way to input a single character – it is always interpreted as string – this trick will help when calling certain .net functions that takes characters as arguments
if true then

'this returns a string'

else

0.asstring

endif

All return paths must result in the same type. Since OCL is a functional language we must be consistent. This is one way to get the expression correct ; add.asstring after the zero
Thing.allinstances-

>select(someint>3)

ValueStore.allinstances-

>select(usedby-

>notEmpty).Thing

Like this:

Thing.allinstances-

>select(someint>3)

->intersection(

ValueStore.allinstances-

>select(usedby-

>notEmpty).Thing

)

Working with OCLps – expressions that will be translated into SQL and executed in a database- it is sometimes easier to do one expression per complex constraint and at the end intersect all the expressions together.
self When you are in the context of an object you can use the variable self to access properties of this

Methods

You may define methods in classes to and implement these with OCL:

Method.png

You will in the OCL implementation in the Body-property:

Method ocl.png

Notice that since this was a method MDriven will treat you OCL as EAL – something that is allowed to have side effects. In this case our method do not have any side effects and I may want to be able to use this method in OCL. But trying to use it in OCL will not succeed. Methods with side effects are not recognized by OCL . There is a flag on the Method definition called IsQuery and if this is set we “promise” that it does not have intentional side effects. Now it is seen by OCL:

Method ocl2.png

We can then use our IsQuery method in any expression in OCL. Thing.allinstances- >select(x|x.MyMethod(x.SomeInt))

This page was edited 46 days ago on 04/03/2024. What links here