-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.py
202 lines (149 loc) · 6.58 KB
/
parse_test.py
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
import pyparsing as pp
import unittest
from parse import *
class TestIdentifiers(unittest.TestCase):
parser = build_identifier_parser()
def test_basics(self):
a = " abc123"
self.assertIn("abc123", self.parser.parseString(a).asList())
b = "_abc123 "
self.assertIn("_abc123", self.parser.parseString(b).asList())
c = "abc_123"
self.assertIn("abc_123", self.parser.parseString(c).asList())
d = "123_abc"
self.assertRaises(pp.ParseException, self.parser.parseString, d)
def test_exclude_boolean_lterals(self):
a = "True"
self.assertRaises(pp.ParseException, self.parser.parseString, a)
b = "False"
self.assertRaises(pp.ParseException, self.parser.parseString, b)
c = "Trueabc"
self.assertIn("Trueabc", self.parser.parseString(c).asList())
d = "abcFalse"
self.assertIn("abcFalse", self.parser.parseString(d).asList())
class TestPropositionalFormulae(unittest.TestCase):
parser = build_propositional_formula_parser()
def test_literals(self):
a = "True"
self.assertEqual(["True"], self.parser.parseString(a).asList())
b = "False"
self.assertEqual(["False"], self.parser.parseString(b).asList())
c = "abc"
self.assertEqual(["abc"], self.parser.parseString(c).asList())
def test_negations(self):
a = "!True"
self.assertEqual(["!", "True"], self.parser.parseString(a).asList())
b = "! False"
self.assertEqual(["!", "False"], self.parser.parseString(b).asList())
c = "!abc"
self.assertEqual(["!", "abc"], self.parser.parseString(c).asList())
d = "!!xyz"
self.assertEqual(["!", "!", "xyz"], self.parser.parseString(d).asList())
def test_ands(self):
a = "True && False"
self.assertEqual(["True", "&&", "False"], self.parser.parseString(a).asList())
b = "True&& abc"
self.assertEqual(["True", "&&", "abc"], self.parser.parseString(b).asList())
c = "abc &&False"
self.assertEqual(["abc", "&&", "False"], self.parser.parseString(c).asList())
d = "abc&&xyz"
self.assertEqual(["abc", "&&", "xyz"], self.parser.parseString(d).asList())
def test_ors(self):
a = "True || False"
self.assertEqual(["True", "||", "False"], self.parser.parseString(a).asList())
b = "True|| abc"
self.assertEqual(["True", "||", "abc"], self.parser.parseString(b).asList())
c = "abc ||False"
self.assertEqual(["abc", "||", "False"], self.parser.parseString(c).asList())
d = "abc||xyz"
self.assertEqual(["abc", "||", "xyz"], self.parser.parseString(d).asList())
def test_implications(self):
a = "True -> False"
self.assertEqual(["True", "->", "False"], self.parser.parseString(a).asList())
b = "True-> abc"
self.assertEqual(["True", "->", "abc"], self.parser.parseString(b).asList())
c = "abc ->False"
self.assertEqual(["abc", "->", "False"], self.parser.parseString(c).asList())
d = "abc->xyz"
self.assertEqual(["abc", "->", "xyz"], self.parser.parseString(d).asList())
def test_nesting(self):
a = "(!p)"
self.assertEqual([["!", "p"]], self.parser.parseString(a).asList())
b = "(!p"
self.assertRaises(pp.ParseException, self.parser.parseString, b)
c = "!p && q"
self.assertEqual(["!", "p", "&&", "q"], self.parser.parseString(c).asList())
d = "(p -> !q) && (q || !p)"
self.assertEqual([["p", "->", "!", "q"], "&&", ["q", "||", "!", "p"]], self.parser.parseString(d).asList())
e = "(((!p) && q) -> (p && (q || (!r))))"
self.assertEqual([[[["!", "p"], "&&", "q"], "->", ["p", "&&", ["q", "||", ["!", "r"]]]]], self.parser.parseString(e).asList())
class TestLTLFormulae(unittest.TestCase):
parser = build_LTL_formula_parser()
def test_unary(self):
a = "XTrue"
self.assertEqual(["X", "True"], self.parser.parseString(a).asList())
b = "F(!p)"
self.assertEqual(["F", ["!", "p"]], self.parser.parseString(b).asList())
c = "GFabc"
self.assertEqual(["G", "F", "abc"], self.parser.parseString(c).asList())
def test_binary(self):
a = "abc U True"
self.assertEqual(["abc", "U", "True"], self.parser.parseString(a).asList())
def test_more(self):
b = "p -> Fq"
self.assertEqual(["p", "->", "F", "q"], self.parser.parseString(b).asList())
c = "F(p->Gr)||((!q)Up)"
self.assertEqual(["F", ["p", "->", "G", "r"], "||", [["!", "q"], "U", "p"]], self.parser.parseString(c).asList())
d = "GFp->F(q||s)"
self.assertEqual(["G", "F", "p", "->", "F", ["q", "||", "s"]], self.parser.parseString(d).asList())
class TestGuardedCommand(unittest.TestCase):
parser = build_guarded_command_parser()
def test_examples(self):
a = "[] True ~> x := True, y := False"
self.assertEqual(["[]", "True", "~>", ["x", ":=", "True"], ["y", ":=", "False"]], self.parser.parseString(a).asList())
b = "[] (x && !y) ~> x:=False, y:=True"
self.assertEqual(["[]", ["x", "&&", "!", "y"], "~>", ["x", ":=", "False"], ["y", ":=", "True"]], self.parser.parseString(b).asList())
c = "[] (!x) ~> x:=True"
self.assertEqual(["[]", ["!", "x"], "~>", ["x", ":=", "True"]], self.parser.parseString(c).asList())
d = "[] True ~> u1 := True, d1 := False"
self.assertEqual(["[]", "True", "~>", ["u1", ":=", "True"], ["d1", ":=", "False"]], self.parser.parseString(d).asList())
class TestRMLFile(unittest.TestCase):
parser = build_RML_parser()
def test_toggle(self):
example = """
module toggle controls x,y
init
[] True ~> x := True, y := False
[] True ~> x := False, y := True
update
[] (x && !y) ~> x := False, y := True
[] (!x && y) ~> x := y, y := x
endmodule
"""
r = self.parser.parseString(example)
def test_peer_to_peer_communication(self):
example = """
module m0 controls u0, d0
init
[] True ~> u0 := True, d0 := False
[] True ~> u0 := False, d0 := True
update
[] True ~> u0 := True, d0 := False
[] True ~> u0 := False, d0 := True
goal
GF(d0 && u1)
endmodule
module m1 controls u1, d1
init
[] True ~> u1 := True, d1 := False
[] True ~> u1 := False, d1 := True
update
[] True ~> u1 := True, d1 := False
[] True ~> u1 := False, d1 := True
goal
GF(d1 && u0)
endmodule
"""
# print self.parser.parseString(example)[0]["update"][0]["condition_part"]
if __name__ == "__main__":
unittest.main()