Class: CircuitBreaker::Place

Inherits:
Object
  • Object
show all
Defined in:
lib/circuit_breaker.rb

Overview

Represents a place in the Petri net workflow Places can hold tokens and connect to transitions via arcs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Place

Initialize a new place with a given name

Parameters:

  • name (Symbol)

    Name of the place



33
34
35
36
37
38
39
# File 'lib/circuit_breaker.rb', line 33

def initialize(name)
  @name = name
  @tokens = []
  @input_arcs = []
  @output_arcs = []
  @mutex = Mutex.new
end

Instance Attribute Details

#input_arcsObject (readonly)

Returns the value of attribute input_arcs.



25
26
27
# File 'lib/circuit_breaker.rb', line 25

def input_arcs
  @input_arcs
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/circuit_breaker.rb', line 17

def name
  @name
end

#output_arcsObject (readonly)

Returns the value of attribute output_arcs.



29
30
31
# File 'lib/circuit_breaker.rb', line 29

def output_arcs
  @output_arcs
end

#tokensObject (readonly)

Returns the value of attribute tokens.



21
22
23
# File 'lib/circuit_breaker.rb', line 21

def tokens
  @tokens
end

Instance Method Details

#add_input_arc(source, weight = 1) ⇒ Arc

Add an input arc from a source to this place

Parameters:

  • source (Transition)

    Source transition

  • weight (Integer) (defaults to: 1)

    Weight of the arc (default: 1)

Returns:

  • (Arc)

    The created arc



63
64
65
66
67
# File 'lib/circuit_breaker.rb', line 63

def add_input_arc(source, weight = 1)
  arc = Arc.new(source, self, weight)
  @input_arcs << arc
  arc
end

#add_output_arc(target, weight = 1) ⇒ Arc

Add an output arc from this place to a target

Parameters:

  • target (Transition)

    Target transition

  • weight (Integer) (defaults to: 1)

    Weight of the arc (default: 1)

Returns:

  • (Arc)

    The created arc



73
74
75
76
77
# File 'lib/circuit_breaker.rb', line 73

def add_output_arc(target, weight = 1)
  arc = Arc.new(self, target, weight)
  @output_arcs << arc
  arc
end

#add_token(token) ⇒ Object

Add a token to this place

Parameters:

  • token (Token)

    Token to add



43
44
45
# File 'lib/circuit_breaker.rb', line 43

def add_token(token)
  @mutex.synchronize { @tokens << token }
end

#remove_tokenToken?

Remove and return the most recently added token

Returns:

  • (Token, nil)

    The removed token or nil if no tokens



49
50
51
# File 'lib/circuit_breaker.rb', line 49

def remove_token
  @mutex.synchronize { @tokens.pop }
end

#token_countInteger

Get the current number of tokens in this place

Returns:

  • (Integer)

    Number of tokens



55
56
57
# File 'lib/circuit_breaker.rb', line 55

def token_count
  @tokens.size
end