UML For Java Developers: Model Constraints & The Object Constraint Language Jason Gorman
UML For Java Developers: Model Constraints & The Object Constraint Language Jason Gorman
advertisement
Activity Diagrams
Object Constraint Language
Model the flow of use cases
Model business rules and create
and single and multi-threaded unambiguous specifications
code
www.parlezuml.com/training.htm
children
parents 0..2
parents
valid instance of Bill : Person
parents
*
Person
children
children
children
parents 0..2
parents
not a valid instance of Bill : Person
parents
*
Person
children
children
parents 0..2
ancestors *
Person
* children
* descendants
/family tree
parents 0..2
ancestors *
Person
Q: To what which type this constraint apply? * children
A: Person
* descendants
/family tree
context Person
context Person
inv: ancestors->excludes(self) and descendants->excludes(self)
inv: ancestors->excludes(self) and descendants->excludes(self)
Person
0..1
sex : Sex
optional constraint name spouse
marry(p : Person)
class Person
class Person
{
{
public int sex;
public int sex;
public Person spouse;
public Person spouse; self
this.spouse = p;
this.spouse = p;
p.spouse = this;
p.spouse = this;
}
}
}
}
Account
balance : Real = 0
deposit(amount : Real)
withdraw(a mount : Real) balance before execution of operation
getBalance() : Real
byte id;
short id;
Eg, 6.7.floor() = 6
© Jason Gorman 2005. All rights reserved.
Operations on String Type
In OCL:
account.holder
Evaluates to a customer object who is in the role holder for that association
And:
customer.accounts
Evaluates to a collection of Account objects in the role accounts for that association
b
A B
1
a.b is allowed
class B
class B
{
{
}
}
Account
id : Integer
status : enum{active, frozen, closed}
balance : Real
nextId : Integer
Account
context Account::withdraw(amount : Real)
context Account::withdraw(amount : Real)
balance : Real pre: amount <= balance
pre: amount <= balance
deposit(amount : Real) pre: status = AccountStatusKind.ACT IVE
pre: status = AccountStatusKind.ACT IVE
withdraw(a mount : Real) post: balance = balance@pre - amount
post: balance = balance@pre - amount
1 status
<<enumeration>>
AccountStatusKind
ACTIVE
FROZEN
CLOSED
Collections in {
{
public double balance;
public double balance;
public int id;
public int id;
Java }
}
class Customer
class Customer
{
{
Account[] accounts;
Account[] accounts;
return selected;
return selected;
}
}
}
}
© Jason Gorman 2005. All rights reserved.
The OCL Collection Hierarchy
Collection
Elements can be included only Elements can be included Elements can be included
once, and in no specific order more than once, in no more than once, but in a
specific order specific order
collect(expression) Collects all of the elements given by expression into a new collection
sortedBy(expression) Returns a Sequence of all the elements in the collection in the order specified (expression must
contain the < operator
account1 : Account
accounts
id = 2543
balance = 450 holder
holder accounts
account2 : Account
id = 4288
balance = 250
account1 : Account
accounts
id = 2543 tsb : Bank
balance = 450 holder
account2 : Account
customers
id = 4569
accounts
balance = 100 holder antony : Customer
holder
accounts
account4 : Account
accounts
id = 5613
account3 : Account balance = 50
id = 4288
balance = 250
holder
account
Account id Customer
0..1
1
balance : Real
id : Integer
customer.account[3435]
Or
customer.account[id = 3435]
C x
C
A
y
c
C
c
x
A
y
Person Organisation
accounts holder
Account Customer
* 1
balance : Real
id : Integer
holder accounts
account2 : Account
id = 4288
balance = 250