Class: Orocos::Async::Log::TaskContext

Inherits:
ObjectBase
  • Object
show all
Extended by:
ObjectBase::Periodic::ClassMethods, Utilrb::EventLoop::Forwardable
Includes:
ObjectBase::Periodic
Defined in:
lib/orocos/async/log/task_context.rb

Instance Attribute Summary

Attributes included from ObjectBase::Periodic::ClassMethods

#default_period

Attributes inherited from ObjectBase

#emitting, #event_loop, #name, #options, #pending_adds

Instance Method Summary collapse

Methods included from ObjectBase::Periodic

#default_period, #period, #period=

Methods inherited from ObjectBase

#add_listener, define_event, define_events, #disable_emitting, #event, event_names, #event_names, #invalidate_delegator!, #listener?, #listeners, #number_of_listeners, #on_event, #proxy_event, #reachable!, #reachable?, #remove_all_listeners, #remove_listener, #remove_proxy_event, #unreachable!, #valid_delegator?, valid_event?, #valid_event?, validate_event, #validate_event, #wait

Constructor Details

#initialize(log_task, options = Hash.new) ⇒ TaskContext


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/orocos/async/log/task_context.rb', line 19

def initialize(log_task,options=Hash.new)
    @options ||= Kernel.validate_options options,:raise => false,:event_loop => Orocos::Async.event_loop,:period => default_period
    super(log_task.name,@options[:event_loop])
    if log_task.has_port? "state"
        log_task.port("state").on_data do |sample|
            emit_state_change log_task.state
        end
    end
    # do not queue reachable event no listeners are registered so far
    disable_emitting do 
        reachable!(log_task)
    end
    log_task.on_port_reachable do |name|
        emit_port_reachable name
    end
    log_task.on_property_reachable do |name|
        emit_property_reachable name
    end
    log_task.on_state_change do |val|
        emit_state_change val
    end
end

Instance Method Details

#attribute(name, options = {}, &block) ⇒ Object


92
93
94
95
96
97
98
99
100
# File 'lib/orocos/async/log/task_context.rb', line 92

def attribute(name,options={},&block)
    if block
        orig_attribute(name) do |attr|
            block.call Attribute.new(self,attr)
        end
    else
        Attribute.new(self,orig_attribute(name))
    end
end

#each_attribute(&block) ⇒ Object

call-seq:

task.each_attribute { |a| ... } => task

Enumerates the attributes that are available on this task, as instances of Orocos::Attribute


152
153
154
155
156
157
158
159
160
161
# File 'lib/orocos/async/log/task_context.rb', line 152

def each_attribute(&block)
    if !block_given?
        return enum_for(:each_attribute)
    end

    names = attribute_names
    names.each do |name|
        yield(attribute(name))
    end
end

#each_port(&block) ⇒ Object

call-seq:

task.each_port { |p| ... } => task

Enumerates the ports that are available on this task, as instances of either Orocos::InputPort or Orocos::OutputPort


168
169
170
171
172
173
174
175
176
177
# File 'lib/orocos/async/log/task_context.rb', line 168

def each_port(&block)
    if !block_given?
        return enum_for(:each_port)
    end

    port_names.each do |name|
        yield(port(name))
    end
    self
end

#each_property(&block) ⇒ Object

call-seq:

task.each_property { |a| ... } => task

Enumerates the properties that are available on this task, as instances of Orocos::Attribute


136
137
138
139
140
141
142
143
144
145
# File 'lib/orocos/async/log/task_context.rb', line 136

def each_property(&block)
    if !block_given?
        return enum_for(:each_property)
    end
    names = property_names
    puts names
    names.each do |name|
        yield(property(name))
    end
end

#port(name, verify = true, options = Hash.new, &block) ⇒ Object


112
113
114
115
116
117
118
119
120
# File 'lib/orocos/async/log/task_context.rb', line 112

def port(name, verify = true,options=Hash.new,&block)
    if block
        orig_port(name,verify) do |port|
            block.call OutputPort.new(self,port)
        end
    else
        OutputPort.new(self,orig_port(name))
    end
end

#property(name, options = {}, &block) ⇒ Object


102
103
104
105
106
107
108
109
110
# File 'lib/orocos/async/log/task_context.rb', line 102

def property(name,options={},&block)
    if block
        orig_property(name) do |prop|
            block.call Property.new(self,prop)
        end
    else
        Property.new(self,orig_property(name))
    end
end

#really_add_listener(listener) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/orocos/async/log/task_context.rb', line 61

def really_add_listener(listener)
    return super unless listener.use_last_value?

    # call new listeners with the current value
    # to prevent different behaviors depending on
    # the calling order
    if listener.event == :state_change
        state = @delegator_obj.current_state
        event_loop.once{listener.call state} if state
    elsif listener.event == :port_reachable
        event_loop.once do
            port_names.each do |name|
                listener.call name if @delegator_obj.port(name).used?
            end
        end
    elsif listener.event == :property_reachable
        event_loop.once do
            property_names.each do |name|
                listener.call name if @delegator_obj.property(name).used?
            end
        end
    elsif listener.event == :attribute_reachable
        event_loop.once do
            attribute_names.each do |name|
                listener.call name if @delegator_obj.attribute(name).used?
            end
        end
    end
    super
end

#ruby_task_context?Boolean

Checks whether this log task has a corresponding ruby task context

See Also:


57
58
59
# File 'lib/orocos/async/log/task_context.rb', line 57

def ruby_task_context?
    !!@ruby_task_context
end

#to_async(options = Hash.new) ⇒ Object


122
123
124
# File 'lib/orocos/async/log/task_context.rb', line 122

def to_async(options=Hash.new)
    self
end

#to_proxy(options = Hash.new) ⇒ Object


126
127
128
129
# File 'lib/orocos/async/log/task_context.rb', line 126

def to_proxy(options=Hash.new)
    options[:use] ||= self
    Orocos::Async.proxy(name,options).wait
end

#to_rubyRubyTasks::TaskContext

Creates a RubyTasks::TaskContext on which all logged values should be mirrored

It will create only one such task context, i.e. the method will always return the same object


49
50
51
# File 'lib/orocos/async/log/task_context.rb', line 49

def to_ruby
    @ruby_task_context ||= TaskContextBase.to_ruby(self)
end