Your SlideShare is downloading. ×
UML for Java Developers
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

UML for Java Developers

540
views

Published on

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
540
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
102
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. UML for Java Developers Model Constraints & The Object Constraint Language Jason Gorman © Jason Gorman 2005. All rights reserved.
  • 2. quot;I am currently working on a team which is [in] the process of adopting RUP and UML UML for Java Developers (5 Days) standards and practices. After Since Autumn 2003, over 100,000 Java and .NET developers have one of our design sessions, I learned the Unified Modeling Language from Parlez UML needed to lookup some (http://www.parlezum l.com) , making it one of the most popular information and came across UML training resources on the Internet. your site. Absolutely great! Most [of] the information I've had questions about is UML for Java Developers is designed to accelerate the learning contained within your tutorials process by explaining UML in a language Java developers can and then some.quot; understand – Java! From Requirements to a Working System quot;Really great site... I have been Many UML courses focus on analysis and high-level design, falling trying to grasp UML since the short of explaining how you get from there to a w orking system. UML day I saw Visual Modeler. I knew a few things but there for Java Developers takes you all the w ay from system were gaps. Thanks to your site requirements to the finished code because that, after all, is w hy we they have shrunk model in the first place. considerably.quot; Learning By Doing UML modeling is a practical skill, like driving a car or flying a plane. quot;I went on a UML training Just as w e don’t learn to drive just by looking at Pow erPoint course three months ago, and came out with a big folder full presentations, you cannot properly learn UML w ithout getting plenty of hand-outs and no real of practice at it. understanding of UML and how to use it on my project. I spent Your skills w ill be developed by designing and building a w orking a day reading the UML for piece of software, giving you a genuine understanding of how UML .NET tutorials and it was a can be applied throughout the development lifecycle. much easier way to learn. 'Here's the diagram. Now here's the code.' Simple.quot; www.parlezuml.com/training.htm advertisement © Jason Gorman 2005. All rights reserved.
  • 3. UML for Java Developers covers the most useful aspects of the UML standard, applying each notation w ithin the context of an iterative, object oriented development process Use Case Diagrams Object Diagrams & Filmstrips Model the users of the system Model snapshots of the running and the goals they can achieve system and show how actions by using it change object state Class Diagrams Implementation Diagrams Model types of objects and Model the physical components of a the relationships between system and their deployment them. architecture Sequence Diagrams Packages & Model how objects interact Model Management to achieve functional goals Organise your logical and physical models with packages Activity Diagrams Object Constraint Language Model the flow of use cases Model business rules and create and single and multi-threaded unambiguous specifications code Statechart Diagrams User Experience Modeling Model the behaviour of objects Design user-centred systems with and event-driven applications UML Design Principles Design Patterns Create well-designed software Apply proven solutions to common that’s easier to change and OO design problems reuse www.parlezuml.com/training.htm © Jason Gorman 2005. All rights reserved.
  • 4. UML Diagrams Don’t Tell Us Everything children parents 0..2 parents valid instance of Bill : Person parents * Person children children © Jason Gorman 2005. All rights reserved.
  • 5. Constraints Make Models More Precise children parents 0..2 parents not a valid instance of Bill : Person parents * Person children children {cannot be own descendant or ancestor } © Jason Gorman 2005. All rights reserved.
  • 6. What is the Object Constraint Language? • A language for expressing necessary extra information about a model • A precise and unambiguous language that can be read and understood by developers and customers • A language that is purely declarative – ie, it has no side-effects (in other words it describes what rather than how) © Jason Gorman 2005. All rights reserved.
  • 7. What is an OCL Constraint? • An OCL constraint is an OCL expression that evaluates to true or false (a Boolean OCL expression, in other words) © Jason Gorman 2005. All rights reserved.
  • 8. OCL Makes Constraints Unambiguous {ancestors = parents->union(parents.ancestors->asSet())} {descendants = children->union(children.descendants->asSet()) parents 0..2 ancestors * Person * children * descendants /family tree {ancestors->excludes(self) and descendants->excludes(self) } © Jason Gorman 2005. All rights reserved.
  • 9. Introducing OCL – Constraints & Contexts {ancestors = parents->union(parents.ancestors->asSet())} {descendants = children->union(children.descendants->asSet()) 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) Q: When does this constraint apply? {ancestors->excludes(self) and descendants->excludes(self) } A: inv = invariant = always © Jason Gorman 2005. All rights reserved.
  • 10. Operations, Pre & Post- conditions <<enumeration>> Sex MALE = 1 FEMALE = 2 Person 0..1 sex : Sex optional constraint name spouse marry(p : Person) applies to the marry() operation of the type Person context Person::marry(p : Person) context Person::marry(p : Person) pre cannot_marry_self: not (p = self) pre cannot_marry_self: not (p = self) pre not_same_sex: not (p.sex = self.sex) comments start with -- pre not_same_sex: not (p.sex = self.sex) -- neither person can be married already -- neither person can be married already pre not_already_married: self.spouse->size() = 0 and p.spouse->size() = 0 pre not_already_married: self.spouse->size() = 0 and p.spouse->size() = 0 post : self.spouse = p and p.spouse = self post : self.spouse = p and p.spouse = self © Jason Gorman 2005. All rights reserved.
  • 11. Design By Contract :assert class Sex class Sex { { static final int MALE = 1; static final int MALE = 1; static final int FEMALE = 2; static final int FEMALE = 2; } } class Person class Person { { public int sex; public int sex; public Person spouse; public Person spouse; self public void marry(Person p) public void marry(Person p) { { assert p != this; assert p != this; context Person::marry(p : Person) context Person::marry(p : Person) assert p.sex != this.sex; pre cannot_marry_self: not (p = self) assert p.sex != this.sex; pre cannot_marry_self: not (p = self) pre not_same_sex: not (p.sex = self.sex) assert this.spouse = null && p.spouse = null; pre not_same_sex: not (p.sex = self.sex) assert this.spouse = null && p.spouse = null; -- neither person can be married a lready -- neither person can be married a lready pre not_already_married: self.spouse->size() = 0 and p.spouse->size() = 0 pre not_already_married: self.spouse->size() = 0 and p.spouse->size() = 0 post : self.spouse = p and p.spouse = self this.spouse = p; post : self.spouse = p and p.spouse = self this.spouse = p; p.spouse = this; p.spouse = this; } self.spouse->size = 0 } } } © Jason Gorman 2005. All rights reserved.
  • 12. Defensive Programming : Throwing Exceptions class Person class Person { { public int sex; public int sex; public Person spouse; public Person spouse; public void marry(Person p) throws ArgumentException { public void marry(Person p) throws ArgumentException { if(p == this) { if(p == this) { throw new ArgumentException(quot;cannot marry selfquot;); throw new ArgumentException(quot;cannot marry selfquot;); } } if(p.sex == this.sex) { if(p.sex == this.sex) { throw new ArgumentException(quot;spouse is sa me sexquot;); throw new ArgumentException(quot;spouse is sa me sexquot;); } } if((p.spouse != null || this.spouse != null) { if((p.spouse != null || this.spouse != null) { throw new ArgumentException(quot;already marriedquot;); throw new ArgumentException(quot;already marriedquot;); } } this.spouse = p; this.spouse = p; p.spouse = this; p.spouse = this; } } } } © Jason Gorman 2005. All rights reserved.
  • 13. Referring to previous values and operation return values Account balance : Real = 0 deposit(amount : Real) withdraw(a mount : Real) balance before execution of operation getBalance() : Real context Account::withdraw (amount : Real) context Account::withdraw (amount : Real) pre: amount <= balance pre: amount <= balance post: balance = balance@pre - amount post: balance = balance@pre - amount context Account::getBalance() : Real context Account::getBalance() : Real post : result = balance post : result = balance return value of operation © Jason Gorman 2005. All rights reserved.
  • 14. @pre and result in Java class Account class Account { { private float balance = 0; context Account::withdraw(amount : Real) private float balance = 0; context Account::withdraw(amount : Real) pre: amount <= balance pre: amount <= balance post: balance = balance@pre - amount public void withdraw(float amount) { post: balance = balance@pre - amount public void withdraw(float amount) { assert amount <= balance; context Account::getBalance() : Real assert amount <= balance; context Account::getBalance() : Real post : result = balance post : result = balance balance = balance - a mount; balance = balance - a mount; } } public void testWithdrawWithSufficientFunds() { Account account = new Account(); public void deposit(float a mount) { public void deposit(float a mount) { balance = balance + a mount; balance = balance + a mount; account.deposit(500); } } float balanceAtPre = account.getBalance(); public float getBalance() { public float getBalance() { return balance; return balance; float amount = 250; } result = balance } } } account.withdraw(amount); assertTrue(account.getBalance() == balanceAtPre – a mount); } balance = balance@pre - amount © Jason Gorman 2005. All rights reserved.
  • 15. OCL Basic Value Types Account • Integer : A whole number of any size balance : Real = 0 name : String • Real : A decimal number of any size id : Integer isActive : Boolean • String : A string of characters deposit(amount : Real) withdraw(a mount : Real) • Boolean : True/False id : Integer balance : Real = 0 name : String isActive : Boolean int id; double balance = 0; string na me; boolean isActive; long id; float balance = 0; char[] na me; byte id; short id; © Jason Gorman 2005. All rights reserved.
  • 16. Operations on Real and Integer 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 multiply a*b Integer or Real divide a /b Real modulus a.mod(b) Integer integer division a.div(b) Integer absolute value a.abs Integer or Real maximum a.max(b) Integer or Real minimum a.min(b) Integer or Real round a.round Integer floor a.floor Integer Eg, 6.7.floor() = 6 © Jason Gorman 2005. All rights reserved.
  • 17. Operations on String Type Operation Expression Result type concatenation s.concat(string) String size s.size Integer to lower case s.toLower String to upper case s.toUpper String substring s.substring(int, int) String equals s1 = s2 Boolean not equals s1 <> s2 Boolean Eg, ‘jason’.concat(‘ gorman’) = ‘jason gorman’ Eg, ‘jason’.substring(1, 2) = ‘ja’ © Jason Gorman 2005. All rights reserved.
  • 18. Operations on 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 implication a implies b Boolean if then else if a then b1 else b2 endif type of b Eg, true or false = true Eg, true and false = false © Jason Gorman 2005. All rights reserved.
  • 19. Navigating in OCL Expressions accounts holder Account Customer * 1 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 Account account = new Account(); Account account = new Account(); Customer customer = new Customer(); Customer customer = new Customer(); customer.accounts = new Account[] {account}; customer.accounts = new Account[] {account}; account.holder = customer; account.holder = customer; © Jason Gorman 2005. All rights reserved.
  • 20. Navigability in OCL Expressions b A B 1 a.b is allowed b.a is not allowed – it is not navigable class A class A { { public B b; public B b; } } class B class B { { } } © Jason Gorman 2005. All rights reserved.
  • 21. Calling class features Account id : Integer status : enum{active, frozen, closed} balance : Real nextId : Integer deposit(amount : Real) context Account::createNew() : Account context Account::createNew() : Account withdraw(a mount : Real) post: result.oclIsNew() and fetch(id : Integer) : Account post: result.oclIsNew() and result.id = Account.nextId@pre and result.id = Account.nextId@pre and Account.nextId = result.id + 1 Account.nextId = result.id + 1 © Jason Gorman 2005. All rights reserved.
  • 22. Enumerations in OCL 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 © Jason Gorman 2005. All rights reserved.
  • 23. Collections in OCL accounts holder Account Customer * 1 balance : Real id : Integer customer.accounts.balance = 0 is not allowed customer.accounts->select(id = 2324).balance = 0 is allowed © Jason Gorman 2005. All rights reserved.
  • 24. class Account class Account Collections in { { public double balance; public double balance; public int id; public int id; Java } } class Customer class Customer { { Account[] accounts; Account[] accounts; public Account SelectAccount(int id) public Account SelectAccount(int id) { { Account selected = null; Account selected = null; for(int i = 0; i < accounts.length; i++) for(int i = 0; i < accounts.length; i++) { { Account account = accounts[i]; Account account = accounts[i]; if(account.id = id) if(account.id = id) { { selected = account; selected = account; break; break; } } } } return selected; return selected; } } } } © Jason Gorman 2005. All rights reserved.
  • 25. The OCL Collection Hierarchy Collection Set Bag Sequence 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 © Jason Gorman 2005. All rights reserved.
  • 26. Operations on All Collections Operation Description size The number of elements in the collection count(object) The number of occurences of object in the collection. includes(object) True if the object is an element of the collection. includesAll(collection) True if all elements of the parameter collection are present in the current collection. isEmpty True if the collection contains no elements. notEmpty True if the collection contains one or more elements. iterate(expression) Expression is evaluated for every element in the collection. sum(collection) The addition of all elements in the collection. exists(expression) True if expression is true for at least one element in the collection. forAll(expression) True if expression is true for all elements. select(expression) Returns the subset of elements that satisfy the expression reject(expression) Returns the subset of elements that do not satisfy the expression collect(expression) Collects all of the elements given by expression into a new collection one(expression) Returns true if exactly one element satisfies the expression sortedBy(expression) Returns a Sequence of all the elements in the collection in the order specified (expression must contain the < operator © Jason Gorman 2005. All rights reserved.
  • 27. Examples of Collection Operations account1 : Account accounts id = 2543 balance = 450 holder jason : Customer holder holder accounts account2 : Account id = 4569 account4 : Account accounts balance = 100 id = 5613 accounts balance = 50 account3 : Account id = 4288 balance = 250 bool forAll = true; bool forAll = true; jason.accounts->forAll(a : Account | a.balance > 0) = true foreach(Account a in accounts) foreach(Account a in accounts) jason.accounts->select(balance > 100) = {account1, account3} { { if(!(a.balance > 0)) jason.accounts->includes(account4) = true if(!(a.balance > 0)) { { jason.accounts->exists(a : account | a.id = 333) = false forAll = forAll && (a.balance > 0); forAll = forAll && (a.balance > 0); jason.accounts->includesAll({account1, account2}) = true } } } jason.accounts.balance->sum() = 850 } Jason.accounts->collect(balance) = {450, 100, 250, 50} © Jason Gorman 2005. All rights reserved.
  • 28. Navigating Across & Flattening Collections account1 : Account accounts id = 2543 tsb : Bank balance = 450 holder jason : Customer customers 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 tsb.customers.accounts = {account1, account2, account3, account} tsb.customers.accounts.balance = {450, 100, 250, 50} © Jason Gorman 2005. All rights reserved.
  • 29. Specialized Collection Operations Collection Set Bag Sequence minus(Set) : Set union(Bag) : bag first() : OclAny union(Set) : Set union(Set) : bag last() : OclAny union(Bag) : Bag intersection(Set) : Set at(Integer) : OclAny symettricDifference(Set) : Set intersection(Bag) : Bag append(OclAny) intersection(Set) : Set including(OclAny) : Bag prepend(OclAny) intersection(Bag) : Set excluding(OclAny) : Bag including(OclAny) : Sequence including(OclAny) : Set asSet() : Set excluding(OclAny) : Sequence excluding(OclAny) : Set asSequence() : Sequence asBag() : Bag asBag() : Bag asSet() : Set asSequence() : Sequence Eg, Set{4, 2, 3, 1}.minus(Set{2, 3}) = Set{4, 1} Eg, Bag{1, 2, 3, 5}.including(6) = Bag{1, 2, 3, 5, 6} Eg, Sequence{1, 2, 3, 4}.append(5) = Sequence{1, 2, 3, 4, 5} © Jason Gorman 2005. All rights reserved.
  • 30. Navigating across Qualified Associations holder account Account id Customer 0..1 1 balance : Real id : Integer customer.account[3435] Or customer.account[id = 3435] © Jason Gorman 2005. All rights reserved.
  • 31. Navigating to Association Classes A B C x C A y context A inv: self.c context B inv: self.c context A inv: self.c[x] context A inv: self.c[y] © Jason Gorman 2005. All rights reserved.
  • 32. Equivalents to Association Classes c c A C B c C c x A y © Jason Gorman 2005. All rights reserved.
  • 33. Built-in OCL Types : OclType Party Person Organisation accounts holder Account Customer * 1 balance : Real id : Integer OclType Eg, Account.name() = “Account” name() : String attributes() : Set(String) Eg, Account.attributes() = Set{“balance”, “id”} associationEnds : Set(String) operations() : Set(String) Eg, Customer.supertypes() = Set{Person} supertypes() : Set(OclType) allSupertypes() ; Set(OclType) Eg, Customer.allSupertypes() = Set{Person, Party} allInstances() : Set(OclAny) © Jason Gorman 2005. All rights reserved.
  • 34. Built-in OCL Types : OclAny account1 : Account accounts id = 2543 balance = 450 holder jason : Customer holder holder accounts account2 : Account id = 4569 account4 : Account accounts balance = 100 id = 5613 accounts balance = 50 account3 : Account id = 4288 balance = 250 OclAny Eg, jason.oclType() = Customer oclIsKindOf(OclType) : Booelan oclIsTypeOf(OclType) : Boolean Eg, jason.oclIsKindOf(Person) = true oclAsType(OclType) : OclAny oclInState(OclState) : Boolean Eg, jason.oclIsTypeOf(Person) = false oclIsNew() : Boolean oclType() : OclType Eg, Account.allInstances() = Set{account1, account2, account3, account4} © Jason Gorman 2005. All rights reserved.
  • 35. More on OCL • OCL 1.5 Language Specification • OCL Evaluator – a tool for editing, syntax checking & evaluating OCL • Octopus OCL 2.0 Plug-in for Eclipse © Jason Gorman 2005. All rights reserved.
  • 36. www.parlezuml.com © Jason Gorman 2005. All rights reserved.