Murphi入门(三)—— 可达集范例讲解(2)

  • 延续上篇可达集范例讲解,本篇将用图解的方式,继续加深对可达集的理解

本文范例选用 互斥协议。

本文的mutualEx.m文件可以在cmurphi/ex/toy中找到

代码及结果

mutualEx

程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const clientNUMS : 2;
type state : enum{I, T, C, E};

client: 1..clientNUMS;

var n : array [client] of state;

x : boolean;

ruleset i : client do
rule "Try" n[i] = I ==> begin
n[i] := T;endrule;

rule "Crit"
n[i] = T& x = true ==>begin
n[i] := C; x := false; endrule;

rule "Exit"
n[i] = C ==>begin
n[i] := E;endrule;


rule "Idle"
n[i] = E ==> begin n[i] := I;
x := true;endrule;
endruleset;

startstate
begin
for i: client do
n[i] := I;
endfor;
x := true;
endstartstate;

ruleset i:client; j: client do
invariant "coherence"
i != j -> (n[i] = C -> n[j] != C);
endruleset;

结果部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
Verbose option selected.  The following is the detailed progress.

Firing startstate Startstate 0
Obtained state:
n[1]:I
n[2]:I
x:true

------------------------------
Unpacking state from queue:
n[1]:I
n[2]:I
x:true

The following next states are obtained:

Firing rule Try, i:1
Obtained state:
n[1]:T
n[2]:I
x:true

Firing rule Try, i:2
Obtained state:
n[1]:I
n[2]:T
x:true

------------------------------
Unpacking state from queue:
n[1]:T
n[2]:I
x:true

The following next states are obtained:

Firing rule Crit, i:1
Obtained state:
n[1]:C
n[2]:I
x:false

Firing rule Try, i:2
Obtained state:
n[1]:T
n[2]:T
x:true

------------------------------
Unpacking state from queue:
n[1]:I
n[2]:T
x:true

The following next states are obtained:

Firing rule Crit, i:2
Obtained state:
n[1]:I
n[2]:C
x:false

Firing rule Try, i:1
Obtained state:
n[1]:T
n[2]:T
x:true

------------------------------
Unpacking state from queue:
n[1]:C
n[2]:I
x:false

The following next states are obtained:

Firing rule Exit, i:1
Obtained state:
n[1]:E
n[2]:I
x:false

Firing rule Try, i:2
Obtained state:
n[1]:C
n[2]:T
x:false

------------------------------
Unpacking state from queue:
n[1]:T
n[2]:T
x:true

The following next states are obtained:

Firing rule Crit, i:1
Obtained state:
n[1]:C
n[2]:T
x:false

Firing rule Crit, i:2
Obtained state:
n[1]:T
n[2]:C
x:false

------------------------------
Unpacking state from queue:
n[1]:I
n[2]:C
x:false

The following next states are obtained:

Firing rule Exit, i:2
Obtained state:
n[1]:I
n[2]:E
x:false

Firing rule Try, i:1
Obtained state:
n[1]:T
n[2]:C
x:false

------------------------------
Unpacking state from queue:
n[1]:E
n[2]:I
x:false

The following next states are obtained:

Firing rule Idle, i:1
Obtained state:
n[1]:I
n[2]:I
x:true

Firing rule Try, i:2
Obtained state:
n[1]:E
n[2]:T
x:false

------------------------------
Unpacking state from queue:
n[1]:C
n[2]:T
x:false

The following next states are obtained:

Firing rule Exit, i:1
Obtained state:
n[1]:E
n[2]:T
x:false

------------------------------
Unpacking state from queue:
n[1]:T
n[2]:C
x:false

The following next states are obtained:

Firing rule Exit, i:2
Obtained state:
n[1]:T
n[2]:E
x:false

------------------------------
Unpacking state from queue:
n[1]:I
n[2]:E
x:false

The following next states are obtained:

Firing rule Idle, i:2
Obtained state:
n[1]:I
n[2]:I
x:true

Firing rule Try, i:1
Obtained state:
n[1]:T
n[2]:E
x:false

------------------------------
Unpacking state from queue:
n[1]:E
n[2]:T
x:false

The following next states are obtained:

Firing rule Idle, i:1
Obtained state:
n[1]:I
n[2]:T
x:true

------------------------------
Unpacking state from queue:
n[1]:T
n[2]:E
x:false

The following next states are obtained:

Firing rule Idle, i:2
Obtained state:
n[1]:T
n[2]:I
x:true


==========================================================================

Status:

No error found.

State Space Explored:

12 states, 20 rules fired in 0.10s.

mutualExFsm

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
------------------------------------------
const, type, var are the same
------------------------------------------


rule "fsm" true ==> begin

