1

I have a class diagram with numerous classes, some of them containing attributes of type string. I want all my strings to be of length at least 1.

The easy (yet ugly) solution is as follows:

context Class1
inv:    self.attributeOfTypeString.size > 0

context Class2
inv:    self.attributeOfTypeString.size > 0

...

Do you know a way to define an OCL constraints for all attributes matching a template? Something like:

global.select(attr | attr.TYPE = string) -> forall (str : string | str.size > 0)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Bastien Pasdeloup
  • 1,089
  • 12
  • 19

1 Answers1

1

Finally got an answer from somewhere else. I share it in case someone needs it someday. There are three possible ways to solve the problem.

1°) The first one is to remember that multiple inheritance is allowed in UML. Therefore, we can make all classes with a string attribute inherit from a WithString class, and set the OCL constraint on this parent class. However this makes the diagrams kinda unreadable.

2°) Another possibility is to create a class String and to store an instance of this class instead of all string attributes. The problem with this encapsulation solution is the performance (use of a getter for all strings).

3°) Finally, the cleanest solution to my opinion is the following: we can declare the OCL constraint at the meta level. In the class diagram describing class diagrams, we can just state that all strings are non-empty.

Bastien Pasdeloup
  • 1,089
  • 12
  • 19