Class: Souffle::System
- Inherits:
-
Object
- Object
- Souffle::System
- Defined in:
- lib/souffle/system.rb
Overview
A system description with nodes and the statemachine to manage them.
Instance Attribute Summary collapse
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#options ⇒ Object
Returns the value of attribute options.
-
#provisioner ⇒ Object
Returns the value of attribute provisioner.
Class Method Summary collapse
-
.from_hash(system_hash) ⇒ Object
Creates a new system from a given hash.
Instance Method Summary collapse
-
#add(node) ⇒ Object
Adds a node to the system tree.
-
#clear_node_heirarchy ⇒ Object
Clears all parents and children from nodes to prepare to rebalancing.
-
#dependencies_on_system(node) ⇒ Array
Gets all of the node dependencies on the system.
-
#dependency_mapping(node) ⇒ Hash
Returns a dependency to node list mapping.
-
#dependent_nodes ⇒ Array
Returns the list of all dependent nodes.
-
#initialize ⇒ System
constructor
Creates a new souffle system.
-
#nodes_except(node) ⇒ Array
Returns the list of all nodes except the given node.
-
#optimized_node_dependencies(node) ⇒ Object
The optimized the node dependencies for the system.
-
#provider ⇒ Souffle::Provider::Base
Returns the current system provider.
-
#rebalance_nodes ⇒ Object
Checks node dependencies and rebalances them accordingly.
-
#roots ⇒ Array
Returns the list of all root nodes.
-
#setup_node_parents(node) ⇒ Object
Finds all of a nodes parent dependencies and setup the parents.
-
#to_hash ⇒ Hash
Returns the description of a system in hash format.
-
#try_opt(opt) ⇒ String
Tries to fetch an option parameter otherwise it grabs it from config.
Constructor Details
#initialize ⇒ System
Creates a new souffle system.
9 10 11 12 |
# File 'lib/souffle/system.rb', line 9 def initialize @nodes = [] @options = {} end |
Instance Attribute Details
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
5 6 7 |
# File 'lib/souffle/system.rb', line 5 def nodes @nodes end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/souffle/system.rb', line 6 def @options end |
#provisioner ⇒ Object
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.
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. = 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. = n[:options] node.[:attributes] ||= Hash.new sys.add(node) end sys end |
Instance Method Details
#add(node) ⇒ Object
Adds a node to the system tree.
17 18 19 20 |
# File 'lib/souffle/system.rb', line 17 def add(node) node.system = self @nodes << node end |
#clear_node_heirarchy ⇒ Object
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.
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.
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_nodes ⇒ Array
Returns the list of all dependent 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.
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.
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 |
#provider ⇒ Souffle::Provider::Base
Returns the current system provider.
109 110 111 |
# File 'lib/souffle/system.rb', line 109 def provider @provisioner.provider end |
#rebalance_nodes ⇒ Object
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 |
#roots ⇒ Array
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.
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.
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_hash ⇒ Hash
Returns 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 |