Class: Roby::TaskStructure::Dependency
- Inherits:
-
Relations::TaskRelationGraph
- Object
- Relations::Graph
- Relations::TaskRelationGraph
- Roby::TaskStructure::Dependency
- Defined in:
- lib/roby/task_structure/dependency.rb
Defined Under Namespace
Modules: Extension, ModelExtension
Instance Attribute Summary collapse
-
#failing_tasks ⇒ Object
readonly
Returns the value of attribute failing_tasks.
-
#interesting_events ⇒ Object
readonly
Returns the value of attribute interesting_events.
Class Method Summary collapse
-
.merge_dependency_options(opt1, opt2) ⇒ Hash
Merges the dependency descriptions (i.e. the relation payload), verifying that the two provided option hashes are compatible.
- .merge_fullfilled_model(model, required_models, required_arguments) ⇒ Object
- .validate_options(options, defaults = {}) ⇒ Object
Instance Method Summary collapse
-
#check_structure(plan) ⇒ Object
Checks the structure of
planw.r.t. -
#initialize(observer: nil) ⇒ Dependency
constructor
A new instance of Dependency.
-
#merge_info(parent, child, opt1, opt2) ⇒ Object
Called by the relation management when two dependency relations need to be merged.
-
#update_triggers_for(parent, child, info) ⇒ Object
private
Updates the dependency internal data to trigger errors / success when relevant events are emitted.
- #watch_emission(generator) ⇒ Object
- #watch_unreachability(generator) ⇒ Object
- #watching_emissions?(generator) ⇒ Boolean
- #watching_unreachability?(generator) ⇒ Boolean
Methods included from Relations::Models::TaskRelationGraph
Constructor Details
#initialize(observer: nil) ⇒ Dependency
Returns a new instance of Dependency.
13 14 15 16 17 18 19 |
# File 'lib/roby/task_structure/dependency.rb', line 13 def initialize(observer: nil) super(observer: observer) @interesting_events = [] @watched_emissions = Set.new @watched_unreachability = Set.new @failing_tasks = Set.new end |
Instance Attribute Details
#failing_tasks ⇒ Object (readonly)
Returns the value of attribute failing_tasks.
11 12 13 |
# File 'lib/roby/task_structure/dependency.rb', line 11 def failing_tasks @failing_tasks end |
#interesting_events ⇒ Object (readonly)
Returns the value of attribute interesting_events.
11 12 13 |
# File 'lib/roby/task_structure/dependency.rb', line 11 def interesting_events @interesting_events end |
Class Method Details
.merge_dependency_options(opt1, opt2) ⇒ Hash
Merges the dependency descriptions (i.e. the relation payload), verifying that the two provided option hashes are compatible
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/roby/task_structure/dependency.rb', line 130 def self.(opt1, opt2) if opt1[:remove_when_done] != opt2[:remove_when_done] raise Roby::ModelViolation, "incompatible dependency specification: trying to change the value of +remove_when_done+" end result = { remove_when_done: opt1[:remove_when_done], consider_in_pending: opt1[:consider_in_pending] } if opt1[:success] || opt2[:success] result[:success] = if !opt1[:success] then opt2[:success] elsif !opt2[:success] then opt1[:success] else opt1[:success].and(opt2[:success]) end end if opt1[:failure] || opt2[:failure] result[:failure] = if !opt1[:failure] then opt2[:failure] elsif !opt2[:failure] then opt1[:failure] else opt1[:failure].or(opt2[:failure]) end end # Check model compatibility models1, arguments1 = opt1[:model] models2, arguments2 = opt2[:model] task_model1 = models1.find { |m| m <= Roby::Task } task_model2 = models2.find { |m| m <= Roby::Task } result_model = [] if task_model1 && task_model2 if task_model1.fullfills?(task_model2) result_model << task_model1 elsif task_model2.fullfills?(task_model1) result_model << task_model2 else raise Roby::ModelViolation, "incompatible models #{task_model1} and #{task_model2}" end elsif task_model1 result_model << task_model1 elsif task_model2 result_model << task_model2 end models1.each do |m| next if m <= Roby::Task if models2.none? { |other_m| other_m.fullfills?(m) } result_model << m end end models2.each do |m| next if m <= Roby::Task if models1.none? { |other_m| other_m.fullfills?(m) } result_model << m end end result[:model] = [result_model] # Merge arguments result[:model][1] = arguments1.merge(arguments2) do |key, old_value, new_value| if old_value != new_value raise Roby::ModelViolation, "incompatible argument constraint #{old_value} and #{new_value} for #{key}" end old_value end result[:model].freeze # Finally, merge the roles (the easy part ;-)) result[:roles] = opt1[:roles] | opt2[:roles] result.freeze result end |
.merge_fullfilled_model(model, required_models, required_arguments) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/roby/task_structure/dependency.rb', line 86 def self.merge_fullfilled_model(model, required_models, required_arguments) model, , arguments = *model = .dup required_models = Array(required_models) for m in required_models if m.kind_of?(Roby::Models::TaskServiceModel) << m elsif m.has_ancestor?(model) model = m elsif !model.has_ancestor?(m) raise Roby::ModelViolation, "inconsistency in fullfilled models: #{model} and #{m} are incompatible" end end .uniq! arguments = arguments.merge(required_arguments) do |name, old, new| if old != new raise Roby::ModelViolation, "inconsistency in fullfilled models: #{old} and #{new}" end old end [model, , arguments] end |
.validate_options(options, defaults = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/roby/task_structure/dependency.rb', line 114 def self.(, defaults = {}) defaults = Hash[model: [[Roby::Task], {}], success: nil, failure: nil, remove_when_done: true, consider_in_pending: true, roles: Set.new, role: nil].merge(defaults) Kernel. , defaults end |
Instance Method Details
#check_structure(plan) ⇒ Object
Checks the structure of plan w.r.t. the constraints of the hierarchy
relations. It returns an array of ChildFailedError for all failed
hierarchy relations
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/roby/task_structure/dependency.rb', line 223 def check_structure(plan) # The Set in #interesting_events is also referenced # *separately* in EventStructure.gather_events. We therefore have to # keep it (and can't use #partition). Yuk events = [] interesting_events.delete_if do |ev| if ev.plan == plan events << ev true else !ev.plan end end tasks = Set.new failing_tasks.delete_if do |task| if task.plan == plan tasks << task true else !task.plan end end return [] if events.empty? && tasks.empty? result = [] # Get the set of tasks for which a possible failure has been # registered The tasks that are failing the hierarchy requirements # are registered in Hierarchy.failing_tasks. events.each do |event| task = event.task tasks << task if event.symbol == :start # also add the children task.each_child do |child_task, _| tasks << child_task end end end for child in tasks # Check if the task has been removed from the plan next unless child.plan removed_parents = [] child.each_parent_task do |parent| next if parent.finished? next unless parent.self_owned? = parent[child, Dependency] success = [:success] failure = [:failure] has_success = success&.evaluate(child) unless has_success has_failure = failure&.evaluate(child) end error = nil if has_success if [:remove_when_done] # Must not delete it here as we are iterating over the # parents removed_parents << parent end elsif has_failure explanation = failure.explain_true(child) error = Roby::ChildFailedError.new(parent, child, explanation, :failed_event) elsif success&.static?(child) explanation = success.explain_static(child) error = Roby::ChildFailedError.new(parent, child, explanation, :unreachable_success) end if error if parent.running? result << error failing_tasks << child elsif [:consider_in_pending] && plan.control.pending_dependency_failed(parent, child, error) result << error failing_tasks << child end end end for parent in removed_parents parent.remove_child child end end result end |
#merge_info(parent, child, opt1, opt2) ⇒ Object
Called by the relation management when two dependency relations need to be merged
212 213 214 215 216 217 218 |
# File 'lib/roby/task_structure/dependency.rb', line 212 def merge_info(parent, child, opt1, opt2) result = Dependency.(opt1, opt2) update_triggers_for(parent, child, result) result rescue Exception => e raise e, e. + " while updating the dependency information for #{parent} -> #{child}", e.backtrace end |
#update_triggers_for(parent, child, info) ⇒ 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.
Updates the dependency internal data to trigger errors / success when relevant events are emitted
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/roby/task_structure/dependency.rb', line 25 def update_triggers_for(parent, child, info) # Transactions and template plans are re-triggering this # # This makes sure that the graph objects are stable and that handlers # won't be duplicated (we assume executable plans won't be deep-copied # into other plans) return unless parent.plan.kind_of?(Roby::ExecutablePlan) events = Set.new if info[:success] for event_name in info[:success].required_events events << child.event(event_name) end end if info[:failure] for event_name in info[:failure].required_events events << child.event(event_name) end end # Initial triggers failing_tasks << child return if events.empty? watch_emission(parent.start_event) events.each do |e| watch_emission(e) watch_unreachability(e) end end |
#watch_emission(generator) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/roby/task_structure/dependency.rb', line 66 def watch_emission(generator) return unless @watched_emissions.add?(generator) generator.on(on_replace: :drop) do |ev| interesting_events << ev.generator end generator.if_unreachable do |reason, g| @watched_emissions.delete(generator) end end |
#watch_unreachability(generator) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/roby/task_structure/dependency.rb', line 77 def watch_unreachability(generator) return unless @watched_unreachability.add?(generator) generator.if_unreachable do |reason, g| interesting_events << g @watched_unreachability.delete(generator) end end |
#watching_emissions?(generator) ⇒ Boolean
58 59 60 |
# File 'lib/roby/task_structure/dependency.rb', line 58 def watching_emissions?(generator) @watched_emissions.include?(generator) end |
#watching_unreachability?(generator) ⇒ Boolean
62 63 64 |
# File 'lib/roby/task_structure/dependency.rb', line 62 def watching_unreachability?(generator) @watched_unreachability.include?(generator) end |