Class: Wolflow::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/wolflow/task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: object_id.to_s, task_spec: nil, workflow: nil, completed: false, root: nil, parents: [], children: [], data: {}) ⇒ Task

Returns a new instance of Task.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/wolflow/task.rb', line 7

def initialize(
  id: object_id.to_s,
  task_spec: nil,
  workflow: nil,
  completed: false,
  root: nil,
  parents: [],
  children: [],
  data: {}
)
  @id = id
  @task_spec = task_spec
  @completed = completed
  @workflow = workflow
  @root = root
  @parents = parents
  @children = children
  @data = data
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/wolflow/task.rb', line 5

def children
  @children
end

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/wolflow/task.rb', line 5

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/wolflow/task.rb', line 5

def id
  @id
end

#parentsObject (readonly)

Returns the value of attribute parents.



5
6
7
# File 'lib/wolflow/task.rb', line 5

def parents
  @parents
end

#rootObject

Returns the value of attribute root.



5
6
7
# File 'lib/wolflow/task.rb', line 5

def root
  @root
end

#task_specObject (readonly)

Returns the value of attribute task_spec.



5
6
7
# File 'lib/wolflow/task.rb', line 5

def task_spec
  @task_spec
end

#workflowObject (readonly)

Returns the value of attribute workflow.



5
6
7
# File 'lib/wolflow/task.rb', line 5

def workflow
  @workflow
end

Class Method Details

.from_hash(hash) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/wolflow/task.rb', line 158

def from_hash(hash)
  if hash.key?(:task_spec) && !hash[:task_spec].is_a?(TaskSpec)
    task_spec = TaskSpec.spec_types[hash[:task_spec]]

    raise Error, "no task spec registered for #{hash[:task_spec]}" unless task_spec

    hash[:task_spec] = task_spec.new
  end

  new(**hash)
end

Instance Method Details

#build_task_treeObject

Raises:



81
82
83
84
85
# File 'lib/wolflow/task.rb', line 81

def build_task_tree
  raise TaskError, "there's already a task tree" unless @children.empty?

  @task_spec.build_next_tasks(self)
end

#complete!Object



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

def complete!
  @task_spec.on_complete(self) if @task_spec
end

#completed?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/wolflow/task.rb', line 41

def completed?
  @completed
end

#connect(*tasks) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wolflow/task.rb', line 57

def connect(*tasks)
  tasks.each do |task|
    connect_one(task)
  end

  if block_given?
    yield(*tasks)
    return self
  end

  return tasks.first if tasks.size <= 1

  tasks
end

#connects_with(tasks) ⇒ Object

private-ish



137
138
139
140
141
142
143
144
# File 'lib/wolflow/task.rb', line 137

def connects_with(tasks)
  @parents = @parents.map do |parent|
    parent.is_a?(Task) ? parent : tasks.fetch(parent)
  end
  @children = @children.map do |child|
    child.is_a?(Task) ? child : tasks.fetch(child)
  end
end

#copy_task_tree(i) ⇒ Object



87
88
89
# File 'lib/wolflow/task.rb', line 87

def copy_task_tree(i)
  @task_spec.build_next_tasks(self, i: i)
end

#disconnect(task) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/wolflow/task.rb', line 72

def disconnect(task)
  unless @children.include?(task) && task.parents.include?(self)
    raise TaskError, "#{self} and #{task} are not connected"
  end

  task.parents.delete(self)
  @children.delete(task)
end

#each(visited = [], &blk) ⇒ Object



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

def each(visited = [], &blk)
  return enum_for(__method__, visited) unless blk

  blk.call(self) unless @task_spec.id.start_with?("__")

  visited << self

  @task_spec.each_child(self) do |child|
    # do not allow multiple visits on the same node
    next if visited.include?(child)

    if child.task_spec.is_a?(Synchronization) && !child.parents.all? { |parent| visited.include?(parent) }
      # only jump in after all parents are consumed
      next
    end

    child.each(visited, &blk)
  end
  self
end

#each_parent(visited = [self], &blk) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wolflow/task.rb', line 112

def each_parent(visited = [self], &blk)
  return enum_for(__method__, visited) unless blk

  @task_spec.each_parent(self) do |parent|
    # do not allow multiple visits on the same node
    next if visited.include?(parent)

    visited << parent

    blk.call(parent)

    parent.each_parent(visited, &blk)
  end
  self
end

#initialize_clone(other, **kwargs) ⇒ Object



34
35
36
37
38
39
# File 'lib/wolflow/task.rb', line 34

def initialize_clone(other, **kwargs)
  super
  @parents = other.instance_variable_get(:@parents).clone(**kwargs)
  @children = other.instance_variable_get(:@children).clone(**kwargs)
  @data = other.instance_variable_get(:@data).clone(**kwargs)
end

#initialize_dup(other) ⇒ Object



27
28
29
30
31
32
# File 'lib/wolflow/task.rb', line 27

def initialize_dup(other)
  super
  @parents = other.instance_variable_get(:@parents).dup
  @children = other.instance_variable_get(:@children).dup
  @data = other.instance_variable_get(:@data).dup
end

#inspectObject



128
129
130
131
132
133
134
# File 'lib/wolflow/task.rb', line 128

def inspect
  "#<#{self.class}:#{hash} " \
    "@id=#{@id} " \
    "@parents=#{@parents.map(&:id)} " \
    "@children=#{@children.map(&:id)} " \
    "@completed=#{@completed}>"
end

#mark_as_complete!Object



49
50
51
# File 'lib/wolflow/task.rb', line 49

def mark_as_complete!
  @completed = true
end

#reset!Object



53
54
55
# File 'lib/wolflow/task.rb', line 53

def reset!
  @completed = false
end

#to_hashObject



146
147
148
149
150
151
152
153
154
155
# File 'lib/wolflow/task.rb', line 146

def to_hash
  {
    id: @id,
    parents: @parents.map(&:id),
    children: @children.map(&:id),
    completed: @completed,
    data: (@data.to_hash unless @data.empty?),
    task_spec_id: (@task_spec.id if @task_spec)
  }.compact
end