Class: Bricolage::JobNet

Inherits:
Object
  • Object
show all
Defined in:
lib/bricolage/jobnet.rb,
lib/bricolage/jobnet.rb

Overview

reopen as namespace

Defined Under Namespace

Classes: FileLoader, JobNetRef, JobRef, Location, Parser, Ref

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref, location) ⇒ JobNet

Returns a new instance of JobNet.



198
199
200
201
202
203
# File 'lib/bricolage/jobnet.rb', line 198

def initialize(ref, location)
  @ref = ref
  @location = location
  @flow = {}   # Ref => [Ref] (src->dest)
  @deps = {}   # Ref => [Ref] (dest->src)
end

Instance Attribute Details

#refObject (readonly)

Returns the value of attribute ref.



209
210
211
# File 'lib/bricolage/jobnet.rb', line 209

def ref
  @ref
end

Class Method Details

.load(path, ref = JobNetRef.for_path(path)) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/bricolage/jobnet.rb', line 165

def JobNet.load(path, ref = JobNetRef.for_path(path))
  File.open(path) {|f|
    Parser.new(ref).parse_stream(f)
  }
rescue SystemCallError => err
  raise ParameterError, "could not load jobnet: #{path} (#{err.message})"
end

.load_multiple_jobs(pathes, ref = JobNetRef.for_job_path(pathes.first)) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/bricolage/jobnet.rb', line 177

def JobNet.load_multiple_jobs(pathes, ref = JobNetRef.for_job_path(pathes.first))
  jobnet_script = StringIO.new
  prev = nil
  pathes.each do |path|
    jobnet_script.print '-> ' if prev
    case path.extname
    when '.job'
      r = JobRef.for_path(path)
      jobnet_script.puts r.to_s
    when '.jobnet'
      r = JobNetRef.for_job_path(path)
      jobnet_script.puts r.to_s
    else
      raise ParameterError, "is not a job nor a jobnet: #{path}"
    end
    prev = r
  end
  jobnet_script.rewind
  Parser.new(ref).parse_stream(jobnet_script)
end

.load_single_job(path, ref = JobNetRef.for_job_path(path)) ⇒ Object



173
174
175
# File 'lib/bricolage/jobnet.rb', line 173

def JobNet.load_single_job(path, ref = JobNetRef.for_job_path(path))
  load_multiple_jobs([path], ref)
end

Instance Method Details

#add_edge(src, dest) ⇒ Object



223
224
225
226
# File 'lib/bricolage/jobnet.rb', line 223

def add_edge(src, dest)
  (@flow[src] ||= []).push dest
  (@deps[dest] ||= []).push src
end

#each_dependenciesObject



271
272
273
274
275
276
277
# File 'lib/bricolage/jobnet.rb', line 271

def each_dependencies
  @deps.each do |ref, deps|
    dest = (ref.net? ? ref.start : ref)
    srcs = deps.map {|r| r.net? ? r.end : r }
    yield dest, srcs
  end
end

#endObject



215
216
217
# File 'lib/bricolage/jobnet.rb', line 215

def end
  @ref.end_ref
end

#fixObject

Adds dummy dependencies (@start and @end) to fix up all jobs into one DAG beginning with @start and ending with @end



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/bricolage/jobnet.rb', line 254

def fix
  refs.each do |ref|
    next if ref.dummy?
    unless @deps[ref]
      (@flow[self.start] ||= []).push ref
      @deps[ref] = [self.start]
    end
    unless @flow[ref]
      (@flow[ref] ||= []).push self.end
      (@deps[self.end] ||= []).push ref
    end
  end
  @deps[self.start] ||= []
  @flow.freeze
  @deps.freeze
end

#inspectObject



205
206
207
# File 'lib/bricolage/jobnet.rb', line 205

def inspect
  "\#<#{self.class} #{ref}>"
end

#nameObject



219
220
221
# File 'lib/bricolage/jobnet.rb', line 219

def name
  @ref.to_s
end

#net_refsObject



248
249
250
# File 'lib/bricolage/jobnet.rb', line 248

def net_refs
  @deps.keys.select {|ref| ref.net? }
end

#refsObject



244
245
246
# File 'lib/bricolage/jobnet.rb', line 244

def refs
  @flow.keys | @flow.values.flatten
end

#startObject



211
212
213
# File 'lib/bricolage/jobnet.rb', line 211

def start
  @ref.start_ref
end

#to_deps_hashObject



236
237
238
239
240
241
242
# File 'lib/bricolage/jobnet.rb', line 236

def to_deps_hash
  h = {}
  @deps.each do |dest, srcs|
    h[dest.to_s] = srcs.map(&:to_s)
  end
  h
end

#to_hashObject



228
229
230
231
232
233
234
# File 'lib/bricolage/jobnet.rb', line 228

def to_hash
  h = {}
  @flow.each do |src, dests|
    h[src.to_s] = dests.map(&:to_s)
  end
  h
end