Class: DecisionAgent::Simulation::ScenarioLibrary

Inherits:
Object
  • Object
show all
Defined in:
lib/decision_agent/simulation/scenario_library.rb

Overview

Library of pre-defined test scenario templates

Class Method Summary collapse

Class Method Details

.build_fraud_scenario(amount, account_age, transaction_count, location, category) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/decision_agent/simulation/scenario_library.rb', line 113

def self.build_fraud_scenario(amount, , transaction_count, location, category)
  {
    context: {
      transaction_amount: amount,
      account_age_days: ,
      transaction_count_24h: transaction_count,
      location: location
    },
    metadata: {
      type: "fraud_detection",
      category: category,
      description: "#{category.capitalize} transaction scenario"
    }
  }
end

.build_loan_scenario(amount, credit_score, income, employment_status, risk_level) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/decision_agent/simulation/scenario_library.rb', line 90

def self.build_loan_scenario(amount, credit_score, income, employment_status, risk_level)
  {
    context: {
      amount: amount,
      credit_score: credit_score,
      income: income,
      employment_status: employment_status
    },
    metadata: {
      type: "loan_approval",
      category: risk_level,
      description: "#{risk_level.capitalize.tr('_', ' ')} loan application scenario"
    }
  }
end

.build_pricing_scenario(tier, order_value, loyalty_points, frequency, category) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/decision_agent/simulation/scenario_library.rb', line 136

def self.build_pricing_scenario(tier, order_value, loyalty_points, frequency, category)
  {
    context: {
      customer_tier: tier,
      order_value: order_value,
      loyalty_points: loyalty_points,
      purchase_frequency: frequency
    },
    metadata: {
      type: "pricing",
      category: category,
      description: "#{category.capitalize.tr('_', ' ')} customer pricing scenario"
    }
  }
end

.create_scenario(template_name, overrides: {}) ⇒ Hash

Create scenario from template

Parameters:

  • template_name (String, Symbol)

    Template name

  • overrides (Hash) (defaults to: {})

    Values to override in template

Returns:

  • (Hash)

    Scenario definition

Raises:



24
25
26
27
28
29
30
31
# File 'lib/decision_agent/simulation/scenario_library.rb', line 24

def self.create_scenario(template_name, overrides: {})
  template = get_template(template_name)
  raise DecisionAgent::Simulation::ScenarioExecutionError, "Template not found: #{template_name}" unless template

  scenario = template.dup
  merge_overrides(scenario, overrides)
  scenario
end

.fraud_detection_templatesObject



106
107
108
109
110
111
# File 'lib/decision_agent/simulation/scenario_library.rb', line 106

def self.fraud_detection_templates
  {
    fraud_detection_suspicious: build_fraud_scenario(10_000, 5, 50, "unusual", "suspicious"),
    fraud_detection_normal: build_fraud_scenario(100, 365, 3, "usual", "normal")
  }
end

.generate_edge_cases(base_context) ⇒ Array<Hash>

Get edge case scenarios for a given context structure

Parameters:

  • base_context (Hash)

    Base context to generate edge cases from

Returns:

  • (Array<Hash>)

    Array of edge case scenarios



36
37
38
39
40
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
# File 'lib/decision_agent/simulation/scenario_library.rb', line 36

def self.generate_edge_cases(base_context)
  scenarios = []

  # Generate scenarios with nil values
  base_context.each_key do |key|
    edge_scenario = base_context.dup
    edge_scenario[key] = nil
    scenarios << { context: edge_scenario, metadata: { type: "edge_case", field: key, value: "nil" } }
  end

  # Generate scenarios with extreme numeric values and empty strings
  base_context.each do |key, value|
    if value.is_a?(Numeric)
      # Zero value
      zero_scenario = base_context.dup
      zero_scenario[key] = 0
      scenarios << { context: zero_scenario, metadata: { type: "edge_case", field: key, value: "zero" } }

      # Negative value (if positive)
      if value.positive?
        neg_scenario = base_context.dup
        neg_scenario[key] = -value
        scenarios << { context: neg_scenario, metadata: { type: "edge_case", field: key, value: "negative" } }
      end

      # Very large value
      large_scenario = base_context.dup
      large_scenario[key] = value * 1000
      scenarios << { context: large_scenario, metadata: { type: "edge_case", field: key, value: "large" } }
    elsif value.is_a?(String)
      # Empty string
      empty_scenario = base_context.dup
      empty_scenario[key] = ""
      scenarios << { context: empty_scenario, metadata: { type: "edge_case", field: key, value: "empty_string" } }
    end
  end

  scenarios
end

.get_template(template_name) ⇒ Hash?

Get scenario template by name

Parameters:

  • template_name (String, Symbol)

    Template name

Returns:

  • (Hash, nil)

    Scenario template or nil if not found



10
11
12
# File 'lib/decision_agent/simulation/scenario_library.rb', line 10

def self.get_template(template_name)
  templates[template_name.to_sym] || templates[template_name.to_s]
end

.list_templatesArray<String>

List all available templates

Returns:

  • (Array<String>)

    Array of template names



16
17
18
# File 'lib/decision_agent/simulation/scenario_library.rb', line 16

def self.list_templates
  templates.keys.map(&:to_s)
end

.loan_approval_templatesObject



82
83
84
85
86
87
88
# File 'lib/decision_agent/simulation/scenario_library.rb', line 82

def self.loan_approval_templates
  {
    loan_approval_high_risk: build_loan_scenario(100_000, 550, 30_000, "unemployed", "high_risk"),
    loan_approval_low_risk: build_loan_scenario(50_000, 800, 100_000, "employed", "low_risk"),
    loan_approval_medium_risk: build_loan_scenario(75_000, 650, 60_000, "employed", "medium_risk")
  }
end

.merge_overrides(scenario, overrides) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/decision_agent/simulation/scenario_library.rb', line 152

def self.merge_overrides(scenario, overrides)
  scenario[:context] = scenario[:context].merge(overrides[:context]) if overrides[:context]

  scenario[:metadata] = (scenario[:metadata] || {}).merge(overrides[:metadata]) if overrides[:metadata]

  overrides.each do |key, value|
    next if %i[context metadata].include?(key)

    scenario[key] = value
  end
end

.pricing_templatesObject



129
130
131
132
133
134
# File 'lib/decision_agent/simulation/scenario_library.rb', line 129

def self.pricing_templates
  {
    pricing_high_value: build_pricing_scenario("premium", 5_000, 10_000, "high", "high_value"),
    pricing_standard: build_pricing_scenario("standard", 100, 500, "medium", "standard")
  }
end

.templatesObject



76
77
78
79
80
# File 'lib/decision_agent/simulation/scenario_library.rb', line 76

def self.templates
  loan_approval_templates
    .merge(fraud_detection_templates)
    .merge(pricing_templates)
end