MDrivenWiki
Log in

Examples on collection operators

From MDrivenWiki

One important aspect of OCL that is worth noting is that it expands lists of lists to just a list. An example in plain English; Thing.allinstances.Details – this will come back as a set of details that are all the details from all the Things. If OCL had not expanded lists automatically one could have expected a set of sets containing the details per thing. But this is not the case. The automatically expansion of lists of lists is sometime referred to as flattening of a collection – referring to the reduction of topology in the result.

Some OCL examples

Examples Results
Bag{'5','1','2','2','3','4'}->ascommalist 5, 1, 2, 2, 3, 4
Bag{'5','1','2','2','3','4'}- >union(Bag{'1','2','2','3','6'})->ascommalist 5, 1, 2, 2, 3, 4, 6
Bag{'5','1','2','2','3','4'}- >union(Bag{'1','2','2','3','6'})->asset- >ascommalist 5, 1, 2, 3, 4, 6
Bag{'5','1','2','2','3','4'}- >union(Bag{'1','2','2','3','6'})->asset- >orderby(a|a)->ascommalist 1, 2, 3, 4, 5, 6
Bag{'5','1','2','2','3','4'}- >intersection(Bag{'1','2','2','3','6'})- >orderby(a|a)->ascommalist 1, 2, 2, 3
Bag{'5','1','2','2','3','4'}- >Difference(Bag{'1','2','2','3','6'})->orderby(a|a)- >ascommalist 4, 5
Bag{'5','1','2','2','3','4'}- >SymmetricDifference(Bag{'1','2','2','3','6'})- >orderby(a|a)->ascommalist 4, 5, 6
If you are in the context of a simple type like string, double, int, datetime or Boolean MDriven will expose the simple operations that are available in the .net Framework. Testing this in the OCL-Editor:

New Column OCL.png

In this case it is a string that is the result – and we can to string operations like compare, indexof, split etc.

The numeric types float, double, decimal and int are sort of apples of the same tree and MDriven expose ways to go from all numeric types to decimal. The operator is called toDecimal.