1 Design by Contract with JML CS 3331 Fall 2009 Gary T. Leavens and Yoonsik Cheon. Design by Contract with JML. Available from

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2013 Lecture 5 Disclaimer. These notes are derived from notes originally.
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 6 Disclaimer. These notes are derived from notes originally.
272: Software Engineering Fall 2012
The Java Modeling Language JML Erik Poll Digital Security Radboud University Nijmegen.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Utilities (Part 3) Implementing static features 1.
Dept. of Computer Science A Runtime Assertion Checker for the Java Modeling Language (JML) Yoonsik Cheon and Gary T. Leavens SERP 2002, June 24-27, 2002.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Software Testing and Quality Assurance
CMT Programming Software Applications
JML and Class Specifications Class invariant JML definitions Queue example Running JML in Eclipse.
Information Hiding and Encapsulation
Software Quality: Testing and Verification II. 2 1.A failure is an unacceptable behaviour exhibited by a system — The frequency of failures measures software.
From Module Breakdown to Interface Specifications Completing the architectural design of Map Schematizer.
1 Documenting with Javadoc CS 3331 Fall 2009 How to Write Doc Comments for the Javadoc TM Tool available from java.sun.com.
Computer Science 340 Software Design & Testing Design By Contract.
Component-Based Software Engineering Components and Interfaces Paul Krause.
Ranga Rodrigo. Class is central to object oriented programming.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1. Reference  2  Algorithm :- Outline the essence of a computational procedure, step by step instructions.  Program :- an.
A Safety-Critical Java Technology Compatibility Kit Hans Søndergaard Stephan Korsholm VIA University College, Horsens, Denmark & Anders P. Ravn Aalborg.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Operation, Algorithm, and Data Structure Specification, and Design Finalization.
Java Language and SW Dev’t
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
111 The Java Modeling Language Based on: Gary T. Leavens, et al., JML Tutorial at OOPSLA Gary. T Leavens, et al., Preliminary Design of JML: A Behavioral.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Design by Contract in Java Concept and Comparison.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
FEN UCN T&B - PBA/CodeContract- Intro 1 Code Contract Introduction Specification of a Person Class.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Today’s Agenda  Quick Review  Continue on JML Formal Methods in Software Engineering1.
A Survey on Java Modeling Languages Gergely Kovásznai,Eszterházy Károly College Wolfgang Schreiner,Johannes Kepler University Gábor Kusper,Eszterházy Károly.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Object Oriented Programming Object and Classes Lecture 3 MBY.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
1 Documenting with Javadoc How to Write Doc Comments for the Javadoc TM Tool available from java.sun.com.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
The need for Programming Languages
Scope, Objects, Strings, Numbers
Accessible Formal Methods A Study of the Java Modeling Language
Design by Contract Fall 2016 Version.
Defining Classes and Methods
Defining Classes and Methods
Java Modeling Language (JML)
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

1 Design by Contract with JML CS 3331 Fall 2009 Gary T. Leavens and Yoonsik Cheon. Design by Contract with JML. Available from Tool support from

2 Outline  Design by contract (DBC)  Java Modeling Language (JML)  DBC with JML  JML tools – JML compiler (jmlc)

3 Contracts in Real World  Contracts specify: Agreements Obligations and rights  Contracts for buying cars Buyers: give money; receive cars Dealers: give cars; receive money

4 Contracts in Software /** Returns a square root approximation of a non-negative number x. x A non-negative number. A square root approximation of x. */ public static double sqrt(double x) { … } Client Implementor Obligations Rights Passes non-negative number Gets square root approximation Computes and returns square root Assumes argument is non-negative

