Module: Workflow::Data

Defined in:
lib/rbbt/workflow/util/data.rb

Defined Under Namespace

Classes: DataNotFound

Instance Method Summary collapse

Instance Method Details

#data(name, options = {}, &block) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rbbt/workflow/util/data.rb', line 99

def data(name, options = {}, &block)
  dep do |jobname, job_options, dependencies|
    if block_given?
      block.call jobname, job_options.merge(options), dependencies
    else
      begin
        path = get_file jobname, name, options[:extension]
        job = Step.new path
        job.task_name = name
        job.result_type = options[:result_type] || options[:type] || options[:extension]
        job
      rescue DataNotFound
        {:task => name, :options => options, :jobname => jobname}
      end
    end
  end
end

#data_dir(directory) ⇒ Object



8
9
10
# File 'lib/rbbt/workflow/util/data.rb', line 8

def data_dir(directory)
  @@data_directory = directory
end

#data_task(name, workflow, oname, *rest, &block) ⇒ Object



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rbbt/workflow/util/data.rb', line 43

def data_task(name, workflow, oname, *rest, &block)
  dep_task(name, workflow, oname, *rest) do |jobname, options,dependencies|
    begin
      task_info = workflow.nil? ? nil : workflow.task_info(oname) 
      options[:extension] ||= task_info[:extension]
      path = get_file jobname, name, options[:extension]
      job = Step.new path
      job.task_name = name
      job.result_type = options[:result_type] || options[:type] || options[:extension]
      job
    rescue DataNotFound
    end

    next job if job

    data_options = {}
    data_options = data_options.merge(Workflow.load_inputs(File.join(@@data_directory,"options"), task_info[:inputs], task_info[:input_types]))

    begin
      data_options = data_options.merge(Workflow.load_inputs(get_file(jobname, :options), task_info[:inputs], task_info[:input_types]))
    rescue DataNotFound
    end

    begin
      data_options = data_options.merge(Workflow.load_inputs(get_datadir(jobname).options, task_info[:inputs], task_info[:input_types]))
    rescue DataNotFound
    end

    begin
      task_info = workflow.nil? ? nil : workflow.task_info(oname) 
      data_options = block.call get_datadir(jobname), data_options, task_info
    rescue
      Log.exception $!
    end if block_given?

    case data_options
    when Step
      next data_options
    when Hash
      if data_options.include?(:inputs)
        data_options = data_options.merge(options)
        workflow = data_options[:workflow] if data_options[:workflow]
        oname = data_options[:task] if data_options[:task]
        inputs = options.merge(data_options[:inputs])
      else
        inputs = options.merge(data_options)
      end

      {:workflow => workflow, :task => oname, :jobname => jobname, :inputs => inputs}
    else
      raise "Cannot understand data_options: #{data_options}"
    end

  end
end

#get_datadir(clean_name) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
20
# File 'lib/rbbt/workflow/util/data.rb', line 12

def get_datadir(clean_name)
  data_dir = File.join(@@data_directory, clean_name)
  raise DataNotFound, "Data dir not found for #{clean_name} in #{@@data_directory}" unless File.directory?(data_dir)
  if Path === @@data_directory
    @@data_directory.annotate data_dir
  else
    Path.setup(data_dir)
  end
end

#get_file(clean_name, type = nil, extension = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rbbt/workflow/util/data.rb', line 22

def get_file(clean_name, type = nil, extension = nil)
  begin
    extension = nil if String === extension && extension.empty?
    file1 = File.join(@@data_directory, type.to_s, (extension.nil? ? clean_name.to_s : clean_name.to_s + ".#{extension}"))
    file2 = File.join(@@data_directory, clean_name.to_s, (extension.nil? ? type.to_s : type.to_s + ".#{extension}"))
    if Open.exists?(file1)
      data_file = file1
    elsif Open.exists?(file2)
      data_file = file2
    else
      raise DataNotFound, "Data type #{type} not found for #{clean_name} in #{@@data_directory}"
    end
  end

  if Path === @@data_directory
    @@data_directory.annotate data_file
  else
    Path.setup(data_file)
  end
end