-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_checker.py
More file actions
212 lines (191 loc) · 7.71 KB
/
Copy pathmodel_checker.py
File metadata and controls
212 lines (191 loc) · 7.71 KB
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
"""
Contains ModelChecker class and model checker testing arena
"""
import copy
from kripkeStructure import KripkeStructure
from node import Node
class ModelChecker:
"""
Given a Kripke structure K and a CTL formula ϕ, compute the set of states
Sϕ = { s | s is a state of K and K,s ⊨ ϕ }
"""
def __init__(self, formula_in_ast: Node, kripke_structure: KripkeStructure) -> None:
self.formula = formula_in_ast
self.kripke_structure = kripke_structure
self.types = {
"OR": 2,
"AND": 2,
"IMP": 2,
"NOT": 1,
"EG": 1,
"EU": 2,
"EX": 1,
"VAR": 0,
"T": 0,
}
self.postorder_traversal_for_model_checking(self.formula)
def postorder_traversal_for_model_checking(self, node: Node) -> None:
"""
traverse AST in postorder traversal.
If we are in var node, we will add the states
that satisfying that proposition and backtrack.
"""
if self.types[node.type] == 1:
if node.child is None:
raise TypeError("node.child's type should be Node, not None")
self.postorder_traversal_for_model_checking(node.child)
elif self.types[node.type] == 2:
if node.left is None:
raise TypeError("node.child's type should be Node, not None")
self.postorder_traversal_for_model_checking(node.left)
if node.right is None:
raise TypeError("node.right's type should be Node, not None")
self.postorder_traversal_for_model_checking(node.right)
self.fill_states(node)
def fill_states(self, node: Node) -> None:
"""
fill node.satisfying_state with states that satisfy that node (formula)
"""
if node.type == "T":
self.fill_t_states(node)
if node.type == "VAR":
self.fill_var_states(node)
elif node.type == "OR":
self.fill_or_states(node)
elif node.type == "AND":
self.fill_and_states(node)
elif node.type == "IMP":
self.fill_imp_states(node)
elif node.type == "NOT":
self.fill_not_states(node)
elif node.type == "EU":
self.fill_eu_states(node)
elif node.type == "EG":
self.fill_eg_states(node)
elif node.type == "EX":
self.fill_ex_states(node)
def fill_t_states(self, node: Node) -> None:
"""
add all states
"""
node.satisfying_states = copy.deepcopy(self.kripke_structure.states)
def fill_var_states(self, node: Node):
"""
go over all states of Kripke structure, find states
that satisfy the proposition of input node
"""
for state in self.kripke_structure.states:
if node.child in self.kripke_structure.labelling_function[state]:
node.satisfying_states.add(state)
def fill_or_states(self, node: Node) -> None:
"""
take union of satisfying states of left and right decendents
"""
if node.left is None:
raise TypeError("node.left is None, which is not allowed. ")
if node.right is None:
raise TypeError("node.right is None, which is not allowed. ")
node.satisfying_states = (
node.left.satisfying_states | node.right.satisfying_states
)
def fill_and_states(self, node: Node) -> None:
"""
take intersection of satisfying states of left and right decendents
"""
if node.left is None:
raise TypeError("node.left is None, which is not allowed. ")
if node.right is None:
raise TypeError("node.right is None, which is not allowed. ")
node.satisfying_states = (
node.left.satisfying_states & node.right.satisfying_states
)
def fill_not_states(self, node: Node) -> None:
"""
Find states that does not satisfy the child descendent
"""
if node.child is None:
raise TypeError("node.child is None, which is not allowed. ")
node.satisfying_states = (
set(self.kripke_structure.states) - node.child.satisfying_states
)
def fill_imp_states(self, node: Node) -> None:
"""
p -> q ≡ ¬p v q
"""
if node.left is None:
raise TypeError("node.left is None, which is not allowed. ")
if node.right is None:
raise TypeError("node.right is None, which is not allowed. ")
node.satisfying_states = (
set(self.kripke_structure.states) - node.left.satisfying_states
) | node.right.satisfying_states
def fill_eu_states(self, node: Node) -> None:
"""
E [ϕ ∪ Ψ] = Ψ v [ϕ ∧ EX E [ϕ ∪ Ψ]]
"""
if node.left is None:
raise TypeError("node.left is None, which is not allowed. ")
if node.right is None:
raise TypeError("node.right is None, which is not allowed. ")
# Initialisation
node.satisfying_states = copy.deepcopy(node.right.satisfying_states)
# Repeat
repeat = True
while repeat:
repeat = False
# for all states s of K :
for s in self.kripke_structure.states:
# if s ∈ Sϕ
if s in node.left.satisfying_states:
s_successors = self.kripke_structure.immediate_successor(s)
# if atleast one immediate successor s_ of s ∈ S_e ϕ ∪ Ψ
for s_ in s_successors:
# if s_ in node.satisfying_states
if s_ in node.satisfying_states:
if s not in node.satisfying_states:
node.satisfying_states.add(s)
repeat = True
# l = len(node.satisfying_states)
# node.satisfying_states.add(s)
# if l != len(node.satisfying_states):
# repeat = True
def fill_eg_states(self, node: Node) -> None:
"""
EG ϕ = ϕ ∧ EX EG ϕ
"""
if node.child is None:
raise TypeError("node.child is None, which is not allowed. ")
# Initialisation
# add all states satisfying ϕ in S EG ϕ
node.satisfying_states = copy.deepcopy(node.child.satisfying_states)
# repeat
repeat = True
while repeat:
repeat = False
# for all states s of K:
for s in self.kripke_structure.states:
# if s ∈ S EG ϕ
if s in node.satisfying_states:
s_successors = self.kripke_structure.immediate_successor(s)
# If no successors s_ ∈ S EG ϕ, remove s from S EG ϕ
for s_ in s_successors:
# if s_ in S EGϕ, nothing to remove. break from loop
if s_ in node.satisfying_states:
break
else:
node.satisfying_states.remove(s)
repeat = True
def fill_ex_states(self, node: Node) -> None:
"""
EX ϕ
"""
if node.child is None:
raise TypeError("node.child is None, which is not allowed. ")
# for all states s of K
for s in self.kripke_structure.states:
# if ∃ s' st s' ∈ Sϕ, add s to S EX ϕ
s_successors = self.kripke_structure.immediate_successor(s)
for s_ in s_successors:
if s_ in node.child.satisfying_states:
node.satisfying_states.add(s)
break