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.
Files changed (56) hide show
  1. data/README.rdoc +6 -3
  2. data/lib/spnet.rb +23 -5
  3. data/lib/spnet/core/block.rb +46 -0
  4. data/lib/spnet/core/in_port.rb +41 -0
  5. data/lib/spnet/core/limiter.rb +30 -0
  6. data/lib/spnet/core/link.rb +70 -0
  7. data/lib/spnet/core/network.rb +54 -0
  8. data/lib/spnet/core/out_port.rb +41 -0
  9. data/lib/spnet/limiters/enum_limiter.rb +24 -0
  10. data/lib/spnet/limiters/lower_limiter.rb +31 -0
  11. data/lib/spnet/limiters/no_limiter.rb +11 -0
  12. data/lib/spnet/limiters/range_limiter.rb +24 -0
  13. data/lib/spnet/limiters/upper_limiter.rb +31 -0
  14. data/lib/spnet/ports/command_in_port.rb +16 -8
  15. data/lib/spnet/ports/command_out_port.rb +17 -11
  16. data/lib/spnet/ports/param_in_port.rb +42 -0
  17. data/lib/spnet/ports/param_out_port.rb +32 -0
  18. data/lib/spnet/ports/signal_in_port.rb +20 -15
  19. data/lib/spnet/ports/signal_out_port.rb +12 -7
  20. data/lib/spnet/storage/block_state.rb +41 -0
  21. data/lib/spnet/storage/link_state.rb +38 -0
  22. data/lib/spnet/storage/network_state.rb +44 -0
  23. data/lib/spnet/storage/port_locater.rb +21 -0
  24. data/lib/spnet/version.rb +2 -1
  25. data/spec/core/block_spec.rb +80 -0
  26. data/spec/core/in_port_spec.rb +46 -0
  27. data/spec/core/limiter_spec.rb +7 -0
  28. data/spec/core/link_spec.rb +103 -0
  29. data/spec/core/network_spec.rb +40 -0
  30. data/spec/core/out_port_spec.rb +46 -0
  31. data/spec/limiters/enum_limiter_spec.rb +28 -0
  32. data/spec/limiters/lower_limiter_spec.rb +48 -0
  33. data/spec/limiters/no_limiter_spec.rb +12 -0
  34. data/spec/limiters/range_limiter_spec.rb +121 -0
  35. data/spec/limiters/upper_limiter_spec.rb +47 -0
  36. data/spec/ports/command_out_port_spec.rb +8 -15
  37. data/spec/ports/{value_in_port_spec.rb → param_in_port_spec.rb} +2 -2
  38. data/spec/ports/param_out_port_spec.rb +34 -0
  39. data/spec/ports/signal_in_port_spec.rb +2 -1
  40. data/spec/ports/signal_out_port_spec.rb +6 -37
  41. data/spec/spec_helper.rb +43 -0
  42. data/spec/storage/block_state_spec.rb +40 -0
  43. data/spec/storage/link_state_spec.rb +28 -0
  44. data/spec/storage/network_state_spec.rb +42 -0
  45. data/spec/storage/port_locater_spec.rb +11 -0
  46. data/spnet.gemspec +0 -1
  47. metadata +55 -35
  48. data/lib/spnet/block.rb +0 -40
  49. data/lib/spnet/in_port.rb +0 -29
  50. data/lib/spnet/out_port.rb +0 -64
  51. data/lib/spnet/ports/value_in_port.rb +0 -27
  52. data/lib/spnet/ports/value_out_port.rb +0 -28
  53. data/spec/block_spec.rb +0 -38
  54. data/spec/in_port_spec.rb +0 -29
  55. data/spec/out_port_spec.rb +0 -46
  56. data/spec/ports/value_out_port_spec.rb +0 -41
