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.
-
#in_use ⇒ Object
Returns the value of attribute in_use.
-
#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, load_from_storage = true) ⇒ ActiveNetwork
Retrieve ActiveNetwork from current Netica Environment instance or an associated redis store, if one is defined.
Instance Method Summary collapse
-
#destroy(memory = true, storage = true) ⇒ Hash
Destroy the ActiveNetwork.
-
#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) ⇒ Object
Load ActiveNetwork from a saved state.
-
#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
Returns a new instance of ActiveNetwork.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/netica/active_network.rb', line 11 def initialize(token, filepath = nil) Netica::NeticaLogger.info "Initializing #{self.class} for #{token}." self.created_at = Time.now self.updated_at = Time.now self.token = token self.in_use = false 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 |
#in_use ⇒ Object
Returns the value of attribute in_use.
9 10 11 |
# File 'lib/netica/active_network.rb', line 9 def in_use @in_use 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, load_from_storage = true) ⇒ ActiveNetwork
Retrieve ActiveNetwork from current Netica Environment instance or an associated redis store, if one is defined.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/netica/active_network.rb', line 75 def self.find(token, load_from_storage = true) environment = Netica::Environment.instance Netica::NeticaLogger.info "Searching in #{environment.network_container.class} #{environment.network_container.object_id} (length: #{environment.network_container.length}) for #{token}." environment.network_container.each do |an| if an.token == token until !an.in_use Netica::NeticaLogger.info "Network #{token} is locked." sleep 1 end return an end end Netica::NeticaLogger.info "Network #{token} not found in current instance #{environment.object_id}." if Netica::Environment.instance.redis stored_state = Netica::Environment.instance.redis.get(token) if stored_state && load_from_storage 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 elsif stored_state return stored_state else Netica::NeticaLogger.info "Network #{token} not found in redis." end end return nil end |
Instance Method Details
#destroy(memory = true, storage = true) ⇒ Hash
Destroy the ActiveNetwork
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/netica/active_network.rb', line 110 def destroy(memory = true, storage = true) outcome = { token: token, deletion: { memory: nil, redis: nil}} environment = Netica::Environment.instance if memory rejection = environment.network_container.reject!{|network| network.token == token} outcome[:deletion][:memory] = rejection.is_a?(Array) end if environment.redis && storage outcome[:deletion][:redis] = (environment.redis.del(token) > 0) end outcome end |
#incr_node(nodeName) ⇒ true, ...
Increment a specified network node
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/netica/active_network.rb', line 33 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, "Node #{nodeName} not found in network." end else raise ActiveNetwork::NetworkNotFound end end |
#load_from_saved_state(hash) ⇒ Object
Load ActiveNetwork from a saved state
128 129 130 131 132 |
# File 'lib/netica/active_network.rb', line 128 def load_from_saved_state(hash) self.network = BayesNetwork.new(hash["network"]["dne_file_path"]) self.reloaded_at = Time.now self.network.load_from_state(hash["network"]) end |
#save ⇒ true, ...
Save ActiveNetwork to an associated redis store, if one is defined.
64 65 66 67 68 |
# File 'lib/netica/active_network.rb', line 64 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
51 52 53 54 55 56 57 58 59 |
# File 'lib/netica/active_network.rb', line 51 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
25 26 27 |
# File 'lib/netica/active_network.rb', line 25 def to_s token end |