Class: Factbase::TermOnce
Overview
Term with a cache, a decorator of another term.
It is NOT thread-safe!
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024-2025 Yegor Bugayenko
- License
-
MIT
Instance Attribute Summary
Attributes inherited from Term
Instance Method Summary collapse
-
#abstract? ⇒ Boolean
Does it have any variables (+$foo+, for example) inside?.
-
#evaluate(fact, maps) ⇒ bool
Does it match the fact?.
-
#initialize(term, cache) ⇒ TermOnce
constructor
Constructor.
-
#simplify ⇒ Factbase::Term
Simplify it if possible.
-
#static? ⇒ Boolean
Does it have any dependencies on a fact?.
-
#to_s ⇒ String
Turns it into a string.
Methods included from Factbase::Term::Debug
Methods included from Factbase::Term::System
Methods included from Factbase::Term::Defn
Methods included from Factbase::Term::Ordering
Methods included from Factbase::Term::Aliases
Methods included from Factbase::Term::Meta
#absent, #exists, #many, #nil, #one, #size, #type
Methods included from Factbase::Term::Casting
#to_float, #to_integer, #to_string, #to_time
Methods included from Factbase::Term::Strings
Methods included from Factbase::Term::Aggregates
#agg, #best, #count, #empty, #first, #max, #min, #nth, #sum
Methods included from Factbase::Term::Logical
#_only_bool, #always, #and, #and_or_simplify, #and_simplify, #either, #never, #not, #or, #or_simplify, #when
Methods included from Factbase::Term::Math
#arithmetic, #cmp, #div, #eq, #gt, #gte, #lt, #lte, #minus, #plus, #times, #zero
Constructor Details
#initialize(term, cache) ⇒ TermOnce
Constructor.
36 37 38 39 40 41 42 |
# File 'lib/factbase/term_once.rb', line 36 def initialize(term, cache) super(nil, nil, nil) # just to please Rubocop @term = term @cache = cache @text = @term.to_s @cacheable = @term.static? && !@term.abstract? end |
Instance Method Details
#abstract? ⇒ Boolean
Does it have any variables (+$foo+, for example) inside?
75 76 77 |
# File 'lib/factbase/term_once.rb', line 75 def abstract? @term.abstract? end |
#evaluate(fact, maps) ⇒ bool
Does it match the fact?
48 49 50 51 52 53 54 |
# File 'lib/factbase/term_once.rb', line 48 def evaluate(fact, maps) return @term.evaluate(fact, maps) unless @cacheable key = [@text, maps.object_id] before = @cache[key] @cache[key] = @term.evaluate(nil, maps) if before.nil? @cache[key] end |
#simplify ⇒ Factbase::Term
Simplify it if possible.
58 59 60 |
# File 'lib/factbase/term_once.rb', line 58 def simplify @term.simplify end |
#static? ⇒ Boolean
Does it have any dependencies on a fact?
If a term is static, it will return the same value for evaluate, no matter what is the fact given.
68 69 70 |
# File 'lib/factbase/term_once.rb', line 68 def static? @term.static? end |
#to_s ⇒ String
Turns it into a string.
81 82 83 |
# File 'lib/factbase/term_once.rb', line 81 def to_s @term.to_s end |