AndTerm.initialize()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 4
rs 10
1
module PropLogic
2
  class AndTerm < Term
3
    def initialize(*terms)
4
      @terms = terms.map { |t| t.is_a?(AndTerm) ? t.terms : t }.flatten.freeze
5
      check_nnf_reduced
6
    end
7
8
    def to_s(in_term = false)
9
      str = @terms.map(&:to_s_in_term).join(' & ')
10
      in_term ? "( #{str} )" : str
11
    end
12
13
    def nnf?
14
      @is_nnf
15
    end
16
17
    def reduced?
18
      @is_reduced
19
    end
20
21
    def reduce
22
      return self if reduced?
23
      reduced_terms = @terms.map(&:reduce).uniq
24
      reduced_terms.reject!{|term| term.equal?(True)}
25
      return True if reduced_terms.empty?
26
      if reduced_terms.any?{|term| term.equal?(False)}
27
        False
28
      elsif reduced_terms.length == 1
29
        reduced_terms[0]
30
      else
31
        # detect contradicted terms
32
        not_terms = reduced_terms.select{|term| term.is_a?(NotTerm)}
33
        negated_terms = not_terms.map{|term| term.terms[0]}
34
        return False unless (negated_terms & reduced_terms).empty?
35
        Term.get self.class, *reduced_terms
36
      end
37
    end
38
39
    def cnf?
40
      return false unless reduced?
41
      @terms.all?(&:cnf?)
42
    end
43
44
    def to_cnf
45
      return super unless reduced?
46
      return self if cnf?
47
      pool = []
48
      without_pools = all_and(*@terms.map{|t| t.tseitin(pool)})
49
      all_and(without_pools, *pool)
50
    end
51
52
    def tseitin(pool)
53
      val = Variable.new
54
      terms = @terms.map{|t| t.cnf? ? t : t.tseitin(pool)}
55
      pool.concat terms.map{|t| ~val | t }
56
      val
57
    end
58
59
  end
60
end
61