7.4 基本值和类型

OCL预定义了一些基本类型,它们在任何时刻对建模者都是可用的。这些预定义的值类型是独立于任何对象模型的,是OCL定义的一部分。

OCL中最基本的值是基本类型中的一个值。OCL的基本类型,连同它们的值的示例,显示在如下的表格中。

Table 7.1 - - Basic OCL types and their values

类型 consistent with implementation definitions
OclInvalid invalid
OclVoid null, invalid
Boolean true, false (MOF) http://www.w3.org/TR/xmlschema-2/\#boolean
Integer 1, -5, 2, 34, 26524, ... (MOF) http://www.w3.org/TR/xmlschema-2/\#integer
Real 1.5, 3.14, ... http://www.w3.org/TR/xmlschema-2/\#double
String 'To be or not to be...' (MOF) http://www.w3.org/TR/xmlschema-2/\#string
UnlimitedNatural 0, 1, 2, 42, ..., * http://www.w3.org/TR/xmlschema-2/\#nonNegativeInteger

OCL在这些预定义类型上定义了一些操作。表7.2给出了一些预定义类型的操作示例。完整的操作列表可以参见11.4 基本类型。

Table 7.2 - - Examples of operations on the predefined types

类型 操作
Integer *, +, -, /, abs()
Real *, +, -, /, floor()
Boolean and, or, xor, not, implies, if-then-else
String concat(), size(), substring()
UnlimitedNatual *, +, /

Collection, Set, Bag, Sequence和Tuple也是基本类型。它们的特殊性将在后续章节介绍。

多个相邻的字符串可以连接起来以允许一个长字符串在多行显示。

'This is a '

'concatenated string'               -- 'This is a concatenated string'

Unicode字符用在单引号序列中,利用如下基于反斜杠的转义序列来定义反斜杠和其它字符。

\b -- backspace
\t -- horizontal tab
\n -- linefeed
\f -- form feed
\r -- carriage return
\" -- double quote
\' -- single quote
\\ -- backslash
\x_hh_ -- \#x00 to \#xFF
\u_hhhh_ -- \#x0000 to \#xFFFF

其中h是一个十六进制数字:0到9,A到F或a到f。

保留字像true以及任意不适合拼写的词可以通过将其包含在以下划线做前缀的单引号中作为名称。

self.'if' = 'tabbed\tvariable'.'spaced operation'()

(译者注:此处并未出现下划线前缀,是个问题)

results matching ""

    No results matching ""