Class: Orocos::Log::TaskContext

Inherits:
TaskContextBase show all
Includes:
Namespace
Defined in:
lib/orocos/log/task_context.rb,
lib/orocos/async/orocos.rb

Overview

Simulates task based on a log file. Each stream is modeled as one OutputPort which supports the connect_to method

Constant Summary

Constants included from Namespace

Namespace::DELIMATOR

Constants inherited from TaskContextBase

TaskContextBase::RUNNING_STATES

Instance Attribute Summary collapse

Attributes inherited from TaskContextBase

#attributes, #configuration_log, #current_state, #ior, #process, #properties

Instance Method Summary collapse

Methods included from Namespace

#basename, #map_to_namespace, #namespace, #namespace=, #same_namespace?, split_name, #split_name, validate_namespace_name, #verify_same_namespace

Methods inherited from TaskContextBase

#add_default_states, #available_states, #basename, connect_to, #doc, #doc?, #each_attribute, #each_input_port, #each_operation, #each_output_port, #each_port, #each_property, #error?, #error_state?, #exception?, #exception_state?, #fatal_error?, #fatal_error_state?, find_one_running, get, get_provides, #has_attribute?, #has_operation?, #has_port?, #has_property?, #implements?, #info, #input_port, #input_port_model, #inspect, #model, #model=, #name, #on_localhost?, #output_port, #output_port_model, #peek_current_state, #peek_state, #ports, #pretty_print, reachable?, #reachable?, #ready?, #running?, #runtime_error?, #runtime_state?, #state, #state_changed?, #states, #to_h, #toplevel_state

Methods included from PortsSearchable

#find_all_input_ports, #find_all_output_ports, #find_all_ports, #find_input_port, #find_output_port, #find_port

Methods included from TaskContextBaseAbstract

#attribute, #operation

Constructor Details

#initialize(log_replay, task_name, file_path, file_path_config) ⇒ TaskContext

Creates a new instance of TaskContext.

  • task_name => name of the task

  • file_path => path of the log file


626
627
628
629
630
631
632
633
634
635
636
637
638
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

This is used to allow the following syntax task.port_name.connect_to(other_port)


889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
# File 'lib/orocos/log/task_context.rb', line 889

def method_missing(m, *args,&block) #:nodoc:
    m = m.to_s
    if m =~ /^(\w+)=/
        name = $1
        Log.warn "Setting the property #{name} the TaskContext #{@name} is not supported"
        return
    end
    if has_port?(m) 
        _port = port(m)
        _port.filter = block if block         #overwirte filer
        return _port
    end
    if has_property?(m) 
        return property(m)
    end
    begin
        super(m.to_sym,*args,&block)
    rescue  NoMethodError => e
        if m.to_sym != :to_ary
            Log.error "#{m} is neither a port nor a property of #{self.name}"
            Log.error "The following ports are availabe:"
            @ports.each_value do |port|
                Log.error "  #{port.name}"
            end
            Log.error "The following properties are availabe:"
            @properties.each_value do |property|
                Log.error "  #{property.name}"
            end
        end
        raise e
    end
end

Instance Attribute Details

#file_pathObject (readonly)

path of the dedicated log file


618
619
620
# File 'lib/orocos/log/task_context.rb', line 618

def file_path
  @file_path
end

#file_path_configObject (readonly)

path of the dedicated log configuration file


619
620
621
# File 'lib/orocos/log/task_context.rb', line 619

def file_path_config
  @file_path_config
end

#log_replayObject (readonly)

Returns the value of attribute log_replay


620
621
622
# File 'lib/orocos/log/task_context.rb', line 620

def log_replay
  @log_replay
end

Instance Method Details

#add_port(file_path, stream) ⇒ Object

Adds a new port to the TaskContext

  • file_path = path of the log file

  • stream = stream which shall be simulated as OutputPort


763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
# 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

#add_property(file_path, stream) ⇒ Object

Adds a new property to the TaskContext

  • file_path = path of the log file

  • stream = stream which shall be simulated as OutputPort

Raises:

  • (ArgumentError)

743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# 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

#add_stream(file_path, stream) ⇒ Object

Adds a new property or port to the TaskContext

  • file_path = path of the log file

  • stream = stream which shall be simulated as OutputPort


730
731
732
733
734
735
736
737
# 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.["rock_stream_type"] == "property"
        log = add_property(file_path,stream)
    else
        log = add_port(file_path,stream)
    end
    log
end

#attribute_namesObject

Returns the array of the names of available attributes on this task context


676
677
678
# File 'lib/orocos/log/task_context.rb', line 676

