Class: JIJI::AgentManager
- Inherits:
-
Object
- Object
- JIJI::AgentManager
- Includes:
- Enumerable, AgentUtil
- Defined in:
- lib/jiji/agent/agent_manager.rb
Defined Under Namespace
Classes: State
Instance Attribute Summary collapse
-
#agent_registry ⇒ Object
Returns the value of attribute agent_registry.
-
#conf ⇒ Object
Returns the value of attribute conf.
-
#operator ⇒ Object
Returns the value of attribute operator.
-
#registry ⇒ Object
Returns the value of attribute registry.
-
#trade_result_dao ⇒ Object
Returns the value of attribute trade_result_dao.
Instance Method Summary collapse
-
#add(id, agent, name = "") ⇒ Object
エージェントを追加する.
-
#clear ⇒ Object
エージェントをすべて破棄する.
-
#each(&block) ⇒ Object
エージェントの一覧を得る.
-
#flush(time) ⇒ Object
取引結果データを強制的にファイルに出力する。.
-
#get(name) ⇒ Object
エージェントを得る.
-
#initialize(id, registry, logger, failsafe = true) ⇒ AgentManager
constructor
A new instance of AgentManager.
-
#next_rates(rates) ⇒ Object
レートを受け取ってエージェントに通知する。.
-
#off(agent_name) ⇒ Object
エージェントへのイベント通知を停止する.
-
#on(agent_name) ⇒ Object
エージェントへのイベント通知を開始する.
-
#on?(agent_name) ⇒ Boolean
エージェントへのイベント通知状態を取得する.
-
#remove(name) ⇒ Object
エージェントを破棄する.
Methods included from AgentUtil
Constructor Details
#initialize(id, registry, logger, failsafe = true) ⇒ AgentManager
Returns a new instance of AgentManager.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/jiji/agent/agent_manager.rb', line 20 def initialize( id, registry, logger, failsafe=true ) @id = id @agents = {} @agent_registry = registry @logger = logger # エージェントでエラーが発生した場合にエラーを無視し実行を継続するか # true の場合、ログ出力後エラーを握って処理を継続(RMTはこちらで動作する) # false の場合、ログ出力後エラーを再スロー(バックテストはこちらで動作する) @failsafe = failsafe end |
Instance Attribute Details
#agent_registry ⇒ Object
Returns the value of attribute agent_registry.
125 126 127 |
# File 'lib/jiji/agent/agent_manager.rb', line 125 def agent_registry @agent_registry end |
#conf ⇒ Object
Returns the value of attribute conf.
128 129 130 |
# File 'lib/jiji/agent/agent_manager.rb', line 128 def conf @conf end |
#operator ⇒ Object
Returns the value of attribute operator.
127 128 129 |
# File 'lib/jiji/agent/agent_manager.rb', line 127 def operator @operator end |
#registry ⇒ Object
Returns the value of attribute registry.
129 130 131 |
# File 'lib/jiji/agent/agent_manager.rb', line 129 def registry @registry end |
#trade_result_dao ⇒ Object
Returns the value of attribute trade_result_dao.
126 127 128 |
# File 'lib/jiji/agent/agent_manager.rb', line 126 def trade_result_dao @trade_result_dao end |
Instance Method Details
#add(id, agent, name = "") ⇒ Object
エージェントを追加する
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jiji/agent/agent_manager.rb', line 32 def add( id, agent, name="" ) if @agents.key? id raise UserError.new( JIJI::ERROR_ALREADY_EXIST, "agent is already exist. id=#{id}") end output = @registry.output( @id, id ) op = AgentOperator.new( @operator, name ) safe( conf.get( [:agent,:safe_level], 4) ){ agent.operator = op agent.logger = @logger agent.output = output agent.init } @agents[id] = State.new( agent, output, true ) end |
#clear ⇒ Object
エージェントをすべて破棄する
66 67 68 |
# File 'lib/jiji/agent/agent_manager.rb', line 66 def clear @agents.clear end |
#each(&block) ⇒ Object
エージェントの一覧を得る
56 57 58 |
# File 'lib/jiji/agent/agent_manager.rb', line 56 def each( &block ) @agents.each( &block ) end |
#flush(time) ⇒ Object
取引結果データを強制的にファイルに出力する。
120 121 122 123 |
# File 'lib/jiji/agent/agent_manager.rb', line 120 def flush( time ) @trade_result_dao.flush( time ) @operator.flush end |
#get(name) ⇒ Object
エージェントを得る
61 62 63 |
# File 'lib/jiji/agent/agent_manager.rb', line 61 def get( name ) @agents[name] end |
#next_rates(rates) ⇒ Object
レートを受け取ってエージェントに通知する。
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/jiji/agent/agent_manager.rb', line 95 def next_rates( rates ) @operator.next_rates( rates ) @agents.each_pair {|n,a| a.output.time = rates.time } safe( conf.get( [:agent,:safe_level], 4) ){ @agents.each_pair {|n,a| next unless a.active if @failsafe JIJI::Util.log_if_error( @logger ) { next unless a.active a.agent.next_rates( rates ) } else JIJI::Util.log_if_error_and_throw( @logger ) { a.agent.next_rates( rates ) } end } } # 取引結果の集計 @trade_result_dao.next( operator, rates.time ) end |
#off(agent_name) ⇒ Object
エージェントへのイベント通知を停止する
79 80 81 82 83 84 |
# File 'lib/jiji/agent/agent_manager.rb', line 79 def off( agent_name ) unless @agents.key? agent_name raise UserError.new( JIJI::ERROR_NOT_FOUND, "agent not found. name=#{agent_name}") end @agents[agent_name].active = false end |
#on(agent_name) ⇒ Object
エージェントへのイベント通知を開始する
71 72 73 74 75 76 |
# File 'lib/jiji/agent/agent_manager.rb', line 71 def on( agent_name ) unless @agents.key? agent_name raise UserError.new( JIJI::ERROR_NOT_FOUND, "agent not found. name=#{agent_name}") end @agents[agent_name].active = true end |
#on?(agent_name) ⇒ Boolean
エージェントへのイベント通知状態を取得する
87 88 89 90 91 92 |
# File 'lib/jiji/agent/agent_manager.rb', line 87 def on?( agent_name ) unless @agents.key? agent_name raise UserError.new( JIJI::ERROR_NOT_FOUND, "agent not found. name=#{agent_name}") end @agents[agent_name].active end |
#remove(name) ⇒ Object
エージェントを破棄する
48 49 50 51 52 53 |
# File 'lib/jiji/agent/agent_manager.rb', line 48 def remove( name ) unless @agents.key? name raise UserError.new( JIJI::ERROR_NOT_FOUND, "agent not found. name=#{name}") end @agents.delete name end |