7.2 Basic Types and Operators


The following sections define the basic predefined types and their operations. Basic types are typically not very interesting to read about, so this section is deliberately kept short. It explains only the more interesting operations of the basic types, those that differ from the ones in most programming languages and therefore offer some surprises .

7.2.1 The Boolean Type

A value of Boolean type can only be one of two values: true or false . The operations defined on Boolean include all the familiar ones, shown in Table 7-1. A standard operation on the Boolean type that is uncommon to most programming languages ”but often encountered in a more theoretical environment or in specification languages ”is the implies operation. This operation states that the result of the total expression is true if it is the case that when the first boolean operand is true, the second Boolean operand is also true. If the first boolean operand is false, the whole implies expression always evaluates to true.

Table 7-1. Standard operations for the Boolean type

Operation

Notation

Result Type

or

a or b

Boolean

and

a and b

Boolean

exclusive or

a xor b

Boolean

negation

not a

Boolean

equals

a = b

Boolean

not equals

a <> b

Boolean

implies

a implies b

Boolean

Take as an example the class Service from the R&L example. The result of the following sample expression is true if for every service it can be said that when it offers bonus points it never burns bonus points. In other words, a customer cannot earn bonus points, when using a service that is bought with bonus points:

  context  Service  inv  : self.pointsEarned > 0 implies not (self.pointsBurned = 0) 

Another interesting operation on the Boolean type is the if-then-else. It is denoted in the following manner:

 if <boolean OCL expression> then <OCL expression> else <OCL expression> endif 

The result value of an if-then-else operation is the result of either the OCL expression in the then clause or the OCL expression in the else clause, depending on the result of the boolean expression in the if clause. You cannot omit the else clause of the expression because an OCL expression must result in a value. Omitting the else clause causes the expression to result in an undefined state if the boolean OCL expression in the if clause is false. Both OCL expressions within the else and the then clauses must be of the same type.

Here are some other examples of valid boolean expressions:

 not true age() > 21 and age() < 65 age() <= 12 xor cards->size() > 3 title = 'Mr.' or title = 'Ms.' name = 'Foobar' if standard = 'UML'    then 'using UML standard'    else 'watch out: non UML features' endif 
Table 7-2. Standard operations for the Integer and Real types

Operation

Notation

Result Type

equals

a = b

Boolean

not equals

a <> b

Boolean

less

a < b

Boolean

more

a > b

Boolean

less or equal

a <= b

Boolean

more or equal

a >= b

Boolean

plus

a + b

Integer or Real

minus

a - b

Integer or Real

multiplication

a * b

Integer or Real

division

a / b

Real

modulus

a.mod(b)

Integer

integer division

a.div(b)

Integer

absolute value

a.abs()

Integer or Real

maximum of a and b

a.max(b)

Integer or Real

minimum of a and b

a.min(b)

Integer or Real

round

a.round()

Integer

floor

a.floor()

Integer

7.2.2 The Integer and Real Types

The Integer type in OCL represents the mathematical natural numbers . Because OCL is a modeling language, there are no restrictions on the integer values; in particular, there is no such thing as a maximum integer value. In the same way, the Real type in OCL represents the mathematical concept of real values. As in mathematics, Integer is a subtype of Real .

Table 7-3. Standard operations for the String type

Operation

Expression

Result Type

concatenation

string.concat(string)

String

size

string.size()

Integer

to lower case

string.toLower()

String

to upper case

string.toUpper()

String

substring

string.substring(int,int)

String

equals

string1 = string2

Boolean

not equals

string1 <> string2

Boolean

For the Integer and Real types, the usual operations apply: addition, subtraction, multiplication, and division. For both the Integer and the Real types, there is an additional operation, abs, that provides the absolute value of the given value; for example, -1.abs() results in 1, and (2.4).abs() results in 2.4. An additional operator on the Real type is the floor operator, which rounds the real value down to an integer number; for example, (4.6).floor() results in an integer instance with the value 4. The round operation on a real value results in the closest integer; for example, (4.6).round() results in the Integer 5. An overview of all operations on integer and real values is provided in Table 7-2.

The following examples illustrate the Real and Integer types. All these examples are expresssions of the Boolean type, which result in true:

 2654 * 4.3 + 101 = 11513.2 (3.2).floor() / 3 = 1 1.175 * (-8.9).abs() - 10 = 0.4575 12 > 22.7 = false 12.max(33) = 33 33.max(12) = 33 13.mod(2) = 1 13.div(2) = 6 33.7.min(12) = 12.0 -24.abs() = 24 (-2.4).floor() = -3 

7.2.3 The String Type

Strings are sequences of characters . Literal strings are written with enclosing single quotes, such as 'apple' or 'weird cow' . The operations available on Strings are toUpper, toLower, size, substring, and concat (see Table 7-3).

The following examples illustrate the String type. All these examples are expressions of the Boolean type, and result in true:

 'Anneke'.size() = 6 ('Anneke' = 'Jos') = false 'Anneke '.concat('and Jos') = 'Anneke and Jos' 'Anneke'.toUpper() = 'ANNEKE' 'Anneke'.toLower() = 'anneke' 'Anneke and Jos'.substring(12, 14) = 'Jos' 


Object Constraint Language, The. Getting Your Models Ready for MDA
The Object Constraint Language: Getting Your Models Ready for MDA (2nd Edition)
ISBN: 0321179366
EAN: 2147483647
Year: 2003
Pages: 137

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