Class: Netica::ActiveNetwork
- Inherits:
-
Object
- Object
- Netica::ActiveNetwork
- Defined in:
- lib/netica/active_network.rb
Overview
provides a persistable object container for a Netica Bayes net.
Defined Under Namespace
Classes: NetworkNotFound, NodeNotFound
Instance Attribute Summary collapse
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#network ⇒ Object
Returns the value of attribute network.
-
#reloaded_at ⇒ Object
Returns the value of attribute reloaded_at.
-
#token ⇒ Object
Returns the value of attribute token.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Class Method Summary collapse
-
.find(token) ⇒ ActiveNetwork
Retrieve ActiveNetwork from current Netica Environment instance or an associated redis store, if one is defined.
Instance Method Summary collapse
-
#incr_node(nodeName) ⇒ true, ...
Increment a specified network node.
-
#initialize(token, filepath = nil) ⇒ ActiveNetwork
constructor
A new instance of ActiveNetwork.
-
#load_from_saved_state(hash) ⇒ Hash
Export the state of the ActiveNetwork as a Hash.
-
#save ⇒ true, ...
Save ActiveNetwork to an associated redis store, if one is defined.
-
#state ⇒ Hash
Export the state of the ActiveNetwork as a Hash.
- #to_s ⇒ Object
Constructor Details
#initialize(token, filepath = nil) ⇒ ActiveNetwork
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/netica/active_network.rb', line 11 def initialize(token, filepath = nil) Netica::NeticaLogger.info "initializing active network for #{token}" self.created_at = Time.now self.updated_at = Time.now self.token = token if filepath self.network = BayesNetwork.new(filepath) end processor = Netica::Environment.instance processor.active_networks << self end |
Instance Attribute Details
#created_at ⇒ Object
Returns the value of attribute created_at.
9 10 11 |
# File 'lib/netica/active_network.rb', line 9 def created_at @created_at end |
#network ⇒ Object
Returns the value of attribute network.
9 10 11 |
# File 'lib/netica/active_network.rb', line 9 def network @network end |
#reloaded_at ⇒ Object
Returns the value of attribute reloaded_at.
9 10 11 |
# File 'lib/netica/active_network.rb', line 9 def reloaded_at @reloaded_at end |
#token ⇒ Object
Returns the value of attribute token.
9 10 11 |
# File 'lib/netica/active_network.rb', line 9 def token @token end |
#updated_at ⇒ Object
Returns the value of attribute updated_at.
9 10 11 |
# File 'lib/netica/active_network.rb', line 9 def updated_at @updated_at end |
Class Method Details
.find(token) ⇒ ActiveNetwork
Retrieve ActiveNetwork from current Netica Environment instance or an associated redis store, if one is defined.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/netica/active_network.rb', line 73 def self.find(token) Netica::Environment.instance.active_networks.each do |an| return an if an.token == token end Netica::NeticaLogger.info "Network #{token} not found in current instance." if Netica::Environment.instance.redis stored_state = Netica::Environment.instance.redis.get(token) if stored_state hash = JSON.parse(stored_state) active_network = Object.const_get(hash['class']).new(token) active_network.load_from_saved_state(hash) Netica::NeticaLogger.info "Network #{token} reloaded from saved state: #{hash}" return active_network else Netica::NeticaLogger.info "Network #{token} not found in redis." end end return nil end |
Instance Method Details
#incr_node(nodeName) ⇒ true, ...
Increment a specified network node
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/netica/active_network.rb', line 31 def incr_node(nodeName) Netica::NeticaLogger.info "Incrementing #{nodeName} for #{token}, object_id: #{self.object_id}." if network node = network.node(nodeName) if node self.updated_at = Time.now return node.incr() else raise ActiveNetwork::NodeNotFound end else raise ActiveNetwork::NetworkNotFound end end |
#load_from_saved_state(hash) ⇒ Hash
Export the state of the ActiveNetwork as a Hash
97 98 99 100 101 |
# File 'lib/netica/active_network.rb', line 97 def load_from_saved_state(hash) self.network = BayesNetwork.new(hash["network"]["dne_file_path"]) self.network.load_from_state(hash["network"]) self.reloaded_at = Time.now end |
#save ⇒ true, ...
Save ActiveNetwork to an associated redis store, if one is defined.
62 63 64 65 66 |
# File 'lib/netica/active_network.rb', line 62 def save if Netica::Environment.instance.redis return Netica::Environment.instance.redis.set(token, JSON.dump(state)) end end |
#state ⇒ Hash
Export the state of the ActiveNetwork as a Hash
49 50 51 52 53 54 55 56 57 |
# File 'lib/netica/active_network.rb', line 49 def state { :network => network.state, :class => self.class.to_s, :created_at => self.created_at, :updated_at => self.updated_at, :reloaded_at => self.reloaded_at } end |
#to_s ⇒ Object
23 24 25 |
# File 'lib/netica/active_network.rb', line 23 def to_s token end |