Class: SPNet::Link

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

Overview

Form a connection between an OutPort and an InPort.

Author:

  • James Tunnell

Constant Summary collapse

ARG_SPECS =

Define arg specs to use in processing hashed arguments during #initialize.

{
  :from => arg_spec(:reqd => true, :type => OutPort),
  :to => arg_spec(:reqd => true, :type => InPort)
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Link

A new instance of Link. Link is not active by default (does not set from.link and to.link to self).

Parameters:

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

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

Raises:

  • (ArgumentError)


21
22
23
24
25
26
# File 'lib/spnet/core/link.rb', line 21

def initialize args = {}
  hash_make args, Link::ARG_SPECS
  
  raise ArgumentError, "from port class #{@from.class} is not a #{@to.matching_class}" unless @from.is_a?(@to.matching_class)
  raise ArgumentError, "to port class #{@to.class} is not a #{@from.matching_class}" unless @to.is_a?(@from.matching_class)
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



15
16
17
# File 'lib/spnet/core/link.rb', line 15

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



15
16
17
# File 'lib/spnet/core/link.rb', line 15

def to
  @to
end

Instance Method Details

#activateObject

Make the link active by setting from.link and to.link to self.



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

def activate
  @from.set_link self
  @to.set_link self
end

#active?Boolean

Return true if the link is active (from.link and to.link are set to to self).

Returns:

  • (Boolean)


41
42
43
# File 'lib/spnet/core/link.rb', line 41

def active?
  (@from.link == self) && (@to.link == self)
end

#deactivateObject

Make the link inactive by setting from.link and to.link to nil.



35
36
37
38
# File 'lib/spnet/core/link.rb', line 35

def deactivate
  @from.clear_link
  @to.clear_link
end

#save_state(blocks) ⇒ Object

Produce a LinkState object from the current Link object.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/spnet/core/link.rb', line 46

def save_state blocks
  from, to = nil, nil
  
  blocks.each do |block_name, block|
    block.out_ports.each do |port_name, port|
      if port == @from
        from = PortLocater.new(:block_name => block_name, :port_name => port_name)
        break
      end
    end
    
    block.in_ports.each do |port_name, port|
      if port == @to
        to = PortLocater.new(:block_name => block_name, :port_name => port_name)
        break
      end
    end
  end
  
  raise "could not find from port" if from.nil?
  raise "could not find to port" if to.nil?
  return LinkState.new(:from => from, :to => to)
end