Class: DecisionAgent::Dmn::CachedDmnEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/decision_agent/dmn/cache.rb

Overview

Enhanced DMN Evaluator with Caching

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dmn_model:, decision_id:, cache: nil, enable_caching: true) ⇒ CachedDmnEvaluator

Returns a new instance of CachedDmnEvaluator.



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/decision_agent/dmn/cache.rb', line 160

def initialize(dmn_model:, decision_id:, cache: nil, enable_caching: true)
  @dmn_model = dmn_model
  @decision_id = decision_id
  @cache = cache || EvaluationCache.new
  @enable_caching = enable_caching

  # Create the underlying evaluator
  @evaluator = Evaluators::DmnEvaluator.new(
    dmn_model: dmn_model,
    decision_id: decision_id
  )
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



158
159
160
# File 'lib/decision_agent/dmn/cache.rb', line 158

def cache
  @cache
end

#evaluatorObject (readonly)

Returns the value of attribute evaluator.



158
159
160
# File 'lib/decision_agent/dmn/cache.rb', line 158

def evaluator
  @evaluator
end

Instance Method Details

#cache_statsObject

Get cache statistics



200
201
202
# File 'lib/decision_agent/dmn/cache.rb', line 200

def cache_stats
  @cache.statistics
end

#clear_cacheObject

Clear cache



205
206
207
# File 'lib/decision_agent/dmn/cache.rb', line 205

def clear_cache
  @cache.clear_results
end

#evaluate(context:) ⇒ Object

Evaluate with caching



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/decision_agent/dmn/cache.rb', line 174

def evaluate(context:)
  return @evaluator.evaluate(context: context) unless @enable_caching

  # Generate context hash for cache key
  context_hash = generate_context_hash(context)

  # Try to get cached result
  cached_result = @cache.get_result(@decision_id, context_hash)
  return cached_result if cached_result

  # Evaluate and cache result
  result = @evaluator.evaluate(context: context)
  @cache.cache_result(@decision_id, context_hash, result)

  result
end

#warm_cache(input_samples) ⇒ Object

Warm up cache with common inputs



192
193
194
195
196
197
# File 'lib/decision_agent/dmn/cache.rb', line 192

def warm_cache(input_samples)
  input_samples.each do |inputs|
    context = Context.new(inputs)
    evaluate(context: context)
  end
end