5 Design by Contract (DBC) requires x >= ensures (Math.abs(\result * \result - x) <= public static double sqrt(double x) { … }  Advantages over informal contracts? Unambiguous Machine manipulation

6 Pre and Postconditions  Definition A method’s precondition says what must be true to call it. A method’s normal postcondition says what is true when it returns normally (i.e., without throwing an exception). A method’s exceptional postcondition says what is true when a method throws an exception. signals (IllegalArgumentException e) x < 0;

7 Relational Model of Methods  Can think of a method as a relation: Inputs  Outputs Input Output     0 0 postcondition precondition

8 Contracts as Documentation  For each method say: What it requires (if anything), and What it ensures.  Contracts are: More abstract than code, Not necessarily constructive, Often machine checkable, so can help with debugging, and Machine checkable contracts can always be up-to- date.

9 Abstraction by Specification  A contract can be satisfied in many ways: E.g., for square root: Linear search Binary search Newton’s method …  These will have varying non-functional properties Efficiency Memory usage  So, a contract abstracts from all these implementations, and thus can change implementations later.

10 More Advantages of Contracts  Blame assignment Who is to blame if:  Precondition doesn’t hold?  Postcondition doesn’t hold?  Avoids inefficient defensive checks requires a != null && (* a is sorted *); public static int binarySearch(Thing[] a, Thing x) { … }

11 Modularity of Reasoning  Typical OO code: … source.close(); dest.close(); getFile().setLastModified(loc.modTime().getTime()); …  How to understand this code? Read the code for all methods? Read the contracts for all methods?

12 Rules for Reasoning  Client code Must work for every implementation that satisfies the contract, and Can thus only use the contract (not the code!), i.e.,  Must establish precondition, and  Gets to assume the postcondition assert 9.0 >= 0; double result = sqrt(9.0); assert result * result  9.0; // can assume result == 3.0?  Implementation code Must satisfy contract, i.e.,  Gets to assume precondition  Must establish postcondition But can do anything permitted by it.

13 Contracts and Intent  Code makes a poor contract, because can’t separate: What is intended (contract) What is an implementation decision E.g., if the square root gives an approximation good to 3 decimal places, can that be changed in the next release?  By contrast, contracts: Allow vendors to specify intent, Allow vendors freedom to change details, and Tell clients what they can count on.  Question What kinds of changes might vendors want to make that don’t break existing contracts?

14 Outline Design by contract (DBC)  Java Modeling Language (JML)  DBC with JML  JML tools – JML compiler (jmlc)

15 JML  What is it? Stands for “Java Modeling Language”  A formal behavioral interface specification language for Java Design by contract for Java Uses Java 1.4 or later Available from JET from

16 Annotations  JML specifications are contained in annotations, which are comments like: … or At-signs at the beginning of lines are ignored within annotations.  Question What’s the advantage of using annotations?

17 Outline Design by contract (DBC) Java Modeling Language (JML)  DBC with JML  JML tools – JML compiler (jmlc)

18 Informal Description  An informal description looks like: (* some text describing a property *) It is treated as a boolean value by JML, and Allows  Escape from formality, and  Organize English as contracts. public class IMath { requires (* x is positive ensures \result >= 0 (* \result is an int approximation to square root of x public static int isqrt(int x) { … } }

19 Question What does the reserved word \result denote? What's its type in this particular case? /** * Returns an approximation to the square root of x. */ requires (* x is positive ensures \result >= 0 (* \result is an approximation to the square root of x public static double sqrt(double x) { //... }

20 Exercise  Write informal pre and postconditions. public class Person { private String name; private int weight; ensures \result != null (* \result is a form of this person public String toString() { return “Person(\” + name + “\”, “ + weight + ”)”; } public int getWeight() { return weight; } public void addKgs(int kgs) { weight += kgs; } public Person(String n) { name = n; weight = 0; }

21 Formal Specifications  Formal assertions are written as Java expressions, but: Can’t have side effects  No use of =, ++, --, etc., and  Can only call pure methods. Can use some extensions to Java: Syntax Meaning \result result of method call a ==> b a implies b a <== b b implies a a b a iff b a b !(a b) \old(E) value of E in the pre-state

22 Example public class Person { private spec_public String name; private int weight; public invariant !name.equals(“”) && weight >= 0; ensures \result != public String toString() { … } ensures \result == weight; public int getWeight() { … } >

23 Example (Cont.) requires kgs > ensures weight == \old(kgs + public void addKgs(int kgs) { … } requires ensures n.equals(name) && weight == public String n) { … } }

24 Better addKgs? requires kgs > ensures weight == \old(kgs + requires kgs <= signals_only public void addKgs(int kgs) { if (kgs > 0) { weight += kgs; } else { throw new IllegalArgumentException(“positive kgs required!”); } }

25 Meaning of Postconditions ensures … kgs > 0 … normal (return) signals … kgs <= 0; exceptional (throw)

26 More Examples ensures \result == (x % 2 == 0); public static boolean mystery0(int x) { /* … */ } ensures \result == (x > y ? x : y); public static int mystery1(int x, int y) { /* … */ } ensures (x > y ==> \result == x) && (x \result == y); public static int mystery2(int x, int y) { /* … */ } ensures \result == Math.max(x, y); public static int mystery3(int x, int y) { /* … */ }

27 Invariants  Definition An invariant is a property that is always true of an object’s state (when control is not inside the object’s methods).  Invariants allow you to define: Acceptable states of an object, and Consistency of an object’s state. public invariant !name.equals(“”) && weight >= 0; private spec_public String name;

28 Exercise  Formally specify the following method (in Person) public void changeName(String newName) { name = newName; } Hint: Watch out for the invariant!

29 Quantifiers  JML supports several forms of quantifiers Universal and existential (\forall and \exists) General quantifiers (\sum, \product, \min, \max) Numeric quantifier (\num_of) (\forall Student s; juniors.contains(s) ==> s.getAdvisor() != null) (\forall Student s; juniors.contains(s); s.getAdvisor() != null)

30 Examples requires a != null && a.length > ensures (\exists int i; 0 <= i && i < a.length; \result == a[i]) (\forall int i; 0 = public static int mystery1(int[] a) { /* … */ } requires a != ensures \result == (\sum int i; 0 <= i && i < a.length; public static int mystery2(int a[]) { /* … */ } requires a != ensures \result == (\num_of int i; 0 public static int mystery3(int a[], int x) { /* … */ }

31 Exercise  Formally specify the missing part, i.e., the fact that a is sorted in ascending order. old boolean hasx = (\exists int i; i >= 0 && i < a.length; a[i] == requires ensures (hasx ==> a[\result] == x) && (!hasx ==> \result == requires_redundantly (* a is sorted in ascending order public static int int[] a, int x) { … } Hint: Use a nested quantification, e.g., (\forall int i, j; …)

32 Model Declarations  What if you want to change a spec_public field’s name? private spec_public String name; to private String fullName;  For specification: need to keep the old name public but don’t want two strings.  So, use a model field: public model non_null String path; and a represents clause private represents path <- fullName;

33 Model Variables  Specification-only variables Like domain-level constructs Values are given only by represents clauses: name abstract (model) fullName concrete (real) represented by

34 Question  What changes would you make to change the representation of a person’s weight from kilograms to pounds?

35 Outline Design by contract (DBC) Java Modeling Language (JML) DBC with JML  JML tools – JML compiler (jmlc)

36 Tools for JML  JML compiler (jmlc)  JML/Java interpreter (jmlrac)  JML/JUnit unit test tool (jmlunit)  HTML generator (jmldoc)

37 JML Compiler (jmlc)  Basic usage $ jmlc Person.java produces Person.class $ jmlc –Q *.java produces *.class, quietly $ jmlc –d../bin Person.java produces../bin/Person.class

38 Running Code Compiled with jmlc  Must have JML’s runtime classes (jmlruntime.jar) in Java’s boot class path  Automatic if you use script jmlrac, e.g., $ jmlrac PersonMain

39 A Main Program public class PersonMain { public static void main(String[] args) { System.out.println(new Person(“Yoonsik”)); System.out.println(new Person(null)); }

40 Example (Formatted) $ jmlc –Q Person.java $ javac PersonMain.java $ jmlrac PersonMain Person(“Yoonsik”,0) Exception in thread "main" org.jmlspecs.jmlrac.runtime.JMLEntryPreconditionError : by method Person.Person regarding specifications at File "Person.refines-java", line 52, character 20 when 'n' is null at org.jmlspecs.samples.jmltutorial.Person.checkPre$$init$$Person( Person.refines-java:1060) at org.jmlspecs.samples.jmltutorial.Person. (Person.refines-java:51) at org.jmlspecs.samples.jmltutorial.PersonMain.main(PersonMain.java:27)

41 Summary  DBC is a way of recording: Details of method responsibilities Avoiding constantly checking arguments Assigning blame across interfaces  JML is a DBC tool for Java  For details on JML, refer to its web page at