class Proposition

Attributes

sentence[RW]
variables[RW]

Public Class Methods

new(sentence) click to toggle source
# File lib/logic_analyzer/proposition.rb, line 8
def initialize(sentence)
  @sentence = syntax_sugars(sentence)

  @sentence.define_singleton_method(:words) do
    split(/\W+/).reject(&:empty?)
  end
end

Public Instance Methods

amount_of_variables() click to toggle source
# File lib/logic_analyzer/proposition.rb, line 52
def amount_of_variables
  2 ** variables.length
end
evaluate() click to toggle source
# File lib/logic_analyzer/proposition.rb, line 31
def evaluate
  context = LogicalContext.new
  values = []
  table = TruthTable.new(variables)
  table.generate_table

  for iteration in 0...amount_of_variables
    variables.each do |var|
      context.define_premise(var, table.truth_value_of(var, iteration))
    end

    values.append(context.truth_value(sentence))
  end

  if values.include?(true) && values.include?(false)
    raise Contingency
  else
    values.send(:all?)
  end
end
parse() click to toggle source
# File lib/logic_analyzer/proposition.rb, line 25
def parse
  @variables = sentence.words.reject do |var|
    %w[and or then if_and_only_if xor not].include?(var)
  end
end
syntax_sugars(sentence) click to toggle source
# File lib/logic_analyzer/proposition.rb, line 16
def syntax_sugars(sentence)
  sugared = sentence.gsub(/and|&&/, '.and')
                .gsub(/or|\|\|/, '.or')
                .gsub(/then|>/, '.then')
                .gsub(/if_and_only_if|<=>/, '.if_and_only_if')
                .gsub(/xor|!=/, '.xor')
                .gsub(/not/, '!')
end