No edit summary |
No edit summary |
||
Line 22: | Line 22: | ||
) | ) | ||
|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 | |I use the “let” construct to assign a result of an expression to a temporary variable. This is so I do not need to repeat myself in the testing of z>4 and the z.asstring. | ||
|- | |- | ||
|Thing.allinstances- | |Thing.allinstances- | ||
>groupby(x|x.SomeValue) | >groupby(x|x.SomeValue) | ||
|Groupby | |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- | |Thing.allinstances- | ||
Line 36: | Line 36: | ||
>collect(y|x, y.Details)) | >collect(y|x, y.Details)) | ||
|Nested collecting. This expression | |Nested collecting. This expression gets 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 have 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 hierarchies on different levels. | ||
|- | |- | ||
|' '.Chars(0) | |' '.Chars(0) | ||
|This expression returns System.Char. Since OCL has | |This expression returns System.Char. Since OCL has no literal way to input a single character – it is always interpreted as string – this trick will help when calling certain .net functions that take characters as arguments. | ||
|- | |- | ||
|if true then | |if true then | ||
Line 49: | Line 49: | ||
endif | 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 | |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- | |Thing.allinstances- | ||
Line 78: | Line 78: | ||
|- | |- | ||
|self | |self | ||
|When you are in the context of an object you can use the variable self to access properties of this | |When you are in the context of an object, you can use the variable self to access the properties of this. | ||
|} | |} | ||
[[Category:OCL]] | [[Category:OCL]] |
Revision as of 07:26, 27 February 2023
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 is 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 gets 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 have 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 hierarchies on different levels. |
' '.Chars(0) | This expression returns System.Char. Since OCL has no literal way to input a single character – it is always interpreted as string – this trick will help when calling certain .net functions that take 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 the properties of this. |