Suppose you have an excerpt of a larger profile for cars:
Now i want to define some constraints for a Car, say one of those constraints states, that if attrA
is true
, then attrB
must be false
like this using OCL:
Context UML::Core::Class inv:
self
.stereotype
.name='Car'
implies
self.attrA=true
implies
self.attrB=false
My question is: If the Mercedes
stereotype specializes the Car stereotype do I stick to the same constraints, in other words: is the stereotype Car
still applied for classes that have the Mercedes
stereotype applied?
I would suppose that self.stereotype.name='Car'
return false
if the applied stereotype is Mercedes
.
This is interesting because I want to have the same attributes on a Mercedes as on a Car, but i want to change the previously stated constraint.
I would suppose that self.stereotype.name='Car' return false if the applied stereotype is Mercedes.
Yes you are right.
Mercedes inherits the constraint as it is, so self.stereotype.name='Car'
is false for a class stereotyped Mercedes rather than Car because 'Mercedes' and 'Car' are two different strings.
If you want to have the first implies active for the metaclasses specializing Car directly or indirectly you can get all the generalizations of the stereotype more itself to search for one named 'Car', also checking the name of the profile of the stereotype and may be its URI. So for instance replace self.stereotype.name='Car'
by :
self.stereotype.profile.name = 'Cars' and
-- self.stereotype.profile.URI= '...' and
self.stereotype.generalization()
->closure(general.generalization).general()
->including(self.stereotype)
->select(name = 'Car')
->notEmpty()
or with an alone profile named Cars and an alone stereotype in it named Car :
self.stereotype.oclIsKindOf(Profile.allInstances()
->any(name = 'Cars') -- may be check also URI
.ownedStereotype->any(name = 'Car'))
Additional notes :
in your proposal you suppose only your stereotype is named Car among all the stereotypes of all the profiles, of course that can be false. You can also check the name of the profile and may be its URI like:
self.stereotype.name='Car'
and self.stereotype.profile.name='Cars'
-- and self.stereotype.profile.URI= '...'
in your diagram the arrow head is wrong because it must be a filled triangle rather than <
(probably you use PlantUML) :