Tải bản đầy đủ - 0 (trang)
Appendix B.  The Object Constraint Language

Appendix B.  The Object Constraint Language

Tải bản đầy đủ - 0trang

www.it-ebooks.info



UML 2.0 in a Nutshell

Dan Pilone,Neil Pitman

B.1. OCL By

Basics

...............................................

Publisher:O'Reilly

Pub Date: June 2005

ISBN:0-596-00795-7

The Object Constraint

Language is just that: a language. It obeys a syntax and has keywords.

However, unlikePages:

other234

languages, it can't be used to express program logic or flow control. By



design, OCL is a query-only language; it can't modify the model (or executing system) in any

way. It can be used to express preconditions, postconditions, invariants (things that must

Table of Contents

| Index

always

be TRue

), guard conditions, and results of method calls.

OCL can be used virtually anywhere in UML and is typically associated with a classifier using a

note.

When

an OCL expression

is evaluated,

it is considered

to be

meaning the

System

developers

have used modeling

languages

for decades

to instantaneous,

specify, visualize,

associated

classifier

can't

change

state

during

the

evaluation

of

an

expression.

construct, and document systems. The Unified Modeling Language (UML) is one of those

languages. UML makes it possible for team members to collaborate by providing a common

language that applies to a multitude of different systems. Essentially, it enables you to

communicate

solutions

B.1.1.

Basic

Typesin a consistent, tool-supported language.



Today, UML has become the standard method for modeling software systems, which means

OCL has several built-in types that can be used in OCL expressions:

you're probably confronting this rich and expressive language more than ever before. And

even though you may not write UML diagrams yourself, you'll still need to interpret

diagrams written by others.

Boolean

UML 2.0 in a Nutshell from O'Reilly feels your pain. It's been crafted for professionals like

you who

read, true

create,

and understand

artifacts

expressed

UML.

Mustmust

be either

or false

. Supports system

the logical

operators

and,or,using

xor,not

,implies,

Furthermore,

it's

been

fully

revised

to

cover

version

2.0

of

the

language.

andif-then-else.



This comprehensive new edition not only provides a quick-reference to all UML 2.0 diagram

types, it also explains key concepts in a way that appeals to readers already familiar with

Integer

UML or object-oriented programming concepts.

Any integer value (e.g., 100, -12345, 5211976, etc.). Supports the operators *,+,-,/,

abs( ).

Topicsand

include:

Real



The role and value of UML in projects

The object-oriented

paradigm

and its

relation

to the

UML the operators *,+,-,/, and

Any

decimal value (e.g.,

2.222003,

-67.76,

etc.).

Supports

floor( ).

An integrated approach to UML diagrams



String Class and Object, Use Case, Sequence, Collaboration, Statechart, Activity, Component,

and Deployment Diagrams

