netica 0.0.5-java
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +79 -0
- data/Rakefile +6 -0
- data/examples/ChestClinic.dne +110 -0
- data/examples/chest_clinic.rb +24 -0
- data/lib/netica.rb +14 -0
- data/lib/netica/active_network.rb +59 -0
- data/lib/netica/bayes_network.rb +59 -0
- data/lib/netica/environment.rb +47 -0
- data/lib/netica/netica_logger.rb +18 -0
- data/lib/netica/node.rb +104 -0
- data/lib/netica/node_list.rb +26 -0
- data/lib/netica/state.rb +9 -0
- data/lib/netica/version.rb +3 -0
- data/netica.gemspec +21 -0
- data/spec/netica/active_network_spec.rb +42 -0
- data/spec/netica/environment_spec.rb +16 -0
- data/spec/netica_spec.rb +7 -0
- data/spec/spec_helper.rb +19 -0
- metadata +75 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Disruptive Ventures, Inc.
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# Netica
|
2
|
+
|
3
|
+
The Netica gem provides tools for interacting with Bayesian networks using JRuby and the Netica-J API, published by [Norsys Software Corp.](http://www.norsys.com).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Download the Netica-J API from Norsys and place the NeticaJ.jar file in your JRuby load path. Possible locations include...
|
8
|
+
|
9
|
+
/Library/Java/Extensions
|
10
|
+
/Network/Library/Java/Extensions
|
11
|
+
/System/Library/Java/Extensions
|
12
|
+
/usr/lib/java
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'netica'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle install
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install netica
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
Here's an example, based the Probabilistic Inference example in the Netica-J Manual.
|
29
|
+
|
30
|
+
Use Netica::Environment.engage to create a Netica Environ singleton object:
|
31
|
+
|
32
|
+
processor = Netica::Environment.engage
|
33
|
+
=> true
|
34
|
+
|
35
|
+
Create an ActiveNetwork, using a `.dne` file created in the Netica Application.
|
36
|
+
|
37
|
+
my_network = Netica::ActiveNetwork.new("some_identifiying_token", "./examples/ChestClinic.dne")
|
38
|
+
=> #<Netica::ActiveNetwork:0x58767e95 @network=#<Netica::BayesNetwork:0x4b709592 @current_network=#<Java::NorsysNetica::Net:0x75e82fc2>, @dne_file_path="./examples/ChestClinic.dne">, @token="some_identifiying_token">
|
39
|
+
|
40
|
+
View the nodes in the network.
|
41
|
+
|
42
|
+
my_network.network.nodes
|
43
|
+
=> #<Java::NorsysNetica::NodeList:1738447710 size:8 nodes:["NatureNode:VisitAsia", "NatureNode:Tuberculosis", "NatureNode:Smoking", "NatureNode:Cancer", "NatureNode:TbOrCa", "NatureNode:XRay", "NatureNode:Bronchitis", "NatureNode:Dyspnea"]>
|
44
|
+
|
45
|
+
|
46
|
+
Read the value of a Belief node.
|
47
|
+
|
48
|
+
tb_node = my_network.network.node("Tuberculosis")
|
49
|
+
=> #<Java::NorsysNetica::NatureNode:Tuberculosis value:{"present"=>0.010399998165667057, "absent"=>0.9896000027656555}>
|
50
|
+
|
51
|
+
tb_node.value("present")
|
52
|
+
=> 0.010399998165667057
|
53
|
+
|
54
|
+
Set the state of the XRay node to Abnormal.
|
55
|
+
|
56
|
+
xray_node = my_network.network.node("XRay")
|
57
|
+
=> #<Java::NorsysNetica::NatureNode:XRay value:{"abnormal"=>0.1102900430560112, "normal"=>0.8897099494934082}>
|
58
|
+
|
59
|
+
xray_node.value = "abnormal"
|
60
|
+
=> "abnormal"
|
61
|
+
|
62
|
+
Then, re-read the value of the Belief node.
|
63
|
+
|
64
|
+
p tb_node.value("present")
|
65
|
+
0.09241089224815369
|
66
|
+
=> 0.09241089224815369
|
67
|
+
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
1. Fork it
|
72
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
73
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
74
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
75
|
+
5. Create new Pull Request
|
76
|
+
|
77
|
+
## Legal
|
78
|
+
|
79
|
+
Netica and Norsys are registered trademarks of Norsys Software Corp.
|
data/Rakefile
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
// ~->[DNET-1]->~
|
2
|
+
|
3
|
+
// File created by an unlicensed user using Netica 5.04 on 10/01/12 at 20:20:06.
|
4
|
+
|
5
|
+
bnet ChestClinic {
|
6
|
+
autoupdate = FALSE;
|
7
|
+
|
8
|
+
node VisitAsia {
|
9
|
+
kind = NATURE;
|
10
|
+
discrete = TRUE;
|
11
|
+
chance = CHANCE;
|
12
|
+
states = (visit, no_visit);
|
13
|
+
statetitles = ("Visited Asia within the last 3 years", );
|
14
|
+
parents = ();
|
15
|
+
probs =
|
16
|
+
// Visited Asia wit no visit
|
17
|
+
(0.01, 0.99);
|
18
|
+
title = "Visit to Asia";
|
19
|
+
};
|
20
|
+
|
21
|
+
node Tuberculosis {
|
22
|
+
kind = NATURE;
|
23
|
+
discrete = TRUE;
|
24
|
+
chance = CHANCE;
|
25
|
+
states = (present, absent);
|
26
|
+
parents = (VisitAsia);
|
27
|
+
probs =
|
28
|
+
// present absent // VisitAsia
|
29
|
+
(0.05, 0.95, // Visited Asia wit
|
30
|
+
0.01, 0.99); // no visit ;
|
31
|
+
};
|
32
|
+
|
33
|
+
node Smoking {
|
34
|
+
kind = NATURE;
|
35
|
+
discrete = TRUE;
|
36
|
+
chance = CHANCE;
|
37
|
+
states = (smoker, nonsmoker);
|
38
|
+
parents = ();
|
39
|
+
probs =
|
40
|
+
// smoker nonsmoker
|
41
|
+
(0.5, 0.5);
|
42
|
+
};
|
43
|
+
|
44
|
+
node Cancer {
|
45
|
+
kind = NATURE;
|
46
|
+
discrete = TRUE;
|
47
|
+
chance = CHANCE;
|
48
|
+
states = (present, absent);
|
49
|
+
parents = (Smoking);
|
50
|
+
probs =
|
51
|
+
// present absent // Smoking
|
52
|
+
(0.1, 0.9, // smoker
|
53
|
+
0.01, 0.99); // nonsmoker ;
|
54
|
+
title = "Lung Cancer";
|
55
|
+
};
|
56
|
+
|
57
|
+
node TbOrCa {
|
58
|
+
kind = NATURE;
|
59
|
+
discrete = TRUE;
|
60
|
+
chance = DETERMIN;
|
61
|
+
states = (true, false);
|
62
|
+
parents = (Tuberculosis, Cancer);
|
63
|
+
functable =
|
64
|
+
// Tuberculosis Cancer
|
65
|
+
(true, // present present
|
66
|
+
true, // present absent
|
67
|
+
true, // absent present
|
68
|
+
false); // absent absent ;
|
69
|
+
equation = "TbOrCa (Tuberculosis, Cancer) = Tuberculosis || Cancer";
|
70
|
+
title = "Tuberculosis or Cancer";
|
71
|
+
};
|
72
|
+
|
73
|
+
node XRay {
|
74
|
+
kind = NATURE;
|
75
|
+
discrete = TRUE;
|
76
|
+
chance = CHANCE;
|
77
|
+
states = (abnormal, normal);
|
78
|
+
parents = (TbOrCa);
|
79
|
+
probs =
|
80
|
+
// abnormal normal // TbOrCa
|
81
|
+
(0.98, 0.02, // true
|
82
|
+
0.05, 0.95); // false ;
|
83
|
+
};
|
84
|
+
|
85
|
+
node Bronchitis {
|
86
|
+
kind = NATURE;
|
87
|
+
discrete = TRUE;
|
88
|
+
chance = CHANCE;
|
89
|
+
states = (present, absent);
|
90
|
+
parents = (Smoking);
|
91
|
+
probs =
|
92
|
+
// present absent // Smoking
|
93
|
+
(0.6, 0.4, // smoker
|
94
|
+
0.3, 0.7); // nonsmoker ;
|
95
|
+
};
|
96
|
+
|
97
|
+
node Dyspnea {
|
98
|
+
kind = NATURE;
|
99
|
+
discrete = TRUE;
|
100
|
+
chance = CHANCE;
|
101
|
+
states = (present, absent);
|
102
|
+
parents = (TbOrCa, Bronchitis);
|
103
|
+
probs =
|
104
|
+
// present absent // TbOrCa Bronchitis
|
105
|
+
(0.9, 0.1, // true present
|
106
|
+
0.7, 0.3, // true absent
|
107
|
+
0.8, 0.2, // false present
|
108
|
+
0.1, 0.9); // false absent ;
|
109
|
+
};
|
110
|
+
};
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env jruby
|
2
|
+
require 'netica'
|
3
|
+
|
4
|
+
# Use Netica::Environment.engage to create a Netica Environ singleton object:
|
5
|
+
processor = Netica::Environment.engage
|
6
|
+
|
7
|
+
# Create an ActiveNetwork, using a `.dne` file created in the Netica Application.
|
8
|
+
my_network = Netica::ActiveNetwork.new("some_identifiying_token", "#{File.dirname(__FILE__)}/ChestClinic.dne")
|
9
|
+
|
10
|
+
# View the nodes in the network.
|
11
|
+
p my_network.network.nodes
|
12
|
+
|
13
|
+
# Read the value of a Belief node.
|
14
|
+
tb_node = my_network.network.node("Tuberculosis")
|
15
|
+
belief = tb_node.value("present")
|
16
|
+
p "The probability of tuberculosis is #{belief}."
|
17
|
+
|
18
|
+
# Set the state of the XRay node to Abnormal.
|
19
|
+
xray_node = my_network.network.node("XRay")
|
20
|
+
xray_node.value = "abnormal"
|
21
|
+
|
22
|
+
# Then, re-read the value of the Belief node.
|
23
|
+
belief2 = tb_node.value("present")
|
24
|
+
p "Given an abnormal X-ray... The probability of tuberculosis is #{belief2}."
|
data/lib/netica.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'java'
|
2
|
+
require '/lib/NeticaJ.jar'
|
3
|
+
|
4
|
+
require "netica/version"
|
5
|
+
require "netica/environment"
|
6
|
+
require "netica/netica_logger"
|
7
|
+
require "netica/node"
|
8
|
+
require "netica/node_list"
|
9
|
+
require "netica/bayes_network"
|
10
|
+
require "netica/active_network"
|
11
|
+
|
12
|
+
module Netica
|
13
|
+
include_package "norsys.netica"
|
14
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Netica
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class ActiveNetwork
|
5
|
+
attr_accessor :network, :token
|
6
|
+
|
7
|
+
def initialize(token, filepath = nil)
|
8
|
+
Netica::NeticaLogger.info "initializing active network for #{token}"
|
9
|
+
self.token = token
|
10
|
+
if filepath
|
11
|
+
self.network = BayesNetwork.new(filepath)
|
12
|
+
end
|
13
|
+
processor = Netica::Environment.instance
|
14
|
+
processor.active_networks << self
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
token
|
19
|
+
end
|
20
|
+
|
21
|
+
def incr_node(nodeName)
|
22
|
+
network.getNode(nodeName).incr() if network
|
23
|
+
end
|
24
|
+
|
25
|
+
def state
|
26
|
+
{
|
27
|
+
:network => network.state,
|
28
|
+
:class => self.class.to_s
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
def save
|
33
|
+
if Netica::Environment.instance.redis
|
34
|
+
Netica::Environment.instance.redis.set(token, JSON.dump(state))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.find(token)
|
39
|
+
Netica::Environment.instance.active_networks.each do |an|
|
40
|
+
return an if an.token == token
|
41
|
+
end
|
42
|
+
if Netica::Environment.instance.redis
|
43
|
+
stored_state = Netica::Environment.instance.redis.get(token)
|
44
|
+
if stored_state
|
45
|
+
hash = JSON.parse(stored_state)
|
46
|
+
active_network = Object.const_get(hash['class']).new(token)
|
47
|
+
active_network.load_from_saved_state(hash)
|
48
|
+
return active_network
|
49
|
+
end
|
50
|
+
end
|
51
|
+
return nil
|
52
|
+
end
|
53
|
+
|
54
|
+
def load_from_saved_state(hash)
|
55
|
+
self.network = BayesNetwork.new(hash["network"]["dne_file_path"])
|
56
|
+
network.load_from_state(hash["network"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Netica
|
2
|
+
class BayesNetwork
|
3
|
+
attr_accessor :current_network, :dne_file_path
|
4
|
+
|
5
|
+
def initialize(dne_file_path = nil)
|
6
|
+
if dne_file_path
|
7
|
+
self.dne_file_path = dne_file_path
|
8
|
+
load_dne_file
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# retrieve a node from the associated network
|
13
|
+
# @param String nodeName
|
14
|
+
# @return Node
|
15
|
+
def node(nodeName)
|
16
|
+
nodes.select{ |n| n if n.name == nodeName }[0]
|
17
|
+
end
|
18
|
+
|
19
|
+
def nodes
|
20
|
+
current_network.nodes
|
21
|
+
end
|
22
|
+
|
23
|
+
def decision_nodes
|
24
|
+
nodes.collect{ |n| n if n.decision_node? }.compact
|
25
|
+
end
|
26
|
+
|
27
|
+
def nature_nodes
|
28
|
+
nodes.collect{ |n| n if n.nature_node? }.compact
|
29
|
+
end
|
30
|
+
|
31
|
+
def load_from_state(network_hash)
|
32
|
+
NeticaLogger.info "network_hash => #{network_hash}"
|
33
|
+
network_hash["decision_nodes"].each do |node_name, node_value|
|
34
|
+
getNode(node_name).value = node_value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def state
|
39
|
+
{ :dne_file_path => dne_file_path, :decision_nodes => node_hash(decision_nodes), :nature_nodes => node_hash(nature_nodes) }
|
40
|
+
end
|
41
|
+
|
42
|
+
def node_hash(nodes)
|
43
|
+
node_hash = {}
|
44
|
+
nodes.collect{|dn| node_hash.store(dn.name, dn.value) }
|
45
|
+
node_hash
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def load_dne_file
|
51
|
+
NeticaLogger.info "Looking for BayesNet .dne file at #{dne_file_path}..."
|
52
|
+
streamer = Java::NorsysNetica::Streamer.new(dne_file_path)
|
53
|
+
self.current_network = Java::NorsysNetica::Net.new(streamer)
|
54
|
+
self.current_network.compile()
|
55
|
+
NeticaLogger.info "Initialized BayesNet -- #{self.current_network.object_id}"
|
56
|
+
self.decision_nodes.each{ |n| n.value = 0 }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'redis'
|
3
|
+
|
4
|
+
module Netica
|
5
|
+
class Environment
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
@@active_networks = []
|
9
|
+
@@explorations = []
|
10
|
+
@@processor = nil
|
11
|
+
@@redis = nil
|
12
|
+
@@logfile = nil
|
13
|
+
|
14
|
+
def self.engage(settings = {})
|
15
|
+
if settings[:logfile]
|
16
|
+
@@logfile = settings[:logfile]
|
17
|
+
else
|
18
|
+
@@logfile = "#{File.dirname(__FILE__)}/../../log/netica.log"
|
19
|
+
end
|
20
|
+
if settings[:license_key]
|
21
|
+
@@processor = Java::NorsysNetica::Environ.new(settings[:license_key])
|
22
|
+
else
|
23
|
+
@@processor = Java::NorsysNetica::Environ.new(nil)
|
24
|
+
end
|
25
|
+
if settings[:redis]
|
26
|
+
@@redis = Redis.new(settings[:redis])
|
27
|
+
end
|
28
|
+
NeticaLogger.info "Initializing the Netica Environment #{@@processor.object_id}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def processor
|
32
|
+
@@processor
|
33
|
+
end
|
34
|
+
|
35
|
+
def active_networks
|
36
|
+
@@active_networks
|
37
|
+
end
|
38
|
+
|
39
|
+
def redis
|
40
|
+
@@redis
|
41
|
+
end
|
42
|
+
|
43
|
+
def logfile_path
|
44
|
+
@@logfile
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Netica
|
2
|
+
class NeticaLogger
|
3
|
+
require 'logger'
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
def self.info(message=nil)
|
7
|
+
self.logfile.info(message) unless message.nil?
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.debug(message=nil)
|
11
|
+
self.logfile.debug(message) unless message.nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.logfile
|
15
|
+
@@my_log ||= Logger.new(File.open(Netica::Environment.instance.logfile_path, File::WRONLY | File::APPEND))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/netica/node.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
class Java::NorsysNetica::Node
|
2
|
+
attr_accessor :value
|
3
|
+
|
4
|
+
# from https://github.com/SixArm/sixarm_ruby_netica
|
5
|
+
NODE_KINDS = {
|
6
|
+
Java::NorsysNetica::Node::NATURE_NODE => :nature,
|
7
|
+
Java::NorsysNetica::Node::DECISION_NODE => :decision,
|
8
|
+
Java::NorsysNetica::Node::UTILITY_NODE => :utility,
|
9
|
+
Java::NorsysNetica::Node::CONSTANT_NODE => :constant,
|
10
|
+
Java::NorsysNetica::Node::DISCONNECTED_NODE => :disconnected,
|
11
|
+
}
|
12
|
+
|
13
|
+
NODE_TYPES = {
|
14
|
+
Java::NorsysNetica::Node::DISCRETE_TYPE => :nature,
|
15
|
+
Java::NorsysNetica::Node::CONTINUOUS_TYPE => :decision,
|
16
|
+
}
|
17
|
+
|
18
|
+
def name
|
19
|
+
getName
|
20
|
+
end
|
21
|
+
|
22
|
+
def kind
|
23
|
+
NODE_KINDS[getKind]
|
24
|
+
end
|
25
|
+
|
26
|
+
def type
|
27
|
+
NODE_TYPES[getType]
|
28
|
+
end
|
29
|
+
|
30
|
+
def decision_node?
|
31
|
+
type == :decision
|
32
|
+
end
|
33
|
+
|
34
|
+
def nature_node?
|
35
|
+
type == :nature
|
36
|
+
end
|
37
|
+
|
38
|
+
def beliefs
|
39
|
+
states.collect{ |st| getBelief(st.to_s) }
|
40
|
+
end
|
41
|
+
|
42
|
+
def hint
|
43
|
+
user().getString("Hint")
|
44
|
+
end
|
45
|
+
|
46
|
+
def top_parent_node
|
47
|
+
getParents().sort_by_belief()[0]
|
48
|
+
end
|
49
|
+
|
50
|
+
def value(state_name = nil)
|
51
|
+
if decision_node?
|
52
|
+
finding().getReal()
|
53
|
+
elsif state_name
|
54
|
+
states.collect{ |st| return getBelief(st.to_s) if state_name == st.to_s }
|
55
|
+
else
|
56
|
+
state_values = {}
|
57
|
+
states.collect{ |st| state_values.store(st.to_s, getBelief(st.to_s)) }
|
58
|
+
state_values
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def value=(new_value)
|
63
|
+
Netica::NeticaLogger.info "Setting #{self.name} to #{new_value}"
|
64
|
+
if decision_node?
|
65
|
+
finding().setReal(new_value)
|
66
|
+
elsif nature_node?
|
67
|
+
finding.enterState(new_value)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def incr
|
72
|
+
if decision_node?
|
73
|
+
self.value = value + 1
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def to_s
|
78
|
+
if type == kind
|
79
|
+
prefix = type.capitalize
|
80
|
+
else
|
81
|
+
prefix = "#{type.capitalize}#{kind.capitalize}"
|
82
|
+
end
|
83
|
+
|
84
|
+
"#{prefix}Node:#{name}"
|
85
|
+
end
|
86
|
+
|
87
|
+
def inspect
|
88
|
+
"#<Java::NorsysNetica::#{self.to_s} value:#{value}>"
|
89
|
+
end
|
90
|
+
|
91
|
+
def states
|
92
|
+
(0...getNumStates()).map{|i| state(i) }
|
93
|
+
end
|
94
|
+
|
95
|
+
def free
|
96
|
+
begin
|
97
|
+
NeticaLogger.info "Deleting #{self.to_s}"
|
98
|
+
finalize()
|
99
|
+
delete()
|
100
|
+
states.each{|s| s==nil or s.free() }
|
101
|
+
rescue Java::NorsysNetica::NeticaException
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Java::NorsysNetica::NodeList
|
2
|
+
def sort_by_belief
|
3
|
+
nodes.sort{|a,b| b.beliefs <=> a.beliefs }
|
4
|
+
end
|
5
|
+
|
6
|
+
def nodes
|
7
|
+
(0...size).map{|i| getNode(i) }
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
"NodeList:#{object_id}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def inspect
|
15
|
+
"#<Java::NorsysNetica::NodeList:#{object_id} size:#{size} nodes:#{nodes.collect{|n| n.to_s }}>"
|
16
|
+
end
|
17
|
+
|
18
|
+
def free
|
19
|
+
begin
|
20
|
+
NeticaLogger.info "Deleting #{self.to_s}"
|
21
|
+
finalize()
|
22
|
+
nodes.each{|n| n==nil or n.free() }
|
23
|
+
rescue Java::NorsysNetica::NeticaException
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/netica/state.rb
ADDED
data/netica.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'netica/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "netica"
|
8
|
+
gem.version = Netica::VERSION
|
9
|
+
gem.authors = ["Jerry Richardson"]
|
10
|
+
gem.email = ["jerry@jerryr.com"]
|
11
|
+
gem.description = "Netica Bayes Network Management"
|
12
|
+
gem.summary = "Tools to manage Bayes Networks with the NeticaJ API"
|
13
|
+
gem.homepage = "http://disruptive.github.io/netica/"
|
14
|
+
gem.license = "MIT"
|
15
|
+
gem.platform = "java"
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Netica::ActiveNetwork do
|
4
|
+
describe "#new" do
|
5
|
+
before(:all) do
|
6
|
+
Netica::Environment.engage
|
7
|
+
end
|
8
|
+
after(:all) do
|
9
|
+
Netica::Environment.instance.processor.finalize
|
10
|
+
end
|
11
|
+
|
12
|
+
context "with ChestClinic.dne" do
|
13
|
+
before(:all) do
|
14
|
+
@active_network = Netica::ActiveNetwork.new("fake_token_identifier", "#{File.dirname(__FILE__)}/../../examples/ChestClinic.dne")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be an active network" do
|
18
|
+
@active_network.should be_an_instance_of(Netica::ActiveNetwork)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should have 8 nodes" do
|
22
|
+
@active_network.network.nodes.length.should == 8
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should export it state as a hash" do
|
26
|
+
@active_network.network.state.should be_an_instance_of(Hash)
|
27
|
+
end
|
28
|
+
|
29
|
+
context "the tuberculosis node" do
|
30
|
+
it "should be less than 0.02 initially" do
|
31
|
+
@active_network.network.node("Tuberculosis").value("present").should be_less_than 0.011
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should be over 0.90 with an abnormal xray" do
|
35
|
+
@active_network.network.node("XRay").value = "abnormal"
|
36
|
+
@active_network.network.node("Tuberculosis").value("present").should be_greater_than 0.092
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Netica::Environment do
|
4
|
+
describe "#engage" do
|
5
|
+
it 'should create a Netica environment' do
|
6
|
+
Netica::Environment.engage.should be_true
|
7
|
+
Netica::Environment.instance.processor.finalize
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should create a logfile' do
|
11
|
+
Netica::Environment.engage
|
12
|
+
Netica::NeticaLogger.info "Test logfile entry"
|
13
|
+
Netica::Environment.instance.processor.finalize
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/spec/netica_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
require 'netica'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.color_enabled = true
|
6
|
+
config.formatter = 'progress'
|
7
|
+
end
|
8
|
+
|
9
|
+
RSpec::Matchers.define :be_less_than do |expected|
|
10
|
+
match do |actual|
|
11
|
+
actual<expected
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
RSpec::Matchers.define :be_greater_than do |expected|
|
16
|
+
match do |actual|
|
17
|
+
actual>expected
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: netica
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
prerelease:
|
6
|
+
platform: java
|
7
|
+
authors:
|
8
|
+
- Jerry Richardson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-14 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Netica Bayes Network Management
|
15
|
+
email:
|
16
|
+
- jerry@jerryr.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".gitignore"
|
22
|
+
- ".rspec"
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- examples/ChestClinic.dne
|
28
|
+
- examples/chest_clinic.rb
|
29
|
+
- lib/netica.rb
|
30
|
+
- lib/netica/active_network.rb
|
31
|
+
- lib/netica/bayes_network.rb
|
32
|
+
- lib/netica/environment.rb
|
33
|
+
- lib/netica/netica_logger.rb
|
34
|
+
- lib/netica/node.rb
|
35
|
+
- lib/netica/node_list.rb
|
36
|
+
- lib/netica/state.rb
|
37
|
+
- lib/netica/version.rb
|
38
|
+
- netica.gemspec
|
39
|
+
- spec/netica/active_network_spec.rb
|
40
|
+
- spec/netica/environment_spec.rb
|
41
|
+
- spec/netica_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
homepage: http://disruptive.github.io/netica/
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: !binary |-
|
55
|
+
MA==
|
56
|
+
none: false
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: !binary |-
|
62
|
+
MA==
|
63
|
+
none: false
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.24
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Tools to manage Bayes Networks with the NeticaJ API
|
70
|
+
test_files:
|
71
|
+
- spec/netica/active_network_spec.rb
|
72
|
+
- spec/netica/environment_spec.rb
|
73
|
+
- spec/netica_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
has_rdoc:
|