Total Complexity | 20 |
Total Lines | 68 |
Duplicated Lines | 0 % |
1 | module PropLogic |
||
2 | class NotTerm < Term |
||
3 | def initialize(term) |
||
4 | @terms = [term].freeze |
||
5 | @is_nnf = @terms[0].is_a?(Variable) |
||
6 | @is_reduced = @is_nnf && ! (@terms[0].is_a?(Constant)) |
||
7 | end |
||
8 | |||
9 | def to_s(*) |
||
10 | "~" + @terms[0].to_s(true) |
||
11 | end |
||
12 | |||
13 | def nnf? |
||
14 | @is_nnf |
||
15 | end |
||
16 | |||
17 | def to_nnf |
||
18 | term = @terms[0] |
||
19 | case term |
||
20 | when NotTerm |
||
21 | term.terms[0].to_nnf |
||
22 | when Variable |
||
23 | self |
||
24 | when ThenTerm |
||
25 | (~(term.to_nnf)).to_nnf |
||
26 | when AndTerm |
||
27 | all_or(*term.terms.map{|t| (~t).to_nnf}) |
||
28 | when OrTerm |
||
29 | all_and(*term.terms.map{|t| (~t).to_nnf}) |
||
30 | end |
||
31 | end |
||
32 | |||
33 | def reduced? |
||
34 | @is_reduced |
||
35 | end |
||
36 | |||
37 | def reduce |
||
38 | return self if reduced? |
||
39 | reduced_term = @terms[0].reduce |
||
40 | case reduced_term |
||
41 | when TrueConstant |
||
42 | False |
||
43 | when FalseConstant |
||
44 | True |
||
45 | else |
||
46 | (~reduced_term).to_nnf |
||
47 | end |
||
48 | end |
||
49 | |||
50 | def to_cnf |
||
51 | if reduced? |
||
52 | self |
||
53 | else |
||
54 | super |
||
55 | end |
||
56 | end |
||
57 | |||
58 | def tseitin(pool) |
||
59 | if nnf? |
||
60 | self |
||
61 | elsif @terms[0].is_a?(NotTerm) && @terms[0].terms[0].is_a(Variable) |
||
62 | @terms[0].terms[0] |
||
63 | else |
||
64 | raise 'Non-NNF terms cannot be converted to Tseitin form.' + self.to_s |
||
65 | end |
||
66 | end |
||
67 | |||
68 | alias_method :cnf?, :reduced? |
||
69 | end |
||
71 |