Class: Dor::Workflow::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/dor/workflow/graph.rb

Defined Under Namespace

Classes: Process

Constant Summary collapse

FILL_COLORS =
{ 'waiting' => "white", 'ready' => "white", 'error' => "#8B0000", 'blocked' => "white", 'completed' => "darkgreen", 'unknown' => "#CFCFCF" }
TEXT_COLORS =
{ 'waiting' => "black", 'ready' => "black", 'error' => "white", 'blocked' => "#8B0000", 'completed' => "white", 'unknown' => "black" }
PATTERNS =
{ 'waiting' => "diagonals", 'ready' => "filled", 'error' => "filled", 'blocked' => "diagonals", 'completed' => "filled", 'unknown' => "filled" }
RESERVED_KEYS =
['repository','name']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, name, parent = nil) ⇒ Graph

Returns a new instance of Graph.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dor/workflow/graph.rb', line 52

def initialize(repo, name, parent = nil)
  @repo = repo
  @name = name
  if parent.nil?
    @graph = GraphViz.new(qname)
    @root = self.add_nodes(name)
  else
    @graph = parent.subgraph(qname)
    @root = parent.add_nodes(name)
  end
  @graph[:truecolor => true]
  @root.shape = 'plaintext'
  @processes = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/dor/workflow/graph.rb', line 94

def method_missing(sym,*args)
  if @graph.respond_to?(sym)
    @graph.send(sym,*args)
  else
    super
  end
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



12
13
14
# File 'lib/dor/workflow/graph.rb', line 12

def graph
  @graph
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/dor/workflow/graph.rb', line 12

def name
  @name
end

#processesObject (readonly)

Returns the value of attribute processes.



12
13
14
# File 'lib/dor/workflow/graph.rb', line 12

def processes
  @processes
end

#repoObject (readonly)

Returns the value of attribute repo.



12
13
14
# File 'lib/dor/workflow/graph.rb', line 12

def repo
  @repo
end

#rootObject (readonly)

Returns the value of attribute root.



12
13
14
# File 'lib/dor/workflow/graph.rb', line 12

def root
  @root
end

Class Method Details

.from_config(name, config, parent = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dor/workflow/graph.rb', line 14

def self.from_config(name, config, parent = nil)
  wf = self.new(config['repository'], name, parent)
  config.keys.each { |p| wf.add_process(p.to_s) unless RESERVED_KEYS.include?(p) }
  config.keys.each { |p|
    if wf.processes[p]
      Array(config[p]['prerequisite']).each { |prereq|
        prereq.sub!(/^#{config['repository']}:#{name}:/e,'')
        if wf.processes[prereq]
          wf.processes[p].depends_on(wf.processes[prereq])
        else
          wf.processes[p].depends_on(wf.add_process(prereq).set_status('external'))
        end
      }
    end
  }
  wf.finish
  return wf
end

.from_processes(repo, name, processes, parent = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dor/workflow/graph.rb', line 33

def self.from_processes(repo, name, processes, parent = nil)
  wf = self.new(repo, name, parent)
  processes.each { |p| 
    wf.add_process(p.name).status = p.state || 'unknown'
  }
  processes.each { |p|
    p.prerequisite.each { |prereq|
      prereq.sub!(/^#{repo}:#{name}:/e,'')
      if wf.processes[prereq]
        wf.processes[p.name].depends_on(wf.processes[prereq])
      else
        wf.processes[p.name].depends_on(wf.add_process(prereq).set_status('external'))
      end
    }
  }
  wf.finish
  return wf
end

Instance Method Details

#add_process(name, external = false) ⇒ Object



71
72
73
74
75
76
# File 'lib/dor/workflow/graph.rb', line 71

def add_process(name, external = false)
  pqname = name.split(/:/).length == 3 ? name : [qname,name].join(':')
  p = Process.new(self, pqname, name)
  @processes[name] = p
  return p
end

#finishObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dor/workflow/graph.rb', line 78

def finish
  @processes.values.each do |process|
    process.node.fontname = 'Helvetica'
    if process.id =~ %r{^#{qname}} and process.prerequisites.length == 0
      (@root << process.node)[:arrowhead => 'none', :arrowtail => 'none', :dir => 'both', :style => 'invisible']
    end
  end

  @root.fontname = 'Helvetica'
  return self
end

#inspectObject



90
91
92
# File 'lib/dor/workflow/graph.rb', line 90

def inspect
  "#{self.to_s[0..-2]} #{repo}:#{name} (#{processes.keys.join(', ')})>"
end

#qnameObject



67
68
69
# File 'lib/dor/workflow/graph.rb', line 67

def qname
  [@repo,@name].join(':')
end