Formale Entwicklung objektorientierter Software Prof. P.H. Schmitt, - - PowerPoint PPT Presentation

formale entwicklung objektorientierter software
SMART_READER_LITE
LIVE PREVIEW

Formale Entwicklung objektorientierter Software Prof. P.H. Schmitt, - - PowerPoint PPT Presentation

JML Formale Entwicklung objektorientierter Software Prof. P.H. Schmitt, C. Engel, F. Werner Fakult at f ur Informatik Universit at Karlsruhe (TH) Winter 2006/2007 Prof. P.H. Schmitt, C. Engel, F. Werner Formale Entwicklung


slide-1
SLIDE 1

JML

Formale Entwicklung objektorientierter Software

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Fakult¨ at f¨ ur Informatik Universit¨ at Karlsruhe (TH)

Winter 2006/2007

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 1 / 40

slide-2
SLIDE 2

JML

The Java Modeling Language JML

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 2 / 40

slide-3
SLIDE 3

JML

JML By Example

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 3 / 40

slide-4
SLIDE 4

JML

A JML specification for enterPIN

General Structure

public class ATM { private BankCard i n s er t ed C a r d = n u l l ; private boolean customerAuthenticated = f a l s e ; public void enterPIN ( int pin ) { // here the implementation f o l l o w s

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 4 / 40

slide-5
SLIDE 5

JML

A JML specification for enterPIN

General Structure

public class ATM { private BankCard i n s er t ed C a r d = n u l l ; private boolean customerAuthenticated = f a l s e ; /*@ public normal behavior // case 1 @*/ /*@ also public normal behavior // case 2 @*/ /*@ also public normal behavior // case 3 @*/ public void enterPIN ( int pin ) { // here the implementation f o l l o w s

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 4 / 40

slide-6
SLIDE 6

JML

A JML specification for enterPIN

General Structure

public class ATM { private/*@ spec public @*/ BankCard i n s er t ed C a r d = n u l l ; private/*@ spec public @*/ boolean customerAuthenticated = f a l s e ; /*@ public normal behavior // case 1 @*/ /*@ also public normal behavior // case 2 @*/ /*@ also public normal behavior // case 3 @*/ public void enterPIN ( int pin ) { // here the implementation f o l l o w s

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 4 / 40

slide-7
SLIDE 7

JML

A JML specification for enterPIN

Visibility Modifiers

public class ATM { private /*@ spec_public @*/ BankCard insertedCard = null; private /*@ spec_public @*/ boolean customerAuthenticated = false; /*@ public normal_behavior

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 5 / 40

slide-8
SLIDE 8

JML

A JML specification for enterPIN

Visibility Modifiers

public class ATM { private /*@ spec_public @*/ BankCard insertedCard = null; private /*@ spec_public @*/ boolean customerAuthenticated = false; /*@ public normal_behavior Modifiers to specification cases have no influence on their semantics.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 5 / 40

slide-9
SLIDE 9

JML

A JML specification for enterPIN

Visibility Modifiers

public class ATM { private /*@ spec_public @*/ BankCard insertedCard = null; private /*@ spec_public @*/ boolean customerAuthenticated = false; /*@ public normal_behavior Modifiers to specification cases have no influence on their semantics. Specification items marked public cannot refer to private fields.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 5 / 40

slide-10
SLIDE 10

JML

A JML specification for enterPIN

Visibility Modifiers

public class ATM { private /*@ spec_public @*/ BankCard insertedCard = null; private /*@ spec_public @*/ boolean customerAuthenticated = false; /*@ public normal_behavior Modifiers to specification cases have no influence on their semantics. Specification items marked public cannot refer to private fields. Private fields can be declared public for specification purposes only.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 5 / 40

slide-11
SLIDE 11

JML

The First Specification Case

/*@ public normal_behavior requires insertedCard != null; requires !customerAuthenticated; requires pin == insertedCard.correctPIN; assignable customerAuthenticated; ensures customerAuthenticated; @*/ public void enterPIN (int pin) { // here the implementation follows

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 6 / 40

slide-12
SLIDE 12

JML

The First Specification Case

/*@ public normal_behavior requires insertedCard != null; requires !customerAuthenticated; requires pin == insertedCard.correctPIN; assignable customerAuthenticated; ensures customerAuthenticated; @*/ public void enterPIN (int pin) { // here the implementation follows The normal behavior keyword specifies that the method may not throw an exception

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 6 / 40

slide-13
SLIDE 13

JML

The First Specification Case

/*@ public normal_behavior requires insertedCard != null; requires !customerAuthenticated; requires pin == insertedCard.correctPIN; assignable customerAuthenticated; ensures customerAuthenticated; @*/ public void enterPIN (int pin) { // here the implementation follows The conjunction of all requires clauses is the precondition.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 6 / 40

slide-14
SLIDE 14

JML

The First Specification Case

/*@ public normal_behavior requires insertedCard != null; requires !customerAuthenticated; requires pin == insertedCard.correctPIN; assignable customerAuthenticated; ensures customerAuthenticated; @*/ public void enterPIN (int pin) { // here the implementation follows The conjunction of all ensures clauses is the postcondition.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 6 / 40

slide-15
SLIDE 15

JML

The First Specification Case

/*@ public normal_behavior requires insertedCard != null; requires !customerAuthenticated; requires pin == insertedCard.correctPIN; assignable customerAuthenticated; ensures customerAuthenticated; @*/ public void enterPIN (int pin) { // here the implementation follows The assignable clause list all expressions that may be assigned to.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 6 / 40

slide-16
SLIDE 16

JML

The Second Specification Case

public normal_behavior requires insertedCard != null; requires !customerAuthenticated; requires pin != insertedCard.correctPIN; requires wrongPINCounter < 2; assignable wrongPINCounter; ensures wrongPINCounter == \old(wrongPINCounter) + 1; ensures !customerAuthenticated;

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 7 / 40

slide-17
SLIDE 17

JML

The Second Specification Case

public normal_behavior requires insertedCard != null; requires !customerAuthenticated; requires pin != insertedCard.correctPIN; requires wrongPINCounter < 2; assignable wrongPINCounter; ensures wrongPINCounter == \old(wrongPINCounter) + 1; ensures !customerAuthenticated; Special JML keywords start with \

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 7 / 40

slide-18
SLIDE 18

JML

The Second Specification Case

public normal_behavior requires insertedCard != null; requires !customerAuthenticated; requires pin != insertedCard.correctPIN; requires wrongPINCounter < 2; assignable wrongPINCounter; ensures wrongPINCounter == \old(wrongPINCounter) + 1; ensures !customerAuthenticated; \old(wrongPINCounter) refers to the value of the field wrongPINCounter before method invocation.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 7 / 40

slide-19
SLIDE 19

JML

The Third Specification Case

public normal_behavior requires insertedCard != null; requires !customerAuthenticated; requires pin != insertedCard.correctPIN; requires wrongPINCounter >= 2; assignable insertedCard, wrongPINCounter, insertedCard.invalid; ensures insertedCard == null; ensures \old(insertedCard).invalid; ensures !customerAuthenticated;

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 8 / 40

slide-20
SLIDE 20

JML

Exceptional Behaviour

/*@ @ (* the contracts as defined above *) @ also public exceptional_behavior @ requires insertedCard==null; @ signals_only ATMException; @ signals (ATMException) !customerAuthenticated; @*/ public void enterPIN (int pin) { // here the implementation follows

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 9 / 40

slide-21
SLIDE 21

JML

Exceptional Behaviour

/*@ @ (* the contracts as defined above *) @ also public exceptional_behavior @ requires insertedCard==null; @ signals_only ATMException; @ signals (ATMException) !customerAuthenticated; @*/ public void enterPIN (int pin) { // here the implementation follows If an exception

  • f

type ATMException is thrown then !customerAuthenticated has to be true.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 9 / 40

slide-22
SLIDE 22

JML

Exceptional Behaviour

/*@ @ (* the contracts as defined above *) @ also public exceptional_behavior @ requires insertedCard==null; @ signals_only ATMException; @ signals (ATMException) !customerAuthenticated; @*/ public void enterPIN (int pin) { // here the implementation follows signals only says that only exceptions of type ATMException may be thrown.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 9 / 40

slide-23
SLIDE 23

JML

Pure Methods

Pure methods terminate and have no side effects.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 10 / 40

slide-24
SLIDE 24

JML

Pure Methods

Pure methods terminate and have no side effects. After declaring public /*@ pure @*/ boolean cardIsInserted() { return insertedCard!=null; }

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 10 / 40

slide-25
SLIDE 25

JML

Pure Methods

Pure methods terminate and have no side effects. After declaring public /*@ pure @*/ boolean cardIsInserted() { return insertedCard!=null; } cardIsInserted() could replace insertedCard != null in the above JML annotations.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 10 / 40

slide-26
SLIDE 26

JML

A Static Invariant

public class BankCard { /∗@ p u b l i c s t a t i c i n v a r i a n t @ (\ f o r a l l BankCard p1 , p2 ; @ p1!=p2 ; @ p1 . cardNumber !=p2 . cardNumber ) @∗/ private /∗@ s p e c p u b l i c @∗/ int cardNumber ; // r e s t

  • f

c l a s s f o l l o w s }

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 11 / 40

slide-27
SLIDE 27

JML

A Static Invariant

public class BankCard { /∗@ p u b l i c s t a t i c i n v a r i a n t @ (\ f o r a l l BankCard p1 , p2 ; @ p1!=p2 ; @ p1 . cardNumber !=p2 . cardNumber ) @∗/ private /∗@ s p e c p u b l i c @∗/ int cardNumber ; // r e s t

  • f

c l a s s f o l l o w s } Compare to OCL version:

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 11 / 40

slide-28
SLIDE 28

JML

A Static Invariant

public class BankCard { /∗@ p u b l i c s t a t i c i n v a r i a n t @ (\ f o r a l l BankCard p1 , p2 ; @ p1!=p2 ; @ p1 . cardNumber !=p2 . cardNumber ) @∗/ private /∗@ s p e c p u b l i c @∗/ int cardNumber ; // r e s t

  • f

c l a s s f o l l o w s } Compare to OCL version: context BankCard inv: BankCard::allInstances() -> forall(p1,p2| p1<>p2 implies p1.cardNumber<>p2.cardNumber)

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 11 / 40

slide-29
SLIDE 29

JML

An Instance Invariant

public class BankCard { /∗@ p u b l i c i n s t a n ce i n v a r i a n t @ (\ f o r a l l BankCard p ; t h i s !=p == > t h i s . cardNumber !=p . cardNumber ) @∗/ private /∗@ s p e c p u b l i c @∗/ int cardNumber ; // r e s t

  • f

c l a s s f o l l o w s }

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 12 / 40

slide-30
SLIDE 30

JML

An Instance Invariant

public class BankCard { /∗@ p u b l i c i n s t a n ce i n v a r i a n t @ (\ f o r a l l BankCard p ; t h i s !=p == > t h i s . cardNumber !=p . cardNumber ) @∗/ private /∗@ s p e c p u b l i c @∗/ int cardNumber ; // r e s t

  • f

c l a s s f o l l o w s } Instance invariants must evaluate to true for all created objects of their class.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 12 / 40

slide-31
SLIDE 31

JML

Variation on the Static Invariant

public class BankCard { /∗@ p u b l i c s t a t i c i n v a r i a n t @ (\ f o r a l l BankCard p1 , p2 ; @ p1!=p2 ; @ p1 . cardNumber !=p2 . cardNumber ) @∗/ private /∗@ s p e c p u b l i c @∗/ int cardNumber ; // r e s t

  • f

c l a s s f o l l o w s }

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 13 / 40

slide-32
SLIDE 32

JML

Variation on the Static Invariant

public class BankCard { /∗@ p u b l i c s t a t i c i n v a r i a n t @ (\ f o r a l l BankCard p1 , p2 ; @ p1!=p2 ; @ p1 . cardNumber !=p2 . cardNumber ) @∗/ private /∗@ s p e c p u b l i c @∗/ int cardNumber ; // r e s t

  • f

c l a s s f o l l o w s } public class BankCard { /∗@ p u b l i c i n s t a n ce i n v a r i a n t @ (\ f o r a l l BankCard p ; t h i s !=p == > t h i s . cardNumber !=p . cardNumber ) @∗/ private /∗@ s p e c p u b l i c @∗/ int cardNumber ; // r e s t

  • f

c l a s s f o l l o w s }

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 13 / 40

slide-33
SLIDE 33

JML

Another Example

OCL constraint: context CentralHost inv: validCardsCount = BankCard::allInstances() -> select(not invalid) -> size()

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 14 / 40

slide-34
SLIDE 34

JML

Another Example

OCL constraint: context CentralHost inv: validCardsCount = BankCard::allInstances() -> select(not invalid) -> size() JML annotation: public class CentralHost { /*@ public instance invariant this.validCardsCount @ ==(\num_of BankCard p; !p.invalid) @*/}

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 14 / 40

slide-35
SLIDE 35

JML

JML Expressions

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 15 / 40

slide-36
SLIDE 36

JML

Definition

Every Java expression according to the language specification which does not include

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 16 / 40

slide-37
SLIDE 37

JML

Definition

Every Java expression according to the language specification which does not include

1 operators with side-effect like e++, e--, ++e, or --e,

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 16 / 40

slide-38
SLIDE 38

JML

Definition

Every Java expression according to the language specification which does not include

1 operators with side-effect like e++, e--, ++e, or --e, 2 non-pure method invocation expressions,

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 16 / 40

slide-39
SLIDE 39

JML

Definition

Every Java expression according to the language specification which does not include

1 operators with side-effect like e++, e--, ++e, or --e, 2 non-pure method invocation expressions, 3 assignment operators

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 16 / 40

slide-40
SLIDE 40

JML

Definition

Every Java expression according to the language specification which does not include

1 operators with side-effect like e++, e--, ++e, or --e, 2 non-pure method invocation expressions, 3 assignment operators

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 16 / 40

slide-41
SLIDE 41

JML

Definition

Every Java expression according to the language specification which does not include

1 operators with side-effect like e++, e--, ++e, or --e, 2 non-pure method invocation expressions, 3 assignment operators

is a JML expression.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 16 / 40

slide-42
SLIDE 42

JML

Definition

Every Java expression according to the language specification which does not include

1 operators with side-effect like e++, e--, ++e, or --e, 2 non-pure method invocation expressions, 3 assignment operators

is a JML expression. Any such expression e has a natural representation in KeY’s first-order logic, which we will denote by [e].

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 16 / 40

slide-43
SLIDE 43

JML

Mapping from JML plus Java to FOL

Selected Items

JML Expression first-order logic formula !e 0 ![e0] e 0 && e 1 [e0] & [e1] e 0 || e 1 [e0] | [e1] e 0 ? e 1 : e 2 if[e0] then[e1] else[e2] e 0 != e 1 !([e0] = [e1]) e 0 >= e 1 [e0] > = [e1] e 0 ==> e 1 [e0] − > [e1] e 0 <==> e 1 [e0] < − > [e1] (\forall T e; e 0; e 1) \forall T e(![e] = null & [e0] − > [e1]) (\exists T e; e 0; e 1) \exists T e(![e] = null & [e0] & [e1])

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 17 / 40

slide-44
SLIDE 44

JML

Quantification in JML

Note that quantifiers bind two expressions, the range predicate and the body expression.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 18 / 40

slide-45
SLIDE 45

JML

Quantification in JML

Note that quantifiers bind two expressions, the range predicate and the body expression. A missing range predicate is by default true.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 18 / 40

slide-46
SLIDE 46

JML

Quantification in JML

Note that quantifiers bind two expressions, the range predicate and the body expression. A missing range predicate is by default true. JML excludes null from the range of quantification.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 18 / 40

slide-47
SLIDE 47

JML

Generalised and Numerical Quantifiers

\num_of C c; e number of elements of class C with property e

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 19 / 40

slide-48
SLIDE 48

JML

Generalised and Numerical Quantifiers

\num_of C c; e number of elements of class C with property e \sum

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 19 / 40

slide-49
SLIDE 49

JML

Generalised and Numerical Quantifiers

\num_of C c; e number of elements of class C with property e \sum \product

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 19 / 40

slide-50
SLIDE 50

JML

Generalised and Numerical Quantifiers

\num_of C c; e number of elements of class C with property e \sum \product \min

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 19 / 40

slide-51
SLIDE 51

JML

Generalised and Numerical Quantifiers

\num_of C c; e number of elements of class C with property e \sum \product \min \max

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 19 / 40

slide-52
SLIDE 52

JML

Generalised and Numerical Quantifiers

\num_of C c; e number of elements of class C with property e \sum \product \min \max

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 19 / 40

slide-53
SLIDE 53

JML

JML Operation Contracts

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 20 / 40

slide-54
SLIDE 54

JML

Clauses in Operation Contracts

Clause Lightweight default Heavyweight default requires \not_specified true assignable \not_specified \everything ensures \not_specified true diverges false false signals \not_specified (Exception)true signals_only All exception types declared in the Ja- va method declaration

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 21 / 40

slide-55
SLIDE 55

JML

Signals Clauses

JML ensures E ; s i g n a l s (ET1 ) S1 ; . . . s i g n a l s (ETn ) Sn ; s i g n a l s o n l y OT1 ,. . . ,OTm ;

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 22 / 40

slide-56
SLIDE 56

JML

Signals Clauses

JML ensures E ; s i g n a l s (ET1 ) S1 ; . . . s i g n a l s (ETn ) Sn ; s i g n a l s o n l y OT1 ,. . . ,OTm ; FOL Translation ( e = null − > [E]) & ( [ET1]::instance(e) = TRUE − > [S1]) . . . & ( [ETn]::instance(e) = TRUE − > [Sn]) & ( [OT1]::instance(e) = TRUE | . . . | [OTm]::instance(e) = TRUE)

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 22 / 40

slide-57
SLIDE 57

JML

Signals Clauses

JML ensures E ; s i g n a l s (ET1 ) S1 ; . . . s i g n a l s (ETn ) Sn ; s i g n a l s o n l y OT1 ,. . . ,OTm ; FOL Translation ( e = null − > [E]) & ( [ET1]::instance(e) = TRUE − > [S1]) . . . & ( [ETn]::instance(e) = TRUE − > [Sn]) & ( [OT1]::instance(e) = TRUE | . . . | [OTm]::instance(e) = TRUE) The variable e stores a thrown exception. If the operation terminates normally then e equals null.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 22 / 40

slide-58
SLIDE 58

JML

The diverges Clause

diverges e with a boolean JML expression e specifies that the methode may not terminate only when e is true in the pre-state.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 23 / 40

slide-59
SLIDE 59

JML

The diverges Clause

diverges e with a boolean JML expression e specifies that the methode may not terminate only when e is true in the pre-state. If diverges false is part of the operation contract for m then m must always terminate.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 23 / 40

slide-60
SLIDE 60

JML

The diverges Clause

diverges e with a boolean JML expression e specifies that the methode may not terminate only when e is true in the pre-state. If diverges false is part of the operation contract for m then m must always terminate. If diverges true is part of the operation contract for m then m may terminate or not.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 23 / 40

slide-61
SLIDE 61

JML

The diverges Clause

diverges e with a boolean JML expression e specifies that the methode may not terminate only when e is true in the pre-state. If diverges false is part of the operation contract for m then m must always terminate. If diverges true is part of the operation contract for m then m may terminate or not. If diverges n == 0 is part of the operation contract for m then m must terminate, when called in a state with n != 0.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 23 / 40

slide-62
SLIDE 62

JML

De-Sugaring

normal behavior r e q u i r e s R ; a s s i g n a b l e A ; ensures E ; d i v e r g e s D ; = ⇒ behavior r e q u i r e s R ; a s s i g n a b l e A ; ensures E ; d i v e r g e s D ; s i g n a l s ( Exception ) f a l s e ;

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 24 / 40

slide-63
SLIDE 63

JML

De-Sugaring

normal behavior r e q u i r e s R ; a s s i g n a b l e A ; ensures E ; d i v e r g e s D ; = ⇒ behavior r e q u i r e s R ; a s s i g n a b l e A ; ensures E ; d i v e r g e s D ; s i g n a l s ( Exception ) f a l s e ; e x c e p t i o n a l b e h a v i o r r e q u i r e s R ; a s s i g n a b l e A ; d i v e r g e s D ; s i g n a l s (ET ) S ; s i g n a l s o n l y (OT ) ; = ⇒ behavior r e q u i r e s R ; a s s i g n a b l e A ; ensures f a l s e ; d i v e r g e s D ; s i g n a l s (ET ) S ; s i g n a l s o n l y (OT ) ;

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 24 / 40

slide-64
SLIDE 64

JML

Inheritance of Specifications in JML

An invariant to a class is inherited by all its subclasses.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 25 / 40

slide-65
SLIDE 65

JML

Inheritance of Specifications in JML

An invariant to a class is inherited by all its subclasses. An operation contract is inherited by all overridden methods.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 25 / 40

slide-66
SLIDE 66

JML

JML Invariants

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 26 / 40

slide-67
SLIDE 67

JML

An Instance Invariant

JML p u b l i c c l a s s BankCard { /∗@ p u b l i c i n s t a n ce i n v a r i a n t @ (\ f o r a l l BankCard p ; t h i s !=p == > t h i s . cardNumber !=p . cardNumber ) @∗/ p r i v a t e /∗@ s p e c p u b l i c @∗/ i n t cardNumber ; // r e s t

  • f

c l a s s f o l l o w s }

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 27 / 40

slide-68
SLIDE 68

JML

An Instance Invariant

JML p u b l i c c l a s s BankCard { /∗@ p u b l i c i n s t a n ce i n v a r i a n t @ (\ f o r a l l BankCard p ; t h i s !=p == > t h i s . cardNumber !=p . cardNumber ) @∗/ p r i v a t e /∗@ s p e c p u b l i c @∗/ i n t cardNumber ; // r e s t

  • f

c l a s s f o l l o w s } FOL \forall BankCard o; o.<created> = TRUE -> \forall BankCard p; p.<created> = TRUE -> !o = p -> !o.cardNumber = p.cardNumber

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 27 / 40

slide-69
SLIDE 69

JML

Visible State Semantics

According to the JML reference manual instance invariants defined in a class C must hold at any visible state for any object o of C.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 28 / 40

slide-70
SLIDE 70

JML

Visible State Semantics

According to the JML reference manual instance invariants defined in a class C must hold at any visible state for any object o of C. A state is visible for an object o if it is reached at one of the following moments during the execution of a program; we leave out finalizers and JML’s helper methods for simplicity: at the end of a constructor invocation which is initialising o,

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 28 / 40

slide-71
SLIDE 71

JML

Visible State Semantics

According to the JML reference manual instance invariants defined in a class C must hold at any visible state for any object o of C. A state is visible for an object o if it is reached at one of the following moments during the execution of a program; we leave out finalizers and JML’s helper methods for simplicity: at the end of a constructor invocation which is initialising o, at the beginning and end of a non-static method invocation with o as receiver,

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 28 / 40

slide-72
SLIDE 72

JML

Visible State Semantics

According to the JML reference manual instance invariants defined in a class C must hold at any visible state for any object o of C. A state is visible for an object o if it is reached at one of the following moments during the execution of a program; we leave out finalizers and JML’s helper methods for simplicity: at the end of a constructor invocation which is initialising o, at the beginning and end of a non-static method invocation with o as receiver, at the beginning and end of a static method which is declared in the class of o or a superclass.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 28 / 40

slide-73
SLIDE 73

JML

Visible State Semantics

According to the JML reference manual instance invariants defined in a class C must hold at any visible state for any object o of C. A state is visible for an object o if it is reached at one of the following moments during the execution of a program; we leave out finalizers and JML’s helper methods for simplicity: at the end of a constructor invocation which is initialising o, at the beginning and end of a non-static method invocation with o as receiver, at the beginning and end of a static method which is declared in the class of o or a superclass. when no constructor, non-static method invocation with o as receiver,

  • r static method invocation for a method in o’s class or a superclass

is in progress.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 28 / 40

slide-74
SLIDE 74

JML

Observed State Semantics

A program P is observed-state correct w.r.t. a specification S, if

1 all operations op fulfil all operation contracts of S for op,

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 29 / 40

slide-75
SLIDE 75

JML

Observed State Semantics

A program P is observed-state correct w.r.t. a specification S, if

1 all operations op fulfil all operation contracts of S for op, 2 all invariants InvS of S are preserved by all operations of P, and

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 29 / 40

slide-76
SLIDE 76

JML

Observed State Semantics

A program P is observed-state correct w.r.t. a specification S, if

1 all operations op fulfil all operation contracts of S for op, 2 all invariants InvS of S are preserved by all operations of P, and 3 all invariants are valid in the initial state of P.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 29 / 40

slide-77
SLIDE 77

JML

Example Program

public class A { private int i = 1; /∗@ i n s t a n ce i n v a r i a n t i >0 ∗/ public int g e t I () { return i ; } /∗@ r e q u i r e s p>0; @ ensures i==p ; @∗/ public void s e t I ( int p ) { i=p ; } public void m1() { s e t I ( 0 ) ; i =1; } public void m2() { i =0; s e t I ( 1 ) ; } public int m3() { i =0; i =(new B ( ) ) . m5( t h i s ) ; } /∗@ ensures \ r e s u l t >0 @∗/ public int m4() { return 42/ i ;} } public class B { /∗@ ensures \ r e s u l t >0 @∗/ public int m5(A a ){ i f ( a . g e t I ()<=0) a . s e t I ( 1 ) ; return a .m4( ) ; } }

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 30 / 40

slide-78
SLIDE 78

JML

Visible vs Observable States

public class A { private int i = 1; /∗@ i n s t a n ce i n v a r i a n t i >0 ∗/ /∗@ r e q u i r e s p>0; @ ensures i==p ; @∗/ public void s e t I ( int p ) { i=p ; } public void m1() { s e t I ( 0 ) ; visible, but not observable state i =1; } }

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 31 / 40

slide-79
SLIDE 79

JML

Visible vs Observable States

public class A { private int i = 1; /∗@ i n s t a n ce i n v a r i a n t i >0 ∗/ /∗@ r e q u i r e s p>0; @ ensures i==p ; @∗/ public void s e t I ( int p ) { i=p ; } public void m1() { s e t I ( 0 ) ; visible, but not observable state i =1; } } Invariant i > 0 not satisfied in visible state semantics.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 31 / 40

slide-80
SLIDE 80

JML

Visible vs Observable States

public class A { private int i = 1; /∗@ i n s t a n ce i n v a r i a n t i >0 ∗/ /∗@ r e q u i r e s p>0; @ ensures i==p ; @∗/ public void s e t I ( int p ) { i=p ; } public void m1() { s e t I ( 0 ) ; visible, but not observable state i =1; } } Invariant i > 0 not satisfied in visible state semantics. Invariant i > 0 satisfied in observable state semantics.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 31 / 40

slide-81
SLIDE 81

JML

A Static Invariant

public class CentralHost { /∗@ p u b l i c s t a t i c i n v a r i a n t maxAccountNumber>=0 @∗/ // . . . must hold already after the static initialisation of CentralHost is completed.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 32 / 40

slide-82
SLIDE 82

JML

JML Model Fields and Methods

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 33 / 40

slide-83
SLIDE 83

JML

Java Interfaces

public interface IBonusCard { public void addBonus ( int newBonusPoints ) ; }

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 34 / 40

slide-84
SLIDE 84

JML

Java Interfaces

public interface IBonusCard { public void addBonus ( int newBonusPoints ) ; } How to add contracts to abstract methods in interfaces?

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 34 / 40

slide-85
SLIDE 85

JML

Java Interfaces

public interface IBonusCard { public void addBonus ( int newBonusPoints ) ; } How to add contracts to abstract methods in interfaces? Remember: There are no attributes in interfaces.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 34 / 40

slide-86
SLIDE 86

JML

Java Interfaces

public interface IBonusCard { public void addBonus ( int newBonusPoints ) ; } How to add contracts to abstract methods in interfaces? Remember: There are no attributes in interfaces. More precisely: Only static final fields.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 34 / 40

slide-87
SLIDE 87

JML

Java Interfaces

Model Fields

public interface IBonusCard { /*@ public instance model int bonusPoints; @*/ public void addBonus ( int newBonusPoints ) ; } How to add contracts to abstract methods in interfaces? Remember: There are no attributes in interfaces. More precisely: Only static final fields.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 34 / 40

slide-88
SLIDE 88

JML

Java Interfaces

Model Fields

public interface IBonusCard { /*@ public instance model int bonusPoints; @*/ /*@ ensures bonusPoints == \old(bonusPoints)+newBonusPoints; public void addBonus ( int newBonusPoints ) ; } How to add contracts to abstract methods in interfaces? Remember: There are no attributes in interfaces. More precisely: Only static final fields.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 34 / 40

slide-89
SLIDE 89

JML

Java Interfaces

Model Fields

public interface IBonusCard { /*@ public instance model int bonusPoints; @*/ /*@ ensures bonusPoints == \old(bonusPoints)+newBonusPoints; @ assignable bonusPoints; @ */ public void addBonus ( int newBonusPoints ) ; } How to add contracts to abstract methods in interfaces? Remember: There are no attributes in interfaces. More precisely: Only static final fields.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 34 / 40

slide-90
SLIDE 90

JML

Implementing Interfaces

Interface p u b l i c i n t e r f a c e IBonusCard { /∗@ p u b l i c i n s t a n ce model i n t bonusPoints ; @∗/ p u b l i c void addBonus ( i n t newBonusPoints ) ;

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 35 / 40

slide-91
SLIDE 91

JML

Implementing Interfaces

Interface p u b l i c i n t e r f a c e IBonusCard { /∗@ p u b l i c i n s t a n ce model i n t bonusPoints ; @∗/ p u b l i c void addBonus ( i n t newBonusPoints ) ; Implementation public class BankCard implements IBonusCard{ public int bankCardPoints ; /∗@ p u b l i c i n s t a n ce model i n t bonusPoints ; @∗/ public void addBonus ( int newBonusPoints ) { bankCardPoints+=newBonusPoints ; }}

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 35 / 40

slide-92
SLIDE 92

JML

Implementing Interfaces

Interface p u b l i c i n t e r f a c e IBonusCard { /∗@ p u b l i c i n s t a n ce model i n t bonusPoints ; @∗/ p u b l i c void addBonus ( i n t newBonusPoints ) ; Implementation public class BankCard implements IBonusCard{ public int bankCardPoints ; /∗@ p u b l i c i n s t a n ce model i n t bonusPoints ; @∗/ /*@ private represents bonusPoints <-bankCardPoints; @*/ public void addBonus ( int newBonusPoints ) { bankCardPoints+=newBonusPoints ; }}

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 35 / 40

slide-93
SLIDE 93

JML

Other Representations

/∗@ p r i v a t e r e p r e s e n t s bonusPoints <− bankCardPoints ; @∗/

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 36 / 40

slide-94
SLIDE 94

JML

Other Representations

/∗@ p r i v a t e r e p r e s e n t s bonusPoints <− bankCardPoints ; @∗/ /∗@ p r i v a t e r e p r e s e n t s bonusPoints <− bankCardPoints ∗ 100; @∗/

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 36 / 40

slide-95
SLIDE 95

JML

Other Representations

/∗@ p r i v a t e r e p r e s e n t s bonusPoints <− bankCardPoints ; @∗/ /∗@ p r i v a t e r e p r e s e n t s bonusPoints <− bankCardPoints ∗ 100; @∗/ /∗@ r e p r e s e n t s x \ such that A( x ) ; @∗/

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 36 / 40

slide-96
SLIDE 96

JML

Problems with Specifications Using Integers

/∗@ r e q u i r e s y >= 0; @ ensures @ \ r e s u l t ∗ \ r e s u l t <= y && @ y < ( abs (\ r e s u l t )+1) ∗ ( abs (\ r e s u l t )+1); @ ∗/ p u b l i c s t a t i c i n t i s q r t ( i n t y )

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 37 / 40

slide-97
SLIDE 97

JML

Problems with Specifications Using Integers

/∗@ r e q u i r e s y >= 0; @ ensures @ \ r e s u l t ∗ \ r e s u l t <= y && @ y < ( abs (\ r e s u l t )+1) ∗ ( abs (\ r e s u l t )+1); @ ∗/ p u b l i c s t a t i c i n t i s q r t ( i n t y ) For y = 1 and \result = 1073741821 = 1

2(max int − 5) the above

postcondition is true, though we do not want 1073741821 to be a square root of 1.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 37 / 40

slide-98
SLIDE 98

JML

Problems with Specifications Using Integers

/∗@ r e q u i r e s y >= 0; @ ensures @ \ r e s u l t ∗ \ r e s u l t <= y && @ y < ( abs (\ r e s u l t )+1) ∗ ( abs (\ r e s u l t )+1); @ ∗/ p u b l i c s t a t i c i n t i s q r t ( i n t y ) For y = 1 and \result = 1073741821 = 1

2(max int − 5) the above

postcondition is true, though we do not want 1073741821 to be a square root of 1. The problem arises since JML uses the Java semantics of integers which yields 1073741821 ∗ 1073741821 = −2147483639 1073741822 ∗ 1073741822 = 4

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 37 / 40

slide-99
SLIDE 99

JML

Advantages of OCL over JML

1 It lives on a higher level of abstraction. A UML diagram can be

annotated with OCL constraints before code is developed.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 38 / 40

slide-100
SLIDE 100

JML

Advantages of OCL over JML

1 It lives on a higher level of abstraction. A UML diagram can be

annotated with OCL constraints before code is developed.

2 As a consequence of the previous item OCL is not committed to a

particular programming language and better suited for model driven system development.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 38 / 40

slide-101
SLIDE 101

JML

Advantages of OCL over JML

1 It lives on a higher level of abstraction. A UML diagram can be

annotated with OCL constraints before code is developed.

2 As a consequence of the previous item OCL is not committed to a

particular programming language and better suited for model driven system development.

3 OCL is an OMG standard, though one has to admit that the official

standard draft still contains serious inconsistencies and many unfinished items.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 38 / 40

slide-102
SLIDE 102

JML

Advantages of JML over OCL

1 JML is closer to Java code, which encourages its use by

programmers and developers. In fact, today JML specifications are much more widespread than OCL specifications.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 39 / 40

slide-103
SLIDE 103

JML

Advantages of JML over OCL

1 JML is closer to Java code, which encourages its use by

programmers and developers. In fact, today JML specifications are much more widespread than OCL specifications.

2 JML offers a greater variety of concepts on the implementation level,

like exceptional behavior, modifies (assignable) clauses and loop invariants.

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 39 / 40

slide-104
SLIDE 104

JML

THE

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 40 / 40

slide-105
SLIDE 105

JML

THE END

  • Prof. P.H. Schmitt, C. Engel, F. Werner

Formale Entwicklung objektorientierter Software Winter 2006/2007 40 / 40