Discussions
Categories
Groups
Documentation
Knowledge base
Developer portal
App catalog
The Hub
GitHub
Home
Newly Imported
User Forums
How it Works
OCL ... when is a collection really empty?
medini_1023909254
Sometimes the handling of collections in derived OCL Properties and in dynamic constraints may behave different then expected. Let's have a look at the ESL Example. The requirement does have a derived property "Derived FTA Events"(user_traced_events), which is defined using the following OCL expression: self.mediniGetTracedElements(FTA::Event) This will show up in the requirements table as a list of FTA events (last column in the screenshot): Now you may also be interested to see, how many events are traced - thus you use an additional OCL property - there are at least two options for this. First option is to use a similar expression as above and just add the size-operator for collections: in that case you would have the following OCL for the counter and it would show the numbers as expected: self.mediniGetTracedElements(FTA::Event)->size() This is not a complex expression - but in case you have much complex expressions, you may think of using the user_trace_events for the expression instead of having the same expression repeated every time - so your property (2nd counter) would look like this: self.getProfilePropertyValue('user_traced_events')->size() This will however give you some surprising results - all "empty" lists will show the size of 1: So why does this happen? Basically the value of a derived property is derived in two steps: First the OCL expression is evaluated The result is assigned to the variable (and remember a derived property in OCL is basically a collection) In case the expression evaluation does not return a result (i.e. it is empty) a collection is still created for the variable which has an "empty" entry. Point (3) results in the fact that size() delivers the value 1 for an empty eval result (and isEMpty() would also return false). So how to overcome this? A solution would be to select from the property only values of a type which you are expecting: self.getProfilePropertyValue('user_traced_events')->select(e |e.oclIsKindOf(FTA::Event))->size() There may also be other options required (e.g. in case the original collection contains elements of different types). To summarize: Be careful when using derived properties in other derived properties or in dynamic constraints. Besides this also the mixture of JS and OCL for derived properties has some additional implications. This will be handled in a separate Forum topic. Happy scripting Eckhardt
Find more posts tagged with
medini
medini legacy
Accepted answers
All comments
There are no accepted answers yet
Quick Links
All Categories
Recent Posts
Activity
Unanswered
Groups
Help
Best Of