3
3
from utils import expr_handle_infix_ops , count , Symbol
4
4
5
5
6
+ def test_is_symbol ():
7
+ assert is_symbol ('x' )
8
+ assert is_symbol ('X' )
9
+ assert is_symbol ('N245' )
10
+ assert not is_symbol ('' )
11
+ assert not is_symbol ('1L' )
12
+ assert not is_symbol ([1 , 2 , 3 ])
13
+
14
+
15
+ def test_is_var_symbol ():
16
+ assert is_var_symbol ('xt' )
17
+ assert not is_var_symbol ('Txt' )
18
+ assert not is_var_symbol ('' )
19
+ assert not is_var_symbol ('52' )
20
+
21
+
22
+ def test_is_prop_symbol ():
23
+ assert not is_prop_symbol ('xt' )
24
+ assert is_prop_symbol ('Txt' )
25
+ assert not is_prop_symbol ('' )
26
+ assert not is_prop_symbol ('52' )
27
+
28
+
29
+ def test_variables ():
30
+ assert variables (expr ('F(x, x) & G(x, y) & H(y, z) & R(A, z, 2)' )) == {x , y , z }
31
+ assert variables (expr ('(x ==> y) & B(x, y) & A' )) == {x , y }
32
+
33
+
6
34
def test_expr ():
7
35
assert repr (expr ('P <=> Q(1)' )) == '(P <=> Q(1))'
8
36
assert repr (expr ('P & Q | ~R(x, F(x))' )) == '((P & Q) | ~R(x, F(x)))'
@@ -14,6 +42,10 @@ def test_extend():
14
42
assert extend ({x : 1 }, y , 2 ) == {x : 1 , y : 2 }
15
43
16
44
45
+ def test_subst ():
46
+ assert subst ({x : 42 , y :0 }, F (x ) + y ) == (F (42 ) + 0 )
47
+
48
+
17
49
def test_PropKB ():
18
50
kb = PropKB ()
19
51
assert count (kb .ask (expr ) for expr in [A , C , D , E , Q ]) is 0
@@ -68,7 +100,7 @@ def test_KB_wumpus():
68
100
assert kb_wumpus .ask (P [2 , 2 ] | P [3 , 1 ]) == {}
69
101
70
102
71
- def test_definite_clause ():
103
+ def test_is_definite_clause ():
72
104
assert is_definite_clause (expr ('A & B & C & D ==> E' ))
73
105
assert is_definite_clause (expr ('Farmer(Mac)' ))
74
106
assert not is_definite_clause (expr ('~Farmer(Mac)' ))
@@ -77,6 +109,12 @@ def test_definite_clause():
77
109
assert not is_definite_clause (expr ('(Farmer(f) | Rabbit(r)) ==> Hates(f, r)' ))
78
110
79
111
112
+ def test_parse_definite_clause ():
113
+ assert parse_definite_clause (expr ('A & B & C & D ==> E' )) == ([A , B , C , D ], E )
114
+ assert parse_definite_clause (expr ('Farmer(Mac)' )) == ([], expr ('Farmer(Mac)' ))
115
+ assert parse_definite_clause (expr ('(Farmer(f) & Rabbit(r)) ==> Hates(f, r)' )) == ([expr ('Farmer(f)' ), expr ('Rabbit(r)' )], expr ('Hates(f, r)' ))
116
+
117
+
80
118
def test_pl_true ():
81
119
assert pl_true (P , {}) is None
82
120
assert pl_true (P , {P : False }) is False
@@ -115,6 +153,22 @@ def test_dpll():
115
153
assert dpll_satisfiable (P & ~ P ) is False
116
154
117
155
156
+ def test_find_pure_symbol ():
157
+ assert find_pure_symbol ([A , B , C ], [A | ~ B ,~ B | ~ C ,C | A ]) == (A , True )
158
+ assert find_pure_symbol ([A , B , C ], [~ A | ~ B ,~ B | ~ C ,C | A ]) == (B , False )
159
+ assert find_pure_symbol ([A , B , C ], [~ A | B ,~ B | ~ C ,C | A ]) == (None , None )
160
+
161
+
162
+ def test_unit_clause_assign ():
163
+ assert unit_clause_assign (A | B | C , {A :True }) == (None , None )
164
+ assert unit_clause_assign (B | C , {A :True }) == (None , None )
165
+ assert unit_clause_assign (B | ~ A , {A :True }) == (B , True )
166
+
167
+
168
+ def test_find_unit_clause ():
169
+ assert find_unit_clause ([A | B | C , B | ~ C , ~ A | ~ B ], {A :True }) == (B , False )
170
+
171
+
118
172
def test_unify ():
119
173
assert unify (x , x , {}) == {}
120
174
assert unify (x , 3 , {}) == {x : 3 }
@@ -131,6 +185,11 @@ def test_tt_entails():
131
185
assert tt_entails (A & (B | C ) & E & F & ~ (P | Q ), A & E & F & ~ P & ~ Q )
132
186
133
187
188
+ def test_prop_symbols ():
189
+ assert set (prop_symbols (expr ('x & y & z | A' ))) == {A }
190
+ assert set (prop_symbols (expr ('(x & B(z)) ==> Farmer(y) | A' ))) == {A , expr ('Farmer(y)' ), expr ('B(z)' )}
191
+
192
+
134
193
def test_eliminate_implications ():
135
194
assert repr (eliminate_implications ('A ==> (~B <== C)' )) == '((~B | ~C) | ~A)'
136
195
assert repr (eliminate_implications (A ^ B )) == '((A & ~B) | (~A & B))'
@@ -156,6 +215,18 @@ def test_move_not_inwards():
156
215
assert repr (move_not_inwards (~ (~ (A | ~ B ) | ~ ~ C ))) == '((A | ~B) & ~C)'
157
216
158
217
218
+ def test_distribute_and_over_or ():
219
+ def test_enatilment (s , has_and = False ):
220
+ result = distribute_and_over_or (s )
221
+ if has_and :
222
+ assert result .op == '&'
223
+ assert tt_entails (s , result )
224
+ assert tt_entails (result , s )
225
+ test_enatilment ((A & B ) | C , True )
226
+ test_enatilment ((A | B ) & C , True )
227
+ test_enatilment ((A | B ) | C , False )
228
+ test_enatilment ((A & B ) | (C | D ), True )
229
+
159
230
def test_to_cnf ():
160
231
assert (repr (to_cnf (wumpus_world_inference & ~ expr ('~P12' ))) ==
161
232
"((~P12 | B11) & (~P21 | B11) & (P12 | P21 | ~B11) & ~B11 & P12)" )
0 commit comments