def attribute_names
    Array.new
end

#cleanup(*args) ⇒ Object


883
884
885
# File 'lib/orocos/log/task_context.rb', line 883

def cleanup(*args)
    raise "Task #{name} does not support this operation"
end

#clear_reader_buffersObject

Clears all reader buffers


865
866
867
868
869
# File 'lib/orocos/log/task_context.rb', line 865

def clear_reader_buffers
    @ports.each_value do |port|
        port.clear_reader_buffers
    end
end

#configure(*args) ⇒ Object


875
876
877
# File 'lib/orocos/log/task_context.rb', line 875

def configure(*args)
    raise "Task #{name} does not support this operation"
end

#connect_to(task = nil, policy = OutputPort::default_policy, &block) ⇒ Object


823
824
825
# 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

#current_state=(val) ⇒ Object


640
641
642
643
644
645
# 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

#on_port_reachable(&block) ⇒ Object


659
660
661
# File 'lib/orocos/log/task_context.rb', line 659

def on_port_reachable(&block)
    @port_reachable_blocks << block
end

#on_property_reachable(&block) ⇒ Object


663
664
665
# File 'lib/orocos/log/task_context.rb', line 663

def on_property_reachable(&block)
    @property_reachable_blocks << block
end

#on_state_change(&block) ⇒ Object


647
648
649
# File 'lib/orocos/log/task_context.rb', line 647

def on_state_change(&block)
    @state_change_blocks << block
end

#operation_namesObject

Returns the array of the names of available operations on this task context


682
683
684
# File 'lib/orocos/log/task_context.rb', line 682

def operation_names
    Array.new
end

#pingObject


696
697
698
# File 'lib/orocos/log/task_context.rb', line 696

def ping
    true
end

#port(name, verify = true) ⇒ Object

Returns the port with the given name. If no port can be found a exception is raised.


715
716
717
718
719
720
721
722
723
724
# 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

#port_namesObject

Returns the names of all the ports defined on this task context


687
688
689
# File 'lib/orocos/log/task_context.rb', line 687

def port_names
    @ports.keys
end

#property(name, verify = true) ⇒ Object

Returns the property with the given name. If no port can be found a exception is raised.


702
703
704
705
706
707
708
709
710
711
# 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

#property_namesObject

Returns the array of the names of available properties on this task context


670
671
672
# File 'lib/orocos/log/task_context.rb', line 670

def property_names
    @properties.values.map(&:name)
end

#rename(name) ⇒ Object


655
656
657
# File 'lib/orocos/log/task_context.rb', line 655

def rename(name)
    @name = name
end

#rtt_stateObject

Reads the state


692
693
694
# File 'lib/orocos/log/task_context.rb', line 692

def rtt_state
    @rtt_state
end

#start(*args) ⇒ Object


871
872
873
# File 'lib/orocos/log/task_context.rb', line 871

def start(*args)
    raise "Task #{name} does not support this operation"
end

#stop(*args) ⇒ Object


879
880
881
# File 'lib/orocos/log/task_context.rb', line 879

def stop(*args)
    raise "Task #{name} does not support this operation"
end

#to_async(options = Hash.new) ⇒ Object


16
17
18
# File 'lib/orocos/async/orocos.rb', line 16

def to_async(options = Hash.new)
    Orocos::Async::Log::TaskContext.new(self,options)
end

#to_proxy(options = Hash.new) ⇒ Object


20
21
22
# 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

#to_sObject


651
652
653
# File 'lib/orocos/log/task_context.rb', line 651

def to_s
    "#<Orocos::Log::TaskContext: #{name}>"
end

#track(value, filter = Hash.new) ⇒ Object

If set to true all ports are replayed otherwise only ports are replayed which have a reader or a connection to an other port


830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
# 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

#unused_portsObject

Returns an array of unused ports


815
816
817
818
819
820
821
# 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

#used?Boolean

Returns true if the task shall be replayed

Returns:

  • (Boolean)

810
811
812
# File 'lib/orocos/log/task_context.rb', line 810

def used?
    !used_ports.empty? || !used_properties.empty?
end

#used_portsObject

Returns an array of ports where each port has at least one connection or tracked set to true.


799
800
801
# File 'lib/orocos/log/task_context.rb', line 799

def used_ports
    @ports.values.find_all &:used?
end

#used_propertiesObject

Returns an array of ports where each port has at least one connection or tracked set to true.


805
806
807
# File 'lib/orocos/log/task_context.rb', line 805

def used_properties
    @properties.values.find_all &:used?
end