Module: Factbase::Logical

Included in:
Term
Defined in:
lib/factbase/terms/logical.rb

Overview

Logical terms.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Instance Method Details

#_only_bool(val, pos) ⇒ Boolean

Helper method to ensure a value is boolean

Parameters:

  • val (Object)

    The value to check

  • pos (Integer)

    The position of the operand

Returns:

  • (Boolean)

    The boolean value

Raises:

  • (RuntimeError)

    If value is not a boolean



118
119
120
121
122
123
# File 'lib/factbase/terms/logical.rb', line 118

def _only_bool(val, pos)
  val = val[0] if val.respond_to?(:each)
  return false if val.nil?
  return val if val.is_a?(TrueClass) || val.is_a?(FalseClass)
  raise "Boolean expected, while #{val.class} received from #{@operands[pos]}"
end

#always(_fact, _maps, _fb) ⇒ Boolean

Always returns true, regardless of the fact

Parameters:

Returns:

  • (Boolean)

    Always returns true



18
19
20
21
# File 'lib/factbase/terms/logical.rb', line 18

def always(_fact, _maps, _fb)
  assert_args(0)
  true
end

#and(fact, maps, fb) ⇒ Boolean

Logical AND of multiple operands

Parameters:

Returns:

  • (Boolean)

    True if all operands evaluate to true, false otherwise



56
57
58
59
60
61
# File 'lib/factbase/terms/logical.rb', line 56

def and(fact, maps, fb)
  (0..(@operands.size - 1)).each do |i|
    return false unless _only_bool(_values(i, fact, maps, fb), i)
  end
  true
end

#and_or_simplifyFactbase::Term

Simplifies AND or OR expressions by removing duplicates

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/factbase/terms/logical.rb', line 87

def and_or_simplify
  strs = []
  ops = []
  @operands.each do |o|
    o = o.simplify
    s = o.to_s
    next if strs.include?(s)
    strs << s
    ops << o
  end
  return ops[0] if ops.size == 1
  self.class.new(@op, ops)
end

#and_simplifyFactbase::Term

Simplifies AND expressions by removing duplicates

Returns:



103
104
105
# File 'lib/factbase/terms/logical.rb', line 103

def and_simplify
  and_or_simplify
end

#either(fact, maps, fb) ⇒ Object

Returns the first non-nil value or the second value

Parameters:

Returns:

  • (Object)

    First operand if not nil, otherwise second operand



78
79
80
81
82
83
# File 'lib/factbase/terms/logical.rb', line 78

def either(fact, maps, fb)
  assert_args(2)
  v = _values(0, fact, maps, fb)
  return v unless v.nil?
  _values(1, fact, maps, fb)
end

#never(_fact, _maps, _fb) ⇒ Boolean

Always returns false, regardless of the fact

Parameters:

Returns:

  • (Boolean)

    Always returns false



27
28
29
30
# File 'lib/factbase/terms/logical.rb', line 27

def never(_fact, _maps, _fb)
  assert_args(0)
  false
end

#not(fact, maps, fb) ⇒ Boolean

Logical negation (NOT) of an operand

Parameters:

Returns:

  • (Boolean)

    Negated boolean result of the operand



36
37
38
39
# File 'lib/factbase/terms/logical.rb', line 36

def not(fact, maps, fb)
  assert_args(1)
  !_only_bool(_values(0, fact, maps, fb), 0)
end

#or(fact, maps, fb) ⇒ Boolean

Logical OR of multiple operands

Parameters:

Returns:

  • (Boolean)

    True if any operand evaluates to true, false otherwise



45
46
47
48
49
50
# File 'lib/factbase/terms/logical.rb', line 45

def or(fact, maps, fb)
  (0..(@operands.size - 1)).each do |i|
    return true if _only_bool(_values(i, fact, maps, fb), i)
  end
  false
end

#or_simplifyFactbase::Term

Simplifies OR expressions by removing duplicates

Returns:



109
110
111
# File 'lib/factbase/terms/logical.rb', line 109

def or_simplify
  and_or_simplify
end

#when(fact, maps, fb) ⇒ Boolean

Logical implication (IF…THEN)

Parameters:

Returns:

  • (Boolean)

    True if first operand is false OR both are true



67
68
69
70
71
72
# File 'lib/factbase/terms/logical.rb', line 67

def when(fact, maps, fb)
  assert_args(2)
  a = @operands[0]
  b = @operands[1]
  !a.evaluate(fact, maps, fb) || (a.evaluate(fact, maps, fb) && b.evaluate(fact, maps, fb))
end