A series of letters, numbers, or symbols interpreted as a string (e.g., "All writing and no

Extension

play

make Mechanisms

Dan..."). Supports the operators concat( ),size( ), and substring( ).



The Object

Language

(OCL) used in your UML model is recognized as a type

In addition

to the Constraint

built-in types,

any classifier

by OCL. Because OCL is a strongly typed language, you can't compare values of one type

directly with values of another type.

If you're new to UML, a tutorial with realistic examples has even been included to help you

quickly familiarize yourself with the system.



B.1.2. Casting



OCL does support casting objects from one type to another as long as they are related

through a generalization relationship. To cast from one type to another use the operation



www.it-ebooks.info



oldType. oclAsType(newType) . For example, to cast a Java List to an ArrayList to call the

size( ) operation, use the expression:

UML 2.0 in a Nutshell



List.oclAsType(ArrayList).size(

ByDan Pilone,Neil Pitman



)



...............................................

Publisher:O'Reilly

If the actual object

isn't an

instance

Pub Date:

June

2005 of the new type, the expression is considered undefined.

ISBN:0-596-00795-7

Pages:234



Table of Contents | Index



System developers have used modeling languages for decades to specify, visualize,

construct, and document systems. The Unified Modeling Language (UML) is one of those

languages. UML makes it possible for team members to collaborate by providing a common

language that applies to a multitude of different systems. Essentially, it enables you to

communicate solutions in a consistent, tool-supported language.

Today, UML has become the standard method for modeling software systems, which means

you're probably confronting this rich and expressive language more than ever before. And

even though you may not write UML diagrams yourself, you'll still need to interpret

diagrams written by others.

UML 2.0 in a Nutshell from O'Reilly feels your pain. It's been crafted for professionals like

you who must read, create, and understand system artifacts expressed using UML.

Furthermore, it's been fully revised to cover version 2.0 of the language.

This comprehensive new edition not only provides a quick-reference to all UML 2.0 diagram

types, it also explains key concepts in a way that appeals to readers already familiar with

UML or object-oriented programming concepts.

Topics include:

The role and value of UML in projects

The object-oriented paradigm and its relation to the UML

An integrated approach to UML diagrams

Class and Object, Use Case, Sequence, Collaboration, Statechart, Activity, Component,

and Deployment Diagrams

Extension Mechanisms

The Object Constraint Language (OCL)



If you're new to UML, a tutorial with realistic examples has even been included to help you

quickly familiarize yourself with the system.



www.it-ebooks.info



UML 2.0 in a Nutshell

Dan Pilone,Neil Pitman

B.2. OCL By

Syntax

...............................................

Publisher:O'Reilly

Pub

Date:

June 2005

The remainder of

this

chapter

uses examples from the class diagram shown in Figure B-1.

ISBN:0-596-00795-7

Pages:234



B.2.1. Constraints on Classifiers

Table of Contents | Index



Each OCL expression must have some sense of context that an expression relates to. Often

the

context

can be determined

by where languages

the expression

is written.

For example,

you can link

System

developers

have used modeling

for decades

to specify,

visualize,

aconstruct,

constraintand

to an

element

using

a

note.

You

can

refer

to

an

instance

of

the

context

classifier

document systems. The Unified Modeling Language (UML) is one of those

using

the keyword

self. itFor

example,

you members

had a constraint

on Student

that theira GPA

must

languages.

UML makes

possible

for if

team

to collaborate

by providing

common

always

be

higher

than

2.0,

you

can

attach

an

OCL

expression

to

Student

using

a

note

and

language that applies to a multitude of different systems. Essentially, it enables you to

refer

to the GPA

as follows:

communicate

solutions

in a consistent, tool-supported language.



Today,

UML has

the standard method for modeling software systems, which means

self.GPA

> become

2.0

you're probably confronting this rich and expressive language more than ever before. And

even though you may not write UML diagrams yourself, you'll still need to interpret

diagrams written by others.



Figure B-1. Example class diagram used in this chapter



UML 2.0 in a Nutshell from O'Reilly feels your pain. It's been crafted for professionals like

you who must read, create, and understand system artifacts expressed using UML.

Furthermore, it's been fully revised to cover version 2.0 of the language.

This comprehensive new edition not only provides a quick-reference to all UML 2.0 diagram

types, it also explains key concepts in a way that appeals to readers already familiar with

UML or object-oriented programming concepts.

Topics include:

The role and value of UML in projects

The object-oriented paradigm and its relation to the UML

An integrated approach to UML diagrams

Class and Object, Use Case, Sequence, Collaboration, Statechart, Activity, Component,

and Deployment Diagrams

Extension Mechanisms

The Object Constraint Language (OCL)

It's important to realize that this OCL expression is an invariant, meaning the system would

be in an invalid state if a student's GPA dropped to less than 2.0. If you want to allow a GPA

of less than 2.0 and send out a letter to the student's parents in the event such a low GPA is

If you're new

to UML,

a tutorial

realistic

examples

has evensuch

been

to help

achieved,

you would

model

such with

behavior

using

a UML diagram

asincluded

an activity

or you

quickly familiarize

interaction

diagram.yourself with the system.

You can follow associations between classifiers using the association end names as though

they were attributes of the originating classifier. The following invariant on Course ensures

that the instructor is being paid:



www.it-ebooks.info



self.instructor.salary > 0.00



a Nutshell of 0..1, you can treat the association end as a Set and

If an associationUML

has2.0a inmultiplicity

ByDan

Pilone,

check to see if the

value

isNeil

setPitman

by using the built-in notEmpty( ) operation. To call the

...............................................

notEmpty( ) operation

on a set you must use an arrow (->) rather than a dot (.). See

O'Reilly

"Collections" forPublisher:

more information

on sets. The following invariant on Course enforces that a

Pub Date: June 2005

course has an instructor:

ISBN:0-596-00795-7

Pages:234



self.instructor->notEmpty(



)



Table of Contents | Index



If an association role name isn't specified, you can use the classifier name. The following

invariant on School checks that each course has a room assignment:



System developers have used modeling languages for decades to specify, visualize,

construct,

and document systems. The Unified<>

Modeling

Language (UML) is one of those

self.Course->forAll(roomAssignment

'No room!')

languages. UML makes it possible for team members to collaborate by providing a common

language that applies to a multitude of different systems. Essentially, it enables you to

communicate

in ainto

consistent,

tool-supported

language.

Comments

cansolutions

be inserted

an expression

by prefixing

each comment with two dashes (),

like this:

Today, UML has become the standard method for modeling software systems, which means

you're probably confronting this rich and expressive language more than ever before. And

make you

suremay

this

could

graduate

even--though

notstudent

write UML

diagrams

yourself, you'll still need to interpret

self.GPA

> 2.0

diagrams

written

by others.



UML 2.0 in a Nutshell from O'Reilly feels your pain. It's been crafted for professionals like

Ifyou

youwho

can't

determine

the context

of an expression

lookingexpressed

at the UMLusing

model,

or if you

must

read, create,

and understand

systemby

artifacts

UML.

want

to be explicit

with fully

the context,

usecover

the OCL

keyword

, followed by the classifier

Furthermore,

it's been

revised to

version

2.0 ofcontext

the language.

name. If you use this notation, you should say what the expression represents. In the

following

case, we're new

showing

an invariant,

so we use

the keyword invto

. Other

types

of

This comprehensive

edition

not only provides

a quick-reference

all UML

2.0 diagram

expressions

are

explained

later sections.

types, it also

explains

keyinconcepts

in a way that appeals to readers already familiar with

UML or object-oriented programming concepts.

context Student

Topics

include:

inv:

self.GPA > 2.0



The role and value of UML in projects

Instead of using the keyword self, you can assign a name to a classifier that you can use in

the expression.

Write the name

you and

wantits

torelation

use, followed

a colon (:) and then the

The object-oriented

paradigm

to the by

UML

classifier name. For example, you can name the instance of Student as s:

An integrated approach to UML diagrams

context s : Student

Class and Object, Use Case, Sequence, Collaboration, Statechart, Activity, Component,

inv: s.GPA > 2.0

and Deployment Diagrams



Extension Mechanisms

Finally, you can name an expression by placing a label after the expression type but before

the colon

:). TheConstraint

