netica 0.0.6-java → 0.0.7-java
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -0
- data/lib/netica/active_network.rb +17 -2
- data/lib/netica/bayes_network.rb +27 -2
- data/lib/netica/node.rb +6 -3
- data/lib/netica/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -3,6 +3,9 @@ module Netica
|
|
3
3
|
|
4
4
|
# provides a persistable object container for a Netica Bayes net.
|
5
5
|
class ActiveNetwork
|
6
|
+
class ActiveNetwork::NodeNotFound < RuntimeError; end
|
7
|
+
class ActiveNetwork::NetworkNotFound < RuntimeError; end
|
8
|
+
|
6
9
|
attr_accessor :network, :token
|
7
10
|
|
8
11
|
def initialize(token, filepath = nil)
|
@@ -24,7 +27,16 @@ module Netica
|
|
24
27
|
# @param nodeName [String] name of the node to be incremented
|
25
28
|
# @return [true,false,nil] outcome of the incr() attempt
|
26
29
|
def incr_node(nodeName)
|
27
|
-
|
30
|
+
if network
|
31
|
+
node = network.node(nodeName)
|
32
|
+
if node
|
33
|
+
return node.incr()
|
34
|
+
else
|
35
|
+
raise ActiveNetwork::NodeNotFound
|
36
|
+
end
|
37
|
+
else
|
38
|
+
raise ActiveNetwork::NetworkNotFound
|
39
|
+
end
|
28
40
|
end
|
29
41
|
|
30
42
|
# Export the state of the ActiveNetwork as a Hash
|
@@ -56,6 +68,7 @@ module Netica
|
|
56
68
|
Netica::Environment.instance.active_networks.each do |an|
|
57
69
|
return an if an.token == token
|
58
70
|
end
|
71
|
+
Netica::NeticaLogger.info "Network #{token} not found in current instance."
|
59
72
|
if Netica::Environment.instance.redis
|
60
73
|
stored_state = Netica::Environment.instance.redis.get(token)
|
61
74
|
if stored_state
|
@@ -63,6 +76,8 @@ module Netica
|
|
63
76
|
active_network = Object.const_get(hash['class']).new(token)
|
64
77
|
active_network.load_from_saved_state(hash)
|
65
78
|
return active_network
|
79
|
+
else
|
80
|
+
Netica::NeticaLogger.info "Network #{token} not found in redis."
|
66
81
|
end
|
67
82
|
end
|
68
83
|
return nil
|
@@ -74,7 +89,7 @@ module Netica
|
|
74
89
|
# @return [Hash] network state and object class name
|
75
90
|
def load_from_saved_state(hash)
|
76
91
|
self.network = BayesNetwork.new(hash["network"]["dne_file_path"])
|
77
|
-
network.load_from_state(hash["network"])
|
92
|
+
self.network.load_from_state(hash["network"])
|
78
93
|
end
|
79
94
|
end
|
80
95
|
end
|
data/lib/netica/bayes_network.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Netica
|
2
2
|
class BayesNetwork
|
3
|
+
require 'json'
|
4
|
+
|
3
5
|
attr_accessor :current_network, :dne_file_path
|
4
6
|
|
5
7
|
def initialize(dne_file_path = nil)
|
@@ -30,9 +32,18 @@ module Netica
|
|
30
32
|
nodes.collect{ |n| n if n.nature_node? }.compact
|
31
33
|
end
|
32
34
|
|
35
|
+
def node_sets
|
36
|
+
current_network.getAllNodesets(false).split(",").collect{|ns_name| node_set(ns_name)}
|
37
|
+
end
|
38
|
+
|
39
|
+
def node_set(name)
|
40
|
+
nodes.collect{ |n| n if n.isInNodeset(name) }.compact.sort{|a,b| b.beliefs <=> a.beliefs }
|
41
|
+
end
|
42
|
+
|
33
43
|
def load_from_state(network_hash)
|
34
|
-
NeticaLogger.info "network_hash => #{network_hash}"
|
44
|
+
NeticaLogger.info "Loading state from network_hash => #{network_hash}"
|
35
45
|
network_hash["decision_nodes"].each do |node_name, node_value|
|
46
|
+
NeticaLogger.info "Setting #{node_name} => #{node_value}"
|
36
47
|
node(node_name).value = node_value
|
37
48
|
end
|
38
49
|
end
|
@@ -47,15 +58,29 @@ module Netica
|
|
47
58
|
node_hash
|
48
59
|
end
|
49
60
|
|
61
|
+
# def analyze(json)
|
62
|
+
# analysis_hash = JSON.parse(json)
|
63
|
+
# analysis_hash['input_nodes'].each do |nodeName, value|
|
64
|
+
# node(nodeName).enterValue(value)
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# outcome = {}
|
68
|
+
# analysis_hash['output_nodes'].each do |nodeName|
|
69
|
+
# outcome[nodeName] = node(nodeName).value
|
70
|
+
# end
|
71
|
+
# outcome
|
72
|
+
# #JSON.dump(:results => outcome)
|
73
|
+
# end
|
74
|
+
|
50
75
|
private
|
51
76
|
|
52
77
|
def load_dne_file
|
53
78
|
NeticaLogger.info "Looking for BayesNet .dne file at #{dne_file_path}..."
|
54
79
|
streamer = Java::NorsysNetica::Streamer.new(dne_file_path)
|
55
80
|
self.current_network = Java::NorsysNetica::Net.new(streamer)
|
56
|
-
self.current_network.compile()
|
57
81
|
NeticaLogger.info "Initialized BayesNet -- #{self.current_network.object_id}"
|
58
82
|
self.decision_nodes.each{ |n| n.value = 0 }
|
83
|
+
self.current_network.compile()
|
59
84
|
end
|
60
85
|
end
|
61
86
|
end
|
data/lib/netica/node.rb
CHANGED
@@ -7,12 +7,12 @@ class Java::NorsysNetica::Node
|
|
7
7
|
Java::NorsysNetica::Node::DECISION_NODE => :decision,
|
8
8
|
Java::NorsysNetica::Node::UTILITY_NODE => :utility,
|
9
9
|
Java::NorsysNetica::Node::CONSTANT_NODE => :constant,
|
10
|
-
Java::NorsysNetica::Node::DISCONNECTED_NODE => :disconnected
|
10
|
+
Java::NorsysNetica::Node::DISCONNECTED_NODE => :disconnected
|
11
11
|
}
|
12
12
|
|
13
13
|
NODE_TYPES = {
|
14
14
|
Java::NorsysNetica::Node::DISCRETE_TYPE => :nature,
|
15
|
-
Java::NorsysNetica::Node::CONTINUOUS_TYPE => :decision
|
15
|
+
Java::NorsysNetica::Node::CONTINUOUS_TYPE => :decision
|
16
16
|
}
|
17
17
|
|
18
18
|
def name
|
@@ -60,11 +60,14 @@ class Java::NorsysNetica::Node
|
|
60
60
|
end
|
61
61
|
|
62
62
|
def value=(new_value)
|
63
|
-
Netica::NeticaLogger.info "Setting #{self.name} to #{new_value}"
|
64
63
|
if decision_node?
|
64
|
+
Netica::NeticaLogger.info "Decision Node: Setting #{kind} #{type} node #{self.name} to #{new_value}"
|
65
65
|
finding().setReal(new_value)
|
66
66
|
elsif nature_node?
|
67
|
+
Netica::NeticaLogger.info "Nature Node: Setting #{kind} #{type} node #{self.name} to #{new_value}"
|
67
68
|
finding.enterState(new_value)
|
69
|
+
else
|
70
|
+
throw "Could not set value for #{kind} #{type} node #{self.name} to #{new_value}"
|
68
71
|
end
|
69
72
|
end
|
70
73
|
|
data/lib/netica/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: netica
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: java
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-05-16 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Netica Bayes Network Management
|
15
15
|
email:
|