Академический Документы
Профессиональный Документы
Культура Документы
Ch. 5
Outline
Discussion of the term "specification"
Types of specifications
operational
descriptive
Entity Relationship Diagrams
Logic-based notations
Algebraic notations
Specification
A broad term that means definition
Used at different stages of software
development for different purposes
Generally, a statement of agreement
(contract) between
producer and consumer of a service
implementer and user
Uses of specification
Statement of user requirements
major failures occur because of
misunderstandings between the
producer and the user
"The hardest single part of building a
softwarem system is deciding
precisely what to build" (F. Brooks)
Ch. 5
Specification qualities
Precise, clear, unambiguous
Consistent
Complete
internal completeness
external completeness
Incremental
Ch. 5
Clear, unambiguous,
understandable
Example: specification fragment
for a word-processor
Selectingistheprocessofdesignating
areasofthedocumentthatyouwantto
workon.Mosteditingandformatting
actionsrequiretwosteps:firstyou
selectwhatyouwanttoworkon,
suchastextorgraphics;thenyou
initiatetheappropriateaction.
can an area be scattered?
Ch. 5
Consistent
Example: specification fragment for a
word-processor
Thewholetextshouldbekeptinlines
ofequal length.Thelengthisspecified
bytheuser.Unlesstheusergivesan
explicithyphenationcommand,
acarriagereturnshouldoccuronly
attheendofaword.
11
Complete
Internal completeness
the specification must define any new
concept or terminology that it uses
glossary helpful for this purpose
Ch. 5
12
Incremental
Referring to the specification
process
start from a sketchy document and
progressively add details
13
Classification of
specification styles
Informal, semi-formal, formal
Operational
Behavior specification in terms of
some abstract machine
Descriptive
Behavior described in terms of
properties
Ch. 5
14
Example 1
Specification of a geometric figure E:
E can be drawn as follows:
1. Select two points P1 and P2 on a plane
2. Get a string of a certain length and fix its ends
to P1 and P2
3. Position a pencil as shown in next figure
4. Move the pen clockwise, keeping the string
tightly stretched, until you reach the point where
you started drawing
this is an operational specification
Ch. 5
15
P1
P
2
Ch. 5
16
A descriptive specification
Geometric figure E is describe by
the following equation
ax2 + by2 + c = 0
where a, b, and c are suitable
constants
Ch. 5
17
Another example
OP
18
How to verify a
specification?
Observe dynamic behavior of
specified system (simulation,
prototyping, testing specs)
Analyze properties of the specified
system
Analogy with traditional engineering
physical model of a bridge
mathematical model of a bridge
Ch. 5
19
Ch. 5
20
Graphical notation
bubbles represent functions
arcs represent data flows
open boxes represent persistent
store
closed boxes represent I/O
interaction
The function symbol
21
Example
specifies evaluation of
(a + b) * (c + a * d)
Ch. 5
22
Output
1
2
Input n
information
...
system
Ch. 5
...
Output
Output
1
2
m
23
H
I
A3
J
A4
A1
K
A2
K2
S
N
A6
P
A5
B2
K3
K1
T
B1
Ag
A7
B3
B4
K4
Ch. 5
24
A library example
Book
Book request
by the user
Shelves
Author
Book
List of Authors
Get a book
Title
Book
reception
Book title;
user name
List of titles
Title
Search by
topics
List of topics
Topic
Topic
Topic request
by the user
List of titles
referring to the topic
Ch. 5
Display of
the list of titles
25
Refinement of
Get a book
Book
Shelves
Author
Get
the book
Book
List of Authors
<shelf#, book#>
Find
book
position
Title
Book
reception
List of titles
Title and author
of requested book;
name of the user
Book title;
user name
Book request
by the user
Ch. 5
26
Clinical
Data
Report
Patient
Monitoring
Nurse
Alarm
Recent data
Persistent data
27
A refinement
Patient archive
Recent
Data
Report
Request
Data for
Report
Nurse
Generate
Report
Update
archive
Report
Formatted data
Central
Monitoring
Limits
Alarm
Nurse
Patient data
Local
Monitoring
Ch. 5
Clinical
Data
Patient
28
More refinement
Pressure
Limits
Pressure, pulse
Format
data
Patient
data
Temperature decode
Check
limit
violations
Pulse
Result
Date clock
Time
produce
message
Formatted data
alarm
Ch. 5
29
A
E
B
D
F
Ch. 5
30
Possible interpretations:
(a) A produces datum, waits until B consumes it
(b) B can read the datum many times without
consuming it
(c) a pipe is inserted between A and B
Ch. 5
31
Formalization/extensions
There have been attempts to
formalize DFDs
There have been attempts to
extend DFDs (e.g., for real-time
d1
systems)
Trigger
d2
.
.
.
dn
Ch. 5
32
return
book
librarian
customer
library
update
Ch. 5
33
Librarian
Catalogue
membercard+
book request
membership
OK
bookrequest
time
bookavailable
bookborrowed
Ch. 5
34
Customer
3: book request
Librarian
5: book borrowed
Catalogue
4: book available
Ch. 5
35
a
b
q0
c
q2
b
q3
Ch. 5
36
Example: a lamp
Push switch
Of
On
Push switch
Ch. 5
37
Another example:
a plant control system
High-pressure alarm
High-temperature alarm
On
Off
Restart
Ch. 5
38
A refinement
Pressure signal
Normal
Pressure
Pressure
action
ac tion
Suc cessful
recovery
Temperature signal
Unsucc essful
recovery
Normal
Of
Off
Succ essful
recovery
Temperature signal
Unsuccessful
recovery
Temperature
ac tion
Ch. 5
Pressure signal
39
Classes of FSMs
Deterministic/nondeterministic
FSMs as recognizers
introduce final states
FSMs as transducers
introduce set of outputs
...
Ch. 5
40
FSMs as recognizers
q1
q2
q3
q4
b
q0
qf
e
q5
q6
qf is a final state
Ch. 5
41
FSMs as recognizers
<letter>
<digit>
q
<letter>
q1
<letter>
Legend:
<letter>
<digit>
q2
<digit>
42
Limitations
Finite memory
State explosion
Given a number of FSMs with k1, k2,
kn states, their composition is a FSM
with k1 * k2 * * kn. This growth is
exponential with the number of FSMs,
not linear (we would like it to be k1 +
k2 + + kn )
Ch. 5
43
Producer
p1
p2
deposit
get
Consumer
c1
c2
consume
deposit
Storage
0
deposit
1
get
Ch. 5
get
44
write
<1, p ,c >
1 1
consume
produce
consume
produce
<0, p ,c >
2 1
<2, p1 ,c >
1
produce
<1, p ,c>
2 1
<2, p ,c >
2 1
read
<0, p ,c >
1 2
<1, p ,c >
1 2
<2, p ,c >
1 2
read
produce
consume
<0, p2 , c2 >
read
write
produce
consume
read
write
<1, p ,c2>
2
Ch. 5
produce
consume
<2, p ,c2>
2
45
Petri nets
A quadruple (P,T,F,W)
P: places T: transitions (P, T are finite)
F: flow relation (F {PT} {TP} )
W: weight function (W: F N {0} )
Properties:
(1) P T =
(2) P T
(3)F (P T) (T P)
(4) W: F N-{0}
Default value of W is 1
State defined by marking: M: P N
Ch. 5
46
Graphical representation
places
marking
transitions
P
flows
3 weight
P
2
t
3
P
5
t
t
P
P
7
Ch. 5
47
Semantics: dynamic
evolution
M(p) W(<p,t>)
Ch. 5
48
Nondeterminism
Any of the enabled transitions may
fire
Model does not specify which fires,
nor when it fires
Ch. 5
49
Ch. 5
50
after (a) either (b) or (c) may occur, and then (d)
t1
t1
P
t4
t3
t5
t6
t6
(b)
P
P1
t1
P
t1
P
P6
P
7
t5
P
5
t4
t3
t6
t4
t3
P
7
t5
t6
Ch. 5
(c)
t2
P3
P5
P2
P1
t2
P3
P
7
t5
(a)
t4
t3
P6
P
7
P
5
P5
t2
P3
t2
P3
P2
P1
P2
P1
51
(d)
Common cases
Concurrency
two transitions are enabled to fire in a given state, and
the firing of one does nor prevent the other from firing
see t1 and t2 in case (a)
Conflict
two transitions are enabled to fire in a given state, but
the firing of one prevents the other from firing
see t3 and t4 in case (d)
place P3 models a shared resource between two processes
Ch. 5
52
P2
P3
t1
P4
t3
t2
imposes alternation
t4
P6
t5
t6
Ch. 5
53
A conflict-free net
P1
P2
t1
t2
R
t'3
t'4
t"3
t1 , t 3 , t 2, t 4
t"4
2
t
t5
Ch. 5
54
A deadlock-free net
P1
P2
t1
t2
R
t'3
t'4
t"3
t5
t"4
2 2
Ch. 5
55
t1
t3
t
Ch. 5
4
56
Producer-consumer example
(1)
write
consume
P1
P
2
C
C1
produce
read
separate nets
for the subsystems
read
read
write
Ch. 5
write
57
C2
read
read
write
P1
produce
Ch. 5
write
P2
58
channel1
channel2
59
Extension 1
assigning values to tokens
Transitions have associated
predicates and functions
Predicate refers to values of tokens
in input places selected for firing
Functions define values of tokens
produced in output places
Ch. 5
60
Example
P1
t1
P2
7
4
t2
P4
P5
P3
Predicate P2 > P1
and function
P4 := P2 + P1
associated with t1
Predicate P3 = P2
and functions
P4 := P3 P2 and
P5 := P2 + P3 are
associated with t2
61
Extension 2
specifying priorities
A
priority
function
pri
from
transitions to natural numbers:
pri: T N
When
several
transitions
are
enabled, only the ones with
maximum priority are allowed to fire
Among them, the one to fire is
chosen nondeterministically
Ch. 5
62
Extension 3
Timed Petri nets
A pair of constants <tmin, tmax> is
associated with each transition
Once a transition is enabled, it must
wait for at least tmin to elapse
before it can fire
If enabled, it must fire before tmax
has elapsed, unless it is disabled by
the firing of another transition before
tmax
Ch. 5
63
Example
combining priorities and time
P
1
t1
tmin = 1
tmax = 4
priority = 1
P
4
P
3
P2
t
2
tmin = 2
tmax = 3
priority = 3
Ch. 5
t
tmin = 0
tmax = 5
priority = 2
64
Original message
= c1
{ tmin
tmax = k1
Message triplication
Message copies
= c2
{ tmin
tmax = k2
PC1
tmin = 0
tmax = 0
for all three transitions
tvoting1
tvoting2
tvoting3
Precise specification
of message triplication
problem
Case (1)
Forwarded message
Ch. 5
65
Original message
tmin = c1
tmax = k1
Message triplication
Message copies
tmin = c2
tmax = k2
tmin = 0
tmax = 0
PC3
Precise specification
of message triplication
problem
Case (2)
tvoting
Forwarded message
Ch. 5
66
Case study
An n elevator system to be installed in a building
with m floors
Natural
language
ambiguities
specs
Formal
specification
ambiguities
using
contain
PNs
several
removes
67
Ch. 5
68
69
Transfer
from floor
j to j+1
Pushing internal
button for floor
j+1
Elevator
at floor j
Ch. 5
70
Button module
C
Push
0.05. .0.05
0.1. .
On
Set
0. .0
Off
Reset
Ch. 5
71
Fm-1
UF
m-1
UF3
F3
UF2
DFm-1
DF3
F2
DF2
F1
Ch. 5
72
UFj+1
t8
t10
t7
UPh
t9
t11
t12
On
Fj"
t
On
DOWNh
Fj'
On
ILBj+1
On
t1
t2
t3
t4
t5
t6
ILBh
On
UPj+1
Fj
On
DOWNj+1
Ch. 5
Assume j+1hm
73
0..0
On
ILBj
Off
Reset
Fj
Ch. 5
74
UPj
Set
ti'
Fj
On
Off
x..x Reset
x time needed by
a person to enter + pushing button
Ch. 5
75
Specifying policy
A fair solution:
Keep the direction unchanged as long as there are
calls that require elevator to go in that direction
[x,x]
U_D
[0,0]
UK
DK
[0,0]
U
D_U
[x,x]
t7, t8, t9 have higher priority than t10, t11, t12
Ch. 5
76
A general scheduler
SCHEDULER
...
all transitions
77
Declarative specifications
ER diagrams: semiformal
specs
Logic specifications
Algebraic specifications
Ch. 5
78
ER diagrams
Often used as a complement to
DFD to describe conceptual data
models
Based on entities, relationships,
attributes
They are the ancestors of class
diagrams in UML
Ch. 5
79
Example
NAM E
AGE
STUDENT
SEX
ENROLLED_IN
SUBJECT
CLASS
COURSE_ID
M AX_ENROLLM ENT
Ch. 5
80
Relations
Relations can be partial
They can be annotated to define
one to one
one to many
many to one
many to many A
Ch. 5
81
Data
Director
HeadOf
Department
Participate
Assigned
Project
Employee
Ch. 5
82
Logic specifications
Examples of first-order theory (FOT) formulas:
x > y and y > z implies x > z
x=yy=x
for all x, y, z (x > y and y > z implies x >
z)
x+1<x1
for all x (exists y (y = x + z))
x > 3 or x < -6
Ch. 5
83
Specifying complete
programs
A property, or requirement, for P is
specified as a formula of the type
{Pre (i1, i2,..., in) }
P
{Post (o1, o2,..., om, i1, i2,..., in)}
Pre: precondition
Post: postcondition
Ch. 5
84
Example
Program to compute greatest
common divisor
{i1 > 0 and i2 > 0}
P
{(exists z1, z2 (i1 = o * z1 and i2 = o * z2)
and not (exists h
(exists z1, z2 (i1 = h * z1 and i2 = h * z2) and h > o))}
Ch. 5
85
Specifying procedures
{n > 0} -- n is a constant value
procedure search (table: in integer_array; n: in integer;
element: in integer; found: out Boolean);
{found (exists i (1 i n and table (i) = element))}
{n > 0 }
procedure reverse (a: in out integer_array; n: in integer);
{for all i (1 i n) implies (a (i) = olda (n - i +1))}
Ch. 5
86
Specifying classes
Invariant predicates and pre/post
conditions for each method
Example of invariant specifying an
array implementing ADT set
for all i, j (1 i length and 1 j length and ij)
implies IMPL[i]IMPL[j]
(no duplicates are stored)
Ch. 5
87
Specifying non-terminating
behaviors
Example: producer+consumer+buffer
Invariant specifies that whatever has
been produced is the concatenation
of what has been taken from the
buffer and what is kept in the buffer
input_sequence = append (output_sequence,
contents(CHAR_BUFFER))
Ch. 5
88
Rules
(at (E, F, T) and on (EB, F1, T) and F1 > F)
implies start (E, F, T, up)
Ch. 5
89
events
instantaneous (caused state change occurs at same time)
represented by predicates that hold only at a particular
time instant
arrived (E, F, T)
Ch. 5
90
Events (1)
arrival (E, F, T)
E in [1..n], F in [1..m], T t0, (t0 initial time)
does not say if it will stop or will proceed, nor where it
comes from
departure(E, F, D, T)
E in [1..n], F in [1..m], D in {up, down}, T t0
stop (E, F, T)
E in [1..n], F in [1.. m], T t0
specifies stop to serve an internal or external request
Ch. 5
91
Events (2)
new_list (E, L, T)
E in [1..n], L in [1.. m]*, T t0
L is the list of floors to visit associated
with elevator (scheduling is performed by
the control component of the system)
call(F, D, T)
external call (with restriction for 1, N)
request(E, F, T)
internal reservation
Ch. 5
92
States
moving (E, F, D, T1, T2)
standing (E, F, T1, T2)
list (E, L, T1, T2)
We implicitly assume that state
predicates hold for any sub- interval (i.e.,
the rules that describe this are assumed
to be automatically added)
Nothing prevents that it holds for larger
interval
Ch. 5
93
94
95
97
Ch. 5
98
Control rules
Ch. 5
99
Ch. 5
100
101
Verifying specifications
The system can be simulated by providing a state
(set of facts) and using rules to make deductions
standing (2, 3, 5, 7) elevator 2 at floor 3 at least
from instant 5 to 7
list(2, empty, 5, 7)
request(2, 8, 7)
new_list(2, {8}, 7)
(excluding other events)
departure (2, up, 7 + Dts)
arrival (2, 8, 7 + Dts + Dta *(8-3))
Ch. 5
102
Verifying specifications
Properties can be stated and
proved via deductions
new_list (E, L, T) and F L
implies
new_list (E, L1, T1) and F L1 and T1 > T2
103
Descriptive specs
The system and its properties are
described in the same language
Proving properties, however,
cannot be fully mechanized for
most languages
Ch. 5
104
Algebraic specifications
Define a heterogeneous algebra
Heterogeneous = more than 1 set
Especially useful to specify ADTs
Ch. 5
105
Example
A system for strings, with operations for
creating new, empty strings (operation new)
concatenating strings (operation append)
adding a new character at the end of a string
(operation add)
checking the length of a given string
(operation length)
checking whether a string is empty (operation
isEmpty)
checking whether two strings are equal
(operation equal)
Ch. 5
106
Specification: syntax
algebra StringSpec;
introduces
sorts String, Char, Nat, Bool;
operations
new: () String;
append: String, String String;
add: String, Char String;
length: String Nat;
isEmpty: String Bool;
equal: String, String Bool
Ch. 5
107
Specification: properties
constrains new, append, add, length, isEmpty, equal so that
for all [s, s1, s2: String; c: Char]
isEmpty (new ()) = true;
isEmpty (add (s, c)) = false;
length (new ()) = 0;
length (add (s, c)) = length (s) + 1;
append (s, new ()) = s;
append (s1, add (s2,c)) = add (append (s1,s2),c);
equal (new (),new ()) = true;
equal (new (), add (s, c)) = false;
equal (add (s, c), new ()) = false;
equal (add (s1, c), add (s2, c) = equal (s1,s2);
end StringSpec.
Ch. 5
108
Example: editor
newF
creates a new, empty file
isEmptyF
states whether a file is empty
addF
adds a string of characters to the end of a file
insertF
inserts a string at a given position of a file (the rest of
the file will be rewritten just after the inserted string)
appendF
concatenates two files
Ch. 5
109
algebra TextEditor;
introduces
sorts Text, String, Char, Bool, Nat;
operations
newF: () Text;
isEmptyF: Text Bool;
addF: Text, String Text;
insertF: Text, Nat, String Text;
appendF: Text, Text Text;
deleteF: Text Text;
lengthF : Text Nat;
equalF : Text, Text Bool;
addFC: Text, Char Text;
{This is an auxiliary operation that will be needed
to define addF and other operations on files.}
Ch. 5
110
112
Example of views
document production
data flow view (1)
Predefined
Text skeletons
Predefined
Formats
Document
production
User
Formatting
options
Customer data
(name, type of
document)
Customers
Print
Document
Ch. 5
113
Print document
(b)
Ch. 5
114
UML notations
Class diagrams
describe static architecture in terms
of classes and associations
dynamic evolution can be described
via Statecharts (see later)
Activity diagrams
describe sequential and parallel
composition of method executions,
and synchronization
Ch. 5
115
An activity diagram
[c1]
A
(1)
[c3]
E
[c4]
[c2]
F
G
end
(2)
Ch. 5
116
Building modular
specifications
The case of algebraic
specifications
How to combine algebras taken from
a library
How to organize them in a hierarchy
Ch. 5
117
Algebras used by
StringSpec
algebra BoolAlg;
introduces
sorts Bool;
operations
true () Bool;
false () Bool;
not : Bool Bool;
and: Bool, Bool Bool;
or: Bool, Bool Bool;
implies: Bool, Bool Bool;
: Bool, Bool Bool;
constrains true, false, not, and, or, implies, so that
Bool generated by [true, false]
for all [a, b: Bool]
not (true) = false;
not (false) = true;
a and b = not (not (a) or not (b));
a implies b = not (a) or b;
end BoolAlg.
Ch. 5
118
Algebras used by
StringSpec (cont.)
algebra NatNumb;
introduces
algebra CharAlg;
sorts Nat, Bool;
introduces
operations
sorts Char, Bool;
0: () Nat;
operations
Succ: Nat Nat;
a: () Char;
+ : Nat, Nat Nat;
b : () Char;
- : Nat, Nat Nat;
end NatNumb.
Ch. 5
119
StringSpec revisited
algebra StringSpec;
imports BoolAlg, NatNumb, CharAlg;
introduces
sorts String, Char, Nat, Bool;
operations
new: () String;
end StringSpec.
Ch. 5
120
Incremental specification of
an ADT
We want to target stacks, queues,
sets
We start from "container" and then
progressively specialize it
We introduce another structuring
clause
assumes
defines inheritance relation among algebras
Ch. 5
121
Container algebra
algebra Container;
imports DataType, BoolAlg, NatNumb;
introduces
sorts Cont;
operations
new: () Cont;
insert: Cont, Data Cont;
{Data is the sort of algebra DataType, to which
elements to be stored in Cont belong}
isEmpty: Cont Bool;
size: Cont Nat;
constrains new, insert, isEmpty, size so that
Cont generated by [new, insert]
for all [d: Data; c: Cont]
isEmpty (new ()) = true;
isEmpty (insert (c, d)) = false;
size (new ()) = 0;
end Container.
Ch. 5
122
algebra TableAlg;
assumes Container;
introduces
sorts Table;
operations
last: Table Data;
rest: Table Table;
equalT : Table, Table Bool;
delete: Table, Data Table;
constrains last, rest, equalT, delete, isEmpty, new, insert so that
for all [d, d1, d2: Data; t, t1, t2: Table]
last (insert (t, d)) = d;
rest (new ()) = new ();
rest (insert (t, d)) = t;
equalT (new (), new ()) = true;
equalT (insert (t, d), new ()) = false;
equalT (new (), insert (t,d)) = false;
equalT (t1,t2) = equalD(last (t1), last (t2)) and
equalT (rest (t1),rest (t2));
delete (new (), d) = new ();
delete (insert (t,d),d) = delete (t, d);
if not equalD(d1, d2) then
delete (insert (t, d1), d2) = Ch.
insert
(delete (t, d2), d1);
5
end TableAlg.
123
A graphical view
Queue
Algebra
Table
Algebra
Container
Algebra
Bool
Algebra
Legend:
Nat
Algebra
DataType
Algebra
imports relation
assumes relation
Ch. 5
125
A richer hierarchy
Set
Multiset
Sorted
Table
Tree
Queue
Algebra
Table
Algebra
Container
Algebra
Array
Algebra
Bool
Algebra
Nat
Algebra
Ch. 5
Sorted
DataType
DataType
Algebra
126
From specs to an
implementation
Algebraic spec language described
so far is based on the "Larch
shared language"
Several "interface languages" are
available to help transitioning to
an implementation
Larch/C++, Larch/Pascal
Ch. 5
127
128
Ch. 5
129
Sequential decomposition
--chemical plant control example-Normal
AnomalyDetection
RecoverySuccess
Recovery
Press
Recovery
Identification
Temp
Done
Pressure
Action
Done
Temperature
Action
Ch. 5
RecoveryFailure
130
Parallel decomposition
Idle
start
stop
ConcurrentWork
write
P1
write
read
produce
read
read
write
P2
Producer
C1
Buffer
Ch. 5
consume
C2
Consumer
131
Push(item)
NotEmpty
y
Pop[stack contains 1 item]
Push(item)
Ch. 5
132
Modularizing logic
specifications: Z
System specified by describing
state space, using Z shemas
Properties of state space described
by invariant predicates
predicates written in first-order logic
133
Ch. 5
134
Ch. 5
135
Ch. 5
136
Ch. 5
137
Operations
(1)
Ch. 5
138
Operations
(2)
Ch. 5
139
140
Conclusions (1)
Specifications describe
what the users need from a system (requirements
specification)
the design of a software system (design and
architecture specification)
the features offered by a system (functional
specification)
the performance characteristics of a system
(performance specification)
the external behavior of a module (module interface
specification)
the internal structure of a module (internal structural
specification)
Ch. 5
141
Conclusions (2)
Descriptions are given via suitable
notations
There is no ideal notation
142