class Orocos::Log::TaskContext
Simulates task based on a log file. Each stream is modeled as one OutputPort which supports the #connect_to method
Attributes
Public Class Methods
Creates a new instance of TaskContext.
-
task_name => name of the task
-
#file_path => path of the log file
# File lib/orocos/log/task_context.rb, line 626 def initialize(log_replay,task_name,file_path,file_path_config) super(task_name) self.model = Orocos.create_orogen_task_context_model @log_replay = log_replay @invalid_ports = Hash.new # ports that could not be loaded @file_path = file_path @file_path_config = nil @file_path_config_reg = file_path_config @rtt_state = :RUNNING @port_reachable_blocks = Array.new @property_reachable_blocks = Array.new @state_change_blocks = Array.new end
Public Instance Methods
Adds a new port to the TaskContext
-
#file_path = path of the log file
-
stream = stream which shall be simulated as OutputPort
# File lib/orocos/log/task_context.rb, line 763 def add_port(file_path,stream) #overwrite ports if the file is different and newer than the current one if @file_path && @file_path != file_path if File.new(@file_path).ctime < File.new(file_path ).ctime Log.warn "For task #{name} using ports from \"#{file_path}\" instead of \"#{@file_path}\", because the file is more recent." @ports.clear @file_path = file_path else Log.warn "For task #{name} ommiting log file \"#{file_path}\", because it is older than \"#{@file_path}\"." return nil end else @file_path = file_path end begin log_port = OutputPort.new(self,stream) raise ArgumentError, "The log file #{file_path} is already loaded" if @ports.has_key?(log_port.name) @ports[log_port.name] = log_port @port_reachable_blocks.each{|b|b.call(log_port.name)} rescue InitializePortError => error @invalid_ports[error.port_name] = error.message raise error end #connect state with task state if log_port.name == "state" log_port.on_data do |sample| @rtt_state = sample end log_port.tracked = false end log_port end
Adds a new property to the TaskContext
-
#file_path = path of the log file
-
stream = stream which shall be simulated as OutputPort
# File lib/orocos/log/task_context.rb, line 743 def add_property(file_path,stream) if @file_path_config && !Regexp.new(@file_path_config).match(file_path) raise "You are trying to add properties to the task from different log files #{@file_path}; #{file_path}!!!" if @file_path_config != file_path end if @file_path == file_path @file_path = nil end @file_path_config = file_path log_property = Property.new(self,stream) raise ArgumentError, "The log file #{file_path} is already loaded" if @properties.has_key?(log_property.name) @properties[log_property.name] = log_property @property_reachable_blocks.each{|b|b.call(log_property.name)} return log_property end
Adds a new property or port to the TaskContext
-
#file_path = path of the log file
-
stream = stream which shall be simulated as OutputPort
# File lib/orocos/log/task_context.rb, line 730 def add_stream(file_path,stream) if Regexp.new(@file_path_config_reg).match(file_path) || stream.metadata["rock_stream_type"] == "property" log = add_property(file_path,stream) else log = add_port(file_path,stream) end log end
Returns the array of the names of available attributes on this task context
# File lib/orocos/log/task_context.rb, line 676 def attribute_names Array.new end
# File lib/orocos/log/task_context.rb, line 883 def cleanup(*args) raise "Task #{name} does not support this operation" end
Clears all reader buffers
# File lib/orocos/log/task_context.rb, line 865 def clear_reader_buffers @ports.each_value do |port| port.clear_reader_buffers end end
# File lib/orocos/log/task_context.rb, line 875 def configure(*args) raise "Task #{name} does not support this operation" end
# File lib/orocos/log/task_context.rb, line 823 def connect_to(task=nil,policy = OutputPort::default_policy,&block) Orocos::TaskContext.connect_to(self,task,policy,&block) end
# File lib/orocos/log/task_context.rb, line 640 def current_state=(val) @current_state=val @state_change_blocks.each do |b| b.call val end end
# File lib/orocos/log/task_context.rb, line 659 def on_port_reachable(&block) @port_reachable_blocks << block end
# File lib/orocos/log/task_context.rb, line 663 def on_property_reachable(&block) @property_reachable_blocks << block end
# File lib/orocos/log/task_context.rb, line 647 def on_state_change(&block) @state_change_blocks << block end
Returns the array of the names of available operations on this task context
# File lib/orocos/log/task_context.rb, line 682 def operation_names Array.new end
# File lib/orocos/log/task_context.rb, line 696 def ping true end
Returns the port with the given name. If no port can be found a exception is raised.
# File lib/orocos/log/task_context.rb, line 715 def port(name, verify = true) name = name.to_str if @ports[name] @ports[name] elsif @invalid_ports[name] raise NotFound, "the port named '#{name}' on log task '#{self.name}' could not be loaded: #{@invalid_ports[name]}" else raise NotFound, "no port named '#{name}' on log task '#{self.name}'" end end
Returns the names of all the ports defined on this task context
# File lib/orocos/log/task_context.rb, line 687 def port_names @ports.keys end
Returns the property with the given name. If no port can be found a exception is raised.
# File lib/orocos/log/task_context.rb, line 702 def property(name, verify = true) name = name.to_str if @properties[name] p = @properties[name] p.tracked = true p else raise NotFound, "no property named '#{name}' on log task '#{self.name}'" end end
Returns the array of the names of available properties on this task context
# File lib/orocos/log/task_context.rb, line 670 def property_names @properties.values.map(&:name) end
# File lib/orocos/log/task_context.rb, line 655 def rename(name) @name = name end
Reads the state
# File lib/orocos/log/task_context.rb, line 692 def rtt_state @rtt_state end
# File lib/orocos/log/task_context.rb, line 871 def start(*args) raise "Task #{name} does not support this operation" end
# File lib/orocos/log/task_context.rb, line 879 def stop(*args) raise "Task #{name} does not support this operation" end
# File lib/orocos/async/orocos.rb, line 16 def to_async(options = Hash.new) Orocos::Async::Log::TaskContext.new(self,options) end
# File lib/orocos/async/orocos.rb, line 20 def to_proxy(options = Hash.new) log_replay.name_service_async.proxy(name,:use => to_async).wait end
# File lib/orocos/log/task_context.rb, line 651 def to_s "#<Orocos::Log::TaskContext: #{name}>" end
If set to true all ports are replayed otherwise only ports are replayed which have a reader or a connection to an other port
# File lib/orocos/log/task_context.rb, line 830 def track(value,filter = Hash.new) options, filter = Kernel::filter_options(filter,[:ports,:types,:limit]) raise "Cannot understand filter: #{filter}" unless filter.empty? @ports.each_value do |port| if(options.has_key? :ports) next unless port.name =~ options[:ports] end if(options.has_key? :types) next unless port.type_name =~ options[:types] end if(options.has_key? :limit) next unless port.number_of_samples <= options[:limit] end port.tracked = value Log.info "set" + port.stream.name + value.to_s end @properties.each_value do |property| if(options.has_key? :propertys) next unless property.name =~ options[:properties] end if(options.has_key? :types) next unless property.type_name =~ options[:types] end if(options.has_key? :limit) next unless property.number_of_samples <= options[:limit] end property.tracked = value @tracked = value Log.info "set" + property.stream.name + value.to_s end end
Returns an array of unused ports
# File lib/orocos/log/task_context.rb, line 815 def unused_ports ports = Array.new @ports.each_value do |port| ports << port if !port.used? end return ports end
Returns true if the task shall be replayed
# File lib/orocos/log/task_context.rb, line 810 def used? !used_ports.empty? || !used_properties.empty? end
Returns an array of ports where each port has at least one connection or tracked set to true.
# File lib/orocos/log/task_context.rb, line 799 def used_ports @ports.values.find_all &:used? end
Returns an array of ports where each port has at least one connection or tracked set to true.
# File lib/orocos/log/task_context.rb, line 805 def used_properties @properties.values.find_all &:used? end