spnet 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -33,7 +33,7 @@ class Block
33
33
 
34
34
  # Produces a BlockState object based on this Block object.
35
35
  # @return [BlockState]
36
- def export_state
36
+ def save_state
37
37
  params = collect_params
38
38
 
39
39
  # discard the params that are the same as the initial port params
@@ -46,6 +46,14 @@ class Block
46
46
  BlockState.new(:class_sym => self.class.to_s.to_sym, :params => params)
47
47
  end
48
48
 
49
+ def restore_state state
50
+ state.params.each do |port_name,value|
51
+ if @in_ports.has_key?(port_name)
52
+ @in_ports[port_name].set_value value
53
+ end
54
+ end
55
+ end
56
+
49
57
  private
50
58
 
51
59
  def collect_params
@@ -43,7 +43,7 @@ class Link
43
43
  end
44
44
 
45
45
  # Produce a LinkState object from the current Link object.
46
- def export_state blocks
46
+ def save_state blocks
47
47
  from, to = nil, nil
48
48
 
49
49
  blocks.each do |block_name, block|
@@ -35,15 +35,15 @@ class Network
35
35
  end
36
36
 
37
37
  # Produce a NetworkState object from the current Network object.
38
- def export_state
38
+ def save_state
39
39
  block_states = {}
40
40
  @blocks.each do |block_name, block|
41
- block_states[block_name] = block.export_state
41
+ block_states[block_name] = block.save_state
42
42
  end
43
43
 
44
44
  link_states = []
45
45
  @links.each do |link|
46
- link_states.push link.export_state(@blocks)
46
+ link_states.push link.save_state(@blocks)
47
47
  end
48
48
 
49
49
  return NetworkState.new(:block_states => block_states, :link_states => link_states)
@@ -5,19 +5,33 @@ module SPNet
5
5
  # @author James Tunnell
6
6
  class SignalOutPort < OutPort
7
7
 
8
+ attr_reader :queue
9
+
8
10
  # A new instance of SignalOutPort.
9
11
  def initialize
12
+ @queue = []
10
13
  super(:matching_class => SignalInPort)
11
14
  end
12
15
 
13
- # If linked, return the result of calling the connected SignalInPort object's
14
- # enqueue_values method. Otherwise, return false.
15
- def send_values values
16
- if linked?
16
+ # Add values to queue or send them directly to linked port
17
+ # (if autosend is true).
18
+ # @param [Array] values The values to add.
19
+ # @param [true/false] autosend If true, and this port is linked to another
20
+ # port, skip this port's queue and enqueue values
21
+ # directly on the linked port's queue.
22
+ def enqueue_values values, autosend = true
23
+ if autosend && linked?
17
24
  @link.to.enqueue_values values
25
+ else
26
+ @queue.concat values
18
27
  end
19
- return false
20
28
  end
21
29
 
30
+ # Remove values to queue.
31
+ # @param [Fixnum] count Number of values to remove.
32
+ def dequeue_values count = @queue.count
33
+ raise ArgumentError, "count is greater than @queue.count" if count > @queue.count
34
+ @queue.slice!(0...count)
35
+ end
22
36
  end
23
37
  end
@@ -9,10 +9,10 @@ class BlockState
9
9
  # Define arg specs to use in processing hashed arguments during #initialize.
10
10
  ARG_SPECS = {
11
11
  :class_sym => arg_spec(:reqd => true, :type => Symbol),
12
- :params => arg_spec_hash(:reqd => false, :type => Object)
12
+ :params => arg_spec_hash(:reqd => false, :type => Object),
13
13
  }
14
14
 
15
- attr_reader :class_sym, :hashed_args, :params
15
+ attr_reader :class_sym, :params
16
16
 
17
17
  # A new instance of NetworkState.
18
18
  # @param [Hash] args Hashed arguments for initialization. See Network::ARG_SPECS
@@ -25,17 +25,26 @@ class BlockState
25
25
  def make_block args
26
26
  raise ArgumentError, "args does not have :sample_rate key" unless args.has_key?(:sample_rate)
27
27
 
28
- klass = Kernel.const_get(@class_sym)
28
+ klass = find_class(@class_sym)
29
29
  block = klass.new :sample_rate => args[:sample_rate]