if (n[i]= I) then n[i]:=T;
elsif (n[i]= T) then if x=true then n[i] := C; x := false; endif;
elsif (n[i]= C) then n[i]:=E;
else n[i]:=I; x:=true;
endif
endrule

endruleset;

------------------------------------------
startstate, invariant are the same
------------------------------------------

结果部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
Verbose option selected.  The following is the detailed progress.

Firing startstate Startstate 0
Obtained state:
n[1]:I
n[2]:I
x:true

------------------------------
Unpacking state from queue:
n[1]:I
n[2]:I
x:true

The following next states are obtained:

Firing rule fsm, i:1
Obtained state:
n[1]:T
n[2]:I
x:true

Firing rule fsm, i:2
Obtained state:
n[1]:I
n[2]:T
x:true

------------------------------
Unpacking state from queue:
n[1]:T
n[2]:I
x:true

The following next states are obtained:

Firing rule fsm, i:1
Obtained state:
n[1]:C
n[2]:I
x:false

Firing rule fsm, i:2
Obtained state:
n[1]:T
n[2]:T
x:true

------------------------------
Unpacking state from queue:
n[1]:I
n[2]:T
x:true

The following next states are obtained:

Firing rule fsm, i:1
Obtained state:
n[1]:T
n[2]:T
x:true

Firing rule fsm, i:2
Obtained state:
n[1]:I
n[2]:C
x:false

------------------------------
Unpacking state from queue:
n[1]:C
n[2]:I
x:false

The following next states are obtained:

Firing rule fsm, i:1
Obtained state:
n[1]:E
n[2]:I
x:false

Firing rule fsm, i:2
Obtained state:
n[1]:C
n[2]:T
x:false

------------------------------
Unpacking state from queue:
n[1]:T
n[2]:T
x:true

The following next states are obtained:

Firing rule fsm, i:1
Obtained state:
n[1]:C
n[2]:T
x:false

Firing rule fsm, i:2
Obtained state:
n[1]:T
n[2]:C
x:false

------------------------------
Unpacking state from queue:
n[1]:I
n[2]:C
x:false

The following next states are obtained:

Firing rule fsm, i:1
Obtained state:
n[1]:T
n[2]:C
x:false

Firing rule fsm, i:2
Obtained state:
n[1]:I
n[2]:E
x:false

------------------------------
Unpacking state from queue:
n[1]:E
n[2]:I
x:false

The following next states are obtained:

Firing rule fsm, i:1
Obtained state:
n[1]:I
n[2]:I
x:true

Firing rule fsm, i:2
Obtained state:
n[1]:E
n[2]:T
x:false

------------------------------
Unpacking state from queue:
n[1]:C
n[2]:T
x:false

The following next states are obtained:

Firing rule fsm, i:1
Obtained state:
n[1]:E
n[2]:T
x:false

Firing rule fsm, i:2
Obtained state:
n[1]:C
n[2]:T
x:false

------------------------------
Unpacking state from queue:
n[1]:T
n[2]:C
x:false

The following next states are obtained:

Firing rule fsm, i:1
Obtained state:
n[1]:T
n[2]:C
x:false

Firing rule fsm, i:2
Obtained state:
n[1]:T
n[2]:E
x:false

------------------------------
Unpacking state from queue:
n[1]:I
n[2]:E
x:false

The following next states are obtained:

Firing rule fsm, i:1
Obtained state:
n[1]:T
n[2]:E
x:false

Firing rule fsm, i:2
Obtained state:
n[1]:I
n[2]:I
x:true

------------------------------
Unpacking state from queue:
n[1]:E
n[2]:T
x:false

The following next states are obtained:

Firing rule fsm, i:1
Obtained state:
n[1]:I
n[2]:T
x:true

Firing rule fsm, i:2
Obtained state:
n[1]:E
n[2]:T
x:false

------------------------------
Unpacking state from queue:
n[1]:T
n[2]:E
x:false

The following next states are obtained:

Firing rule fsm, i:1
Obtained state:
n[1]:T
n[2]:E
x:false

Firing rule fsm, i:2
Obtained state:
n[1]:T
n[2]:I
x:true


==========================================================================

Status:

No error found.

State Space Explored:

12 states, 24 rules fired in 0.10s.

图解

mutualEx通过看murphi程序可以观察到,它是需要满足一些条件,才能进入执行,并且每一步执行,都不可能到达本身。

而mutualExFsm则只有一条规则,并且无条件可执行,进入动作内部,再进行判断。并且通过if...else语句可以发现,当状态为T时,只有当x = true 时,才会执行,否则该动作不做改变,直接退出。这样就导致本身的后继可以包括本身。

不多说,上图。

mutualEx

mutualExFsm

可以看到,标红的地方,是mutualExFsm与mutualEx的不同之处。这就是为什么mutualEx只需要执行20条规则,而mutualExFsm需要执行24条规则的原因。