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
-
#_only_bool(val, pos) ⇒ Boolean
Helper method to ensure a value is boolean.
-
#always(_fact, _maps, _fb) ⇒ Boolean
Always returns true, regardless of the fact.
-
#and(fact, maps, fb) ⇒ Boolean
Logical AND of multiple operands.
-
#and_or_simplify ⇒ Factbase::Term
Simplifies AND or OR expressions by removing duplicates.
-
#and_simplify ⇒ Factbase::Term
Simplifies AND expressions by removing duplicates.
-
#either(fact, maps, fb) ⇒ Object
Returns the first non-nil value or the second value.
-
#never(_fact, _maps, _fb) ⇒ Boolean
Always returns false, regardless of the fact.
-
#not(fact, maps, fb) ⇒ Boolean
Logical negation (NOT) of an operand.
-
#or(fact, maps, fb) ⇒ Boolean
Logical OR of multiple operands.
-
#or_simplify ⇒ Factbase::Term
Simplifies OR expressions by removing duplicates.
-
#when(fact, maps, fb) ⇒ Boolean
Logical implication (IF…THEN).
Instance Method Details
#_only_bool(val, pos) ⇒ Boolean
Helper method to ensure a value is 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
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
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_simplify ⇒ Factbase::Term
Simplifies AND or OR expressions by removing duplicates
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_simplify ⇒ Factbase::Term
Simplifies AND expressions by removing duplicates
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
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
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
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
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_simplify ⇒ Factbase::Term
Simplifies OR expressions by removing duplicates
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)
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 |