Class: Factbase::TermOnce

Inherits:
Term
  • Object
show all
Defined in:
lib/factbase/term_once.rb

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

#op, #operands

Instance Method Summary collapse

Methods included from Factbase::Term::Debug

#traced

Methods included from Factbase::Term::System

#env

Methods included from Factbase::Term::Defn

#defn, #undef

Methods included from Factbase::Term::Ordering

#prev, #unique

Methods included from Factbase::Term::Aliases

#as, #join

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

#concat, #matches, #sprintf

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.

Parameters:



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?

Returns:

  • (Boolean)

    TRUE if abstract



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?

Parameters:

Returns:

  • (bool)

    TRUE if matches



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

#simplifyFactbase::Term

Simplify it if possible.

Returns:



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.

Returns:

  • (Boolean)

    TRUE if static



68
69
70
# File 'lib/factbase/term_once.rb', line 68

def static?
  @term.static?
end

#to_sString

Turns it into a string.

Returns:

  • (String)

    The string of it



81
82
83
# File 'lib/factbase/term_once.rb', line 81

def to_s
  @term.to_s
end