Module: YPetri::Core

Defined in:
lib/y_petri/core.rb

Overview

This module represents a simulation core (execution machine), which can be either timed (class Core::Timed) or timeless (class Core::Timeless).

Defined Under Namespace

Modules: Guarded Classes: Timed, Timeless

Constant Summary collapse

DEFAULT_METHOD =
:basic

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clamped_ppObject (readonly)

Returns the value of attribute clamped_pp.



26
27
28
# File 'lib/y_petri/core.rb', line 26

def clamped_pp
  @clamped_pp
end

#free_ppObject (readonly)

Returns the value of attribute free_pp.



26
27
28
# File 'lib/y_petri/core.rb', line 26

def free_pp
  @free_pp
end

#guardedObject (readonly) Also known as: guarded?

Returns the value of attribute guarded.



23
24
25
# File 'lib/y_petri/core.rb', line 23

def guarded
  @guarded
end

#marking_of_clamped_placesObject (readonly)

Returns the value of attribute marking_of_clamped_places.



26
27
28
# File 'lib/y_petri/core.rb', line 26

def marking_of_clamped_places
  @marking_of_clamped_places
end

#marking_of_free_placesObject (readonly)

Returns the value of attribute marking_of_free_places.



26
27
28
# File 'lib/y_petri/core.rb', line 26

def marking_of_free_places
  @marking_of_free_places
end

#ppObject (readonly)

Returns the value of attribute pp.



26
27
28
# File 'lib/y_petri/core.rb', line 26

def pp
  @pp
end

#simulationObject (readonly)

Instead of getting the parametrized subclass of Timed/Timeless core belonging to a simulation, I am passing a simulation instance to the constructor now in order to gradually begin decoupling in my mind core simulation. If I ever make the core independent from the simulation, it will have its own representation of the net and its own capability to form wiring and apply the required numerical procedures.



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

def simulation
  @simulation
end

#simulation_methodObject (readonly)

“reader” is “selector” in Landin’s language



22
23
24
# File 'lib/y_petri/core.rb', line 22

def simulation_method
  @simulation_method
end

Instance Method Details

#delta_timelessObject Also known as: delta_t

Delta contribution to free places by timeless transitions.



80
81
82
# File 'lib/y_petri/core.rb', line 80

def delta_timeless
  delta_ts + delta_tS
end

#delta_tsObject

Delta contribution by ts transitions.



93
94
95
96
# File 'lib/y_petri/core.rb', line 93

def delta_ts
  simulation.ts_delta_closure.call
  # @delta_closure_for_ts_transitions.call
end

#delta_tSObject

Delta contribution by tS transitions.



87
88
89
# File 'lib/y_petri/core.rb', line 87

def delta_tS
  simulation.tS_stoichiometry_matrix * firing_vector_tS
end

#fire_all_assignment_transitions!Object Also known as: assignment_transitions_all_fire!

Fires all the assignment transitions.



129
130
131
132
# File 'lib/y_petri/core.rb', line 129

def fire_all_assignment_transitions!
  simulation.A_direct_assignment_closure.call
  # @assignment_closure_for_A_transitions.call
end

#firing_vector_tSObject

Firing vector of tS transitions.



100
101
102
103
# File 'lib/y_petri/core.rb', line 100

def firing_vector_tS
  simulation.tS_firing_closure.call
  # @firing_closure_for_tS_transitions.call
end

#increment_free_vector(by: fail( "No delta given!" )) ⇒ Object

For now, alias method for #increment_marking_vector. TODO: I already said to myself that I want the core not to rely on the simulation’s increment_marking_vector_closure.



119
120
121
122
123
124
125
# File 'lib/y_petri/core.rb', line 119

def increment_free_vector( by: fail( "No delta given!" ) )
  print '.'
  simulation.increment_marking_vector_closure.( by )
  # Also, here it is not clear that #increment_marking_vector_closure
  # returns a closure that expects only Δ for free places. Should have
  # better mnemonic name.
end

#increment_marking_vector(delta) ⇒ Object

Increments the marking vector by a given delta.



107
108
109
110
111
112
113
# File 'lib/y_petri/core.rb', line 107

def increment_marking_vector( delta )
  print '.'
  # TODO: From now on, this won't touch the simulation's property
  # at all. It will be left to the simulation to ask for the results,
  # or to rig the core to message back when done.
  simulation.increment_marking_vector_closure.( delta )
end

#initialize(simulation: nil, method: nil, guarded: false, **named_args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/y_petri/core.rb', line 32

def initialize simulation: nil, method: nil, guarded: false, **named_args
  @simulation = simulation or fail ArgumentError, "Core requires simulation!"
  @simulation_method = method || DEFAULT_METHOD

  if guarded then            # TODO: Guarded is unfinished business.
    fail NotImplementedMethod, "Guarded core is not implemented yet!"
    require_relative 'core/guarded' # TODO: Should be replaced with autoload.
  else @guarded = false end

  @free_pp = simulation.free_pp
  @clamped_pp = simulation.clamped_pp
  @pp = simulation.pp
  # TODO: Try to make the lines below simpler. In particular, in the future, core should
  # not depend on simulation at this level.
  @marking_of_free_places = simulation.MarkingVector.starting( @free_pp )
  @marking_of_clamped_places = simulation.MarkingVector.starting( @clamped_pp )

  # TODO: I don't remember how to load this in a simple way.
  simulation.state.to_hash.each do |place, value|
    if @marking_of_free_places.annotation.include? place then
      @marking_of_free_places.set( place, value )
    elsif @marking_of_clamped_places.annotation.include? place then
      @marking_of_clamped_places.set( place, value )
    else
      fail "Problem loading marking vector to timed core."
    end
  end
end

#stateObject

Selector of the core’s own state vector.



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

def state
  # TODO: Make it more efficient. Later, when core is detached from
  # simulation, use own assets instead of simulation.MarkingVector
  simulation.MarkingVector.zero.tap do |mv|
    @marking_of_free_places.annotation.each do |place|
      mv.set place, @marking_of_free_places.fetch( place )
    end
    @marking_of_clamped_places.annotation.each do |place|
      mv.set place, @marking_of_clamped_places.fetch( place )
    end
  end
end