Class: SPNet::Block

Inherits:
Object
  • Object
show all
Includes:
Hashmake::HashMakeable
Defined in:
lib/spnet/core/block.rb

Overview

Base class for encapsulating processing functionality. Connects to other blocks via input and output ports.

Author:

  • James Tunnell

Constant Summary collapse

ARG_SPECS =

Define ArgSpec’s to use in processing hashed arguments during #initialize.

{
  :sample_rate => arg_spec(:reqd => true, :type => Numeric, :validator => ->(a){ a > 0 }),
  :algorithm => arg_spec(:reqd => true, :type => Proc, :validator => ->(p){ p.arity == 1 }),
  :in_ports => arg_spec_hash(:reqd => false, :type => InPort),
  :out_ports => arg_spec_hash(:reqd => false, :type => OutPort),
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Block

A new instance of Block.

Parameters:

  • args (Hash) (defaults to: {})

    Hashed arguments for initialization. See Block::ARG_SPECS for details of which keys are required.



23
24
25
26
# File 'lib/spnet/core/block.rb', line 23

def initialize args = {}
  hash_make args, Block::ARG_SPECS
  @initial_params = collect_params
end

Instance Attribute Details

#in_portsObject (readonly)

Returns the value of attribute in_ports.



10
11
12
# File 'lib/spnet/core/block.rb', line 10

def in_ports
  @in_ports
end

#out_portsObject (readonly)

Returns the value of attribute out_ports.



10
11
12
# File 'lib/spnet/core/block.rb', line 10

def out_ports
  @out_ports
end

#sample_rateObject (readonly)

Returns the value of attribute sample_rate.



10
11
12
# File 'lib/spnet/core/block.rb', line 10

def sample_rate
  @sample_rate
end

Instance Method Details

#restore_state(state) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/spnet/core/block.rb', line 49

def restore_state state
  state.params.each do |port_name,value|
    if @in_ports.has_key?(port_name)
      @in_ports[port_name].set_value value
    end
  end
end

#save_stateBlockState

Produces a BlockState object based on this Block object.

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/spnet/core/block.rb', line 36

def save_state
  params = collect_params
  
  # discard the params that are the same as the initial port params
  params.keys.each do |key|
    if params[key] == @initial_params[key]
      params.delete key
    end
  end
  
  BlockState.new(:class_sym => self.class.to_s.to_sym, :params => params)
end

#step(count) ⇒ Object

Execute the block algorithm.

Parameters:

  • count (Fixnum)

    The number of steps to execute. Passed on to the algorithm Proc.



30
31
32
# File 'lib/spnet/core/block.rb', line 30

def step count
  @algorithm.call count
end