class Roby::App::RobotNames

The part of the configuration related to robot declaration

Attributes

aliases[R]

@return [Hash<String,String>] a set of aliases, i.e. short names

for robots
default_robot_name[R]

@return [String,nil] the default robot name

robots[R]

@return [Hash<(String,String)>] the set of declared robots, as

robot_name => robot_type

Public Class Methods

new(options = Hash.new) click to toggle source

Create a RobotConfiguration object based on a hash formatted as-is from the app.yml file

# File lib/roby/app/robot_names.rb, line 20
def initialize(options = Hash.new)
    @robots = options['robots'] || Hash.new
    @default_robot_name = options['default_robot'] || 'default'
    if !has_robot?(default_robot_name)
        robots[default_robot_name] = default_robot_name
    end

    @aliases = options['aliases'] || Hash.new
    aliases.each do |name_alias, name|
        if !has_robot?(name)
            raise ArgumentError, "cannot use #{name_alias} as an alias to #{name}: #{name} is not a declared robot"
        end
        if has_robot?(name_alias)
            raise ArgumentError, "cannot use #{name_alias} as an alias to #{name}: #{name_alias} is already a declared robot"
        end
    end

    self.strict = !!options['robots']
end

Public Instance Methods

declare_robot_type(robot_name, robot_type) click to toggle source

Declare the type of an existing robot

# File lib/roby/app/robot_names.rb, line 41
def declare_robot_type(robot_name, robot_type)
    if strict? && !has_robot?(robot_type)
        raise ArgumentError, "#{robot_type} is not a known robot"
    end
    robots[robot_name] = robot_type
end
default_robot_type() click to toggle source

@return [String,nil] the type of the default robot

# File lib/roby/app/robot_names.rb, line 61
def default_robot_type
    if default_robot_name
        robots[default_robot_name]
    end
end
each(&block) click to toggle source

Enumerate the robot names and types

# File lib/roby/app/robot_names.rb, line 49
def each(&block)
    robots.each(&block)
end
error(klass, message) click to toggle source

Helper method which either warns or raises depending on the value of {#strict?}

# File lib/roby/app/robot_names.rb, line 77
def error(klass, message)
    if strict?
        raise klass, message
    else
        Roby::Application.warn message
    end
end
has_robot?(name, type = nil) click to toggle source

Tests whether the given name is a declared robot

@return [Boolean]

# File lib/roby/app/robot_names.rb, line 70
def has_robot?(name, type = nil)
    robots.has_key?(name.to_s) &&
        (!type || robots[name.to_s] == type.to_s)
end
names() click to toggle source

Enumerate the list of known robots

@yieldparam [String] robot_name the robot name

# File lib/roby/app/robot_names.rb, line 56
def names
    robots.keys
end
resolve(name, type = nil) click to toggle source

Returns the robot name and type matching the given name

It resolves aliases

@param [String] name a robot name or alias @return [(String,String)] @raises ArgumentError if the given robot name does not exist

# File lib/roby/app/robot_names.rb, line 92
def resolve(name, type = nil)
    robot_name = aliases[name] || name || default_robot_name
    if !robot_name
        error(ArgumentError, "no robot name given and no default name declared in app.yml, defaulting to #{default_robot_name}:#{default_robot_type}")
        return default_robot_name, default_robot_type
    elsif robots.has_key?(robot_name)
        robot_type = robots[robot_name]
        type ||= robot_type
        if type != robot_type
            error(ArgumentError, "invalid robot type when resolving #{name}:#{type}, #{name} is declared to be of type #{robot_type}")
        end
        return robot_name, type
    else
        if !robots.empty? || strict?
            error(Application::NoSuchRobot, "#{name} is neither a robot name, nor an alias. Known names: #{robots.keys.sort.join(", ")}, known aliases: #{aliases.keys.join(", ")}")
        end
        return robot_name, (type || robot_name)
    end
end