1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All...

50
Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specificatio n IS301 – Software Engineering Lectures # 11&12 – 2004-09-24&27 M. E. Kabay, PhD, CISSP Assoc. Prof. Information Assurance Division of Business & Management, Norwich University mailto:[email protected] V: 802.479.7937

Transcript of 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All...

Page 1: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

FormalSpecification

IS301 – Software EngineeringLectures # 11&12 – 2004-09-24&27

M. E. Kabay, PhD, CISSPAssoc. Prof. Information Assurance

Division of Business & Management, Norwich University

mailto:[email protected] V: 802.479.7937

Page 2: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

2 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Objectives

To explain why formal specification techniques help discover problems in system requirements

To describe the use of algebraic techniques for interface specification

To describe the use of model-based techniques for behavioral specification

We will work on this chapter for two sessions:today and Monday. Homework is due a week fromMonday.

Page 3: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

3 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Topics

Formal specification in the software processSub-system interface specificationBehavioral specification

We will use __ of Prof Sommerville’s slides today in our overview of this topic.

More on Friday during the Formal Specification Workshop.

Page 4: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

4 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Formal Methods

Formal specification part of more general collection of techniques that known as ‘formal methods’

These all based on mathematical representation and analysis of software

Formal methods includeFormal specificationSpecification analysis and proofTransformational developmentProgram verification

Page 5: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

5 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Poor Acceptance of Formal Methods

Have not become mainstream software development techniques (as was once predicted)

Other software engineering techniques have been successful at increasing system quality

Market changes time-to-market rather than software with low error count as key factor

Scope of formal methods limited – not well-suited to specifying and analyzing user interfaces and user interaction

Formal methods hard to scale up to large systems

Page 6: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

6 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Use of Formal Methods

Formal methods have limited practical applicability

Principal benefits:Reducing number of errors in systems

Main area of applicability: critical systemsIn this area, use of formal methods most

likely to be cost-effective

Page 7: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

7 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Specification in the Software Process

Specification and design inextricably intermingled

Architectural design essential to structure specification

Formal specificationsExpressed in mathematical notation Precisely defined

vocabularysyntaxsemantics

Page 8: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

8 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Specification and Design

Architecturaldesign

Requirementsspecification

Requirementsdefinition

Softwarespecification

High-leveldesign

Increasing contractor involvement

Decreasing client involvement

Specification

Design

Page 9: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

9 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Specification in the Software Process

Requirementsspecification

Formalspecification

Systemmodelling

Architecturaldesign

Requirementsdefinition

High-leveldesign

Page 10: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

10 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Specification Techniques

Algebraic approachSystem specified in terms of

Its operations and Their relationships

Model-based approachSystem specified in terms of state model

Constructed using mathematical constructs; e.g.,

SetsSequences

Operations defined by modifications to system’s state

Page 11: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

11 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Use of formal specification

Formal specification involves investing more effort in the early phases of software development.

This reduces requirements errors as it forces a detailed analysis of the requirements.

Incompleteness and inconsistencies can be discovered and resolved.

Hence, savings as made as the amount of rework due to requirements problems is reduced.

Page 12: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

12 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Cost profile

The use of formal specification means that the cost profile of a project changesThere are greater up front costs as more

time and effort are spent developing the specification;

However, implementation and validation costs should be reduced as the specification process reduces errors and ambiguities in the requirements.

Page 13: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

13 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Specification

Design andImplementation

Validation

Specification

Design andImplementation

Validation

Cost

Without formalspecification

With formalspecification

Development Costs With Formal Specification

Page 14: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

14 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Specification techniques

Algebraic specificationThe system is specified in terms of its

operations and their relationships.Model-based specification

The system is specified in terms of a state model that is constructed using mathematical constructs such as sets and sequences. Operations are defined by modifications to the system’s state.

Page 15: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

15 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Interface Specification

Large systems decomposed into subsystems Well-defined interfaces between these

subsystemsSpecification of subsystem interfaces allows

independent development of different subsystems

Interfaces may be defined as abstract data types or object classes

Algebraic approach to formal specification particularly well-suited to interface specification

Page 16: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

16 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Sub-System Interfaces

Sub-systemA

Sub-systemB

Interfaceobjects

Page 17: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

17 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

WE STOP HERE FRIDAY

You absolutely have to read the chapter carefully before Monday. The rest

