Module: Task

Defined in:
lib/rbbt/workflow/task.rb,
lib/rbbt/workflow/usage.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#extensionObject

Returns the value of attribute extension.



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

def extension
  @extension
end

#input_defaultsObject

Returns the value of attribute input_defaults.



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

def input_defaults
  @input_defaults
end

#input_descriptionsObject

Returns the value of attribute input_descriptions.



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

def input_descriptions
  @input_descriptions
end

#input_optionsObject

Returns the value of attribute input_options.



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

def input_options
  @input_options
end

#input_typesObject

Returns the value of attribute input_types.



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

def input_types
  @input_types
end

#inputsObject

Returns the value of attribute inputs.



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

def inputs
  @inputs
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#required_inputsObject

Returns the value of attribute required_inputs.



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

def required_inputs
  @required_inputs
end

#result_descriptionObject

Returns the value of attribute result_description.



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

def result_description
  @result_description
end

#result_typeObject

Returns the value of attribute result_type.



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

def result_type
  @result_type
end

#resumableObject

Returns the value of attribute resumable.



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

def resumable
  @resumable
end

#workflowObject

Returns the value of attribute workflow.



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

def workflow
  @workflow
end

Class Method Details

.dep_inputs(deps, workflow = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rbbt/workflow/task.rb', line 74

def self.dep_inputs(deps, workflow = nil)
  seen = []
  task_inputs = {}
  deps.each do |dep|
    if Symbol === dep
      wf, task = [workflow, workflow.tasks[dep.to_sym]]
    elsif Array === dep and dep.first
      wf, task_name, options = dep
      options, task_name = task_name, nil if Hash === task_name
      options, wf = wf, nil if Hash === wf
      task_name, wf = wf, workflow if task_name.nil? and Symbol === wf or String === wf
      next if task_name.nil?
      task = wf.tasks[task_name.to_sym]
    else
      next
    end

    task_name ||= task.name

    maps = (Array === dep and Hash === dep.last) ? dep.last.keys : []
    raise "Dependency task not found: #{dep}" if task.nil?
    next if seen.include? [wf, task_name, maps]

    task.workflow = wf if wf

    seen << [wf, task_name, maps]
    new_inputs = task.inputs - maps
    next unless new_inputs.any?
    if task_inputs[task].nil?
      task_inputs[task] = new_inputs
    else
      task_inputs[task] = (task_inputs[task] + new_inputs).uniq
    end
  end
  task_inputs
end

.setup(options = {}, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rbbt/workflow/task.rb', line 7

def self.setup(options = {}, &block)
  block.extend Task
  options = IndiferentHash.setup options
  block.singleton_methods.
    select{|method| method.to_s[-1] != "="[0]}.each{|method|
    if block.respond_to?(method.to_s + "=") and options.include? method.to_sym
      block.send(method.to_s + '=', options[method.to_sym]) 
    end
  }
  block
end

Instance Method Details

#dep_inputs(deps, workflow = nil) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/rbbt/workflow/task.rb', line 111

def dep_inputs(deps, workflow = nil)
  return {} if deps.empty?
  task_inputs = Task.dep_inputs deps, workflow
  task_inputs.each do |task, inputs|
    inputs.replace (inputs - self.inputs)
  end
end

#doc(workflow = nil, deps = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rbbt/workflow/usage.rb', line 4

def doc(workflow = nil, deps = nil)
  puts Log.color(:yellow, "## #{ name }") << ":"
  puts "\n" << Misc.format_paragraph(description.strip)  << "\n" if description and not description.empty?
  puts

  selects = []
  if inputs.any?
    inputs.zip(input_types.values_at(*inputs)).select{|i,t| t.to_sym == :select && input_options[i] && input_options[i][:select_options] }.each{|i,t| selects << [i, input_options[i][:select_options]]  }
    puts SOPT.input_doc(inputs, input_types, input_descriptions, input_defaults, true)
    puts
  end

  if deps and deps.any?
    puts Log.color(:magenta, "Inputs from dependencies:")
    puts
    seen = []
    task_inputs = dep_inputs deps, workflow
    task_inputs.each do |task,new_inputs|
      new_inputs.zip(task.input_types.values_at(*new_inputs)).select do |i,t| 
        t.to_sym == :select && task.input_options.include?(i) && task.input_options[i][:select_options] 
      end.each do |i,t| 
        selects << [i, task.input_options[i][:select_options]] 
      end

      next if new_inputs.empty?

      if task.workflow and task.workflow != workflow
        puts "  #{Log.color :yellow, ["[#{task.workflow.to_s}]", task.name.to_s] *" "}:"
      else
        puts "  #{Log.color :yellow, task.name.to_s}:"
      end
      puts unless Log.compact
      puts SOPT.input_doc(new_inputs, task.input_types, task.input_descriptions, task.input_defaults, true)
      puts unless Log.compact
    end
    puts
  end

  case
  when (input_types.values & [:array]).any?
    puts Log.color(:green, Misc.format_paragraph("Lists are specified as arguments using ',' or '|'. When specified as files the '\\n'
    also works in addition to the others. You may use the '--array_separator' option
    the change this default. Whenever a file is specified it may also accept STDIN using
    the '-' character."))
    puts

  when (input_types.values & [:text, :tsv]).any?
    puts Log.color(:green, Misc.format_paragraph("Whenever a file is specified it may also accept STDIN using the '-' character."))
    puts
  end

  puts Log.color(:magenta, "Returns: ") << Log.color(:blue, result_type.to_s) << "\n"
  puts

  if selects.any?
    puts Log.color(:magenta, "Input select options")
    puts
    selects.collect{|p| p}.uniq.each do |input,options|
      puts Log.color(:blue, input.to_s + ": ") << Misc.format_paragraph(options.collect{|o| Array === o ? o.first.to_s : o.to_s} * ", ") << "\n"
      puts unless Log.compact
    end
    puts
  end
end

#exec(*args) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/rbbt/workflow/task.rb', line 44

def exec(*args)
  case
  when (args.length == 1 and not inputs.nil? and inputs.length > 1 and Hash === args.first)
    self.call *take_input_values(IndiferentHash.setup(args.first))
  else
    self.call *args
  end
end

#exec_in(object, *args) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/rbbt/workflow/task.rb', line 53

def exec_in(object, *args)
  case
  when (args.length == 1 and not inputs.nil? and inputs.length > 1 and Hash === args.first)
    object.instance_exec *IndiferentHash.setup(args.first).values_at(*inputs), &self
  else
    object.instance_exec *args, &self 
  end
end

#parse_descriptionObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rbbt/workflow/task.rb', line 19

def parse_description
  if description =~ /\n\n/
    short_description, rest = description.match(/(.*?)\n\n(.*)/).values_at 1, 2
  else
    short_description = description
    rest = nil
  end

  if rest.nil?
    long_description = ""
  end
end

#persist_exec(filename, *args) ⇒ Object



62
63
64
65
66
# File 'lib/rbbt/workflow/task.rb', line 62

def persist_exec(filename, *args)
  Persist.persist "Task", @persistence_type, :file => filename do
    exec *args
  end
end

#persist_exec_in(filename, *args) ⇒ Object



68
69
70
71
72
# File 'lib/rbbt/workflow/task.rb', line 68

def persist_exec_in(filename, *args)
  Persist.persist "Task", @persistence_type, :file => filename do
    exec_in *args
  end
end

#take_input_values(input_values) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rbbt/workflow/task.rb', line 32

def take_input_values(input_values)
  return [] if @inputs.nil?
  values = []
  defaults = IndiferentHash.setup(@input_defaults || {})
  @inputs.each do |input|
    value = input_values[input]
    value = defaults[input] if value.nil?
    values << value
  end
  values
end