Class: MAS

Inherits:
Object
  • Object
show all
Defined in:
lib/agens/mas.rb

Constant Summary collapse

@@counters =
{}
@@recipes =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMAS

Returns a new instance of MAS.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/agens/mas.rb', line 18

def initialize
  @config = Celluloid::Supervision::Configuration.new
  @names = []
  
  @@recipes.each do |agent_recipe|
    if !agent_recipe.args.nil? && agent_recipe.args.any?
      @config.define(type: agent_recipe.type, as: agent_recipe.name, args: agent_recipe.args)
    else
      @config.define(type: agent_recipe.type, as: agent_recipe.name)
    end
    
    @names << agent_recipe.name
  end
end

Class Method Details

.agent(agent, options = {}) ⇒ Object

region DSL agent MyAgent agent MyAgent, as: :james_bond agent MyAgent, as: :james_bond, args: [1, 2]



63
64
65
# File 'lib/agens/mas.rb', line 63

def self.agent(agent, options = {})
  @@recipes << self.generate_recipe(agent, options)
end

.agents(agent, options = {}) ⇒ Object

agents MyAgent agents MyAgent, as: :james_bonds agents MyAgent, count: 10 agents MyAgent, as: :james_bonds, count: 10 agents MyAgent, as: :james_bonds, count: 10, args: [1, 2]



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/agens/mas.rb', line 72

def self.agents(agent, options = {})
  mas_name = self.name.to_sym
  pool_name = "#{agent.class.name.to_sym}_pool"
  @@counters[pool_name] ||= 0
  @@counters[pool_name] += 1
  
  name = options.key?(:as) ? options[:as] : "#{mas_name}_#{pool_name}_#{@@counters[pool_name]}"
  count = options.key?(:count) ? options[:count] : nil
  
  count = 2 if count == nil
  
  count.times do
    self.agent(agent, args: options[:args])
  end
  
  # TODO add support for new celluloid supervision pools (when they will be documented, see https://github.com/celluloid/celluloid-supervision)
  # if count == nil
  #  pool agent, as: name
  # else
  #  pool agent, as: name, size: count
  # end
end

.generate_recipe(agent, options = {}) ⇒ Object

endregion DSL



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/agens/mas.rb', line 96

def self.generate_recipe(agent, options = {})
  name = nil
  if options.key?(:as)
    name = options[:as]
  else
    mas_name = self.name.to_sym
    agent_name = agent.name.to_sym
    @@counters[agent_name] ||= 0
    @@counters[agent_name] += 1
    
    name = "#{mas_name}_#{agent_name}_#{@@counters[agent_name]}"
  end
  
  AgentConfigRecipe.new(agent, name, options[:args])
end

Instance Method Details

#add_agent(agent, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/agens/mas.rb', line 47

def add_agent(agent, options = {})
  recipe << self.generate_recipe(agent, options)
  
  if !recipe.args.nil? && recipe.args.any?
    @config.add(type: recipe.type, as: recipe.name, args: recipe.args)
  else
    @config.add(type: recipe.type, as: recipe.name)
  end
  
  self
end

#runObject



33
34
35
36
# File 'lib/agens/mas.rb', line 33

def run
  puts "Running MAS #{self.class.name}..."
  @config.deploy
end

#shutdownObject



38
39
40
41
# File 'lib/agens/mas.rb', line 38

def shutdown
  puts "Stoping MAS #{self.class.name}..."
  @config.shutdown
end

#worldObject



43
44
45
# File 'lib/agens/mas.rb', line 43

def world
  World.new(@names)
end