label is purely

decorative;

The (Object

Language

(OCL) it serves no OCL function.

context s : Student

inv minimumGPARule: s.GPA > 2.0

If you're new to UML, a tutorial with realistic examples has even been included to help you

quickly familiarize yourself with the system.



B.2.2. Constraints on Operations

Beyond basic classifiers, OCL expressions can be associated with operations to capture



www.it-ebooks.info



preconditions and postconditions. Place the signature of the target operation (classifier,

operation name, parameters, etc.) after the context keyword. Instead of the invariant

keyword, use either pre or post for preconditions and postconditions, respectively.

UML 2.0 in a Nutshell



The following expression

ensures

that any student who will be registered for a course has paid

ByDan Pilone,

Neil Pitman

their tuition:

...............................................

Publisher:O'Reilly

Pub Date: June 2005

context Course::registerStudent(s : Student) : boolean

ISBN:0-596-00795-7

pre: s.tuitionPaid = true

Pages:234



When

writing| postconditions,

you can use the keyword result to refer to the value returned

Table of Contents

Index

by an operation. The following expressions verify that a student's tuition was paid before

registering for a course and that the operation registerStudent returned true:



System developers have used modeling languages for decades to specify, visualize,

construct,

andCourse::registerStudent(s

document systems. The Unified

Modeling Language

(UML) is one of those

context

: Student)

: boolean

languages.

UML makes it possible

pre: s.tuitionPaid

= true for team members to collaborate by providing a common

language

applies

to a multitude of different systems. Essentially, it enables you to

post:that

result

= true

communicate solutions in a consistent, tool-supported language.

Today,

UMLwith

hasinvariants,

become the

standard

method

for modeling

systems,

which means

As

you can

you

can name

preconditions

and software

postconditions

by placing

a label

you're

confronting

this rich and expressive language more than ever before. And

after

theprobably

pre or post

keywords:

even though you may not write UML diagrams yourself, you'll still need to interpret

diagrams written by others.

context Course::registerStudent(s : Student) : boolean

pre hasPaidTuition: s.tuitionPaid = true

UML 2.0 in a Nutshell from O'Reilly feels your pain. It's been crafted for professionals like

post studentWasRegistered: result = true

you who must read, create, and understand system artifacts expressed using UML.

Furthermore, it's been fully revised to cover version 2.0 of the language.

Postconditions

can use

theedition

@pre keyword

refer toathe

value of sometoelement

an

This comprehensive

new

not onlyto

provides

quick-reference

all UMLbefore

2.0 diagram

operation

executes.

The

following

expression

ensures

that

a

student

was

registered

and

that

types, it also explains key concepts in a way that appeals to readers already familiar with

the

number

of students in

the course has

increased by 1. This expression uses the self

UML

or object-oriented

programming

concepts.

keyword to reference the object that owns the registerStudent operation.



Topics include:

context Course::registerStudent(s : Student) : boolean

pre: s.tuitionPaid = true

The role

and value

of UML

projects

post:

result

= true

AND in

self.students

= self.students@pre + 1

The object-oriented paradigm and its relation to the UML

You may

the approach

results of to

a query

operation using the keyword body. Because OCL

An specify

integrated

UML diagrams

doesn't have syntax for program flow, you are limited to relatively simple expressions. The

following

expression

indicates

that Sequence,

honors students

are students

with GPAs

higherComponent,

than 3.5.

Class

and Object,

Use Case,

Collaboration,

Statechart,

Activity,

The collection

syntax used

in this example is described in the "Collections" section.

and Deployment

Diagrams



Extension Mechanisms

context Course::getHonorsStudents( ) : Student

body: self.students->select(GPA > 3.5)

The Object Constraint Language (OCL)



B.2.3.

on Attributes

If you'reConstraints

new to UML, a tutorial

with realistic examples has even been included to help you

quickly familiarize yourself with the system.

OCL expressions can specify the initial and subsequent values for attributes of classifiers.

When using OCL expressions with attributes, you state the context of an expression using the

classifier name, two colons (::), the attribute name, another colon (:), and then the type of

the attribute. You specify the initial value of an attribute using the keyword init:



www.it-ebooks.info



context School::tuition : float

init: 2500.00

UML 2.0 in a Nutshell

ByDan Pilone,Neil Pitman



You can specify ...............................................

the value of attributes after their initial value using the derive keyword. The

following example

increases

the tuition value by 10% every time you query it:

Publisher:

O'Reilly

Pub Date: June 2005

ISBN:0-596-00795-7: float

context: School::tuition

Pages:234



derive: tution * 10%

Table of Contents | Index



System developers have used modeling languages for decades to specify, visualize,

construct, and document systems. The Unified Modeling Language (UML) is one of those

languages. UML makes it possible for team members to collaborate by providing a common

language that applies to a multitude of different systems. Essentially, it enables you to

communicate solutions in a consistent, tool-supported language.

Today, UML has become the standard method for modeling software systems, which means

you're probably confronting this rich and expressive language more than ever before. And

even though you may not write UML diagrams yourself, you'll still need to interpret

diagrams written by others.

UML 2.0 in a Nutshell from O'Reilly feels your pain. It's been crafted for professionals like

you who must read, create, and understand system artifacts expressed using UML.

Furthermore, it's been fully revised to cover version 2.0 of the language.

This comprehensive new edition not only provides a quick-reference to all UML 2.0 diagram

types, it also explains key concepts in a way that appeals to readers already familiar with

UML or object-oriented programming concepts.

Topics include:

The role and value of UML in projects

The object-oriented paradigm and its relation to the UML

An integrated approach to UML diagrams

Class and Object, Use Case, Sequence, Collaboration, Statechart, Activity, Component,

and Deployment Diagrams

Extension Mechanisms

The Object Constraint Language (OCL)



If you're new to UML, a tutorial with realistic examples has even been included to help you

quickly familiarize yourself with the system.



www.it-ebooks.info



UML 2.0 in a Nutshell

ByDan Pilone,Neil Pitman

B.3. Advanced

OCL Modeling

...............................................

Publisher:O'Reilly

Pub Date: June

Like any other language,

OCL2005

has an order of precedence for operators, variable declarations, and

ISBN:

0-596-00795-7

logical constructs

(only

for evaluating your expressions, not for program flow). The following

Pages:

234

sections describe

constructs

that you can use in any OCL expression.



Table of Contents | Index



B.3.1. Conditionals

OCL

supports

basic boolean

expression

the if-then-else-endif

keywords. The

System

developers

have used

modelingevaluation

languagesusing

for decades

to specify, visualize,

conditions

are

used

only

to

determine

which

expression

is

evaluated;

they

can't

be

construct, and document systems. The Unified Modeling Language (UML) is one of used

thoseto

influence

theUML

underlying

system

or for

to affect

The following

invariant aenforces

languages.

makes it

possible

team program

membersflow.

to collaborate

by providing

commonthat a

student's

graduation

is valid only

if she has

paid herEssentially,

tuition:

languageyear

that of

applies

to a multitude

of different

systems.

it enables you to



communicate solutions in a consistent, tool-supported language.

context Student inv:

Today,

has become

the standard

method for modeling software systems, which means

if UML

tuitionPaid

= true

then

you're yearOfGraduation

probably confronting= this

rich and expressive language more than ever before. And

2005

evenelse

though you may not write UML diagrams yourself, you'll still need to interpret

diagrams

written by others.

yearOfGraduation

= 0000

endif

UML 2.0 in a Nutshell from O'Reilly feels your pain. It's been crafted for professionals like

you who must read, create, and understand system artifacts expressed using UML.

Furthermore,

it'sare

been

fully revised

cover

version

2.0 of the language.

OCL's

logic rules

slightly

differenttofrom

typical

programming

language logic rules. The boolean

evaluation rules are:

This comprehensive new edition not only provides a quick-reference to all UML 2.0 diagram

types, it also explains key concepts in a way that appeals to readers already familiar with

UML or object-oriented programming concepts.

1. TrueOR-ed with anything is true.

Topics include:

2. FalseAND-ed with anything is false.

3. False

IMPLIES

TRue

The role

andanything

value of is

UML

in. projects

Theimplies

keyword evaluate

the first

half

an expression,

and, if that first half is true, the result

The object-oriented

paradigm

and

itsof

relation

to the UML

is taken from the second half. For example, the following expression enforces that if a student's

integrated

diagrams

GPA isAn

less

than 1.0,approach

their yeartoofUML

graduation

is set to 0. If the GPA is higher than 1.0, Rule #3

applies, and the entire expression is evaluated as true (meaning the invariant is valid).

Class and Object, Use Case, Sequence, Collaboration, Statechart, Activity, Component,

and Deployment Diagrams

context Student inv:

self.GPA

1.0 IMPLIES self.yearOfGraduation = 0000

Extension


The Object Constraint Language (OCL)

OCL's boolean expressions are valid regardless of the order of the arguments . Specifically, if the

first argument of an AND operator is undefined, but the second operator is false, the entire

expression is false. Likewise, even if one of the arguments to an OR operator is undefined, if the

If you're

new

to UML,

a tutorial

with.If-then-else-endif

realistic examples has

even been

to similarly;

help you if the

other

is true

, the

expression

is true

constructs

areincluded

evaluated

quickly

familiarize

yourself

with

the

system.

chosen branch can be evaluated to TRue or false, the nonchosen branch is completely disregarded

(even if it would be undefined).



B.3.2. Variable Declaration



www.it-ebooks.info



OCL supports several complex constructs you can use to make your constraints more expressive

and easier to write. You can break complex expressions into reusable pieces (within the same

UML 2.0 in a Nutshell

expression) by using the let and in keywords to declare a variable. You declare a variable by

Byfollowed

Dan Pilone,Neil

Pitman

giving it a name,

by a

colon (:), its type, an expression for its value, and the in keyword.

...............................................

The following example declares an expression that ensures a teacher of a high-level course has an

Publisher:O'Reilly

appropriate salary:

Pub Date: June 2005

ISBN:0-596-00795-7

context Course

inv:

Pages:234



let salary : float = self.instructor.salary in

if self.courseLevel > 4000 then

salary

> 80000.00

Table of Contents

| Index

else

salary < 80000.00

endif

System developers have used modeling languages for decades to specify, visualize,

construct, and document systems. The Unified Modeling Language (UML) is one of those

languages. UML makes it possible for team members to collaborate by providing a common

You

can define

variables

can be used

in multiple

expressions

on a it

classifier-by-classifier

basis

language

that applies

to that

a multitude

of different

systems.

Essentially,

enables you to

using

the def keyword.

instead

of declaring

salary as a variable using the let

communicate

solutions For

in aexample,

consistent,

tool-supported

language.

keyword, you can define it using the def keyword for the Course context. Once you define a

variable

usinghas

thebecome

def keyword,

you may

use that

any expression

that

is inmeans

the same

Today, UML

the standard

method

for variable

modelinginsoftware

systems,

which

context.

The syntax

for the def

the same as

that formore

the let

keyword:

you're probably

confronting

thiskeyword

rich andisexpressive

language

than

ever before. And

even though you may not write UML diagrams yourself, you'll still need to interpret

diagrams

written

by others.

context

Course

def: salary : float = self.instructor.salary

UML 2.0 in a Nutshell from O'Reilly feels your pain. It's been crafted for professionals like

you who must read, create, and understand system artifacts expressed using UML.

Furthermore, it's been fully revised to cover version 2.0 of the language.

So, now you can write the previous invariant as:



This comprehensive new edition not only provides a quick-reference to all UML 2.0 diagram

context

Course inv:

types,

it also explains

key concepts in a way that appeals to readers already familiar with

> 4000 then

UMLif

or self.courseLevel

object-oriented programming

concepts.

salary > 80000.00

getHonorsStudentselse

Topics

include:

salary < 80000.00

endif

The role and value of UML in projects

The object-oriented paradigm and its relation to the UML



B.3.3. Operator Precedence



An integrated approach to UML diagrams



Class and Object, Use Case, Sequence, Collaboration, Statechart, Activity, Component,

OCL operators have the following order of precedence (from highest to lowest):

and Deployment Diagrams

Extension Mechanisms

@pre

The(.Object

Constraint

Language (OCL)

dot

) and arrow

(->) operations

not and unary minus (-)



If you're

* andnew

/ to UML, a tutorial with realistic examples has even been included to help you

quickly familiarize yourself with the system.

+ and if-then-else-endif

<,>,<=, and >=



www.it-ebooks.info



= and <>

and,or, and xor

UML 2.0 in a Nutshell



implies



ByDan Pilone,Neil Pitman

...............................................



You can use parentheses

to group expressions, which will be evaluated from the innermost set of

Publisher:O'Reilly

parentheses to the

outermost.

Pub Date:

June 2005

ISBN:0-596-00795-7

Pages:234



B.3.4. Built-in Object Properties

Table of Contents | Index



OCL provides a set of properties on all objects in a system. You can invoke these properties in your

expressions as you do any other properties. The built-in properties are:



System developers have used modeling languages for decades to specify, visualize,

construct, and document systems. The Unified Modeling Language (UML) is one of those

languages. UML

possible for team members to collaborate by providing a common

oclIsTypeOf

(t :makes

Type ):itBoolean

language that applies to a multitude of different systems. Essentially, it enables you to

communicate

solutions

a consistent,

language.

Returnstrue

if the in

tested

object is tool-supported

exactly the same

type as t.

Today, UML has become the standard method for modeling software systems, which means

oclIsKindOf(

t : confronting

Type ): Boolean

you're probably

this rich and expressive language more than ever before. And

even though you may not write UML diagrams yourself, you'll still need to interpret

Returns

TRue by

if the

tested object is the same type or a subtype of t.

diagrams

written

others.

UML 2.0 in a Nutshell from O'Reilly feels your pain. It's been crafted for professionals like

oclInState(s

Statecreate,

): Boolean

you who must :read,

and understand system artifacts expressed using UML.

Furthermore, it's been fully revised to cover version 2.0 of the language.

ReturnsTRue if the tested object is in state s. The states you can test must be part of a state

machine attached

the classifier

being

tested.

This comprehensive

newtoedition

not only

provides

a quick-reference to all UML 2.0 diagram

types, it also explains key concepts in a way that appeals to readers already familiar with

UML or object-oriented programming concepts.

oclIsNew( ) : Boolean

TopicsDesigned

include: to be used in a postcondition for an operation, it returns true if the object being

tested was created as a result of executing the operation.

The role and value of UML in projects

oclAsType (t : Type ): Type

The object-oriented paradigm and its relation to the UML

Returns the owning object casted to Type . If the object isn't a descendant of t, the operation

An integrated approach to UML diagrams

is undefined.



Class and Object, Use Case, Sequence, Collaboration, Statechart, Activity, Component,

Here are

examples

of the built-in properties:

andsome

Deployment

Diagrams

Extension

Mechanisms

-test that

the instructor is an instance of Teacher

context Course

The Object

Constraint Language (OCL)

inv:

self.instructor.oclIsTypeOf(Teacher)

-- cast a Date class to a java.sql.Date to verify the minutes

-- (it's very unlikely the foundationDate would be a java.sql.Date

If you're

newthis

to UML,

a tutorialwould

with realistic

examplesbut

hasthis

even is

been

-- so

invariant

be undefined,

anincluded

exampleto help you

quickly

familiarize

yourself

with

the

system.

-- of using oclAsType( ))

context School

inv: self.foundationDate.oclAsType(java.sql.Date).getMinutes( ) = 0



www.it-ebooks.info



B.3.5. Collections

UML 2.0 in a Nutshell



OCL defines several types of collections that represent several instances of a classifier. The basic

ByDan Pilone,Neil Pitman

type is Collection, which is the base class for the other OCL collection classes. Quite a few

...............................................

operations are defined for Collection; see the OCL specification for the complete list.



Publisher:O'Reilly

Pub Date: June 2005

All collections support a way to select or reject elements using the operations select( ) and

ISBN:0-596-00795-7

reject( ), respectively.

To invoke an operation on a collection, you use an arrow (->) rather than a

Pages:234



dot (.) (a dot accesses a property). The result of select or reject is another collection containing

the appropriate elements. Remember that since OCL can't modify a system in any way, the original

collection

is unchanged. The notation for a select is:

Table of Contents | Index

collection->select(boolean expression)

System developers have used modeling languages for decades to specify, visualize,

construct, and document systems. The Unified Modeling Language (UML) is one of those

languages.

makes

it possible

for team

to collaborate

by providing a common

So,

to selectUML

students

with

GPAs higher

thanmembers

3.0, you can

use the expression:

language that applies to a multitude of different systems. Essentially, it enables you to

communicate solutions in a consistent, tool-supported language.

context Course::getHonorsStudents( ) : Student

body: self.students->select(GPA > 3.0)

Today, UML has become the standard method for modeling software systems, which means

you're probably confronting this rich and expressive language more than ever before. And

even though you may not write UML diagrams yourself, you'll still need to interpret

Or,

to eliminate

honor

students that haven't paid their tuition:

diagrams

written

by others.



UML context

2.0 in a Nutshell

from O'Reilly feels your pain.

been crafted for professionals like

Course::getHonorsStudents(

) : It's

Student

you body:

who must

read, create, and understand

system artifacts expressed using

UML.

self.students->select(GPA

> 3.0)->reject(tuitionPaid

= false)

Furthermore, it's been fully revised to cover version 2.0 of the language.

This

new edition

not only

provides

a and

quick-reference

to all UML

diagram

In

thecomprehensive

previous examples,

the context

for the

select

reject statements

was2.0

implied.

You can

types, it name

also explains

key concepts

wayin

that

readers already

familiar

with

explicitly

the element

you wantintoa use

theappeals

booleantoexpression

by prefixing

the

expression

UMLaorlabel

object-oriented

programming

with

and a pipe symbol

(|). So,concepts.

a rewrite of the GPA example using a label to identify each

student looks like this:

Topics include:

context Course::getHonorsStudents( ) : Student

body:

self.students->select(curStudent

| curStudent.GPA > 3.0)

The role

and value of UML in projects



The object-oriented paradigm and its relation to the UML

Finally, you can specify the type of the element you want to evaluate. You indicate the type by

approach

to UMLtype

diagrams

placingAna integrated

colon (:) and

the classifier

after the label. Each element of the collection you are

evaluating must be of the specified type, or else the expression is undefined. You can rewrite the

Class and

Case,

Sequence,

Collaboration,

Statechart,

Activity,s:Component,

GPA example

to Object,

be evenUse

more

specific

and require

that it evaluate

only Student

and Deployment Diagrams

context

Course::getHonorsStudents(

) : Student

Extension

Mechanisms

body: self.students->select(curStudent : Student | curStudent.GPA > 3.0)

The Object Constraint Language (OCL)

Often you will need to express a constraint across an entire collection of objects, so OCL provides

theforAll operation that returns true if a given Boolean expression evaluates to true for all of the

If you're new to UML, a tutorial with realistic examples has even been included to help you

elements in a collection. The syntax for forAll is the same as that for select and reject. So, you

quickly familiarize yourself with the system.

can write a constraint that enforces that all students in a Course have paid their tuition:

context Course

inv: self.students->forAll(tuitionPaid = true)



www.it-ebooks.info



As you can with select, you can name and type the variable used in the expression:

UML 2.0 in a Nutshell

context Course

inv: self.students->forAll(curStudent

: Student | curStudent.tuitionPaid = true)

ByDan Pilone,Neil Pitman

...............................................

Publisher:O'Reilly

Pub Date:

June

If you need to check

to see

if 2005

there is at least one element in a collection that satisfies a boolean

0-596-00795-7

expression, youISBN:

can use

the operation exists. The syntax is the same as that for select. The

Pages:ensures

234

following expression

that at least one of the students has paid their tuition:



context Course

inv: self.students->exists(tuitionPaid = true)



Table of Contents | Index



System developers have used modeling languages for decades to specify, visualize,

Like

select, and

you document

can name systems.

and type The

the variables

used in Language

the expression:

construct,

Unified Modeling

(UML) is one of those

languages. UML makes it possible for team members to collaborate by providing a common

language

thatCourse

applies to a multitude of different systems. Essentially, it enables you to

context

communicate

solutions in a consistent, tool-supported

language.

inv: self.students->exists(curStudent

: Student

| curStudent.tuitionPaid = true)

Today, UML has become the standard method for modeling software systems, which means

you're

this rich

expressive

more

than or

ever

before.. And

You

canprobably

check to confronting

see if a collection

is and

empty

using thelanguage

operations

isEmpty

notEmpty

The

even

though

you

may

not

write

UML

diagrams

yourself,

you'll

still

need

to

interpret

following expression ensures that the school has at least one course offering:

diagrams written by others.

School from O'Reilly feels your pain. It's been crafted for professionals like

UMLcontext

2.0 in a Nutshell

self.Course->notEmpty(

)

you inv:

who must

read, create, and understand

system artifacts expressed using UML.

Furthermore, it's been fully revised to cover version 2.0 of the language.



This comprehensive new edition not only provides a quick-reference to all UML 2.0 diagram

types, it also explains key concepts in a way that appeals to readers already familiar with

UML or object-oriented programming concepts.

Topics include:

The role and value of UML in projects

The object-oriented paradigm and its relation to the UML

An integrated approach to UML diagrams

Class and Object, Use Case, Sequence, Collaboration, Statechart, Activity, Component,

and Deployment Diagrams

Extension Mechanisms

The Object Constraint Language (OCL)



If you're new to UML, a tutorial with realistic examples has even been included to help you

quickly familiarize yourself with the system.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Appendix B.&nbsp; The Object Constraint Language

Tải bản đầy đủ ngay(0 tr)

×