of this material is extremely challenging and

you must be prepared before class.

Page 18: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

18 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Structure of Algebraic Specification

<SPECIFICATION NAME > (Generic Parameter) class* < name >Imports < LIST OF SPECIFICATION NAMES >

Informal description of class and its operations

Operation signatures setting out names and types ofparameters to operations defined over class

Axioms defining operations over class

*In Figure 10.6 and discussion on page 224ff, Sommerville usesthe word “sort” where US specialists would use the word “class.”

Page 19: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

19 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Specification components

IntroductionDefines the sort (the type name) and declares

other specifications that are used.Description

Informally describes the operations on the type.

SignatureDefines the syntax of the operations in the

interface and their parameters.Axioms

Defines the operation semantics by defining axioms which characterize behavior.

Page 20: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

20 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Systematic algebraic specification

Algebraic specifications of a system may be developed in a systematic waySpecification structuring; Specification naming;Operation selection; Informal operation specification;Syntax definition;Axiom definition.

Page 21: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

21 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Specification operations

Constructor operations. Operations which create entities of the type being specified.

Inspection operations. Operations which evaluate entities of the type being specified.

To specify behavior, define the inspector operations for each constructor operation.

Page 22: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

22 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Operations on a list ADT

Constructor operations which evaluate to type ListCreate, Cons (construct) and Tail.

Inspection operations which take type list as a parameter and return some other typeHead and Length.

Page 23: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

23 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

List specification (1)LIST (Elem)

Type List

Imports INTEGER Defines a list where elements are added at the end and

removed from the front. The operations Create, which brings an empty list into

existence, Cons, which creates a new list with an added member, Length, which valuates the size, Head, which valuates the front element of the list, and Tail, which creates a list by removing the head from its

input list. Undefined represents an undefined value of type Elem.

Page 24: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

24 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

List specification (2)

Operation signatures:Create ListCons (List, Elem) ListHead (List) ElemLength (List) IntegerTail (List) List

Page 25: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

25 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

List specification (3)