30
+ block.restore_state self
30
31
 
31
- @params.each do |port_name,value|
32
- if block.in_ports.has_key?(port_name)
33
- port = block.in_ports[port_name]
34
- port.set_value value
35
- end
32
+ return block
33
+ end
34
+
35
+ private
36
+
37
+ def find_class sym
38
+ s = sym.to_s
39
+ parts = s.split("::")
40
+
41
+ cur_space = Kernel
42
+
43
+ for i in (0...(parts.count-1))
44
+ cur_space = cur_space.const_get(parts[i].to_sym)
36
45
  end
37
46
 
38
- return block
47
+ return cur_space.const_get parts.last.to_sym
39
48
  end
40
49
  end
41
50
  end
@@ -1,5 +1,5 @@
1
1
  # Provide infrastructure for forming processing networks.
2
2
  module SPNet
3
3
  # spnet version
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.7"
5
5
  end
@@ -61,20 +61,20 @@ describe SPNet::Block do
61
61
  end
62
62
  end
63
63
 
64
- context '#export_state' do
64
+ context '#save_state' do
65
65
  it 'should set class_sym to :Block' do
66
- TestBlock.new(:sample_rate => @sample_rate).export_state.class_sym.should eq(:TestBlock)
66
+ TestBlock.new(:sample_rate => @sample_rate).save_state.class_sym.should eq(:TestBlock)
67
67
  end
68
68
 
69
69
  it 'should have empty port params since no params were modified' do
70
- TestBlock.new(:sample_rate => @sample_rate).export_state.params.should be_empty
70
+ TestBlock.new(:sample_rate => @sample_rate).save_state.params.should be_empty
71
71
  end
72
72
 
73
73
  context 'one param modified' do
74
74
  it 'should set params according to ParamInPort settings' do
75
75
  block = TestBlock.new :sample_rate => @sample_rate
76
76
  block.in_ports["VALUE1"].set_value(4.4)
77
- state = block.export_state
77
+ state = block.save_state
78
78
  state.params.should eq("VALUE1" => 4.4)
79
79
  end
80
80
  end
@@ -84,7 +84,7 @@ describe SPNet::Block do
84
84
  block = TestBlock.new :sample_rate => @sample_rate
85
85
  block.in_ports["VALUE1"].set_value(4.4)
86
86
  block.in_ports["VALUE2"].set_value(5.5)
87
- state = block.export_state
87
+ state = block.save_state
88
88
  state.params.should eq("VALUE1" => 4.4, "VALUE2" => 5.5)
89
89
  end
90
90
  end
@@ -75,7 +75,7 @@ describe SPNet::Link do
75
75
  end
76
76
  end
77
77
 
78
- context '#export_state' do
78
+ context '#save_state' do
79
79
  before :all do
80
80
  blocks = {
81
81
  "A" => TestBlock.new(:sample_rate => 2),
@@ -83,7 +83,7 @@ describe SPNet::Link do
83
83
  }
84
84
  link = Link.new(:from => blocks["A"].out_ports["OUT"], :to => blocks["B"].in_ports["IN"])
85
85
  link.activate
86
- @state = link.export_state blocks
86
+ @state = link.save_state blocks
87
87
  end
88
88
 
89
89
  it 'should make a LinkState object' do
@@ -8,7 +8,7 @@ describe SPNet::SignalInPort do
8
8
  end
9
9
  end
10
10
 
11
- describe 'enqueue_values' do
11
+ describe '#enqueue_values' do
12
12
  it 'should add values to queue' do
13
13
  port = SPNet::SignalInPort.new
14
14
  values = [2.4, 2.6, 4.9, 5.1]
@@ -27,7 +27,7 @@ describe SPNet::SignalInPort do
27
27
  end
28
28
  end
29
29
 
30
- describe 'dequeue_values' do
30
+ describe '#dequeue_values' do
31
31
  it 'should remove N values from queue' do
32
32
  port = SPNet::SignalInPort.new
33
33
  values = [2.4, 2.6, 4.9, 5.1]
@@ -6,14 +6,83 @@ describe SPNet::SignalOutPort do
6
6
  @in_port = SPNet::SignalInPort.new
