Class: Smith::Workflow::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/smith/workflow/router.rb

Class Method Summary collapse

Class Method Details

.resolve(classifier_output, config, workflow_class:) ⇒ Object



6
7
8
9
10
11
# File 'lib/smith/workflow/router.rb', line 6

def self.resolve(classifier_output, config, workflow_class:)
  validate!(classifier_output, config)
  transition_name = select_transition(classifier_output, config)
  validate_transition_exists!(transition_name, workflow_class)
  transition_name
end

.select_transition(output, config) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/smith/workflow/router.rb', line 38

def self.select_transition(output, config)
  if output[:confidence] >= config[:confidence_threshold]
    config[:routes][output[:route].to_sym]
  else
    config[:fallback]
  end
end

.validate!(output, config) ⇒ Object



13
14
15
16
17
# File 'lib/smith/workflow/router.rb', line 13

def self.validate!(output, config)
  validate_structure!(output)
  validate_confidence!(output[:confidence])
  validate_route_key!(output[:route].to_sym, output[:confidence], config)
end

.validate_confidence!(confidence) ⇒ Object

Raises:



25
26
27
28
29
# File 'lib/smith/workflow/router.rb', line 25

def self.validate_confidence!(confidence)
  return if confidence.is_a?(Numeric) && confidence >= 0.0 && confidence <= 1.0

  raise WorkflowError, "router confidence must be a number in 0.0..1.0"
end

.validate_route_key!(route_key, confidence, config) ⇒ Object

Raises:



31
32
33
34
35
36
# File 'lib/smith/workflow/router.rb', line 31

def self.validate_route_key!(route_key, confidence, config)
  return if confidence < config[:confidence_threshold]
  return if config[:routes].key?(route_key)

  raise WorkflowError, "router route :#{route_key} not found in declared routes"
end

.validate_structure!(output) ⇒ Object

Raises:



19
20
21
22
23
# File 'lib/smith/workflow/router.rb', line 19

def self.validate_structure!(output)
  raise WorkflowError, "router classifier output must be a Hash" unless output.is_a?(Hash)
  raise WorkflowError, "router classifier output missing :route" unless output.key?(:route)
  raise WorkflowError, "router classifier output missing :confidence" unless output.key?(:confidence)
end

.validate_transition_exists!(transition_name, workflow_class) ⇒ Object

Raises:



46
47
48
49
50
# File 'lib/smith/workflow/router.rb', line 46

def self.validate_transition_exists!(transition_name, workflow_class)
  return if workflow_class.find_transition(transition_name)

  raise WorkflowError, "router selected transition :#{transition_name} which is not declared on the workflow"
end