Class: Roby::PlanningTask

Inherits:
Task show all
Defined in:
lib/roby/planning/task.rb

Overview

An asynchronous planning task using Ruby threads

Defined Under Namespace

Classes: TransactionProxy

Constant Summary

Constants included from Log::TaskHooks

Log::TaskHooks::HOOKS

Constants included from Log::BasicObjectHooks

Log::BasicObjectHooks::HOOKS

Instance Attribute Summary collapse

Attributes inherited from PlanObject

#executable, #plan, #removed_at

Attributes inherited from BasicObject

#distribute

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Task

#_dump, _load, #droby_dump, improves, #improves?, improves?, match, needs, #needs?, needs?

Methods included from TaskStructure::ModelConflicts

#conflicts_with, #conflicts_with?

Methods included from Distributed::DRobyTaskModel::Dump

#droby_dump

Methods included from Distributed::DRobyModel::Dump

#droby_dump

Methods included from Distributed::TaskNotifications

#updated_data

Methods included from TaskOperations

#+, #|

Methods included from Log::TaskHooks

#added_child_object, #removed_child_object

Methods inherited from PlanObject

#add_child_object, #apply_relation_changes, child_plan_object, #each_plan_child, #executable?, #finalized?, #forget_peer, #read_write?, #remotely_useful?, #removing_child_object, #replace_by, #replace_subplan_by, #root_object, #root_object?, #subscribed?, #update_on?, #updated_by?

Methods included from Distributed::RelationModificationHooks

#added_child_object, #removed_child_object

Methods included from Transactions::PlanObjectUpdates

#adding_child_object, #removing_child_object

Methods included from DirectedRelationSupport

#add_child_object, #add_parent_object, #check_is_relation, #related_objects, #relations, #remove_child_object, #remove_children, #remove_parent_object, #remove_parents, #remove_relations

Methods inherited from BasicObject

#add_sibling_for, #distribute?, distribute?, #finalized?, #forget_peer, #has_sibling_on?, #initialize_copy, local_only, #read_write?, #remotely_useful?, #remove_sibling_for, #self_owned?, #sibling_of, #sibling_on, #subscribe, #subscribed?, #update_on?, #updated?, #updated_by?, #updated_peers

Methods included from Log::BasicObjectHooks

#added_owner, #removed_owner

Constructor Details

#initialize(options) ⇒ PlanningTask

Returns a new instance of PlanningTask.



35
36
37
38
# File 'lib/roby/planning/task.rb', line 35

def initialize(options)
      task_options = PlanningTask.filter_options(options)
    super(task_options)
end

Instance Attribute Details

#plannerObject (readonly)

Returns the value of attribute planner.



9
10
11
# File 'lib/roby/planning/task.rb', line 9

def planner
  @planner
end

#resultObject (readonly)

The planner result. It is either an exception or a task object



68
69
70
# File 'lib/roby/planning/task.rb', line 68

def result
  @result
end

#threadObject (readonly)

The thread that is running the planner



63
64
65
# File 'lib/roby/planning/task.rb', line 63

def thread
  @thread
end

#transactionObject (readonly)

The transaction in which we build the new plan. It gets committed on success.



66
67
68
# File 'lib/roby/planning/task.rb', line 66

def transaction
  @transaction
end

Class Method Details

.filter_options(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/roby/planning/task.rb', line 15

def self.filter_options(options)
    task_options, method_options = Kernel.filter_options options,
  :planner_model => nil,
  :method_name => nil,
  :method_options => {},
  :planned_model => nil,
  :planning_owners => nil

    if !task_options[:planner_model]
  raise ArgumentError, "missing required argument 'planner_model'"
    elsif !task_options[:method_name]
  raise ArgumentError, "missing required argument 'method_name'"
    end
    task_options[:planned_model] ||= nil
    task_options[:method_options] ||= Hash.new
    task_options[:method_options].merge! method_options
    task_options[:planning_owners] ||= nil
    task_options
end

Instance Method Details

#planned_modelObject



40
41
42
# File 'lib/roby/planning/task.rb', line 40

def planned_model
    arguments[:planned_model] ||= planner_model.model_of(method_name, method_options).returns || Roby::Task
end

#planned_taskObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/roby/planning/task.rb', line 49

def planned_task
    if success? || result
  result
    elsif task = planned_tasks.find { true }
  task
    elsif pending?
  task = planned_model.new
  task.planned_by self
  task.executable = false
  task
    end
end

#planning_thread(context) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/roby/planning/task.rb', line 90

def planning_thread(context)
    result_task = planner.send(method_name, method_options.merge(:context => context))

    # Don't replace the planning task with ourselves if the
    # transaction specifies another planning task
    if !result_task.planning_task
  result_task.planned_by transaction[self]
    end

    if placeholder = planned_task
  placeholder = transaction[placeholder]
  transaction.replace(placeholder, result_task)
  placeholder.remove_planning_task transaction[self]
    end

    # If the transaction is distributed, and is not proposed to all
    # owners, do it
    transaction.propose
    transaction.commit_transaction do
  @result = result_task
    end
end

#to_sObject



45
46
47
# File 'lib/roby/planning/task.rb', line 45

def to_s
    "#{super}[#{method_name}:#{method_options}] -> #{@planned_task || "nil"}"
end