OCL Stereotype Constraint: Any association that is coloured all connected classes have the same colour

443 Views Asked by At

Quite new to OCL, thanks for the help.

So I have a profile with the stereotypes as shown:

Profile

Can someone tell me how to write an invariant constraint that says any association that is Coloured means that all connected classes must have the same colour value in their colour property?

I had this so far:

Context UML::InfrastructureLibrary::Core::Constructs::Association
inv: Association.allInstances() -> forAll(a:Association|a.oclIsTypeOf(Coloured) implies 
    a.associatedElement.colour = a.colour)

Pretty sure this is wrong because a.associatedElement does not exist.. but I do not understand how to access this 'all connected classes' or what that means. Perhaps I need something like

a.[association].colour ?

Thanks

1

There are 1 best solutions below

0
On

From the UML spec (2.4.1 Superstructure, Sec. 7.3.3):

memberEnd : Property [2..*] Each end represents participation of instances of the classifier connected to the end in links of the association. This is an ordered association. Subsets Namespace::member.

So, something like this:

(...) -> forAll(a:Association|a.oclIsTypeOf(Coloured))->forAll(memberEnd->colour() = a.colour)

I doubt that's the exact statement, but it should get you started in the right direction.

Your diagram should specify the constraint as well. Just attach a note to your Coloured stereotype saying if the instance is an association both connected class objects have to have the same colour property.