Class: Souffle::System

Inherits:
Object
  • Object
show all
Defined in:
lib/souffle/system.rb

Overview

A system description with nodes and the statemachine to manage them.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSystem

Creates a new souffle system.



9
10
11
12
# File 'lib/souffle/system.rb', line 9

def initialize
  @nodes = []
  @options = {}
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes.



5
6
7
# File 'lib/souffle/system.rb', line 5

def nodes
  @nodes
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/souffle/system.rb', line 6

def options
  @options
end

#provisionerObject

Returns the value of attribute provisioner.



6
7
8
# File 'lib/souffle/system.rb', line 6

def provisioner
  @provisioner
end

Class Method Details

.from_hash(system_hash) ⇒ Object

Creates a new system from a given hash.

Parameters:

  • system_hash (Hash)

    The hash representation of the system.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/souffle/system.rb', line 138

def from_hash(system_hash)
  guarentee_valid_hash(system_hash)
  system_hash[:options] ||= {}

  sys = Souffle::System.new
  sys.options = system_hash[:options]

  system_hash[:nodes].each do |n|
    n[:options] ||= Hash.new
    
    node = Souffle::Node.new
    node.name = n[:name]
    Array(n[:run_list]).each { |rl| node.run_list << rl }
    Array(n[:dependencies]).each { |dep| node.dependencies << dep }
    node.options = n[:options]
    node.options[:attributes] ||= Hash.new
    sys.add(node)
  end
  sys
end

Instance Method Details

#add(node) ⇒ Object

Adds a node to the system tree.

Parameters:



17
18
19
20
# File 'lib/souffle/system.rb', line 17

def add(node)
  node.system = self
  @nodes << node
end

#clear_node_heirarchyObject

Clears all parents and children from nodes to prepare to rebalancing.



32
33
34
# File 'lib/souffle/system.rb', line 32

def clear_node_heirarchy
  @nodes.each { |n| n.parents = []; n.children = [] }
end

#dependencies_on_system(node) ⇒ Array

Gets all of the node dependencies on the system.

Parameters:

  • node (Souffle::Node)

    The node to retrieve dependencies for.

Returns:

  • (Array)

    The tuple of [ node, dependency_list ] for the node.



49
50
51
52
53
54
55
56
# File 'lib/souffle/system.rb', line 49

def dependencies_on_system(node)
  node_dependencies = []
  nodes_except(node).each do |n|
    is_dependant, dep_list = node.depends_on?(n)
    node_dependencies << [n, dep_list] if is_dependant
  end
  node_dependencies
end

#dependency_mapping(node) ⇒ Hash

Returns a dependency to node list mapping.

Returns:

  • (Hash)

    The mapping of depdencies to nodes.



61
62
63
64
65
66
67
68
69
# File 'lib/souffle/system.rb', line 61

def dependency_mapping(node)
  mapping = {}
  dependencies_on_system(node).each do |n, deps|
    deps.each do |dep|
      mapping[dep] ||= []; mapping[dep] << n
    end
  end
  mapping
end

#dependent_nodesArray

Returns the list of all dependent nodes.

Returns:

  • (Array)

    The list of all dependant nodes.



102
103
104
# File 'lib/souffle/system.rb', line 102

def dependent_nodes
  @nodes.select { |n| n.dependencies.any? }
end

#nodes_except(node) ⇒ Array

Returns the list of all nodes except the given node.

Returns:

  • (Array)

    The list of all nodes except the given node.



85
86
87
# File 'lib/souffle/system.rb', line 85

def nodes_except(node)
  @nodes.select { |n| n != node }
end

#optimized_node_dependencies(node) ⇒ Object

The optimized the node dependencies for the system.

Parameters:



74
75
76
77
78
79
80
# File 'lib/souffle/system.rb', line 74

def optimized_node_dependencies(node)
  deps = Set.new
  dependency_mapping(node).each do |dep, nodes|
    deps << nodes.sort_by { |n| n.weight }.first
  end
  deps.to_a
end

#providerSouffle::Provider::Base

Returns the current system provider.

Returns:



109
110
111
# File 'lib/souffle/system.rb', line 109

def provider
  @provisioner.provider
end

#rebalance_nodesObject

Checks node dependencies and rebalances them accordingly.

If a node has no dependencies, it’s a root node! If a node has depdendencies, setup the node’s parents.



26
27
28
29
# File 'lib/souffle/system.rb', line 26

def rebalance_nodes
  clear_node_heirarchy
  dependent_nodes.each { |n| setup_node_parents(n) }
end

#rootsArray

Note:

We use dependencies here to validate whether a node is root here

Returns the list of all root nodes.

because the parents are only determined after a rebalance is run.

Returns:

  • (Array)

    The list of all root nodes. (Nodes without parents).



95
96
97
# File 'lib/souffle/system.rb', line 95

def roots
  @nodes.select { |n| n.dependencies.empty? }
end

#setup_node_parents(node) ⇒ Object

Finds all of a nodes parent dependencies and setup the parents.

Parameters:



39
40
41
42
# File 'lib/souffle/system.rb', line 39

def setup_node_parents(node)
  optimal_deps = optimized_node_dependencies(node)
  optimal_deps.each { |n, node_deps| n.add_child(node) }
end

#to_hashHash

Returns the description of a system in hash format.

Returns:

  • (Hash)

    The description of a system in hash format.



127
128
129
130
131
132
# File 'lib/souffle/system.rb', line 127

def to_hash
  {
    :nodes => @nodes.map { |n| n.to_hash },
    :options => @options
  }
end

#try_opt(opt) ⇒ String

Tries to fetch an option parameter otherwise it grabs it from config.

Parameters:

  • opt (Symbol)

    The option to try and fetch.

Returns:

  • (String)

    The option return value.



118
119
120
121
122
# File 'lib/souffle/system.rb', line 118

def try_opt(opt)
  options.fetch(opt, Souffle::Config[opt])
rescue
  nil
end