Module: Syskit::Actions::LibraryExtension

Includes:
MetaRuby::DSLs::FindThroughMethodMissing
Included in:
Syskit::Actions, Syskit::Actions
Defined in:
lib/syskit/actions/interface_model_extension.rb

Overview

Extension to the models of Roby::Actions::Interface

Instance Method Summary collapse

Instance Method Details

#find_through_method_missing(m, args) ⇒ Object



123
124
125
126
# File 'lib/syskit/actions/interface_model_extension.rb', line 123

def find_through_method_missing(m, args)
    MetaRuby::DSLs.find_through_method_missing(
        profile, m, args, '_tag'.freeze => :find_tag) || super
end

#has_through_method_missing?(m) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
# File 'lib/syskit/actions/interface_model_extension.rb', line 118

def has_through_method_missing?(m)
    MetaRuby::DSLs.has_through_method_missing?(
        profile, m, '_tag'.freeze => :has_tag?) || super
end

#profile(name = nil, &block) ⇒ Object

The main Syskit::Actions::Profile object that is used in an action interface



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/syskit/actions/interface_model_extension.rb', line 7

def profile(name = nil, &block)
    return super if name

    if !@profile
        @profile = super("Profile") { self }
        setup_main_profile(@profile)
    end

    if block
        Roby.warn_deprecated "calling profile do ... end in an action interface is deprecated, call use_profile do .. end instead"
        use_profile(&block)
    else
        @profile
    end
end

#profile_libraryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An action library that is created and included on-the-fly to support the actions derived from #profile



40
41
42
43
44
45
46
# File 'lib/syskit/actions/interface_model_extension.rb', line 40

def profile_library
    if !@profile_library
        @profile_library = Roby::Actions::Library.new_submodel
        use_library @profile_library
    end
    @profile_library
end

#register_action_from_profile(action_model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Registers an action that has been derived from a profile definition or device



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/syskit/actions/interface_model_extension.rb', line 66

def register_action_from_profile(action_model)
    action_model = action_model.rebind(self)
    action_name  = action_model.name
    profile_library.register_action(action_name, action_model)
    action_model = find_action_by_name(action_name)

    args = action_model.each_arg.to_a
    if args.any?(&:required?)
        profile_library.send(:define_method, action_name) do |arguments|
            action_model.to_instance_requirements(arguments)
        end
    elsif !args.empty?
        profile_library.send(:define_method, action_name) do |arguments = Hash.new|
            action_model.to_instance_requirements(arguments)
        end
    else
        profile_library.send(:define_method, action_name) do
            action_model.to_instance_requirements(Hash.new)
        end
    end
end

#robot(&block) ⇒ Syskit::Robot::RobotDefinition

Returns the robot definition object used by this action interface



50
51
52
53
54
55
56
57
# File 'lib/syskit/actions/interface_model_extension.rb', line 50

def robot(&block)
    existing_devices = profile.robot.each_master_device.to_a
    profile.robot(&block)
    new_devices = profile.robot.each_master_device.to_a - existing_devices
    new_devices.each do |dev|
        register_action_from_profile(dev.to_action_model)
    end
end

#setup_main_profile(profile) ⇒ Object



23
24
# File 'lib/syskit/actions/interface_model_extension.rb', line 23

def setup_main_profile(profile)
end

#tag(name, model) ⇒ Object

Define a tag on #profile



60
# File 'lib/syskit/actions/interface_model_extension.rb', line 60

def tag(name, model); profile.tag(name, model) end

#use_profile(used_profile = nil, tag_selection = Hash.new, transform_names: ->(name) { name }) ⇒ void

This method returns an undefined value.

Export the definitions contained in the given profile as actions on this action interface

Parameters:

  • used_profile (Profile) (defaults to: nil)

    the profile that should be used

  • tag_selection (Hash) (defaults to: Hash.new)

    selection for the profile tags, see Profile#use_profile



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/syskit/actions/interface_model_extension.rb', line 95

def use_profile(used_profile = nil, tag_selection = Hash.new, transform_names: ->(name) { name })
    if block_given?
        if !tag_selection.empty?
            raise ArgumentError, "cannot provide a tag selection when defining a new anonymous profile"
        end

        used_profile = Profile.new("#{self.name}::<anonymous>", register: true)
        used_profile.instance_eval(&proc)
        tag_selection = use_profile_tags(used_profile)
    elsif !used_profile
        raise ArgumentError, "must provide either a profile object or a block"
    end

    @current_description = nil
    new_definitions =
        profile.use_profile(used_profile, tag_selection, transform_names: transform_names)
    new_definitions.each do |definition|
        register_action_from_profile(definition.to_action_model)
    end
end

#use_profile_tags(profile) ⇒ Object

Define on self tags that match the profile's tags



27
28
29
30
31
32
33
34
# File 'lib/syskit/actions/interface_model_extension.rb', line 27

def use_profile_tags(profile)
    tag_map = Hash.new
    profile.each_tag do |tag|
        tagged_models = [*tag.proxied_data_service_models]
        tag_map[tag.tag_name] = @profile.tag(tag.tag_name, *tagged_models)
    end
    tag_map
end