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

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_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

#in_useObject

Returns the value of attribute in_use.



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

def in_use
  @in_use
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.

Parameters:

  • token (String)

    identifying token for ActiveNetwork sought

Returns:



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
# File 'lib/netica/active_network.rb', line 75

def self.find(token)
  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."
        wait 1000
      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
      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

#destroyObject



103
104
105
106
107
108
109
# File 'lib/netica/active_network.rb', line 103

def destroy
  environment = Netica::Environment.instance
  environment.network_container.delete_if{|network| network.token == token}
  if environment.redis
    environment.redis.del(token)
  end
end

#incr_node(nodeName) ⇒ true, ...

Increment a specified network node

Parameters:

  • nodeName (String)

    name of the node to be incremented

Returns:

  • (true, false, nil)

    outcome of the incr() attempt



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) ⇒ Hash

Export the state of the ActiveNetwork as a Hash

Parameters:

  • hash (Hash)

    network state to be restored

Returns:

  • (Hash)

    network state and object class name



115
116
117
118
119
# File 'lib/netica/active_network.rb', line 115

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.

Returns:

  • (true, false, nil)

    outcome of redis.set, or nil if redis is not found



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

#stateHash

Export the state of the ActiveNetwork as a Hash

Returns:

  • (Hash)

    network state and object class name



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_sObject



25
26
27
# File 'lib/netica/active_network.rb', line 25

def to_s
  token
end