Axioms defining operations over classHead (Create) = Undefined exception (empty list)Head (Cons (L, v))= if L = Create then v else Head (L)Length (Create) = 0Length (Cons (L, v) = Length (L) + 1Tail (Create) = CreateTail (Cons (L, v) = if L = Create then Create else Cons

(Tail (L), v)

Page 26: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

26 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Recursion in specifications

Operations are often specified recursively.Tail (Cons (L, v)) = if L = Create then Create

else Cons (Tail (L), v).Cons ([5, 7], 9) = [5, 7, 9]Tail ([5, 7, 9]) = Tail (Cons ( [5, 7], 9)) = Cons (Tail ([5, 7]), 9) = Cons (Tail (Cons ([5],

7)), 9) =Cons (Cons (Tail ([5]), 7), 9) = Cons (Cons (Tail (Cons ([], 5)), 7), 9) =Cons (Cons ([Create], 7), 9) = Cons ([7], 9) =

[7, 9]

Page 27: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

27 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Interface Specification in Critical Systems

Consider air traffic control system where aircraft fly through managed sectors of airspace

Each sector may include number of aircraft but, for safety reasons, these must be separated

In this example, simple vertical separation of 300m proposed

The system should warn controller if aircraft instructed to move so that separation rule breached

Page 28: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

28 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

A Sector Object(Air-Traffic System)

Critical operations on object representing controlled sector Enter: Add aircraft to controlled airspaceLeave: Remove aircraft from controlled

airspaceMove: Move aircraft from one height to

anotherLookup: Given aircraft identifier, return its

current height

Page 29: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

29 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Primitive Operations(Air-Traffic System)

Sometimes necessary to introduce additional operations to simplify specification

Other operations can then be defined using these more primitive operations

Primitive operationsCreate: Bring instance of sector into

existencePut: Add aircraft without safety checksIn-space: Determine if given aircraft in

sectorOccupied: Given height, determine if

aircraft within 300m of that height

Page 30: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

30 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Sector Specification (1)

Enter (S, CS, H) = if In-space (S, CS ) then S exception (A ircraft already in sector) elsif Occupied (S, H) then S exception (Height conflict) else Put (S, CS, H)

Leave (Create, CS) = Create exception (A ircraft not in sector)Leave (Put (S, CS1, H1), CS) = if CS = CS1 then S else Put (Leave (S, CS), CS1, H1)

Move (S, CS, H) = if S = Create then Create exception (No aircraft in sector) elsif not In-space (S, CS) then S exception (A ircraft not in sector) elsif Occupied (S, H) then S exception (Height conflict) else Put (Leave (S, CS), CS, H)

-- NO-HEIGHT is a constant indicating that a valid height cannot be returned

Lookup (Create, CS) = NO-HEIGHT exception (A ircraft not in sector)Lookup (Put (S, CS1, H1), CS) = if CS = CS1 then H1 else Lookup (S, CS)

Occupied (Create, H) = falseOccupied (Put (S, CS1, H1), H) = if (H1 > H and H1 - H Š 300) or (H > H1 and H - H1 Š 300) then true else Occupied (S, H)

In-space (Create, CS) = falseIn-space (Put (S, CS1, H1), CS ) = if CS = CS1 then true else In-space (S, CS)

sort Sectorimports INTEGER, BOOLEAN

Enter - adds an aircraft to the sector if safety conditions are satisfedLeave - removes an aircraft from the sectorMove - moves an aircraft from one height to another if safe to do soLookup - Finds the height of an aircraft in the sector

Create - creates an empty sectorPut - adds an aircraft to a sector with no constraint checksIn-space - checks if an aircraft is already in a sectorOccupied - checks if a specified height is available

Enter (Sector, Call-sign, Height) SectorLeave (Sector, Call-sign) SectorMove (Sector, Call-sign, Height) SectorLookup (Sector, Call-sign) Height

Create SectorPut (Sector, Call-sign, Height) SectorIn-space (Sector, Call-sign) BooleanOccupied (Sector, Height) Boolean

SECTOR

This diagram is broken down into more readable sections on following slides.

This diagram is broken down into more readable sections on following slides.

Page 31: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

31 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Sector Specification (2)

SECTOR

Sort Sector

Imports INTEGER, BOOLEAN

______________________________________________________

Enter – adds an aircraft to the sector if safety conditions are satisfied

Leave – removes an aircraft from the sector

Move – moves an aircraft from one height to another if safe to do so

Lookup – Finds the height of an aircraft in the sector

Create – creates an empty sector

Put – adds an aircraft to a sector with no constraint checks

In-space – checks if an aircraft is already in a sector

Occupied – checks if a specified height is available

Page 32: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

32 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Sector Specification (3)_____________________________________________________Enter (Sector, Call-sign, Height) Sector

Leave (Sector, Call-sign) Sector

Move (Sector, Call-sign, Height) Sector

Lookup (Sector, Call-sign) Height

Create Sector

Put (Sector, Call-sign, Height) Sector

In-space (Sector, Call-sign) Boolean

Occupied (Sector, Height) Boolean

_____________________________________________________

Page 33: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

33 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Sector Specification (4)

Enter (S, CS, H) = if In-space (S, CS ) then S exception (A ircraft already in sector) elsif Occupied (S, H) then S exception (Height conflict) else Put (S, CS, H)

Leave (Create, CS) = Create exception (A ircraft not in sector)Leave (Put (S, CS1, H1), CS) = if CS = CS1 then S else Put (Leave (S, CS), CS1, H1)

Move (S, CS, H) = if S = Create then Create exception (No aircraft in sector) elsif not In-space (S, CS) then S exception (A ircraft not in sector) elsif Occupied (S, H) then S exception (Height conflict) else Put (Leave (S, CS), CS, H)

-- NO-HEIGHT is a constant indicating that a valid height cannot be returned

Lookup (Create, CS) = NO-HEIGHT exception (A ircraft not in sector)Lookup (Put (S, CS1, H1), CS) = if CS = CS1 then H1 else Lookup (S, CS)

Occupied (Create, H) = falseOccupied (Put (S, CS1, H1), H) = if (H1 > H and H1 - H Š 300) or (H > H1 and H - H1 Š 300) then true else Occupied (S, H)

In-space (Create, CS) = falseIn-space (Put (S, CS1, H1), CS ) = if CS = CS1 then true else In-space (S, CS)

sort Sectorimports INTEGER, BOOLEAN

Enter - adds an aircraft to the sector if safety conditions are satisfedLeave - removes an aircraft from the sectorMove - moves an aircraft from one height to another if safe to do soLookup - Finds the height of an aircraft in the sector

Create - creates an empty sectorPut - adds an aircraft to a sector with no constraint checksIn-space - checks if an aircraft is already in a sectorOccupied - checks if a specified height is available

Enter (Sector, Call-sign, Height) SectorLeave (Sector, Call-sign) SectorMove (Sector, Call-sign, Height) SectorLookup (Sector, Call-sign) Height

Create SectorPut (Sector, Call-sign, Height) SectorIn-space (Sector, Call-sign) BooleanOccupied (Sector, Height) Boolean

SECTOR

Page 34: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

34 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Sector Specification (5)

Enter (S, CS, H) = if In-space (S, CS ) then S exception (A ircraft already in sector) elsif Occupied (S, H) then S exception (Height conflict) else Put (S, CS, H)

Leave (Create, CS) = Create exception (A ircraft not in sector)Leave (Put (S, CS1, H1), CS) = if CS = CS1 then S else Put (Leave (S, CS), CS1, H1)

Move (S, CS, H) = if S = Create then Create exception (No aircraft in sector) elsif not In-space (S, CS) then S exception (A ircraft not in sector) elsif Occupied (S, H) then S exception (Height conflict) else Put (Leave (S, CS), CS, H)

-- NO-HEIGHT is a constant indicating that a valid height cannot be returned

Lookup (Create, CS) = NO-HEIGHT exception (A ircraft not in sector)Lookup (Put (S, CS1, H1), CS) = if CS = CS1 then H1 else Lookup (S, CS)

Occupied (Create, H) = falseOccupied (Put (S, CS1, H1), H) = if (H1 > H and H1 - H Š 300) or (H > H1 and H - H1 Š 300) then true else Occupied (S, H)

In-space (Create, CS) = falseIn-space (Put (S, CS1, H1), CS ) = if CS = CS1 then true else In-space (S, CS)

sort Sectorimports INTEGER, BOOLEAN

Enter - adds an aircraft to the sector if safety conditions are satisfedLeave - removes an aircraft from the sectorMove - moves an aircraft from one height to another if safe to do soLookup - Finds the height of an aircraft in the sector

Create - creates an empty sectorPut - adds an aircraft to a sector with no constraint checksIn-space - checks if an aircraft is already in a sectorOccupied - checks if a specified height is available

Enter (Sector, Call-sign, Height) SectorLeave (Sector, Call-sign) SectorMove (Sector, Call-sign, Height) SectorLookup (Sector, Call-sign) Height

Create SectorPut (Sector, Call-sign, Height) SectorIn-space (Sector, Call-sign) BooleanOccupied (Sector, Height) Boolean

SECTOR

Page 35: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

35 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Specification Commentary

Use basic constructors Create and Put to specify other operations

Define Occupied and In-space using Create and Put and use them to make checks in other operation definitions

All operations that result in changes to sector must check that safety criterion holds

Page 36: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

36 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Behavioral SpecificationAlgebraic specification can be cumbersome

when object operations not independent of object state

Model-based specification Exposes system state and Defines operations in terms of changes to

that stateZ notation

Mature technique for model-based specification.

Combines formal and informal description and

Uses graphical highlighting when presenting specifications

Page 37: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

37 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Structure of Z Schema

contents Š capacity

Containercontents: capacity:

Schema name Schema signature Schema predicate

Page 38: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

38 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Insulin Pump

Needleassembly

Sensor

Display1 Display2

Alarm

Pump Clock

Power supply

Insulin reservoir

Controller

Page 39: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

39 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Modeling Insulin Pump

Schema models insulin pump as number of state variablesreading?dose, cumulative_doser0, r1, r2capacityalarm!pump!display1!, display2!

Names followed by ? = inputs, Names followed by ! = outputs

Page 40: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

40 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Schema Invariant

Each Z schema has invariant part which defines conditions always true

For insulin pump schema always true thatDose must be less than or equal to

capacity of insulin reservoirNo single dose may be more than 5 units

of insulin and Total dose delivered in time period must

not exceed 50 units of insulin.Display1! shows status of insulin reservoir.

Page 41: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

41 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Insulin Pump Schema

Insulin_pumpreading? : dose, cumulative_dose: r0, r1, r2: // used to record the last 3 readings takencapacity: alarm!: {off, on}pump!: display1!, display2!: STRING

dose Š capacity dose Š 5 cumulative_dose Š 50capacity 40 display1! = " "capacity Š 39 capacity 10 display1! = "Insulin low"capacity Š 9 alarm! = on display1! = "Insulin very low"r2 = reading?

Page 42: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

42 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

State invariants

r2 = Reading?

dose! Š insulin_available

insulin_available Š capacity

// The cumulative dose of insulin delivered is set to zero once every 24 hours

clock? = 000000 cumulative_dose = 0

// If the cumulative dose exceeds the limit then operation is suspended

cumulative_dose max_daily_dose status = error display1! = “Daily dose exceeded”

// Pump configuration parameterscapacity = 100 safemin = 6 safemax = 14

max_daily_dose = 25 max_single_dose = 4 minimum_dose = 1

display2! = nat_to_string (dose!)clock! = clock?

Page 43: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

43 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

The dosage computation

The insulin pump computes the amount of insulin required by comparing the current reading with two previous readings.

If these suggest that blood glucose is rising then insulin is delivered.

Information about the total dose delivered is maintained to allow the safety check invariant to be applied.

Note that this invariant always applies - there is no need to repeat it in the dosage computation.

Page 44: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

44 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

RUN schema (1)

RUNINSULIN_PUMP_STATE

switch? = auto status = running status = warning

insulin_available max_single_dose

cumulative_dose < max_daily_dose

// The dose of insulin is computed depending on the blood sugar level

(SUGAR_LOW SUGAR_OK SUGAR_HIGH)

// 1. If the computed insulin dose is zero, don’t deliver any insulin

CompDose = 0 dose! = 0

// 2. The maximum daily dose would be exceeded if the computed dose was delivered so the insulindose is set to the difference between the maximum allowed daily dose and the cumulative dosedelivered so far

CompDose + cumulative_dose > max_daily_dose alarm! = on status’ = warning dose! =max_daily_dose – cumulative_dose

Page 45: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

45 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

RUN schema (2)

// 3. The normal situation. If maximum single dose is not exceeded then deliver the computed dose. Ifthe single dose computed is too high, restrict the dose delivered to the maximum single dose

CompDose + cumulative_dose < max_daily_dose ( CompDose Š max_single_dose dose! = CompDose

CompDose > max_single_dose dose! = max_single_dose )

insulin_available’ = insulin_available – dose!

cumulative_dose’ = cumulative_dose + dose!

insulin_available Š max_single_dose * 4 status’ = warning display1! = “Insulin low”

r1’ = r2r0’ = r1

Page 46: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

46 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Sugar OK schema

SUGAR_OKr2 safemin r2 Š safemax

// sugar level stable or falling

r2 Š r1 CompDose = 0

// sugar level increasing but rate of increase falling

r2 > r1 (r2-r1) < (r1-r0) CompDose = 0

// sugar level increasing and rate of increase increasing compute dose// a minimum dose must be delivered if rounded to zero

r2 > r1 (r2-r1) (r1-r0) (round ((r2-r1)/4) = 0) CompDose = minimum_dose

r2 > r1 (r2-r1) (r1-r0) (round ((r2-r1)/4) > 0)

CompDose = round ((r2-r1)/4)

Page 47: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

47 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Key points

Formal system specification complements informal specification techniques.

Formal specifications are precise and unambiguous. They remove areas of doubt in a specification.

Formal specification forces an analysis of the system requirements at an early stage. Correcting errors at this stage is cheaper than modifying a delivered system.

Formal specification techniques are most applicable in the development of critical systems and standards.

Page 48: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

48 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Key points

Algebraic techniques are suited to interface specification where the interface is defined as a set of object classes.

Model-based techniques model the system using sets and functions. This simplifies some types of behavioral specification.

Operations are defined in a model-based spec. by defining pre and post conditions on the system state.

Page 49: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

49 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

Homework

RequiredBy Fri 1 Oct 2004For a total of 25 points, answer fully:

10.2 (5 pts), 10.5 (10), 10.6 (10)

OptionalBy Fri 8 Oct 2004For up to 12 extra points, answer any or all

of the following:10.7 (5), 10.8 (5), 10.10 (2)

Page 50: 1 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved. Formal Specification IS301 – Software Engineering.

50 Note content copyright © 2004 Ian Sommerville. NU-specific content © 2004 M. E. Kabay. All rights reserved.

DISCUSSION