Module: YPetri::Transition::Type_T

Defined in:
lib/y_petri/transition/T.rb

Overview

Mixin for timed Petri net transitions.

Instance Method Summary collapse

Instance Method Details

#action(Δt) ⇒ Object

Transition’s action (before validation). Requires Δt as an argument.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/y_petri/transition/T.rb', line 14

def action Δt
  # TODO: Unhelpful error occurs if the user constructs a transition
  # like this:
  #
  #   T = Transition s: { A: -1, B: 2 },
  #                  rate: -> { 0.1 }    # constant rate
  #
  # The user meant to construct a TS transition with constant rate and
  # stoichiometry { A: -1, B: 2 }, but the lambda given under :rate
  # parameter is nullary, while the stoichiometry is taken to imply that
  # the domain consists of place 1.
  #
  #   T.action 5.0
  #
  # then causes error because it tries to supply the marking of A to
  # the user-supplied rate closure, which is nullary.
  #
  # There are 2 problems with this:
  #
  # Firstly, if we choose to see this as the user's problem, the user
  # supplied the Transition constructor with invalid input, but received
  # no warning (problem 1). The user learned about the error by typing
  # T.action 5.0, and the error message is quite unhelpful (problem 2) -
  # it does not inform the user that the rate closure has wrong arity.
  #
  # We, we might deside to see this as a missing feature and make sure
  # that in these cases, the constructor infers that the codomain is
  # empty from the fact that the supplied rate closure is nullary. This
  # requires additional thinking, because it is not possible to infer
  # domain from rate lamdas with non-matching arity in general.
  # 
  if stoichiometric? then
    rate = rate_closure.( *domain_marking )
    stoichiometry.map { |coeff| rate * coeff * Δt }
  else
    Array( rate_closure.( *domain_marking ) ).map { |e| e * Δt }
  end
end

#enabled?(Δt) ⇒ Boolean

YPetri transitions are enabled if and only if the intended action would lead to a legal codomain marking. For timed transitions, #enabled? method takes Δt as an argument.

Returns:

  • (Boolean)


78
79
80
81
82
83
# File 'lib/y_petri/transition/T.rb', line 78

def enabled? Δt
  codomain.zip( action Δt ).all? do |place, change|
    begin; place.guard.( place.marking + change )
    rescue YPetri::GuardError; false end
  end
end

#f(simulation = world.simulation) ⇒ Object

Transition’s flux under current simulation.



95
96
97
# File 'lib/y_petri/transition/T.rb', line 95

def f simulation=world.simulation
  simulation.net.State.Feature.Flux( self ) % simulation
end

#fir(simulation = world.simulation, **nn) ⇒ Object

Transition’s firing under current simulation.



87
88
89
90
91
# File 'lib/y_petri/transition/T.rb', line 87

def fir simulation=world.simulation, **nn
  nn.must_have :delta_time, syn!: :Δt
  Δt = nn.delete( :delta_time ) || simulation.step
  simulation.net.State.Feature.Firing( self ) % [ simulation, Δt: Δt ]
end

#fire(Δt) ⇒ Object

Fires the transition, honoring cocking. Returns true if the transition fired, false if it wasn’t cocked.



56
57
58
# File 'lib/y_petri/transition/T.rb', line 56

def fire Δt
  cocked?.tap { |x| ( uncock; fire! Δt ) if x }
end

#fire!(Δt) ⇒ Object

Fires the transition regardless of cocking. For timed transitions, takes Δt as an argument.



63
64
65
66
67
68
69
70
71
72
# File 'lib/y_petri/transition/T.rb', line 63

def fire! Δt
  action = Array( action Δt )
  fail TypeError, "Wrong output arity of the action " +
    "closure of #{self}!" if action.size != codomain.size
  codomain.each_with_index do |place, index|
    # Adding action place no. index to place"
    place.add action.fetch( index )
  end
  return nil
end

#functionObject

For timed transitions, “function” refers to their rate closure.



8
9
10
# File 'lib/y_petri/transition/T.rb', line 8

def function
  rate_closure
end

#pa(simulation = world.simulation, **nn) ⇒ Object

Prints the transition’s action under current simulation.



101
102
103
104
105
106
# File 'lib/y_petri/transition/T.rb', line 101

def pa simulation=world.simulation, **nn
  nn.must_have :delta_time, syn!: :Δt
  Δt = nn.delete( :delta_time ) || simulation.step
  ff = simulation.net.State.Features.Delta( codomain, transitions: self )
  ( ff >> ff % simulation ).pretty_print_numeric_values( **nn )
end