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, load_from_storage = true) ⇒ 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
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

Parameters:

  • memory (Boolean) (defaults to: true)

    destroy the in-memory object?, default is true

  • storage (Boolean) (defaults to: true)

    destroy object in redis?, default is true

Returns:

  • (Hash)

    outcome of deletion attempts per storage location



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

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

Load ActiveNetwork from a saved state

Parameters:

  • hash (Hash)

    network state to be restored



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

#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