简体   繁体   中英

Write OCL restrictions related to associations with other classes

How do you model the following restriction?

A place is popular if it has been bookmarked by 2 or more than 2 users.

Here the corresponding uml diagram:

在此处输入图像描述 uml

I tried several ways, for example:

context place inv: place.popular = (self.user.place.popular>=2)

but nothing worked well...

The constraint that you expressed is an interesting start but does not work because self.user in the place context is a collection. Your expression moreover attempts to use popular as if it were an integer.

If there would be an unambiguous user , you'd just need to check its size() :

context place inv: 
  place.popular = (self.user->size()>1)

Unfortunately, there are two associations with User : one for the favorites (bookmarks) and for for historic (past visits whether they appreciated or not). This makes that expression ambiguous. To disambiguate, in absence of a role name (name at the association end), you'll need to qualify the user with the association name:

context place inv: 
  place.popular = (self.favorites::user->size()>1)

(Btw, in case of absence of association name, you'd need to use the default name A_place_user instead of favorites ).

See also section 7.5.3 of the OCL specifications for more information about navigating associations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM