CS 501 Software Engineering Fall 2000 Lecture 10

  • Slides: 32
Download presentation
CS 501: Software Engineering Fall 2000 Lecture 10 Formal Specification

CS 501: Software Engineering Fall 2000 Lecture 10 Formal Specification

Administration Nomadic laptops • Study of student use Next Monday Discussion about the first

Administration Nomadic laptops • Study of student use Next Monday Discussion about the first presentation

Formal Specification Why? • Precise standard to define and validate software Why not? •

Formal Specification Why? • Precise standard to define and validate software Why not? • May be time consuming • Methods not suitable for all applications

Formal Specification Ben Potter, Jane Sinclair, David Till, An Introduction to Formal Specification and

Formal Specification Ben Potter, Jane Sinclair, David Till, An Introduction to Formal Specification and Z (Prentice Hall) 1991 Jonathan Jacky The Way of Z (Cambridge University Press) 1997

Mathematical Specification Example of specification B 1, B 2, . . . Bk is

Mathematical Specification Example of specification B 1, B 2, . . . Bk is a sequence of m x m matrices 1, 2, . . . k is a sequence of m x m elementary matrices B 1 -1 = 1 B 2 -1 = 2 1 Bk-1 = k. . . 2 1 The numerical accuracy must be such that, for all k, Bk. Bk-1 - I <

Specification of Programming Languages <unsigned number> : : = <unsigned integer> | <unsigned real>

Specification of Programming Languages <unsigned number> : : = <unsigned integer> | <unsigned real> <unsigned integer> : : = <digit> {<digit>} <unsigned real> : : = <unsigned integer>. <digit> {<digit>} | <unsigned integer>. <digit> {<digit>} E <scale factor> | <unsigned integer> E <scale factor> : : = <unsigned integer> | <sign> <unsigned integer> <sign> : : = + | Pascal number syntax

Formal Specification Using Diagrams unsigned integer digit unsigned number unsigned integer +. digit unsigned

Formal Specification Using Diagrams unsigned integer digit unsigned number unsigned integer +. digit unsigned integer E -

Two Rules • Formal specification does not guarantee correctness • Formal specification does not

Two Rules • Formal specification does not guarantee correctness • Formal specification does not prescribe the implementation

Example: Z Specification Language Informal: The function intrt(a) returns the largest integer whose square

Example: Z Specification Language Informal: The function intrt(a) returns the largest integer whose square is less than or equal to a. Formal (Z): intrt: N N a : N • intrt(a) * intrt(a) < a < (intrt(a) + 1) * (intrt(a) + 1)

Example: Algorithm 1 + 3 + 5 +. . . (2 n - 1)

Example: Algorithm 1 + 3 + 5 +. . . (2 n - 1) = n 2

Example: Program intrt (int a) /* Calculate integer square root */ { int i,

Example: Program intrt (int a) /* Calculate integer square root */ { int i, term, sum; term = 1; sum = 1; for (i = 0; sum <= a; i++) { term = term + 2; sum = sum + term; } return i; }

Finite State Machine A broadly used method of formal specification: • Event driven systems

Finite State Machine A broadly used method of formal specification: • Event driven systems (e. g. , games) • User interfaces • Protocol specification etc. , . . .

Finite State Machine Example: Therapy control console [informal description]

Finite State Machine Example: Therapy control console [informal description]

State Transition Diagram Select field Enter Patients Enter Fields Start (ok) Setup Beam on

State Transition Diagram Select field Enter Patients Enter Fields Start (ok) Setup Beam on Ready Stop (interlock) Select patient

State Transition Table Select Enter Patient Field Patients Fields Patients Setup Patients Fields Ready

State Transition Table Select Enter Patient Field Patients Fields Patients Setup Patients Fields Ready Patients Fields Beam on ok Start Stop interlock Fields Setup Ready Beam on Setup Ready Setup

Z Specification STATE : : = patients | fields | setup | ready |

Z Specification STATE : : = patients | fields | setup | ready | beam_on EVENT : : = select_patient | select_field | enter | start | stop | ok | interlock FSM == (STATE X EVENT) STATE no_change, transitions, control : FSM Continued on next slide

Z Specification (continued) control = no_change transitions no_change = { s : STATE; e

Z Specification (continued) control = no_change transitions no_change = { s : STATE; e : EVENT • (s, e) transitions = { (patients, enter) (fields, select_patient) s} fields, patients, (fields, enter) setup, (setup, select_patient) patients, (setup, select_field) (setup, ok) ready, fields, (ready, select_patient) patients, (ready, select_field) fields, (ready, start) beam_on, (ready, interlock) setup, (beam_on, stop) ready, (beam_on, interlock) setup }

Schemas Schema: • The basic unit of formal specification. • Describes admissible states and

Schemas Schema: • The basic unit of formal specification. • Describes admissible states and operations of a system.

Lib. Sys: An Example of Z Library system: • Stock of books • Registered

Lib. Sys: An Example of Z Library system: • Stock of books • Registered users. • Each copy of a book has a unique identifier. • Some books on loan; other books on shelves available for loan. • Maximum number of books that any user may have on loan.

Lib. Sys: Operations • Issue a copy of a book to a reader. •

Lib. Sys: Operations • Issue a copy of a book to a reader. • • • Reader return a book. Add a copy to the stock. Remove a copy from the stock. Inquire which books are on loan to a reader. Inquire which readers has a particular copy of a book. Register a new reader. • Cancel a reader's registration.

Lib. Sys Level of Detail: Assume given sets: Copy, Book, Reader Global constant: maxloans

Lib. Sys Level of Detail: Assume given sets: Copy, Book, Reader Global constant: maxloans

Schemas Describing Operations Naming conventions for objects: Before: plain variables, e. g. , r

Schemas Describing Operations Naming conventions for objects: Before: plain variables, e. g. , r After: with appended dash, e. g. , r' Input: with appended ? , e. g. , r? Output: with appended !, e. g. , r!

Operation: Issue a Book • Inputs: copy c? , reader r? • Copy must

Operation: Issue a Book • Inputs: copy c? , reader r? • Copy must be shelved initially: c? shelved • Reader must be registered: r? readers • Reader must have less than maximum number of books on loan: #(issued {r? }) < maxloans • Copy must be recorded as issued to the reader: issued' = issued {c? r? } • The stock and the set of registered readers are unchanged: stock' = stock; readers' = readers

Domain and Range X ran m y dom m x m: X Y Y

Domain and Range X ran m y dom m x m: X Y Y dom m = { x X : y Y x y} ran m = { y Y : x X x y}

Operation: Issue a Book Issue stock, stock' : Copy Book issued, issued' : Copy

Operation: Issue a Book Issue stock, stock' : Copy Book issued, issued' : Copy Reader shelved, shelved': F Copy readers, readers' : F Reader c? : Copy; r? : Reader [See next slide]

Operation: Issue a Book (continued) Issue [See previous slide] shelved dom issued = dom

Operation: Issue a Book (continued) Issue [See previous slide] shelved dom issued = dom stock shelved' dom issued' = dom stock' shelved dom issued = Ø; shelved' dom issued' = Ø ran issued readers; ran issued' readers' r : readers #(issued {r})< maxloans r : readers' #(issued' {r})< maxloans c? shelved; r? readers; #(issued {r? }) < maxloans issued' = issued {c? r? } stock' = stock; readers' = readers

Lib. Sys: Schema for Abstract States Library stock : Copy Book issued : Copy

Lib. Sys: Schema for Abstract States Library stock : Copy Book issued : Copy Reader shelved : F Copy readers: F Reader shelved dom issued = dom stock shelved dom issued = Ø ran issued readers r : readers • #(issued {r}) < maxloans

Schema Inclusion Lib. DB stock : Copy Book readers: F Reader Lib. Loans issued

Schema Inclusion Lib. DB stock : Copy Book readers: F Reader Lib. Loans issued : Copy Reader shelved : F Copy r : Reader • #(issued {r}) < maxloans shelved dom issued = Ø

Schema Inclusion (continued) Library Lib. DB Lib. Loans dom stock = shelved dom issued

Schema Inclusion (continued) Library Lib. DB Lib. Loans dom stock = shelved dom issued ran issued readers

Schema Decoration Issue Library' c? : Copy; r? : Reader c? shelved; r? readers

Schema Decoration Issue Library' c? : Copy; r? : Reader c? shelved; r? readers #(issued {r? }) < maxloans issued' = issued {c? r? } stock' = stock; readers' = readers

Schema Decoration Issue Library c? : Copy; r? : Reader c? shelved; r? readers

Schema Decoration Issue Library c? : Copy; r? : Reader c? shelved; r? readers #(issued {r? }) < maxloans issued' = issued {c? r? } stock' = stock; readers' = readers

The Schema Calculus Schema inclusion Schema decoration Schema disjunction: ^ Add. Known. Title Add.

The Schema Calculus Schema inclusion Schema decoration Schema disjunction: ^ Add. Known. Title Add. New. Title Add. Copy = Schema conjunction: ^ Enter. New. Copy Add. Copy. Admin Add. Copy = Schema negation Schema composition