Section A.3. Operators


A.3. Operators

OCL has the basic arithmetic, logic, and comparison operators. OCL also has more advanced functions such as returning the maximum of two values and concatenating Strings. OCL is a typed language, so the operator has to make sense for its values. For example, you can't take the sum of an Integer and a Boolean. Table A-2 shows commonly used operators in OCL expressions.

Table A-2. Commonly used operators in OCL expressions

Group

Operators

Used with types

Example OCL expression

Arithmetic

+, -, *, /

Integer, Real

baseCost + tax

Additional Arithmetic

abs(), max(), min( )

Integer, Real

score1.max(score2)

 

Comparison

<, <=, >, >=

Integer, Real

rate > .75

Equality

=, <>

All

age = 65

title <> 'CEO'

Boolean

and, or, xor, not

Boolean

isMale and (age >= 65)

String

concat(), size(), substring( ), toInteger(), toReal( )

String

title.substring(1,3)


Operators in the groups Comparison, Equality, and Boolean all return results of type Boolean. For example, age = 65 evaluates to true or false. The other operators in Table A-2 return the same type with which they're used. For example, if baseCost and tax are Real, then baseCost + tax will also be Real.

Figure A-2 shows that getTaxRate( ) returns a double (this model was written with Java types), but the table in Table A-2 mentions that the operator + is defined on Reals and Integers. That's perfectly fine; when building an OCL expression, you can match your types to the closest OCL type.

OCL can also express operations on collections, such as unions of sets. For a more complete list of OCL expressions, see UML 2.0 in a Nutshell (O'Reilly).





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