Class: Roby::App::RobotNames

Inherits:
Object
  • Object
show all
Defined in:
lib/roby/app/robot_names.rb

Overview

The part of the configuration related to robot declaration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RobotNames

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



30
31
32
33
34
35
36
37
# File 'lib/roby/app/robot_names.rb', line 30

def initialize(options = {})
    @robots = { "default" => "default" }
    @default_robot_name = "default"
    @aliases = {}
    @strict = false

    load_config_yaml(options)
end

Instance Attribute Details

#aliasesHash<String,String> (readonly)

Returns a set of aliases, i.e. short names for robots.

Returns:

  • (Hash<String,String>)

    a set of aliases, i.e. short names for robots



14
15
16
# File 'lib/roby/app/robot_names.rb', line 14

def aliases
  @aliases
end

#default_robot_nameString? (readonly)

Returns the default robot name.

Returns:

  • (String, nil)

    the default robot name



8
9
10
# File 'lib/roby/app/robot_names.rb', line 8

def default_robot_name
  @default_robot_name
end

#robotsHash<(String,String)> (readonly)

Returns the set of declared robots, as robot_name => robot_type.

Returns:

  • (Hash<(String,String)>)

    the set of declared robots, as robot_name => robot_type



11
12
13
# File 'lib/roby/app/robot_names.rb', line 11

def robots
  @robots
end

#strict=(value) ⇒ Object (writeonly)

Sets #strict?

See Also:



26
27
28
# File 'lib/roby/app/robot_names.rb', line 26

def strict=(value)
  @strict = value
end

Instance Method Details

#declare_robot_type(robot_name, robot_type) ⇒ Object

Declare the type of an existing robot



64
65
66
67
68
69
70
# File 'lib/roby/app/robot_names.rb', line 64

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_typeString?

Returns the type of the default robot.

Returns:

  • (String, nil)

    the type of the default robot



85
86
87
88
89
# File 'lib/roby/app/robot_names.rb', line 85

def default_robot_type
    if default_robot_name
        robots[default_robot_name]
    end
end

#each(&block) ⇒ Object

Enumerate the robot names and types



73
74
75
# File 'lib/roby/app/robot_names.rb', line 73

def each(&block)
    robots.each(&block)
end

#error(klass, message) ⇒ Object

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



101
102
103
104
105
106
107
# File 'lib/roby/app/robot_names.rb', line 101

def error(klass, message)
    if strict?
        raise klass, message
    else
        Roby::Application.warn message
    end
end

#has_robot?(name, type = nil) ⇒ Boolean

Tests whether the given name is a declared robot

Returns:

  • (Boolean)


94
95
96
97
# File 'lib/roby/app/robot_names.rb', line 94

def has_robot?(name, type = nil)
    robots.has_key?(name.to_s) &&
        (!type || robots[name.to_s] == type.to_s)
end

#load_config_yaml(options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/roby/app/robot_names.rb', line 39

def load_config_yaml(options)
    @robots = options["robots"] || @robots
    @strict = !!options["robots"]

    @default_robot_name = options["default_robot"] || @default_robot_name
    unless has_robot?(default_robot_name)
        robots[default_robot_name] = default_robot_name
    end

    @aliases = options["aliases"] || @aliases
    aliases.each do |name_alias, name|
        unless 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
end

#names {|robot_name| ... } ⇒ Object

Enumerate the list of known robots

Yield Parameters:

  • robot_name (String)

    the robot name



80
81
82
# File 'lib/roby/app/robot_names.rb', line 80

def names
    robots.keys
end

#resolve(name, type = nil) ⇒ (String,String)

Returns the robot name and type matching the given name

It resolves aliases

Parameters:

  • name (String)

    a robot name or alias

Returns:

  • ((String,String))

Raises:

  • ArgumentError if the given robot name does not exist



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/roby/app/robot_names.rb', line 116

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}"
        )
        [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
        [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
        [robot_name, type || robot_name]
    end
end

#strict?Boolean

Returns if true, Roby will generate an error if a non-declared robot is used. Otherwise, it will only issue a warning.

Returns:

  • (Boolean)

    if true, Roby will generate an error if a non-declared robot is used. Otherwise, it will only issue a warning



19
20
21
# File 'lib/roby/app/robot_names.rb', line 19

def strict?
    @strict
end