Class: Ravensat::PropLogic

Inherits:
Object
  • Object
show all
Defined in:
lib/ravensat/prop_logic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init_formula) ⇒ PropLogic

Returns a new instance of PropLogic.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ravensat/prop_logic.rb', line 6

def initialize( init_formula )
  @formula = init_formula
     # (a | b) & (~a | b) & (a | ~b) & (~a | ~b)

     # [:and,
     #    [:or, a, b],
     #    [:or, [:not, a], b],
     #    [:or, a, [:not, b]],
     #    [:or, [:not, a], [:not, b]]
     # ]
end

Instance Attribute Details

#formulaObject (readonly)

Returns the value of attribute formula.



3
4
5
# File 'lib/ravensat/prop_logic.rb', line 3

def formula
  @formula
end

#name_tableObject (readonly)

Returns the value of attribute name_table.



4
5
6
# File 'lib/ravensat/prop_logic.rb', line 4

def name_table
  @name_table
end

Instance Method Details

#&(object) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ravensat/prop_logic.rb', line 60

def &( object )
  if @formula.first == :and
    @formula.append object.formula
  else
    @formula = [:and, @formula, object.formula]
  end
  self
  # Ravensat::PropLogic.new [:and, @formula, object]
  # formulaがcnfである前提の実装
  # @formula & other_formula
end

#to_cnfObject



18
19
20
# File 'lib/ravensat/prop_logic.rb', line 18

def to_cnf

end

#to_dimacsObject



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
# File 'lib/ravensat/prop_logic.rb', line 22

def to_dimacs
  cnf_text = String.new
  tmp_name_table = @formula.flatten.uniq.reject{|e| e.class == Symbol}
  @name_table = tmp_name_table.zip((1..tmp_name_table.size).to_a.map{|e| e.to_s}).to_h

  nr_vars = tmp_name_table.size
  nr_clses = @formula.size - 1
  cnf_header = 'p cnf ' << nr_vars.to_s << ' ' << nr_clses.to_s << "\n"
  # @formula.is_cnf?
  @formula.each do |clause|
    # next if clause.first != :and
    case clause
    when Symbol
      next
    when Array
      case clause.first
      when :or
        clause.each do |literal|
          case literal
          when Symbol
            next
          when Ravensat::PropVar
            cnf_text << @name_table[literal] << ' '
          when Array #&& literal.first == :not
            cnf_text << '-' << @name_table[literal.last] << ' '
          end
        end
      when :not
        cnf_text << '-' << @name_table[clause.last] << ' '
      end
    when Ravensat::PropVar
      cnf_text << @name_table[clause] << ' '
    end
    cnf_text << '0' << "\n"
  end
  cnf_header + cnf_text
end

#|(object) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ravensat/prop_logic.rb', line 72

def |( object )
  if @formula.first == :or
    @formula.append object.formula
  else
    @formula = [:or, @formula, object.formula]
  end
  self
  # Ravensat::PropLogic.new [:or, @formula, object]
  # 木構造に落とすのが難しそう,実装見送り
  # @formula | other_formula
end