Domanda

I have two questions concerning the same UML class diagram. The first one is about how to model template class with UML native types. The second one is about how to handle template classes in OCL constraints.

Question 1: template class

I would like to use a template class for intervals and represent it using UML standards. Intervals must be usable with integers and floats. The best method I found so far is the following:

Best solution found so far

The idea here is to have a template class, with parameter T being a superclass of either Integer and Float classes.

The problem I see is that I need to redefine the basic types of UML in order to group them. I would like to know if there is a clean way to define a template class and saying that T is either of type integer or float (being here the primitives of UML).

Question 2: OCL constraints for template classes

The second aspect in my problem is that I want to be able to add OCL constraints to say that my intervals must contain at least 2 elements. The problem is that the rules must not be the same depending on the binding of T in the previous class diagram.

For floats:

context  Interval
inv :    self.supBound > self.infBound

For integers:

context Interval
inv :   (self.infBoundIncluded and self.supBoundIncluded) implies supBound - infBound >= 1

context Interval
inv :   (not(self.infBoundIncluded) and self.supBoundIncluded) implies supBound - infBound >= 2

context Interval
inv :   (self.infBoundIncluded and not(self.supBoundIncluded)) implies supBound - infBound >= 2

context Interval
inv :   (not(self.infBoundIncluded) and not(self.supBoundIncluded)) implies supBound - infBound >= 3

So I need to find a way in OCL to say that some rules only apply when T is bound to Integer, and others when it is bound to Float. I am not an expert in OCL, and I couldn't find any helpful information, so I'm asking for some help.

Thanks in advance,

Bastien.

È stato utile?

Soluzione

After more research, I came out with the following solutions:

Question 1

The solution is to use a template class with a generic type (this class won't be instantiable according to UML standards), and to bind it with realization classes. The corresponding UML class diagram is as follows:

enter image description here

Here, we have two usable classes IntegerInterval and RealInterval derived from a general template class Interval, which keeps things simple, in addition to using UML basic types integer and real.

Question 2

Because the separation between integer and real intervals is done at the class level, the OCL differentiation is straightforward. Thus the constraints are as follows:

context IntegerInterval
inv:    ...

context RealInterval
inv:    ...

Anyway, I'm still open to other suggestions :)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top