Class: Orocos::ProcessBase

Inherits:
Object
  • Object
show all
Defined in:
lib/orocos/process.rb

Overview

Base class for process representation objects

Constant Summary collapse

@@logfile_indexes =
Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, model) ⇒ ProcessBase

Returns a new instance of ProcessBase.


166
167
168
169
170
171
# File 'lib/orocos/process.rb', line 166

def initialize(name, model)
    @name, @model = name, model
    @name_mappings = Hash.new
    @logged_ports = Set.new
    @tasks = []
end

Instance Attribute Details

#default_logger#log, false

Returns the logger object that should be used, by default, to log data coming out of this process, or false if none can be found.

Returns:

  • (#log, false)

    the logger object that should be used, by default, to log data coming out of this process, or false if none can be found


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/orocos/process.rb', line 301

def default_logger
    if !@logger.nil?
        return @logger
    end

    if logger_name = default_logger_name
        begin
            @logger = TaskContext.get logger_name
        rescue Orocos::NotFound
            Orocos.warn "no default logger defined on #{name}, tried #{logger_name}"
            @logger = false # use false to mark "can not find"
        end
    else
        if Orocos.warn_for_missing_default_loggers?
            Orocos.warn "cannot determine the default logger name for process #{name}"
        end
        @logger = false
    end

    @logger
end

#logged_portsObject (readonly)

The set of [task_name, port_name] that represent the ports being currently logged by this process' default logger


161
162
163
# File 'lib/orocos/process.rb', line 161

def logged_ports
  @logged_ports
end

#modelOroGen::Spec::Deployment (readonly)

The deployment oroGen model

Returns:

  • (OroGen::Spec::Deployment)

152
153
154
# File 'lib/orocos/process.rb', line 152

def model
  @model
end

#nameString (readonly)

The process name

Returns:

  • (String)

149
150
151
# File 'lib/orocos/process.rb', line 149

def name
  @name
end

#name_mappingsObject

A set of mappings from task names in the deployment model to the actual name in the running process


158
159
160
# File 'lib/orocos/process.rb', line 158

def name_mappings
  @name_mappings
end

#tasksObject (readonly)

The set of task contexts for this process. This is valid only after the process is actually started


164
165
166
# File 'lib/orocos/process.rb', line 164

def tasks
  @tasks
end

Class Method Details

.resolve_prefix(model, prefix) ⇒ Hash<String,String>

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.

Applies a prefix to this process' task names and returns the names

Parameters:

  • model (OroGen::Spec::Deployment)

    the deployment model

  • prefix (String, nil)

    the prefix string, no prefix is going to be applied if it is nil

Returns:

  • (Hash<String,String>)

    the name mappings that should be applied when spawning the process


333
334
335
336
337
338
339
340
341
# File 'lib/orocos/process.rb', line 333

def self.resolve_prefix(model, prefix)
    name_mappings = Hash.new
    if prefix
        model.task_activities.each do |act|
            name_mappings[act.name] = "#{prefix}#{act.name}"
        end
    end
    return name_mappings
end

Instance Method Details

#default_logger_nameString

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.

Returns the name of the default logger for this process.

Returns:

  • (String)

    the name of the default logger for this process


281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/orocos/process.rb', line 281

def default_logger_name
    candidates = model.task_activities.
        find_all { |d| d.task_model.name == "logger::Logger" }.
        map { |c| name_mappings[c.name] || c.name }

    if candidates.size > 1
        if t = candidates.find { |c| c.name == "#{process.name}_Logger" }
            return t.name
        end
    elsif candidates.size == 1
        return candidates.first
    end
end

#each_taskObject

Enumerate the TaskContext instances of the tasks that are running in this process.

See also #task_names


213
214
215
216
217
218
# File 'lib/orocos/process.rb', line 213

def each_task
    return enum_for(:each_task) if !block_given?
    task_names.each do |name|
        yield(task(name))
    end
end

#get_mapped_name(name) ⇒ 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.

use a mapping if exists


192
193
194
# File 'lib/orocos/process.rb', line 192

def get_mapped_name(name)
    name_mappings[name] || name
end

#log_all_ports(options = Hash.new) ⇒ Object

Requires all known ports of self to be logged by the default logger


246
247
248
# File 'lib/orocos/process.rb', line 246

def log_all_ports(options = Hash.new)
    @logged_ports |= Orocos.log_all_process_ports(self, options)
end

#map_name(old, new) ⇒ Object

Require that to rename the task called old in this deployment to new during execution

See Also:


185
186
187
# File 'lib/orocos/process.rb', line 185

def map_name(old, new)
    name_mappings[old] = new
end

#orogenObject

Deprecated.

For backward compatibility only


155
# File 'lib/orocos/process.rb', line 155

def orogen; model end

#register_task(task) ⇒ Object


240
241
242
243
# File 'lib/orocos/process.rb', line 240

def register_task(task)
    @tasks.delete_if { |t| t.name == task.name }
    @tasks << task
end

#setup_default_logger(remote: false, log_dir: Orocos.default_working_directory) ⇒ 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.

Sets up the default logger of this process


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/orocos/process.rb', line 255

def setup_default_logger(remote: false, log_dir: Orocos.default_working_directory)
    is_remote     = remote
    log_dir       = log_dir

    if !(logger = self.default_logger)
        return
    end
    log_file_name = logger.basename[/.*(?=_[L|l]ogger)/] || logger.basename

    index = 0
    if remote
        index = (@@logfile_indexes[name] ||= -1) + 1
        @@logfile_indexes[name] = index
        logger.file = "#{log_file_name}.#{index}.log"
    else
        while File.file?( logfile = File.join(log_dir, "#{log_file_name}.#{index}.log"))
            index += 1
        end
        logger.file = logfile 
    end
    logger
end

#task(task_name, name_service = Orocos.name_service) ⇒ Object

Returns the TaskContext instance for a task that runs in this process, or raises Orocos::NotFound.


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/orocos/process.rb', line 222

def task(task_name, name_service = Orocos.name_service)
    full_name = "#{name}_#{task_name}"
    if result = tasks.find { |t| t.basename == task_name || t.basename == full_name }
        return result
    end

    result = if task_names.include?(task_name)
                 name_service.get task_name, process: self
             elsif task_names.include?(full_name)
                 name_service.get full_name, process: self
             else
                 raise Orocos::NotFound, "no task #{task_name} defined on #{name}"
             end

    @tasks << result
    result
end

#task_namesObject

Returns the name of the tasks that are running in this process

See also #each_task


199
200
201
202
203
204
205
206
207
# File 'lib/orocos/process.rb', line 199

def task_names
    if !model
        raise Orocos::NotOrogenComponent, "#{name} does not seem to have been generated by orogen"
    end
    model.task_activities.map do |deployed_task|
        name = deployed_task.name
        get_mapped_name(name)
    end
end