Class: TruthTable

Inherits:
Object
  • Object
show all
Defined in:
lib/truthtable.rb,
lib/truthtable/qm.rb

Defined Under Namespace

Modules: QM

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&b) ⇒ TruthTable

Returns a new instance of TruthTable.



19
20
21
22
# File 'lib/truthtable.rb', line 19

def initialize(&b)
  table = TruthTable.test(&b)
  @table = table
end

Class Method Details

.test(&b) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/truthtable.rb', line 7

def self.test(&b)
  r = []
  o = TruthTableObject.new
  begin
    result = !!b.call(o)
    inputs = o.plan
    order = o.order
    r << [inputs, result, order]
  end while o.next_plan
  r
end

Instance Method Details

#all_namesObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/truthtable.rb', line 92

def all_names
  return @all_names if defined? @all_names
  @all_names = {}
  @table.each {|inputs, output, order|
    order.each {|name|
      if !@all_names.has_key?(name)
        @all_names[name] = @all_names.size
      end
    }
  }
  @all_names
end

#cnfObject

obtains a formula in conjunctive normal form.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/truthtable.rb', line 138

def cnf
  r = []
  @table.each {|inputs, output|
    return output.to_s if inputs.empty?
    next if output
    term = []
    each_input(inputs) {|name, input|
      if input
        term << "!#{name}"
      else
        term << name
      end
    }
    if term.length == 1
      r << term.join('|')
    else
      r << "(#{term.join('|')})"
    end
  }
  return "true" if r.empty?
  r.join(' & ')
end

#dnfObject

obtains a formula in disjunctive normal form.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/truthtable.rb', line 118

def dnf
  r = []
  @table.each {|inputs, output|
    return output.to_s if inputs.empty?
    next if !output
    term = []
    each_input(inputs) {|name, input|
      if input
        term << name
      else
        term << "!#{name}"
      end
    }
    r << term.join('&')
  }
  return "false" if r.empty?
  r.join(' | ')
end

#each_input(inputs) ⇒ Object



110
111
112
113
114
# File 'lib/truthtable.rb', line 110

def each_input(inputs)
  sort_names(inputs.keys).each {|name|
    yield name, inputs[name]
  }
end

#formulaObject

obtains a minimal formula using Quine-McCluskey algorithm.



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
# File 'lib/truthtable.rb', line 162

def formula
  input_names = all_names
  input_names_ary = sort_names(input_names.keys)
  tbl = {}
  @table.each {|inputs, output|
    return output.to_s if inputs.empty?
    inputs2 = [:x] * input_names.length
    inputs.each {|name, input|
      inputs2[input_names[name]] = input ? 1 : 0
    }
    tbl[inputs2] = output ? 1 : 0
  }
  qm = QM.qm(tbl)
  r = []
  qm.each {|term|
    t = []
    num_dontcare = 0
    term.each_with_index {|v, i|
      if v == false
        t << ("!" + input_names_ary[i])
      elsif v == true
        t << input_names_ary[i]
      else # :x
        num_dontcare += 1
      end
    }
    if num_dontcare == term.length
      r << 'true'
    else
      r << t.join('&')
    end
  }
  return "false" if r.empty?
  r.join(' | ')
end

#inspectObject

:stopdoc:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/truthtable.rb', line 57

def inspect
  result = "#<#{self.class}:"
  @table.each {|inputs, output, order|
    term = []
    each_input(inputs) {|name, input|
      if input
        term << name
      else
        term << "!#{name}"
      end
    }
    result << " #{term.join('&')}=>#{output}"
  }
  result << ">"
  result
end

#pretty_print(q) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/truthtable.rb', line 74

def pretty_print(q)
  q.object_group(self) {
    q.text ':'
    q.breakable
    q.seplist(@table, lambda { q.breakable('; ') }) {|inputs, output, order|
      term = []
      each_input(inputs) {|name, input|
        if input
          term << " #{name}"
        else
          term << "!#{name}"
        end
      }
      q.text "#{term.join('&')}=>#{output}"
    }
  }
end

#sort_names(names) ⇒ Object



105
106
107
108
# File 'lib/truthtable.rb', line 105

def sort_names(names)
  total_order = all_names
  names.sort_by {|n| total_order[n] }
end

#to_sObject



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
# File 'lib/truthtable.rb', line 24

def to_s
  r = ''
  names = sort_names(all_names.keys)
  format = ''
  sep = ''
  names.each {|name|
    format << "%-#{name.length}s "
    sep << '-' * (name.length+1)
  }
  format << "| %s\n"
  sep << "+--\n"
  r << sprintf(format, *(names + ['']))
  r << sep
  @table.each {|inputs, output, order|
    h = {}
    each_input(inputs) {|name, input|
      h[name] = input
    }
    args = []
    names.each {|name|
      if h.has_key? name
        args << (h[name] ? 't' : 'f').center(name.length)
      else
        args << '?'.center(name.length)
      end
    }
    args << (output ? 't' : 'f')
    r << sprintf(format, *args)
  }
  r
end