spnet 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +6 -3
- data/lib/spnet.rb +23 -5
- data/lib/spnet/core/block.rb +46 -0
- data/lib/spnet/core/in_port.rb +41 -0
- data/lib/spnet/core/limiter.rb +30 -0
- data/lib/spnet/core/link.rb +70 -0
- data/lib/spnet/core/network.rb +54 -0
- data/lib/spnet/core/out_port.rb +41 -0
- data/lib/spnet/limiters/enum_limiter.rb +24 -0
- data/lib/spnet/limiters/lower_limiter.rb +31 -0
- data/lib/spnet/limiters/no_limiter.rb +11 -0
- data/lib/spnet/limiters/range_limiter.rb +24 -0
- data/lib/spnet/limiters/upper_limiter.rb +31 -0
- data/lib/spnet/ports/command_in_port.rb +16 -8
- data/lib/spnet/ports/command_out_port.rb +17 -11
- data/lib/spnet/ports/param_in_port.rb +42 -0
- data/lib/spnet/ports/param_out_port.rb +32 -0
- data/lib/spnet/ports/signal_in_port.rb +20 -15
- data/lib/spnet/ports/signal_out_port.rb +12 -7
- data/lib/spnet/storage/block_state.rb +41 -0
- data/lib/spnet/storage/link_state.rb +38 -0
- data/lib/spnet/storage/network_state.rb +44 -0
- data/lib/spnet/storage/port_locater.rb +21 -0
- data/lib/spnet/version.rb +2 -1
- data/spec/core/block_spec.rb +80 -0
- data/spec/core/in_port_spec.rb +46 -0
- data/spec/core/limiter_spec.rb +7 -0
- data/spec/core/link_spec.rb +103 -0
- data/spec/core/network_spec.rb +40 -0
- data/spec/core/out_port_spec.rb +46 -0
- data/spec/limiters/enum_limiter_spec.rb +28 -0
- data/spec/limiters/lower_limiter_spec.rb +48 -0
- data/spec/limiters/no_limiter_spec.rb +12 -0
- data/spec/limiters/range_limiter_spec.rb +121 -0
- data/spec/limiters/upper_limiter_spec.rb +47 -0
- data/spec/ports/command_out_port_spec.rb +8 -15
- data/spec/ports/{value_in_port_spec.rb → param_in_port_spec.rb} +2 -2
- data/spec/ports/param_out_port_spec.rb +34 -0
- data/spec/ports/signal_in_port_spec.rb +2 -1
- data/spec/ports/signal_out_port_spec.rb +6 -37
- data/spec/spec_helper.rb +43 -0
- data/spec/storage/block_state_spec.rb +40 -0
- data/spec/storage/link_state_spec.rb +28 -0
- data/spec/storage/network_state_spec.rb +42 -0
- data/spec/storage/port_locater_spec.rb +11 -0
- data/spnet.gemspec +0 -1
- metadata +55 -35
- data/lib/spnet/block.rb +0 -40
- data/lib/spnet/in_port.rb +0 -29
- data/lib/spnet/out_port.rb +0 -64
- data/lib/spnet/ports/value_in_port.rb +0 -27
- data/lib/spnet/ports/value_out_port.rb +0 -28
- data/spec/block_spec.rb +0 -38
- data/spec/in_port_spec.rb +0 -29
- data/spec/out_port_spec.rb +0 -46
- data/spec/ports/value_out_port_spec.rb +0 -41
data/lib/spnet/block.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
|
2
|
-
module SPNet
|
3
|
-
class Block
|
4
|
-
include Hashmake::HashMakeable
|
5
|
-
|
6
|
-
attr_reader :name, :in_ports, :out_ports
|
7
|
-
|
8
|
-
DO_NOTHING = ->(){}
|
9
|
-
|
10
|
-
HASHED_ARG_SPECS = [
|
11
|
-
Hashmake::ArgSpec.new(:reqd => false, :key => :name, :type => String, :default => "UNNAMED"),
|
12
|
-
Hashmake::ArgSpec.new(:reqd => false, :key => :algorithm, :type => Proc, :default => DO_NOTHING),
|
13
|
-
Hashmake::ArgSpec.new(:reqd => false, :key => :in_ports, :type => InPort, :container => Hashmake::ArgSpec::CONTAINER_ARRAY, :default => ->(){ Array.new } ),
|
14
|
-
Hashmake::ArgSpec.new(:reqd => false, :key => :out_ports, :type => OutPort, :container => Hashmake::ArgSpec::CONTAINER_ARRAY, :default => ->(){ Array.new }),
|
15
|
-
]
|
16
|
-
|
17
|
-
def initialize args = {}
|
18
|
-
hash_make Block::HASHED_ARG_SPECS, args
|
19
|
-
end
|
20
|
-
|
21
|
-
def find_ports name, ignore_case = true
|
22
|
-
matches = (@in_ports + @out_ports).select do |port|
|
23
|
-
if ignore_case
|
24
|
-
port.name.casecmp(name) == 0
|
25
|
-
else
|
26
|
-
port.name == name
|
27
|
-
end
|
28
|
-
end
|
29
|
-
return matches
|
30
|
-
end
|
31
|
-
|
32
|
-
def find_first_port name, ignore_case = true
|
33
|
-
return find_ports(name, ignore_case).first
|
34
|
-
end
|
35
|
-
|
36
|
-
def step count
|
37
|
-
@algorithm.call count
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
data/lib/spnet/in_port.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'hashmake'
|
2
|
-
|
3
|
-
module SPNet
|
4
|
-
class InPort
|
5
|
-
|
6
|
-
include Hashmake::HashMakeable
|
7
|
-
|
8
|
-
ARG_SPECS = [
|
9
|
-
Hashmake::ArgSpec.new(:reqd => false, :key => :name, :type => String, :default => "UNNAMED"),
|
10
|
-
Hashmake::ArgSpec.new(:reqd => true, :key => :matching_port_class, :type => Class),
|
11
|
-
]
|
12
|
-
|
13
|
-
attr_reader :name, :link
|
14
|
-
|
15
|
-
def initialize args
|
16
|
-
hash_make InPort::ARG_SPECS, args
|
17
|
-
@link = nil
|
18
|
-
end
|
19
|
-
|
20
|
-
def clear_link
|
21
|
-
@link = nil
|
22
|
-
end
|
23
|
-
|
24
|
-
def set_link link
|
25
|
-
raise ArgumentError, "link #{link} is not a #{@matching_port_class}" unless link.is_a?(@matching_port_class)
|
26
|
-
@link = link
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
data/lib/spnet/out_port.rb
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
require 'set'
|
2
|
-
require 'hashmake'
|
3
|
-
|
4
|
-
module SPNet
|
5
|
-
class OutPort
|
6
|
-
|
7
|
-
include Hashmake::HashMakeable
|
8
|
-
|
9
|
-
ARG_SPECS = [
|
10
|
-
Hashmake::ArgSpec.new(:reqd => false, :key => :name, :type => String, :default => "UNNAMED"),
|
11
|
-
Hashmake::ArgSpec.new(:reqd => true, :key => :matching_port_class, :type => Class),
|
12
|
-
]
|
13
|
-
|
14
|
-
attr_reader :name, :links
|
15
|
-
|
16
|
-
def initialize hashed_args = {}
|
17
|
-
hash_make OutPort::ARG_SPECS, hashed_args
|
18
|
-
@links = Set.new
|
19
|
-
end
|
20
|
-
|
21
|
-
def add_link link
|
22
|
-
raise ArgumentError, "link #{link} is not a #{@matching_port_class}" unless link.is_a?(@matching_port_class)
|
23
|
-
raise ArgumentError, "link #{link} is already linked" if link.link
|
24
|
-
@links.add link
|
25
|
-
link.set_link self
|
26
|
-
end
|
27
|
-
|
28
|
-
def remove_link link
|
29
|
-
raise ArgumentError, "@links does not include link #{link}" unless @links.include? link
|
30
|
-
@links.delete link
|
31
|
-
link.clear_link
|
32
|
-
end
|
33
|
-
|
34
|
-
def remove_bad_links
|
35
|
-
find_bad_links_and :remove
|
36
|
-
end
|
37
|
-
|
38
|
-
def correct_bad_links
|
39
|
-
find_bad_links_and :correct
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
def find_bad_links_and action
|
45
|
-
marked = []
|
46
|
-
|
47
|
-
@links.each do |link|
|
48
|
-
bad = (link.link == nil) or (link.link != self)
|
49
|
-
if bad
|
50
|
-
marked.push link
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
marked.each do |link|
|
55
|
-
if action == :remove
|
56
|
-
@links.delete link
|
57
|
-
elsif action == :correct
|
58
|
-
link.set_link self
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'hashmake'
|
2
|
-
|
3
|
-
module SPNet
|
4
|
-
class ValueInPort < InPort
|
5
|
-
|
6
|
-
include Hashmake::HashMakeable
|
7
|
-
|
8
|
-
ARG_SPECS = [
|
9
|
-
Hashmake::ArgSpec.new(:key => :get_value_handler, :reqd => true, :type => Proc),
|
10
|
-
Hashmake::ArgSpec.new(:key => :set_value_handler, :reqd => true, :type => Proc)
|
11
|
-
]
|
12
|
-
|
13
|
-
def initialize hashed_args = {}
|
14
|
-
hash_make(ValueInPort::ARG_SPECS, hashed_args)
|
15
|
-
hashed_args.merge!(:matching_port_class => ValueOutPort)
|
16
|
-
super(hashed_args)
|
17
|
-
end
|
18
|
-
|
19
|
-
def set_value value
|
20
|
-
@set_value_handler.call value
|
21
|
-
end
|
22
|
-
|
23
|
-
def get_value
|
24
|
-
@get_value_handler.call
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'set'
|
2
|
-
|
3
|
-
module SPNet
|
4
|
-
class ValueOutPort < OutPort
|
5
|
-
|
6
|
-
def initialize hashed_args = {}
|
7
|
-
hashed_args.merge!(:matching_port_class => ValueInPort)
|
8
|
-
super(hashed_args)
|
9
|
-
end
|
10
|
-
|
11
|
-
def set_value value
|
12
|
-
rvs = []
|
13
|
-
@links.each do |link|
|
14
|
-
rvs.push link.set_value value
|
15
|
-
end
|
16
|
-
return rvs
|
17
|
-
end
|
18
|
-
|
19
|
-
def get_value
|
20
|
-
rvs = []
|
21
|
-
@links.each do |link|
|
22
|
-
rvs.push link.get_value
|
23
|
-
end
|
24
|
-
return rvs
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
end
|
data/spec/block_spec.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
describe SPNet::Block do
|
4
|
-
context '.new' do
|
5
|
-
context 'no I/O ports given' do
|
6
|
-
before :all do
|
7
|
-
@block = SPNet::Block.new
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'should have no input ports' do
|
11
|
-
@block.in_ports.should be_empty
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'should have no output ports' do
|
15
|
-
@block.out_ports.should be_empty
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
context '1 in and 1 out port given' do
|
20
|
-
before :all do
|
21
|
-
@block = SPNet::Block.new(
|
22
|
-
:in_ports => [SPNet::SignalInPort.new(:name => "IN")],
|
23
|
-
:out_ports => [SPNet::SignalOutPort.new(:name => "OUT")],
|
24
|
-
)
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'should have 1 input port' do
|
28
|
-
@block.in_ports.count.should be 1
|
29
|
-
@block.in_ports.first.name.should eq("IN")
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'should have 1 output port' do
|
33
|
-
@block.out_ports.count.should be 1
|
34
|
-
@block.out_ports.first.name.should eq("OUT")
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
data/spec/in_port_spec.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
describe SPNet::InPort do
|
4
|
-
describe '.new' do
|
5
|
-
it 'should not have any links' do
|
6
|
-
port = SPNet::InPort.new :matching_port_class => SPNet::OutPort
|
7
|
-
port.link.should be_nil
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
describe '#set_link' do
|
12
|
-
it 'should set link to given OutPort' do
|
13
|
-
in_port = SPNet::InPort.new :matching_port_class => SPNet::OutPort
|
14
|
-
out_port = SPNet::OutPort.new :matching_port_class => SPNet::InPort
|
15
|
-
in_port.set_link out_port
|
16
|
-
in_port.link.should eq(out_port)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
describe '#clear_link' do
|
21
|
-
it 'should set link to given OutPort' do
|
22
|
-
in_port = SPNet::InPort.new :matching_port_class => SPNet::OutPort
|
23
|
-
out_port = SPNet::OutPort.new :matching_port_class => SPNet::InPort
|
24
|
-
in_port.set_link out_port
|
25
|
-
in_port.clear_link
|
26
|
-
in_port.link.should be_nil
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
data/spec/out_port_spec.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
describe SPNet::OutPort do
|
4
|
-
before :each do
|
5
|
-
@in_port = SPNet::InPort.new :matching_port_class => SPNet::OutPort
|
6
|
-
@out_port = SPNet::OutPort.new :matching_port_class => SPNet::InPort
|
7
|
-
end
|
8
|
-
|
9
|
-
describe '.new' do
|
10
|
-
it 'should have no links' do
|
11
|
-
@out_port.links.should be_empty
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
describe '#add_link' do
|
16
|
-
it 'should add the given input port to links' do
|
17
|
-
@out_port.add_link @in_port
|
18
|
-
@out_port.links.count.should be 1
|
19
|
-
@out_port.links.first.should eq(@in_port)
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'should also link the output port to the given input port' do
|
23
|
-
@out_port.add_link @in_port
|
24
|
-
@in_port.link.should eq(@out_port)
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'should raise ArgumentError if the given input port is already linked' do
|
28
|
-
@out_port.add_link @in_port
|
29
|
-
lambda { @out_port.add_link(@in_port) }.should raise_error(ArgumentError)
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'should raise ArgumentError if port is not input port' do
|
33
|
-
out_port2 = SPNet::OutPort.new :matching_port_class => SPNet::InPort
|
34
|
-
lambda { @out_port.add_link(out_port2) }.should raise_error(ArgumentError)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe '#remove_link' do
|
39
|
-
it 'should remove the given input port (if it is already linked to the output port)' do
|
40
|
-
@out_port.add_link @in_port
|
41
|
-
@out_port.remove_link @in_port
|
42
|
-
@out_port.links.should be_empty
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
describe SPNet::ValueOutPort do
|
4
|
-
before :each do
|
5
|
-
@value = 0
|
6
|
-
|
7
|
-
set_value_handler = lambda do |value|
|
8
|
-
return @value = value
|
9
|
-
end
|
10
|
-
|
11
|
-
get_value_handler = lambda do
|
12
|
-
return @value
|
13
|
-
end
|
14
|
-
|
15
|
-
@out_port = SPNet::ValueOutPort.new
|
16
|
-
@in_port = SPNet::ValueInPort.new :get_value_handler => get_value_handler, :set_value_handler => set_value_handler
|
17
|
-
end
|
18
|
-
|
19
|
-
describe '#add_link' do
|
20
|
-
it 'should raise ArgumentError if port is not ValueInPort' do
|
21
|
-
@in_port2 = SPNet::SignalInPort.new
|
22
|
-
lambda { @out_port.add_link(@in_port2) }.should raise_error(ArgumentError)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe '#set_value' do
|
27
|
-
it 'should pass the given value through ValueInPort#set_value' do
|
28
|
-
@out_port.add_link @in_port
|
29
|
-
rv = @out_port.set_value 5
|
30
|
-
@value.should eq(5)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
describe '#get_value' do
|
35
|
-
it "should return the value from each linked port's ValueInPort#get_value" do
|
36
|
-
@value = 7
|
37
|
-
@out_port.add_link @in_port
|
38
|
-
@out_port.get_value.first.should eq(7)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|