constraintsumlclass-diagramocl

One-to-many association constraint in ocl


I am new in ocl and I am stuck in a problem. I had built an UML class diagram for a school and I want to create an association that connects one teacher from class Teachers to many students from class Students.

My problem not with creating the association but in creating the constraint that assure that exact one teacher will connect to exact number students with their names, lets say for example, teacher Smith will teach a group of students named (john, lily, sami, diana), those names are already in class students with other student names.


Solution

  • Constraints in a class-diagram intend to express general statements about the class and its instances. So the contraint must be valid for all the possible instances of the association, and not only one particular instance.

    For example some stupid hypothetical constraints that are for illustration only:

    If you would define a very specific constraint, it would have to be true for every association between teachers and students:

    This would be a very small school or perhaps many student and teachers have the same name ;-) THis is not what OCL is meant for.

    If you want to describe a specific case in a scenario, then you should not use OCL constraints. In olde UML 1.4.x, there was the object diagram. It allowed to draw specific instances and document their specific value at a given point in time. In such a diagram, you would have one rectangle for teacher Smith and 4 rectangles, on for each strudent of the group.

    Apparently, this kind of diagram was not often used: UML 2.5 doesn't mention it at all. So in principle it shouldn't be used. But it's not forbidden either. I suspect that this form of diagram would help you more than OCL if you're into describing a specific case.