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, #state_symbols

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, #pre_operational?, #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) ⇒ TaskContext #initialize(log_replay, task_name, file_path, file_path_config) ⇒ TaskContext

Creates a new instance of TaskContext.

Overloads:

  • #initialize(log_replay, task_name) ⇒ TaskContext

    Parameters:

    • log_replay (Orocos::Log::Replay)

      the replay instance

    • task_name (String)

      the task name

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

    Deprecated, use the other form



650
651
652
653
654
655
656
657
658
659
# File 'lib/orocos/log/task_context.rb', line 650

def initialize(log_replay,task_name,file_path = nil,file_path_config = nil)
    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
    @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)



922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
# File 'lib/orocos/log/task_context.rb', line 922

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



638
639
640
# File 'lib/orocos/log/task_context.rb', line 638

def file_path
  @file_path
end

#file_path_configObject (readonly)

path of the dedicated log configuration file



639
640
641
# File 'lib/orocos/log/task_context.rb', line 639

def file_path_config
  @file_path_config
end

#log_replayObject (readonly)

Returns the value of attribute log_replay



640
641
642
# File 'lib/orocos/log/task_context.rb', line 640

def log_replay
  @log_replay
end

Instance Method Details

#add_port(stream) ⇒ Log::Property #add_port(file_path, stream) ⇒ Object

Adds a new port to the TaskContext, backed by a data stream

Overloads:

  • #add_port(stream) ⇒ Log::Property

    Parameters:

    • stream (Pocolog::Datastream)

      the stream that backs the new port

    Returns:

  • #add_port(file_path, stream) ⇒ Object

    Deprecated. Use the other form.



805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
# File 'lib/orocos/log/task_context.rb', line 805

def add_port(stream, _backward = nil)
    stream = _backward if _backward
    begin
        log_port = OutputPort.new(self, stream)
        if @ports.has_key?(log_port.name)
            raise ArgumentError, "port '#{log_port.name}' already exists, "\
                "probably from a different log stream"
        end
        @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(stream) ⇒ Log::Property #add_port(file_path, stream) ⇒ Object

Adds a new property to the TaskContext, backed by a datastream

Overloads:

  • #add_property(stream) ⇒ Log::Property

    Parameters:

    • stream (Pocolog::Datastream)

      the stream that backs the new property

    Returns:

  • #add_port(file_path, stream) ⇒ Object

    Deprecated. Use the other form.



784
785
786
787
788
789
790
791
792
793
794
# File 'lib/orocos/log/task_context.rb', line 784

def add_property(stream, _backward = nil)
    stream = _backward if _backward
    log_property = Property.new(self,stream)
    if @properties.has_key?(log_property.name)
        raise ArgumentError, "property '#{log_property.name}' already "\
            "exists, probably from a different log stream"
    end
    @properties[log_property.name] = log_property
    @property_reachable_blocks.each{|b|b.call(log_property.name)}
    return log_property
end

#add_stream(stream) ⇒ Log::Property, Log::TaskContext

Register a property/port backed by a data stream

The two types are recognized by the rock_stream_type metadata, which should either be 'port' or 'property'.

Parameters:

  • stream (Pocolog::Datastream)

    the property/port type is autodetected by the rock_stream_type metadata ('property' or 'port')

Returns:



756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# File 'lib/orocos/log/task_context.rb', line 756

def add_stream(stream, _backward = nil, type: nil)
    stream = _backward if _backward
    type ||= stream.["rock_stream_type"]
    case type
    when "property"
        add_property(stream)
    when "port"
        add_port(stream)
    when NilClass
        raise ArgumentError, "stream '#{stream.name}' has no "\
            "rock_stream_type metadata, cannot guess whether it should "\
            "back a port or a property"
    else
        raise ArgumentError, "the rock_stream_type metadata of "\
            "'#{stream.name}' is '#{type}', expected either "\
            "'port' or 'property'"
    end
end

#attribute_namesObject

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



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

def attribute_names
    Array.new
end

#cleanup(*args) ⇒ Object



916
917
918
# File 'lib/orocos/log/task_context.rb', line 916

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

#clear_reader_buffersObject

Clears all reader buffers



898
899
900
901
902
# File 'lib/orocos/log/task_context.rb', line 898

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

#configure(*args) ⇒ Object



908
909
910
# File 'lib/orocos/log/task_context.rb', line 908

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

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



856
857
858
# File 'lib/orocos/log/task_context.rb', line 856

def connect_to(task=nil,policy = OutputPort::default_policy,&block)
    Orocos::TaskContext.connect_to(self,task,policy,&block)
end

#current_state=(val) ⇒ Object



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

def current_state=(val)
    @current_state=val
    @state_change_blocks.each do |b|
        b.call val
    end
end

#on_port_reachable(&block) ⇒ Object



680
681
682
# File 'lib/orocos/log/task_context.rb', line 680

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

#on_property_reachable(&block) ⇒ Object



684
685
686
# File 'lib/orocos/log/task_context.rb', line 684

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

#on_state_change(&block) ⇒ Object



668
669
670
# File 'lib/orocos/log/task_context.rb', line 668

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



703
704
705
# File 'lib/orocos/log/task_context.rb', line 703

def operation_names
    Array.new
end

#pingObject



717
718
719
# File 'lib/orocos/log/task_context.rb', line 717

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.



736
737
738
739
740
741
742
743
744
745
# File 'lib/orocos/log/task_context.rb', line 736

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



708
709
710
# File 'lib/orocos/log/task_context.rb', line 708

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.



723
724
725
726
727
728
729
730
731
732
# File 'lib/orocos/log/task_context.rb', line 723

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



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

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

#rename(name) ⇒ Object



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

def rename(name)
    @name = name
end

#rtt_stateObject

Reads the state



713
714
715
# File 'lib/orocos/log/task_context.rb', line 713

def rtt_state
    @rtt_state
end

#start(*args) ⇒ Object



904
905
906
# File 'lib/orocos/log/task_context.rb', line 904

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

#stop(*args) ⇒ Object



912
913
914
# File 'lib/orocos/log/task_context.rb', line 912

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



672
673
674
# File 'lib/orocos/log/task_context.rb', line 672

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



863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
# File 'lib/orocos/log/task_context.rb', line 863

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



848
849
850
851
852
853
854
# File 'lib/orocos/log/task_context.rb', line 848

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)


843
844
845
# File 'lib/orocos/log/task_context.rb', line 843

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.



832
833
834
# File 'lib/orocos/log/task_context.rb', line 832

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.



838
839
840
# File 'lib/orocos/log/task_context.rb', line 838

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