introduction to fsm-modelling and testpeople.cs.aau.dk/~bnielsen/tov07/material/fsm-intro.pdf ·...

47
Introduction to FSM-modelling and test Brian Nielsen [email protected] Department of Computer Science, Aalborg University, Denmark C SS 1010111011010101 1011010101110111

Upload: others

Post on 21-Sep-2020

24 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Introduction to FSM-modelling and

testBrian Nielsen [email protected]

Department of Computer Science,Aalborg University, Denmark

C SS10101110110101011011010101110111

Page 2: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Menu• Basic definitions and fundamental

results• Testing with FSMs• Modelling untimed systems using

Uppaal• Model checking

Page 3: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Basic Definitions

Page 4: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

System StructureSystem 1

Component 1

Component 2

Component 3

Component 6

Component 5

Component 4

•How do we model components?•How does components interact?•How do we specify environment assumptions?•How do we ensure correct behaviour

Page 5: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Behavior

Unified Model = State Machine!

a

b

x

ya?

b?

x!

y!b?

Control states

Inputports

Outputports

Page 6: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Finite State Machine (Mealy)q1

q2

q3

coin / -tea-but / tea

cof-but / cof

coin / -

Inputs = {cof-but, tea-but, coin}Outputs = {cof,tea}States: {q1,q2,q3}Initial state = q1Transitions= {

(q1, coin, -, q2),(q2, coin, -, q3),(q3, cof-but, cof, q1),(q3, tea-but, tea, q1) }

q1teatea-butq3

q1cofcof-butq3

q3-coinq2

q2-coinq1

next state

outputinputcurrent state

effectcondition

Sample run:

coin/ - coin/- coin/ -cof-but / cof

coin/ -

q1 q2 q3 q1

q2cof-but / cof q1q3

Page 7: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Finite State Machine (Moore)q1

q2

q3

coin

tea-butcof-but q4selecttea-butq3

q1cofcup-takenq5

q1teacup-takenq4

q5selectcof-butq3

q3need1coinq2

q2need2coinq1

next state

Activityinputcurrent state

effectcondition

q4q5

coin

cup-taken

teacof

cup-taken

select

need1

need2

Input sequence: coin.coin.cof-but.coin.coin.cof-butOutput sequence: need2.need1.select.cof. need2.need1.select.cof

need2=display shows “insert two coins”

Page 8: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

IO-FSM

Inputs = {cof-but, tea-but, coin}Outputs = {cof,tea}States: {q1,q2,q3}Initial state = q1Transitions= {

(q1, coin, q2),(q2, coin, q3),(q3, cof-but, q5),(q3, tea-but, q4), (q4, tea, q1),(q5, cof, q1)}

q4tea-but!q3

q1cof?q4

q1tea!q5

q5cof-but!q3

q3coin?q2

q2coin?q1

next stateactioncurrent state

effectcondition

Sample run:

coin? coin? cofcof-but?

coin

q1 q2 q3 q5

cof q1q3

action trace: coin?.coin?.cof!-coin?.coin?.cof!input sequence: coin.coin.coin.coinOutput sequence: cof.cof

q1

q2

q3

coin?

tea-but?cof-but?q4q5

coin?

cof! tea!

cof-but? q5coin?q1 q2

Page 9: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Fully Specified FSM

q1

q2

q3

coin / - tea-but / tea

cof-but / cof

coin / -

q2-tea-butq2

q2-cof-butq2

q1-tea-butq1

q1teatea-butq3

q3coincoinq3

q1-cof-butq1

q1cofcof-butq3

q3-coinq2

q2-coinq1

next state

outputinputcurrent state

effectcondition

coin / coin

cof-but / -tea-but / -

cof-but / -tea-but / -

Page 10: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

FSM as program 1enum currentState {q1,q2,q3};enum input {coin, cof_but,tea_but};int nextStateTable[3][3] = {

q2,q1,q1, q3,q2,q2,q3,q1,q1 };

int outputTable[3][3] = { 0,0,0, 0,0,0,coin,cof,tea};

While(Input=waitForInput()) {OUTPUT(outputTable[currentState,input])currentState=nextStateTable[currentState,input];

}

Page 11: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

FSM as program 2enum currentState {q1,q2,q3};enum input {coin,cof,tea_but,cof_but};

