Appendix A. Object Constraint Language


Chapter 5 introduced writing constraints in your class diagrams using OCL. You don't have to use OCL to express constraintsyou can use your favorite programming language syntax or even natural language. This appendix discusses the advantages of OCL and provides more details about how to more use OCL.

Recall from Chapter 5 that a constraint is written in curly braces after the element it constrains or displayed in an attached note. Figure A-1 shows different ways of specifying that the attribute rating has to be non-negative.

Figure A-1. Different ways of attaching and expressing a constraint


Figure A-1 shows that the words expressing a constraint can vary. Constraints can be written in natural language, such as:

 rating is greater than or equal to zero 

Constraints can also look like a programming language expression, such as:

 rating >= 0 

Because natural language can be ambiguous (and long-winded!), many modelers use syntax similar to their preferred programming language: notice that rating >= 0 looks like a Java or C expression.

Constraints can get more complicated; for example, they can specify that a value isn't null. This means you have a lot of options for expressing constraints, so how do you decide which notion to use? Such an expression may look different in different programming languages. If constraints are expressed in a standard and predictable way, not only can you easily understand the constraint, but also automated tools can understand the constraint. This allows automatic checking of constraints in your diagrams and in the code generated from the diagrams.

Because of this, the Object Management Group (OMG, the group behind UML) was convinced that a single formal constraint language was needed, but the language had specific requirements. The language had to allow values to be checked but not changedin other words, it had to be an expression language. The language had to be general enough that you could use it to express constraints in your UML diagrams regardless of your target implementation language. And finally, the language had to be simple enough that people would actually use it, which is not true of many formal languages.

OCL, developed at IBM for business modeling, had all of these features, and so it was a perfect match. So, OCL was chosen to work alongside UML to provide a formal yet easy-to-understand language for specifying constraints.

You don't have to use OCL. In general, modelers decide to use OCL depending on a combination of factors, including how extensively they model and how important they consider design by contract (discussed later). If these factors apply to you, OCL is worth considering because automated constraint checking allows greater integrity of your model.

In UML diagrams, OCL is primarily used to write constraints in class diagrams and guard conditions in state and activity diagrams.




Learning UML 2.0
Learning UML 2.0
ISBN: 0596009828
EAN: 2147483647
Year: 2007
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net