class Souffle::System
A system description with nodes and the statemachine to manage them.
Attributes
Public Class Methods
Creates a new system from a given hash.
@param [ Hash ] system_hash The hash representation of the system.
# 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
Creates a new souffle system.
# File lib/souffle/system.rb, line 9 def initialize @nodes = [] @options = {} end
Private Class Methods
Guarentee that the system hash that was passed in is valid.
@param [ Hash ] system_hash The hash representation of the system.
# File lib/souffle/system.rb, line 164 def guarentee_valid_hash(system_hash) if system_hash.nil? or !system_hash.has_key?(:nodes) raise Souffle::Exceptions::InvalidSystemHash, "The system hash must have a nodes key with a list of nodes." end end
Public Instance Methods
Adds a node to the system tree.
@param [ Souffle::Node
] node The node to add into the tree.
# File lib/souffle/system.rb, line 17 def add(node) node.system = self @nodes << node end
Clears all parents and children from nodes to prepare to rebalancing.
# File lib/souffle/system.rb, line 32 def clear_node_heirarchy @nodes.each { |n| n.parents = []; n.children = [] } end
Gets all of the node dependencies on the system.
@param [ Souffle::Node
] node The node to retrieve dependencies for.
@return [ Array ] The tuple of [ node, dependency_list ] for the node.
# 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
Returns a dependency to node list mapping.
@return [ Hash ] The mapping of depdencies to nodes.
# 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
Returns the list of all dependent nodes.
@return [ Array ] The list of all dependant nodes.
# File lib/souffle/system.rb, line 102 def dependent_nodes @nodes.select { |n| n.dependencies.any? } end
Returns the list of all nodes except the given node.
@return [ Array ] The list of all nodes except the given node.
# File lib/souffle/system.rb, line 85 def nodes_except(node) @nodes.select { |n| n != node } end
The optimized the node dependencies for the system.
@param [ Souffle::Node
] node The node that you want to optimize.
# 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
Returns the current system provider.
@return [ Souffle::Provider::Base
] The current system provider.
# File lib/souffle/system.rb, line 109 def provider @provisioner.provider end
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.
# File lib/souffle/system.rb, line 26 def rebalance_nodes clear_node_heirarchy dependent_nodes.each { |n| setup_node_parents(n) } end
Returns the list of all root nodes.
@note We use dependencies here to validate whether a node is root here because the parents are only determined after a rebalance is run.
@return [ Array ] The list of all root nodes. (Nodes without parents).
# File lib/souffle/system.rb, line 95 def roots @nodes.select { |n| n.dependencies.empty? } end
Finds all of a nodes parent dependencies and setup the parents.
@param [ Souffle::Node
] node The node to check and configure.
# 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
Returns the description of a system in hash format.
@return [ Hash ] The description of a system in hash format.
# File lib/souffle/system.rb, line 127 def to_hash { :nodes => @nodes.map { |n| n.to_hash }, :options => @options } end
Tries to fetch an option parameter otherwise it grabs it from config.
@param [ Symbol ] opt The option to try and fetch.
@return [ String ] The option return value.
# File lib/souffle/system.rb, line 118 def try_opt(opt) options.fetch(opt, Souffle::Config[opt]) rescue nil end