7
7
  end
8
8
 
9
- describe '#send_values' do
10
- it 'should enqueue the values on the linked input port' do
11
- @out_port.set_link Link.new(:to => @in_port, :from => @out_port)
9
+ describe '#enqueue_values' do
10
+ context 'not linked to a SignalInPort' do
11
+ before :each do
12
+ @port = SPNet::SignalOutPort.new
13
+ @values = [2.4, 2.6, 4.9, 5.1]
14
+ end
12
15
 
13
- @in_port.queue.should be_empty
14
- @out_port.send_values [1,2,3,4]
15
- @in_port.queue.should eq([1,2,3,4])
16
+ context 'autosend flag set false' do
17
+ it 'should add values to queue' do
18
+ @port.enqueue_values @values.clone, false
19
+ @values.should eq(@port.queue)
20
+ end
21
+ end
22
+
23
+ context 'autosend flag set true' do
24
+ it 'should add values to queue' do
25
+ @port.enqueue_values(@values.clone)
26
+ @values.should eq(@port.queue)
27
+ end
28
+ end
29
+ end
30
+
31
+ context 'linked to a SignalInPort' do
32
+ before :each do
33
+ @port = SPNet::SignalOutPort.new
34
+ @in_port = SignalInPort.new
35
+ Link.new(:from => @port, :to => @in_port).activate
36
+ @values = [2.4, 2.6, 4.9, 5.1]
37
+ end
38
+
39
+ context 'autosend flag set false' do
40
+ it 'should add values to the queue' do
41
+ @port.enqueue_values @values.clone, false
42
+ @values.should eq(@port.queue)
43
+ @in_port.queue.should be_empty
44
+ end
45
+ end
46
+
47
+ context 'autosend flag set true' do
48
+ it 'should not add values to the queue, but instead add values to the linked port queue' do
49
+ @port.enqueue_values @values.clone
50
+ @values.should eq(@in_port.queue)
51
+ @port.queue.should be_empty
52
+ end
53
+ end
16
54
  end
17
55
  end
18
56
 
57
+ describe '#dequeue_values' do
58
+ before :each do
59
+ @port = SPNet::SignalOutPort.new
60
+ @values = [2.4, 2.6, 4.9, 5.1]
61
+ @port.enqueue_values(@values.clone)
62
+ end
63
+
64
+ it 'should remove N values from queue' do
65
+ values2 = @port.dequeue_values 2
66
+ @port.queue.count.should be 2
67
+ values2.count.should be 2
68
+ values2.first.should eq(@values.first)
69
+ end
70
+
71
+ it 'should remove all values from queue if no count is given' do
72
+ values2 = @port.dequeue_values
73
+ @port.queue.should be_empty
74
+ values2.should eq(@values)
75
+ end
76
+ end
77
+
78
+ #describe '#send_values' do
79
+ # it 'should enqueue the values on the linked input port' do
80
+ # @out_port.set_link Link.new(:to => @in_port, :from => @out_port)
81
+ #
82
+ # @in_port.queue.should be_empty
83
+ # @out_port.evalues [1,2,3,4]
84
+ # @in_port.queue.should eq([1,2,3,4])
85
+ # end
86
+ #end
87
+
19
88
  end
@@ -34,7 +34,7 @@ class TestBlock < Block
34
34
  )
35
35
 
36
36
  pass_through = lambda do |count|
37
- output.send_values input.dequeue_values(count)
37
+ output.enqueue_values input.dequeue_values(count)
38
38
  end
39
39
 
40
40
  super(
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spnet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-18 00:00:00.000000000 Z
12
+ date: 2013-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hashmake
@@ -186,7 +186,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
186
186
  version: '0'
187
187
  segments:
188
188
  - 0
189
- hash: 654422513
189
+ hash: -2015029166841238127
190
190
  required_rubygems_version: !ruby/object:Gem::Requirement
191
191
  none: false
192
192
  requirements:
@@ -195,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
195
  version: '0'
196
196
  segments:
197
197
  - 0
198
- hash: 654422513
198
+ hash: -2015029166841238127
199
199
  requirements: []
200
200
  rubyforge_project:
201
201
  rubygems_version: 1.8.23