Class: Netica::BayesNetwork

Inherits:
Object
  • Object
show all
Defined in:
lib/netica/bayes_network.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dne_file_path = nil) ⇒ BayesNetwork

Returns a new instance of BayesNetwork.



7
8
9
10
11
12
13
# File 'lib/netica/bayes_network.rb', line 7

def initialize(dne_file_path = nil)
  Netica::NeticaLogger.info "Initializing #{self.class} #{self.object_id}"
  if dne_file_path
    self.dne_file_path = dne_file_path
    load_dne_file
  end
end

Instance Attribute Details

#current_networkObject

Returns the value of attribute current_network.



5
6
7
# File 'lib/netica/bayes_network.rb', line 5

def current_network
  @current_network
end

#dne_file_pathObject

Returns the value of attribute dne_file_path.



5
6
7
# File 'lib/netica/bayes_network.rb', line 5

def dne_file_path
  @dne_file_path
end

Instance Method Details

#analyze(input_values, output_nodes) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/netica/bayes_network.rb', line 66

def analyze(input_values, output_nodes)
  outcome = []
  input_values.each do |value_collection|
    id = value_collection.delete("id")
    value_collection.each do |nodeName, value|
      node(nodeName).enterValue(value)
    end
    result = { :id => id }
    output_nodes.each do |nodeName|
      result[nodeName] = node(nodeName).value
    end
    outcome << result
    current_network.retractFindings
  end
  outcome
end

#decision_nodesObject



28
29
30
# File 'lib/netica/bayes_network.rb', line 28

def decision_nodes
  nodes.collect{ |n| n if n.decision_node? }.compact
end

#load_from_state(network_hash) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/netica/bayes_network.rb', line 44

def load_from_state(network_hash)
  Netica::NeticaLogger.info "Loading state from network_hash => #{network_hash}"
  network_hash["decision_nodes"].each do |node_name, node_value|
    Netica::NeticaLogger.info "Setting #{node_name} => #{node_value}"
    node(node_name).value = node_value
  end
end

#nature_nodesObject



32
33
34
# File 'lib/netica/bayes_network.rb', line 32

def nature_nodes
  nodes.collect{ |n| n if n.nature_node? }.compact
end

#node(nodeName) ⇒ Node

retrieve the node from the associated network whose name matches the “nodeName” supplied.

Parameters:

  • nodeName (String)

    Name of the node to find

Returns:

  • (Node)


20
21
22
# File 'lib/netica/bayes_network.rb', line 20

def node(nodeName)
  nodes.select{ |n| n if n.name == nodeName }[0]
end

#node_hash(nodes) ⇒ Object



60
61
62
63
64
# File 'lib/netica/bayes_network.rb', line 60

def node_hash(nodes)
  node_hash = {}
  nodes.collect{|dn| node_hash.store(dn.name, dn.value) }
  node_hash
end

#node_set(name) ⇒ Object



40
41
42
# File 'lib/netica/bayes_network.rb', line 40

def node_set(name)
  nodes.collect{ |n| n if n.isInNodeset(name) }.compact.sort{|a,b| b.beliefs <=> a.beliefs }
end

#node_setsObject



36
37
38
# File 'lib/netica/bayes_network.rb', line 36

def node_sets
  current_network.getAllNodesets(false).split(",").collect{|ns_name| node_set(ns_name)}
end

#nodesObject



24
25
26
# File 'lib/netica/bayes_network.rb', line 24

def nodes
  current_network.nodes
end

#stateObject



52
53
54
55
56
57
58
# File 'lib/netica/bayes_network.rb', line 52

def state
  {
    :dne_file_path  => dne_file_path,
    :decision_nodes => node_hash(decision_nodes),
    :nature_nodes   => node_hash(nature_nodes)
  }
end