Class: Netica::ActiveNetwork

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_atObject

Returns the value of attribute created_at.



9
10
11
# File 'lib/netica/active_network.rb', line 9

def created_at
  @created_at
end

#networkObject

Returns the value of attribute network.



9
10
11
# File 'lib/netica/active_network.rb', line 9

def network
  @network
end

#reloaded_atObject

Returns the value of attribute reloaded_at.



9
10
11
# File 'lib/netica/active_network.rb', line 9

def reloaded_at
  @reloaded_at
end

#tokenObject

Returns the value of attribute token.



9
10
11
# File 'lib/netica/active_network.rb', line 9

def token
  @token
end

#updated_atObject

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

#savetrue, ...

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

#stateHash

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_sObject



23
24
25
# File 'lib/netica/active_network.rb', line 23

def to_s
  token
end