Вы находитесь на странице: 1из 2

2008 Carmen Avila & Yoonsik Cheon OCL 2.

0 QUICK REFERENCE The University of Texas at El Paso

OCL Constructs
result
context In a postcondition, denotes the result of an operation.
Specifies the context for OCL expressions.
context Account::getBalance(): Integer
context Account post: result = balance

inv @pre
States a condition that must always be met by all instances of a In a postcondition, denotes the value of a property at the start of
context type. an operation.
context Account context Account::deposit(amt: Integer): void
inv: balance >= 0 post: balance = balance@pre + amt

pre Navigation
States a condition that must be true at the moment when an Navigation through attributes, association ends, association
operation starts its execution. classes, and qualified associations.
context Account::deposit(amt: Integer): void
context Account
pre: amt > 0
inv: self.balance >= 0 -- dot notation
-- collection operator (->)
post
inv: owners->size() > 0
States a condition that must be true at the moment when an -- association class, TransInfo
operation ends its execution. inv: transactions.TransInfo->forall(amount > 0)
context Account::deposit(amt: Integer): void -- qualified association, owners
post: balance = balance@pre + amt inv: not owners[primary].isOclUndefined()

init if-then-else expression


Specifies the initial value of an attribute or association role. Conditional expression with a condition and two expressions.
context Account::balance: Integer context Account::interestRate: Real
init: 0 derive: if balance > 5000 then .03 else .02 endif

derive let-in expression


Specifies the value of a derived attribute or association role. Expression with local variables.
context Account::interest: Real context Account::canWithdraw(amt: Integer): boolean
derive: balance * .03 def: let newBalance: Integer = balance amt
in newBalance > minimumBalance
body
Defines the result of a query operation. Messaging (^)
Indicates that communication has taken place.
context Account::getBalance(): Integer
context Account::deposit(s: Sequence(Integer)): void
body: balance pre: s->forAll(amt: Integer | amt > 0)
post: balance = balance@pre + s->sum()
def post: s->forAll(amt: Integer | self^deposit(amt))
Introduces a new attribute or query operation.
context Account OCL Standard Library
def: getBalance(): Integer = balance
Basic Types
package
Specifies explicitly the package in which OCL expressions Type Values Operations
belong. Boolean false, true or, and, xor, not, =, <>, implies
=, <>, <, >, <=, >=, +, -, *, /,
package BankSystem::Accounting Integer -10, 0, 10,
context Account mod(), div(), abs(), max(),
inv: balance >= 0 Real -1.5, 3.14, min(), round(), floor()
endpackage
=, <>, concat(), size(),
String Carmen toLower(), toUpper(),
OCL Expressions substring()
self
Denotes the contextual instance.
OclAny
context Account
Supertype of all UML and OCL types
inv: self.balance >= 0
1
2008 Carmen Avila & Yoonsik Cheon OCL 2.0 QUICK REFERENCE The University of Texas at El Paso

Operation Description Collection operations


= True if self and the argument are the same Operation Set OrderedSet Bag Sequence
<> True if self and the argument are not the same = O O O O
oclIsNew() True if sel was created during the operation <> O O O O
oclIsUndefined() True if self is undefined - O O
oclAsType(type) self as of the given type, type append(o) O O
oclIsTypeOf(type) True if self is an instance of the given type, type asBag() O O O O
oclIsKindOf(type) True if self conforms to the given type, type asOrderedSet() O O O O
oclisInState(state) True if self is in the given state, state asSequence() O O O O
T::allInstance() Set of all instances of the type T asSet() O O O O
at(i)* O O
excluding(o) O O O O
OclVoid first() O O
Type with one single instance (undefined) that conforms to all flatten() O O O O
others types including(o) O O O O
indexOf(o) O O
Operation Description insertAt(i, o) O O
intersection(c) O O
oclIsUndefined() Always true
last() O O
prepend(o) O O
subOrderedSet(l, u) O
OclMessage
subsequence(l, u) O
Messages that can be sent to and received by objects symmetricDifference(c) O
union(c) O O O O
Operation Description
*OCL uses 1-based index for ordered sets and sequences.
hasReturned() Is the operation (self) called and returned?
result() Result of the operation (self) or undefined including(o): new collection as self but with o added
isSignalSent() Is self a sending of a UML signal? excluding(o): new collection as self but with o removed
isOperationCall() Is self a UML operation call?
Iteration operations
Tuple Operation Description
A tuple consists of named parts each of which can have a any(expr) Returns any element for which expr is true
distinct type. collect(expr) Returns a collection that results from evaluating
expr for each element of self
-- Tuple(name: String, age: Integer)
collectNested(expr) Returns a collection of collections that result
Tuple {name: String = John, age: Integer = 20}
from evaluating expr for each element of self
exists(expr) Has at least one element for which expr is true?
Collection Types forAll(expr) Is expr true for all elements?
Four collection types (Set, OrderedSet, Bag, and Sequence) with isUnique(expr) Does expr has unique value for all elements?
iterate(x: S; y: T| expr) Iterates over all elements
Collection as the abstract supertype.
one(expr) Has only one element for which expr is true?
Collection constants reject(expr) Returns a collection containing all elements for
which expr is false
Set {1, 2, 3} -- Set(Integer) select(expr) Returns a collection containing all elements for
OrderedSet {apple, pear, orange} -- OrderedSet(String) which expr is true
Bag {1, 1, 2, 2} -- Bag(Integer) sortedBy(expr) Returns a collection containing all elements
Sequence {1..(4 + 6), 15} Sequence(Integer) ordered by expr

accounts->any(a: Account | a.balance > 1000)


Standard operations accounts->collect(name) -- all the names
Operation Description accounts->collectNested(owners)
accounts->exists(balance > 5000)
count(o) Number of occurrences of o in the collection (self)
execludes(o) Is o not an element of the collection? accounts->forAll(balance >= 0)
excludesAll(c) Are all the elements of c not present in the collection? accounts->isUnique(name)
includes(o) Is o an element of the collection? accounts->iterate(a: Account; sum: Integer = 0 | sum + a.balance)
includesAll(c) Are all the elements of c contained in the collection? accounts->one(name = Carmen)
isEmpty() Does the collection contain no element? accounts->reject(balance > 1000)
notEmpty() Does the collection contain one or more elements? accounts->select(balance <= 1000)
size() Number of elements in the collection accounts->sortedBy(balance)
sum() Addition of all elements in the collection

Вам также может понравиться