@@ -0,0 +1,46 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SPNet::InPort do
4
+ describe '.new' do
5
+ it 'link should be nil' do
6
+ port = InPort.new :matching_class => OutPort
7
+ port.link.should be_nil
8
+ end
9
+ end
10
+
11
+ describe '#set_link' do
12
+ it 'should set link to given link' do
13
+ in_port = InPort.new(:matching_class => OutPort)
14
+ out_port = OutPort.new(:matching_class => InPort)
15
+ link = Link.new :from => out_port, :to => in_port
16
+ in_port.set_link link
17
+ in_port.link.should eq(link)
18
+ end
19
+ end
20
+
21
+ describe '#clear_link' do
22
+ it 'should set link to nil' do
23
+ in_port = InPort.new(:matching_class => OutPort)
24
+ out_port = OutPort.new(:matching_class => InPort)
25
+ link = Link.new :from => out_port, :to => in_port
26
+ in_port.set_link link
27
+ in_port.link.should eq(link)
28
+ in_port.clear_link
29
+ in_port.link.should be_nil
30
+ end
31
+ end
32
+
33
+ describe '#linked?' do
34
+ it 'should return false if port is not linked' do
35
+ in_port = InPort.new(:matching_class => OutPort)
36
+ in_port.linked?.should be_false
37
+ end
38
+
39
+ it 'should return true if port is linked' do
40
+ in_port = InPort.new(:matching_class => OutPort)
41
+ out_port = OutPort.new(:matching_class => InPort)
42
+ Link.new(:from => out_port, :to => in_port).activate
43
+ in_port.linked?.should be_true
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SPNet::Limiter do
4
+ it 'should raise NotImplementedError if apply_limit is called' do
5
+ lambda { Limiter.new.apply_limit 5, 4 }.should raise_error(NotImplementedError)
6
+ end
7
+ end
@@ -0,0 +1,103 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SPNet::Link do
4
+ context '.new' do
5
+ it 'should contain the :to and :from given' do
6
+ out_port = SignalOutPort.new
7
+ in_port = SignalInPort.new
8
+ link = Link.new(:from => out_port,:to => in_port)
9
+
10
+ link.to.should eq(in_port)
11
+ link.from.should eq(out_port)
12
+ end
13
+
14
+ it 'should raise ArgumentError if to port does not match from port' do
15
+ out_port = ParamOutPort.new
16
+ in_port = SignalInPort.new
17
+ lambda { Link.new(:from => out_port, :in => in_port) }.should raise_error(ArgumentError)
18
+ end
19
+
20
+ it 'should not set to and from link to eachother' do
21
+ out_port = SignalOutPort.new
22
+ in_port = SignalInPort.new
23
+ link = Link.new(:from => out_port,:to => in_port)
24
+ link.to.link.should be_nil
25
+ link.from.link.should be_nil
26
+ end
27
+ end
28
+
29
+ context '#activate' do
30
+ it 'should set.link to and from.link to self' do
31
+ out_port = SignalOutPort.new
32
+ in_port = SignalInPort.new
33
+ link = Link.new(:from => out_port,:to => in_port)
34
+ link.activate
35
+ link.to.link.should eq(link)
36
+ link.from.link.should eq(link)
37
+ end
38
+ end
39
+
40
+ context '#activate' do
41
+ it 'should set.link to and from.link to nil' do
42
+ out_port = SignalOutPort.new
43
+ in_port = SignalInPort.new
44
+ link = Link.new(:from => out_port,:to => in_port)
45
+ link.activate
46
+ link.deactivate
47
+ link.to.link.should eq(nil)
48
+ link.from.link.should eq(nil)
49
+ end
50
+ end
51
+
52
+ context '#active?' do
53
+ it 'should return true if activate has been called and deactivate has never been called' do
54
+ out_port = SignalOutPort.new
55
+ in_port = SignalInPort.new
56
+ link = Link.new(:from => out_port,:to => in_port)
57
+ link.activate
58
+ link.active?.should be_true
59
+ end
60
+
61
+ it 'should return false if activate has never been called' do
62
+ out_port = SignalOutPort.new
63
+ in_port = SignalInPort.new
64
+ link = Link.new(:from => out_port,:to => in_port)
65
+ link.active?.should be_false
66
+ end
67
+
68
+ it 'should return false if activate been called but deactivate was called later' do
69
+ out_port = SignalOutPort.new
70
+ in_port = SignalInPort.new
71
+ link = Link.new(:from => out_port,:to => in_port)
72
+ link.activate
73
+ link.deactivate
74
+ link.active?.should be_false
75
+ end
76
+ end
77
+
78
+ context '#export_state' do
79
+ before :all do
80
+ blocks = {
81
+ "A" => TestBlock.new(:sample_rate => 2),
82
+ "B" => TestBlock.new(:sample_rate => 2),
83
+ }
84
+ link = Link.new(:from => blocks["A"].out_ports["OUT"], :to => blocks["B"].in_ports["IN"])
85
+ link.activate
86
+ @state = link.export_state blocks
87
+ end
88
+
89
+ it 'should make a LinkState object' do
90
+ @state.should be_a(LinkState)
91
+ end
92
+
93
+ it 'should be from "A.OUT"' do
94
+ @state.from.block_name.should eq("A")
95
+ @state.from.port_name.should eq("OUT")
96
+ end
97
+
98
+ it 'should be to "B.IN"' do
99
+ @state.to.block_name.should eq("B")
100
+ @state.to.port_name.should eq("IN")
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SPNet::Network do
4
+ context '.new' do
5
+ before :all do
6
+ @sample_rate = 1.0
7
+ end
8
+
9
+ context 'no name, blocks, or links given' do
10
+ before :all do
11
+ @network = Network.new :sample_rate => @sample_rate
12
+ end
13
+
14
+ it 'should have empty name' do
15
+ @network.name.should be_empty
16
+ end
17
+
18
+ it 'should have no blocks' do
19
+ @network.blocks.should be_empty
20
+ end
21
+
22
+ it 'should have no links' do
23
+ @network.links.should be_empty
24
+ end
25
+ end
26
+
27
+ it 'should activate links' do
28
+ a = TestBlock.new(:sample_rate => @sample_rate)
29
+ b = TestBlock.new(:sample_rate => @sample_rate)
30
+ link = Link.new(:from => a.out_ports["OUT"], :to => b.in_ports["IN"])
31
+ network = Network.new(
32
+ :sample_rate => @sample_rate,
33
+ :blocks => { "A" => a, "B" => b },
34
+ :links => [ link ],
35
+ )
36
+ link.active?.should be_true
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,46 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SPNet::OutPort do
4
+ describe '.new' do
5
+ it 'link should be nil' do
6
+ port = OutPort.new :matching_class => InPort
7
+ port.link.should be_nil
8
+ end
9
+ end
10
+
11
+ describe '#set_link' do
12
+ it 'should set link to given link' do
13
+ in_port = InPort.new(:matching_class => OutPort)
14
+ out_port = OutPort.new(:matching_class => InPort)
15
+ link = Link.new :from => out_port, :to => in_port
16
+ out_port.set_link link
17
+ out_port.link.should eq(link)
18
+ end
19
+ end
20
+
21
+ describe '#clear_link' do
22
+ it 'should set link to nil' do
23
+ in_port = InPort.new(:matching_class => OutPort)
24
+ out_port = OutPort.new(:matching_class => InPort)
25
+ link = Link.new :from => out_port, :to => in_port
26
+ out_port.set_link link
27
+ out_port.link.should eq(link)
28
+ out_port.clear_link
29
+ out_port.link.should be_nil
30
+ end
31
+ end
32
+
33
+ describe '#linked?' do
34
+ it 'should return false if port is not linked' do
35
+ out_port = OutPort.new(:matching_class => InPort)
36
+ out_port.linked?.should be_false
37
+ end
38
+
39
+ it 'should return true if port is linked' do
40
+ in_port = InPort.new(:matching_class => OutPort)
41
+ out_port = OutPort.new(:matching_class => InPort)
42
+ Link.new(:from => out_port, :to => in_port).activate
43
+ out_port.linked?.should be_true
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SPNet::EnumLimiter do
4
+ describe '.new' do
5
+ it 'should raise ArgumentError if non-Enumerable is given' do
6
+ lambda { EnumLimiter.new(5) }.should raise_error(ArgumentError)
7
+ end
8
+ end
9
+
10
+ describe '.apply_limit' do
11
+ it 'should return the given value if it is contained in @values' do
12
+ values = [1,3,5,7]
13
+ limiter = EnumLimiter.new values
14
+ values.each do |value|
15
+ limiter.apply_limit(value, 0).should eq(value)
16
+ end
17
+ end
18
+
19
+ it 'should return the given current_value if the given value is not contained in @values' do
20
+ values = [1,3,5,7]
21
+ non_values = [2,4,6,8]
22
+ limiter = EnumLimiter.new values
23
+ non_values.each do |value|
24
+ limiter.apply_limit(value, 0).should eq(0)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SPNet::LowerLimiter do
4
+ describe '.apply_limit' do
5
+ context 'non-inclusive' do
6
+ before :all do
7
+ @lower = 1
8
+ @limiter = LowerLimiter.new(@lower, false)
9
+ end
10
+
11
+ it 'should return the given value it it is above lower limit' do
12
+ ok_values = [@lower + Float::EPSILON, @lower * 2.0]
13
+ limited_values = ok_values.map { |value| @limiter.apply_limit value }
14
+ limited_values.should eq(ok_values)
15
+ end
16
+
17
+ it 'should return the lower limit + Float::EPSILON if the given value is at or below the lower limit' do
18
+ bad_values = [@lower, @lower - Float::EPSILON, @lower / 2.0]
19
+ limited_values = bad_values.map { |value| @limiter.apply_limit value }
20
+ limited_values.each do |value|
21
+ value.should eq(@lower + Float::EPSILON)
22
+ end
23
+ end
24
+ end
25
+
26
+ context 'inclusive lower limit, non-inclusive upper limit' do
27
+ before :all do
28
+ @lower = 1
29
+ @limiter = LowerLimiter.new(@lower, true)
30
+ end
31
+
32
+ it 'should return the given value it it is at or above the lower limit' do
33
+ ok_values = [@lower, @lower + Float::EPSILON, @lower * 2.0]
34
+ limited_values = ok_values.map { |value| @limiter.apply_limit value }
35
+ limited_values.should eq(ok_values)
36
+ end
37
+
38
+ it 'should return the lower limit if the given value is below the lower limit' do
39
+ bad_values = [@lower - Float::EPSILON, @lower / 2.0]
40
+ limited_values = bad_values.map { |value| @limiter.apply_limit value }
41
+ limited_values.each do |value|
42
+ value.should eq(@lower)
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SPNet::NoLimiter do
4
+ describe '.new' do
5
+ it 'should always return the given value' do
6
+ limiter = NoLimiter.new
7
+ [-1, 1, Float::MAX, Float::MIN, "horse"].each do |value|
8
+ value.should eq(limiter.apply_limit(value))
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,121 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SPNet::RangeLimiter do
4
+ describe '.apply_limit' do
5
+ context 'non-inclusive lower limit, non-inclusive upper limit' do
6
+ before :all do
7
+ @lower, @upper = 1, 3
8
+ @limiter = RangeLimiter.new(@lower, false, @upper, false)
9
+ end
10
+
11
+ it 'should return the given value it it is above lower limit and below upper limit' do
12
+ ok_values = [@lower + Float::EPSILON, (@lower + @upper) / 2.0, @upper - Float::EPSILON]
13
+ limited_values = ok_values.map { |value| @limiter.apply_limit value }
14
+ limited_values.should eq(ok_values)
15
+ end
16
+
17
+ it 'should return the lower limit + Float::EPSILON if the given value is at or below the lower limit' do
18
+ bad_values = [@lower, @lower - Float::EPSILON, @lower - 1]
19
+ limited_values = bad_values.map { |value| @limiter.apply_limit value }
20
+ limited_values.each do |value|
21
+ value.should eq(@lower + Float::EPSILON)
22
+ end
23
+ end
24
+
25
+ it 'should return the upper limit - Float::EPSILON if the given value is at or above the upper limit' do
26
+ bad_values = [@upper, @upper + Float::EPSILON, @upper + 1]
27
+ limited_values = bad_values.map { |value| @limiter.apply_limit value }
28
+ limited_values.each do |value|
29
+ value.should eq(@upper - Float::EPSILON)
30
+ end
31
+ end
32
+ end
33
+
34
+ context 'inclusive lower limit, non-inclusive upper limit' do
35
+ before :all do
36
+ @lower, @upper = 1, 3
37
+ @limiter = RangeLimiter.new(@lower, true, @upper, false)
38
+ end
39
+
40
+ it 'should return the given value it it is at or above the lower limit and below the upper limit' do
41
+ ok_values = [@lower, (@lower + @upper) / 2.0, @upper - Float::EPSILON]
42
+ limited_values = ok_values.map { |value| @limiter.apply_limit value }
43
+ limited_values.should eq(ok_values)
44
+ end
45
+
46
+ it 'should return the lower limit if the given value is below the lower limit' do
47
+ bad_values = [@lower - Float::EPSILON, @lower - 1]
48
+ limited_values = bad_values.map { |value| @limiter.apply_limit value }
49
+ limited_values.each do |value|
50
+ value.should eq(@lower)
51
+ end
52
+ end
53
+
54
+ it 'should return the upper limit - Float::EPSILON if the given value is at or above the upper limit' do
55
+ bad_values = [@upper, @upper + 1]
56
+ limited_values = bad_values.map { |value| @limiter.apply_limit value }
57
+ limited_values.each do |value|
58
+ value.should eq(@upper - Float::EPSILON)
59
+ end
60
+ end
61
+ end
62
+
63
+ context 'non-inclusive lower limit, inclusive upper limit' do
64
+ before :all do
65
+ @lower, @upper = 1, 3
66
+ @limiter = RangeLimiter.new(@lower, false, @upper, true)
67
+ end
68
+
69
+ it 'should return the given value it it is above the lower limit and at or below the upper limit' do
70
+ ok_values = [@lower + Float::EPSILON, (@lower + @upper) / 2.0, @upper]
71
+ limited_values = ok_values.map { |value| @limiter.apply_limit value }
72
+ limited_values.should eq(ok_values)
73
+ end
74
+
75
+ it 'should return the lower limit + Float::EPSILON if the given value is at or below the lower limit' do
76
+ bad_values = [@lower, @lower - Float::EPSILON, @lower - 1]
77
+ limited_values = bad_values.map { |value| @limiter.apply_limit value }
78
+ limited_values.each do |value|
79
+ value.should eq(@lower + Float::EPSILON)
80
+ end
81
+ end
82
+
83
+ it 'should return the upper limit if the given value is above the upper limit' do
84
+ bad_values = [@upper + Float::EPSILON, @upper + 1]
85
+ limited_values = bad_values.map { |value| @limiter.apply_limit value }
86
+ limited_values.each do |value|
87
+ value.should eq(@upper)
88
+ end
89
+ end
90
+ end
91
+
92
+ context 'inclusive lower limit, inclusive upper limit' do
93
+ before :all do
94
+ @lower, @upper = 1, 3
95
+ @limiter = RangeLimiter.new(@lower, true, @upper, true)
96
+ end
97
+
98
+ it 'should return the given value it it is at or above the lower limit and at or below the upper limit' do
99
+ ok_values = [@lower, (@lower + @upper) / 2.0, @upper]
100
+ limited_values = ok_values.map { |value| @limiter.apply_limit value }
101
+ limited_values.should eq(ok_values)
102
+ end
103
+
104
+ it 'should return the lower limit if the given value is below the lower limit' do
105
+ bad_values = [@lower - Float::EPSILON, @lower - 1]
106
+ limited_values = bad_values.map { |value| @limiter.apply_limit value }
107
+ limited_values.each do |value|
108
+ value.should eq(@lower)
109
+ end
110
+ end
111
+
112
+ it 'should return the upper limit if the given value is above the upper limit' do
113
+ bad_values = [@upper + Float::EPSILON, @upper + 1]
114
+ limited_values = bad_values.map { |value| @limiter.apply_limit value }
115
+ limited_values.each do |value|
116
+ value.should eq(@upper)
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end