Module: YPetri::Simulation::MarkingVector::Access

Defined in:
lib/y_petri/simulation/marking_vector/access.rb

Instance Method Summary collapse

Instance Method Details

#increment_marking(Δ_free) ⇒ Object

Expects a Δ marking vector for free places and performs the specified change on the marking vector of the simulation.



170
171
172
173
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 170

def increment_marking Δ_free
  @m_vector += f2a * Δ_free
  return nil
end

#m(*args) ⇒ Object



36
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 36

def m *args; m_vector( *args ).to_a end

#M(*args) ⇒ Object

Array-returning equivalents of #M_vector and m_vector.



35
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 35

def M *args; M_vector( *args ).to_a end

#m_vector(*places) ⇒ Object

Convenience method that accepts any number of places or place ids as arguments, and returns their marking as a column vector. If no arguments are supplied, the method returns the simulation’s state vector.



26
27
28
29
30
31
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 26

def m_vector *places
  begin
  return state if places.empty?
  m_vector.select( places )
  end
end

#M_vector(array) ⇒ Object Also known as: m_Vector

Marking of the selected places as a column vector. Expects a single array argument.



10
11
12
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 10

def M_vector array
  m_vector.select( array )
end

#marking_vector(*places) ⇒ Object

Marking vector of free places. Expects an arbitrary number of free places or place ids and returns the marking vector for them.



121
122
123
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 121

def marking_vector *places
  m_vector *free_places( *places )
end

#Marking_vector(array) ⇒ Object

Marking vector of free places. Expects an array of places or place ids, for which the marking vectro is returned.



114
115
116
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 114

def Marking_vector array
  M_vector Free_places( array )
end

#p_m(*places) ⇒ Object Also known as: pn_m

Marking of the indicated places as a hash of { place name => marking } pairs. Expects and arbitrary number of places or place ids and arguments. If no arguments are given, marking of all the places is returned.



64
65
66
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 64

def p_m *places
  places( *places ).names( true ) >> m( *places )
end

#P_m(places) ⇒ Object Also known as: p_M, Pn_m

Marking of the indicated places as a hash of { place name => marking } pairs. Expects a single array of places or place ids as an argument.



54
55
56
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 54

def P_m places
  Places( places ).names( true ) >> M( places )
end

#Pm(places, **named_args) ⇒ Object

Pretty prints marking of the indicated places. Expects an array of places or place ids as an argument. In addition, accepts 2 optional named arguments, :gap and :precision (alias :p), that control the layout of the printed table, like in #pretty_print_numeric_values method.



74
75
76
77
78
79
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 74

def Pm places, **named_args
  gap = named_args[:gap] || 0
  named_args.may_have :precision, syn!: :pn
  precision = named_args.delete( :precision ) || 3
  P_m( places ).pretty_print_numeric_values gap: gap, precision: precision
end

#pm(*places, **named_args) ⇒ Object

Pretty prints marking of the indicated places. Expects an arbitrary number of places or place ids, and 2 optional named arguments, :gap and :precision (alias :p), that control the layout of the printed table, like in #pretty_print_numeric_values method.



86
87
88
89
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 86

def pm *places, **named_args
  return Pm places() if places.empty?
  Pm( places, **named_args )
end

#stateObject

Acts as a getter of the simulation’s state vector, instance variable @m_vector.



18
19
20
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 18

def state
  @m_vector or fail TypeError, "State not constructed yet!"
end

#update_m(new_m) ⇒ Object Also known as: set_m

Modifies the marking vector. Takes one argument. If the argument is a hash of pairs { place => new value }, only the specified places’ markings are updated. If the argument is an array, it must match the number of places in the simulation, and all marking values are updated.



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 96

def update_m new_m
  case new_m
  when Hash then # assume { place => marking } hash
    new_m.each_pair { |id, val| m_vector.set( id, val ) }
  when Array then
    msg = "T be a collection with size == number of net's places!"
    fail TypeError, msg unless new_m.size == places.size
    update_m( places >> new_m )
  else # convert it with #each
    update_m( new_m.each.to_a )
  end
  return nil
end

#update_marking(new_m) ⇒ Object Also known as: set_marking

Modifies the marking vector. Like #update_m, but the places must be free places, and if the argument is an array, it must match the number of free places in the simulation’s net.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/y_petri/simulation/marking_vector/access.rb', line 151

def update_marking new_m
  case new_m
  when Hash then # assume { place => marking } hash
    ( free_places( new_m.keys ) >> new_m.values )
      .each_pair { |id, val| m_vector.set( id, val ) }
  when Array then
    msg = "T be a collection with size == number of net's free places!"
    fail TypeError, msg unless new_m.size == free_places.size
    update_m( free_places >> new_m )
  else # convert it with #each
    update_marking( new_m.each.to_a )
  end
  return nil
end