While(input=waitForInput){Switch(currentState){case q1: {

switch (input) {case coin: currentState=q2; break;case cuf_but:case tea_but: break;default: ERROR(”Unexpected Input”);}

break;case q3: {

switch(input) { case cof_buf: {currentState=q3;

OUTPUT(cof);break;}

… default: ERROR(”unknown currentState}

Page 12: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Spontaneous Transitionsq1

q2

q3

coin / -tea-but / tea

cof-but / cof

coin / -

q4--q3

q1teatea-butq3

q1-fixq4

q1cofcof-butq3

q3-coinq2

q2-coinq1

next state

outputinputcurrent state

effectcondition

q4

fix / -

cof-but / -tea-but / -coin / -

- / -

alias internal transitions alias unobservable transisions

Page 13: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Non-deterministic FSM

q1cofcof-butq3

q1-coinq1

q1moccacof-butq3

q1 teatea-butq3

q3-coinq2

q2-coinq1

next state

outputinputcurrent state

effectcondition

q1

q2

q3

coin / -tea-but / teacof-but / cof

coin / -

coin / -

cof-but / mocca

Page 14: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Extended FSM

q1

coin / -total++

total>=2 and cap > 1cof-but / cofcapacity--, total:=0

•EFSM = FSMs + variables + enabling conditions + assignments•Easier way of expressing an FSM •Can be translated into FSM if variables have bounded domain•State: control location+variable states: (q,total,capacity)

total>=1 and capacity > 1tea-but / tea

capacity--, total:=0

coin / -(q1,0,10) (q1,1,10) coin / - (q1,2,10) cof-but / cof (q1,0,9)

Page 15: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Parallel Composition (independent)

q1

q2

q3

j++

j++

p1

p2

p3

i++

i++

P QP || Q

(p1, q1)

(p2, q1)

(p2, q2)

(p1, q2)

i++

j++

j++ i++(p3, q1)

i++

(p1, q3)

j++

(p3, q2)

(p3, q3)

(p2, q3)

i++j++

i++

i++

j++

j++

(0,0)

(1,0)

(2,0)

(2,1)

(2,2)

(1,2)

(0,2)

(0,1)

(1,1)

Page 16: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

State Explosion Problem• n parallelle FSMs• Med hver k tilstande• Giver k=^n tilstande I

produkt FSM• EXPONENTIEL

VÆKST• 10^2 =100• 10^3 = 1000• 10^4 = 10000• 10^10=10000000000

Page 17: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Queued Parallel Composition

q1

q2

q3

coin / -tea-but / tea

cof-but / cof

coin / -

p1

p2

p3

- / coin

-/ cof-but

p3

- / coin

cof/ report

System state: Snapshot of each EFSM and queues

Output is queued in (un) bounded queueQ may be per process, per action,or explicity defined

Page 18: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Synchronous Parallel Composition

q1

q2

q3

coin / -tea-but / tea

cof-but / cofcoin / -

p1

p2

p3

- / coin

cof / cof-but

p3

- / coin

-/ report

Handshake on complementary actions

Page 19: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Asynchronous Parallel Composition

q1

q2

q3

coin / -tea-but / tea

cof-but / cof

coin / -

p1

p2

p3

- / coin

- / cof-but

p3

- / coin

cof / report

Single output variable per FSM holds last “written” output

Page 20: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Bank-box code

To open a bank boxthe code must contain at least 2

To open a bank boxthe code must end with

To open a bank box the code must end with a palindrome.g:. O

BG

……..

?

To open a bank boxthe code most end with

or with

!!

Page 21: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Determinization+minimization

Page 22: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Concepts• Two states s and t are (language)

equivalent iff • s and t accepts same language• has same traces: tr(s) = tr(t)

• Two Machines M0 and M1 are equivalent iff initial states are equivalent

• A minimized / reduced M is one that has no equivalent states• for no two states s,t, s!=t, s equivalent t

Page 23: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Fundamental Results

• Every FSM may be determinized accepting the same language (potential explosion in size).

• For each FSM there exist a language-equivalent minimal deterministic FSM.

• FSM’s are closed under ∩ and ∪

• FSM’s may be described as regular expressions (and vise versa)

Page 24: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

High-level FSM languages

Page 25: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

UML-Statecharts

Page 26: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

SDL

Page 27: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Esterel

Page 28: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Textual Notations for LTS

int x;proctype P(){do:: x<200 --> x=x+1od}

proctype Q(){do:: x>0 --> x=x-1od}

proctype R(){do:: x==200 --> x=0od}

init {run P(); run Q(); run R()}

SERVERv2 = (accept.request->service>accept.reply->SERVERv2).

CLIENTv2 = (call.request->call.reply->continue->CLIENTv2).

||CLIENT_SERVERv2 = (CLIENTv2 || SERVERv2)/{call/accept}.

Promela/Spin FSP/LTSA

Page 29: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

SPIN, G

erald Holzm

annA

T&T

Page 30: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

UppAal Navigator

Page 31: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

UppAal

Modelling and cerifyinguntimed systems

Page 32: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Tool Support (model checking)

System Description A

Requirement F Yes,PrototypesExecutable CodeTest sequences

No!Debugging Information

Tools: UPPAAL, visualSTATE, ESTEREL, SPIN, Statemate, FormalCheck, VeriSoft, Java Pathfinder,Telelogic…

TOOLTOOL

Page 33: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

UppAal Navigator

Page 34: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Home-Banking?

• Are the accounts in balance after the transactions?

int accountA, accountB; //Shared global variables//Two concurrent bank costumers

Thread costumer1 () { int a,b; //local tmp copy

a=accountA;b=accountB;a=a-10;b=b+10;accountA=a;accountB=b;

}

Thread costumer2 () { int a,b;

a=accountA;b=accountB;a=a-20; b=b+20;accountA=a;accountB=b;

}

Page 35: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Home Banking

A[] (pc1.finished and pc2.finished) imply (accountA+accountB==200)?

Page 36: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Home Bankingint accountA, accountB; //Shared global variablesSemaphore A,B; //Protected by sem A,B//Two concurrent bank costumers

Thread costumer1 () { int a,b; //local tmp copy

wait(A);wait(B);a=accountA;b=accountB;a=a-10;b=b+10;accountA=a;accountB=b;signal(A);signal(B);

}

Thread costumer2 () { int a,b;

wait(B);wait(A);a=accountA;b=accountB;a=a-20; b=b+20;accountA=a;accountB=b;signal(B);signal(A);

}

Page 37: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Semaphore FSM ModelBinary Semaphore Counting Semaphore

Page 38: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Semaphore Solution?

Page 39: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Semaphore Solution?

1. Consistency? (Balance)2. Race conditions?3. Deadlock?

Page 40: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Semaphore Solution?

1. Consistency? (Balance)2. Race conditions?3. Deadlock?

1. A[] (mc1.finished and mc2.finished) imply (accountA+accountB==200)2. E<> mc1.critical_section and mc2.critical_section3. A[] not (mc1.finished and mc2.finished) imply not deadlock

Page 41: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Semaphore Solution?

1. Consistency? (Balance)2. Race conditions?3. Deadlock?

1. A[] (mc1.finished and mc2.finished) imply (accountA+accountB==200)2. E<> mc1.critical_section and mc2.critical_section3. A[] not (mc1.finished and mc2.finished) imply not deadlock ÷

1. Consistency? (Balance)2. Race conditions?3. Deadlock?

Page 42: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Global shared variables:int accountA, accountB;

UppAal Network of Automata

Local variables:int a,b;Local control:

•system state=snapshot of each machines control location + local variables + global variables

mc1.control=requestB,mc1.a=0,mc1.b=0,mc2.control=requestB, mc2.a=0,mc2.b=0,bsem1.control=closed, bsem2.control=open, accountA=100,accountB=100

Local variables:int a,b;Local control:

Local control:

Local control:

wait_A

signal_A

wait_A

signal_A

wait_B

signal_Bwait_B

signal_B

wait_A

wait_B

signal_A

signal_B

Page 43: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Process Interaction• ! = Output, ? = Input• Handshake communication• Two-way

Coffee Machine Lecturer

University=Coffee Machine || Lecturer•LTS?•How many states?•Traces ?

4 states4 states

4 states:Interaction constrain overall behavior

synchronization results in internal actions

Page 44: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Broad-casts• chan coin, cof, cofBut;• broadcast chan join;

• sending: output join!• every automaton that listens to join moves• ie. every automaton with enabled “join?”

transition moves in one step• may be zero!

Page 45: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

Comitted Locations• Locations marked C

• No delay in committed location.• Next transition must involve automata in

committed location.

• Handy to model atomic sequences• The use of committed locations

reduces the number of states in a model, and allows for more space and time efficient analysis.

• S0 to s5 executed atomically

Page 46: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

The Cruise Controller

Engine

User

Controller

SpeedControl

CruiseControl

speedsetThrottle

engineOff, engineOn, acc, brakeon, off, resume

enableControl,disableControl, recordSpeed

Page 47: Introduction to FSM-modelling and testpeople.cs.aau.dk/~bnielsen/TOV07/material/fsm-intro.pdf · Introduction to FSM-modelling and test Brian Nielsen bnielsen@cs.auc.dk Department

END