Class: AdaTruthy::TruthTable

Inherits:
Object
  • Object
show all
Defined in:
lib/ada_truthy/truth_table/truth_table.rb

Constant Summary collapse

MAX_LIMIT =
6
MIN_LIMIT =
2
SELF =
'S'
COMPLEMENT =
'C'
TRUE_VALUE =
'T'
FALSE_VALUE =
'F'
@@letters =
%w[A B C D E F G H I J]
@@combinations =
{
  2 => 04, 3 => 8, 4 => 16,
  5 => 32, 6 => 64, 7 => 128,
  8 => 256, 9 => 512, 10 => 1024
}

Instance Method Summary collapse

Constructor Details

#initialize(number_of_terms) ⇒ TruthTable

Returns a new instance of TruthTable.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ada_truthy/truth_table/truth_table.rb', line 21

def initialize(number_of_terms)
  case number_of_terms
  when number_of_terms > MAX_LIMIT
    raise TruthyException, "The maximum number of terms is #{MAX_LIMIT}"
  when number_of_terms < MIN_LIMIT
    raise TruthyException, "The minimum number of terms is #{MIN_LIMIT}"
  end

  @number_of_terms = number_of_terms
  @number_of_combinations = @@combinations[number_of_terms]

  @cache = Hash.new
  @formula = Array.new
  @rows = Array.new

  @using_sum_of_products = true
  @using_product_of_sums = false

  @number_of_zeros = 0
  @number_of_ones = 0
end

Instance Method Details

#add_row(*terms) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ada_truthy/truth_table/truth_table.rb', line 43

def add_row(*terms)
  table_has_enough_rows = @rows.size == @number_of_combinations

  if table_has_enough_rows
    raise TruthyException, "There should be only #{@number_of_combinations} rows."
  end

  right_number_of_terms = terms.size == @number_of_terms + 1

  unless right_number_of_terms
    raise TruthyException, "There should be only #{@number_of_terms} terms."
  end

  row = terms

  unless row_is_valid? row
    raise TruthyException, "The combination #{row} has been used already."
  end

  @cache = {}

  row_output = row[-1]
  change_algorithm = (@rows.size == 0).and(row_output == 0)

  use_product_of_sums if change_algorithm

  @rows << row
  (row_output == 1) ? @number_of_ones += 1 : @number_of_zeros += 1

  compute row
end

#check(*terms) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ada_truthy/truth_table/truth_table.rb', line 75

def check(*terms)
  has_right_number_of_terms = terms.size == @number_of_terms

  unless has_right_number_of_terms
    raise TruthyException, "There should be only #{@number_of_terms} terms."
  end

  change_algorithm_if_needed

  cache_code = TruthTable.get_row_cache_code terms
  cache_result = @cache[cache_code]

  return cache_result unless cache_result.nil?

  terms_cursor = 0
  partial_result = true
  result = false

  if @using_product_of_sums
    partial_result = false
    result = true
  end

  @formula.each do |t|
    if @using_sum_of_products
      partial_result = (t == SELF ? partial_result.and(terms[terms_cursor]) :
                          partial_result.and(!terms[terms_cursor]))
    else
      partial_result = (t == SELF ? partial_result.or(terms[terms_cursor]) :
                          partial_result.or(!terms[terms_cursor]))
    end

    terms_cursor += 1
    next if terms_cursor != @number_of_terms
    terms_cursor = 0

    if (@using_sum_of_products)
      return true if partial_result
      result = result.or(partial_result)
      partial_result = true
    else
      return false if not partial_result
      result = result.and(partial_result)
      partial_result = false
    end
  end

  @cache[cache_code] = result
  return result
end

#to_sObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ada_truthy/truth_table/truth_table.rb', line 126

def to_s
  terms_cursor = 0
  partial_result = ''
  result = ''

  @formula.each do |t|
    if @using_sum_of_products
      partial_result += (t == SELF ? "#{@@letters[terms_cursor]}." : "~#{@@letters[terms_cursor]}.")
    else
      partial_result += (t == SELF ? "#{@@letters[terms_cursor]}+" : "~#{@@letters[terms_cursor]}+")
    end

    terms_cursor += 1
    next if terms_cursor != @number_of_terms
    terms_cursor = 0

    result += (@using_sum_of_products ? "(#{partial_result[0...-1]})+" : "(#{partial_result[0...-1]}).")
    partial_result = ''
  end

  result[0...-1]
end