Class: Factbase::Compare

Inherits:
TermBase show all
Defined in:
lib/factbase/terms/compare.rb

Overview

Compares two values using a specified operation.

Instance Method Summary collapse

Methods included from TermShared

#to_s

Constructor Details

#initialize(operation, operands) ⇒ Compare

Constructor.

Parameters:

  • operation (Symbol)

    Operation to perform, e.g. :>, :<, :<=, :>=, :==

  • operands (Array)

    Operands



13
14
15
16
17
# File 'lib/factbase/terms/compare.rb', line 13

def initialize(operation, operands)
  super()
  @op = operation
  @operands = operands
end

Instance Method Details

#evaluate(fact, maps, fb) ⇒ Boolean

Evaluate term on a fact.

Parameters:

Returns:

  • (Boolean)

    The result of the comparison



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/factbase/terms/compare.rb', line 24

def evaluate(fact, maps, fb)
  assert_args(2)
  lefts = _values(0, fact, maps, fb)
  return false if lefts.nil?
  rights = _values(1, fact, maps, fb)
  return false if rights.nil?
  lefts.any? do |l|
    l = l.floor if l.is_a?(Time) && @op == :==
    rights.any? do |r|
      r = r.floor if r.is_a?(Time) && @op == :==
      l.send(@op, r)
    end
  end
end