Class: DecisionAgent::Evaluators::DmnEvaluator

Inherits:
Base
  • Object
show all
Defined in:
lib/decision_agent/evaluators/dmn_evaluator.rb

Overview

Evaluates DMN decision models

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, decision_id:, name: nil) ⇒ DmnEvaluator

Returns a new instance of DmnEvaluator.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/decision_agent/evaluators/dmn_evaluator.rb', line 14

def initialize(model:, decision_id:, name: nil)
  @model = model
  @decision_id = decision_id.to_s
  @name = name || "DmnEvaluator(#{@decision_id})"

  # Find and validate decision
  @decision = @model.find_decision(@decision_id)
  raise Dmn::InvalidDmnModelError, "Decision '#{@decision_id}' not found" unless @decision
  raise Dmn::InvalidDmnModelError, "Decision '#{@decision_id}' has no decision table" unless @decision.decision_table

  # Convert to JSON rules for execution
  adapter = Dmn::Adapter.new(@decision.decision_table)
  @rules_json = adapter.to_json_rules

  # Create internal JSON rule evaluator
  @json_evaluator = JsonRuleEvaluator.new(
    rules_json: @rules_json,
    name: @name
  )

  # Freeze for thread safety
  @model.freeze
  @decision_id.freeze
  @name.freeze
  freeze
end

Instance Attribute Details

#decision_idObject (readonly)

Returns the value of attribute decision_id.



12
13
14
# File 'lib/decision_agent/evaluators/dmn_evaluator.rb', line 12

def decision_id
  @decision_id
end

#modelObject (readonly)

Returns the value of attribute model.



12
13
14
# File 'lib/decision_agent/evaluators/dmn_evaluator.rb', line 12

def model
  @model
end

Instance Method Details

#evaluate(context, feedback: {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/decision_agent/evaluators/dmn_evaluator.rb', line 41

def evaluate(context, feedback: {})
  hit_policy = @decision.decision_table.hit_policy

  # Collect explainability traces
  explainability_result = collect_explainability(context, hit_policy)

  # Short-circuit for FIRST and PRIORITY policies
  if %w[FIRST PRIORITY].include?(hit_policy)
    first_match = find_first_matching_evaluation(context, explainability_result: explainability_result)
    return first_match if first_match

    # If no match found, return nil (consistent with apply_first_policy behavior)
    return nil
  end

  # For UNIQUE, ANY, COLLECT - need all matches
  matching_evaluations = find_all_matching_evaluations(context, explainability_result: explainability_result)

  # Apply hit policy to select the appropriate evaluation
  result = apply_hit_policy(matching_evaluations)

  # Add explainability to metadata by creating new Evaluation with updated metadata
  if result && explainability_result
     = result..dup
    [:explainability] = explainability_result.to_h
    result = Evaluation.new(
      decision: result.decision,
      weight: result.weight,
      reason: result.reason,
      evaluator_name: result.evaluator_name,
      metadata: 
    )
  end

  result
end