Class: Phronomy::Agent::SharedState

Inherits:
Object
  • Object
show all
Defined in:
lib/phronomy/agent/shared_state.rb

Overview

Implements peer coordination through a shared KnowledgeStore.

Defined Under Namespace

Classes: KnowledgeStore

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._aggregatorObject



66
# File 'lib/phronomy/agent/shared_state.rb', line 66

def _aggregator = @aggregator

._coordinationObject



62
# File 'lib/phronomy/agent/shared_state.rb', line 62

def _coordination = @coordination

._max_cyclesObject



63
# File 'lib/phronomy/agent/shared_state.rb', line 63

def _max_cycles = @max_cycles

._membersObject



61
# File 'lib/phronomy/agent/shared_state.rb', line 61

def _members = Array(@members)

._terminate_whenObject



65
# File 'lib/phronomy/agent/shared_state.rb', line 65

def _terminate_when = @terminate_when

._timeoutObject



64
# File 'lib/phronomy/agent/shared_state.rb', line 64

def _timeout = @timeout

.aggregate(&block) ⇒ Object



57
58
59
# File 'lib/phronomy/agent/shared_state.rb', line 57

def aggregate(&block)
  block ? @aggregator = block : @aggregator
end

.coordination(text = nil) ⇒ Object



37
38
39
# File 'lib/phronomy/agent/shared_state.rb', line 37

def coordination(text = nil)
  text ? @coordination = text : @coordination
end

.max_cycles(value = nil) ⇒ Object



42
43
44
# File 'lib/phronomy/agent/shared_state.rb', line 42

def max_cycles(value = nil)
  value ? @max_cycles = Integer(value) : @max_cycles
end

.member(klass, instruction: nil) ⇒ Object



31
32
33
34
# File 'lib/phronomy/agent/shared_state.rb', line 31

def member(klass, instruction: nil)
  @members ||= []
  @members << {klass: klass, instruction: instruction}
end

.terminate_when(&block) ⇒ Object



52
53
54
# File 'lib/phronomy/agent/shared_state.rb', line 52

def terminate_when(&block)
  block ? @terminate_when = block : @terminate_when
end

.timeout(value = nil) ⇒ Object



47
48
49
# File 'lib/phronomy/agent/shared_state.rb', line 47

def timeout(value = nil)
  value ? @timeout = value.to_f : @timeout
end

Instance Method Details

#invoke(input, config: {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/phronomy/agent/shared_state.rb', line 70

def invoke(input, config: {})
  validate_termination!

  store = KnowledgeStore.new
  max_cycles = self.class._max_cycles
  deadline = self.class._timeout ? Time.now + self.class._timeout : nil
  terminated_by = :max_cycles
  completed_cycles = 0
  cycle_limit = max_cycles || Float::INFINITY

  (1..cycle_limit).each do |cycle|
    self.class._members.each do |member_config|
      invoke_researcher(
        member_config[:klass],
        store,
        cycle,
        input,
        member_config[:instruction]
      )
    end
    completed_cycles = cycle

    if self.class._terminate_when&.call(store)
      terminated_by = :terminate_when
      break
    end

    if deadline && Time.now >= deadline
      terminated_by = :timeout
      break
    end
  end

  output = if self.class._aggregator
    self.class._aggregator.call(store)
  else
    store.read_all
  end

  {output: output, cycles: completed_cycles, terminated_by: terminated_by}
end