MDrivenWiki
Log in

Certain important constructs

From MDrivenWiki

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