Class: YPetri::Simulation::MarkingVector

Inherits:
Matrix
  • Object
show all
Extended by:
Dependency
Defined in:
lib/y_petri/simulation/marking_vector/access.rb,
lib/y_petri/simulation/marking_vector.rb

Overview

A mixin.

Defined Under Namespace

Modules: Access

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dependency

A_transitions, S_transitions, TS_transitions, T_transitions, Ts_transitions, clamped_places, delegate_to_simulation!, element, elements, free_places, place, places, s_transitions, tS_transitions, t_transitions, transition, transitions, ts_transitions

Methods inherited from Matrix

column_vector_access_code, column_vector_assignment_code, column_vector_increment_by_array_code, column_vector_increment_code, #increment_at_indices_closure

Class Attribute Details

.annotationObject (readonly)

Returns the value of attribute annotation.



12
13
14
# File 'lib/y_petri/simulation/marking_vector.rb', line 12

def annotation
  @annotation
end

Class Method Details

.[](arg) ⇒ Object

Constructs a marking vector from a hash places >> values, or from an array, in which case, it is assumed that the marking vector corresponds to all the places in the simulation.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/y_petri/simulation/marking_vector.rb', line 18

def [] arg
  case arg
  when Hash then annotated_with( arg.keys )[ arg.values ]
  when Array then
    if annotation then
      msg = "The size of the argument (#{arg.size}) does not " +
        "correspond to the annotation size (#{annotation.size})!"
      fail ArgumentError, msg unless arg.size == annotation.size
      column_vector arg
    else
      annotated_with( places )[ args ]
    end
  else
    self[ args.each.to_a ]
  end
end

.annotated_with(place_ids) ⇒ Object

Returns a subclass of self annotated with the supplied places.



37
38
39
40
41
42
43
44
# File 'lib/y_petri/simulation/marking_vector.rb', line 37

def annotated_with place_ids
  annot = if annotation then
            annotation.subset place_ids
          else
            places( place_ids )
          end
  Class.new self do @annotation = annot end
end

.starting(place_ids = nil) ⇒ Object

Without arguments, constructs the starting marking vector for all places, using either initial values, or clamp values. Optionally, places can be specified, for which the starting vector is returned.



50
51
52
53
54
55
56
57
58
# File 'lib/y_petri/simulation/marking_vector.rb', line 50

def starting place_ids=nil
  st = -> p { p.free? ? p.initial_marking : p.clamp } # starting value
  if place_ids.nil? then
    return starting places if annotation.nil?
    self[ annotation.map &st ]
  else
    annotated_with( place_ids ).starting
  end
end

.zero(place_ids = nil) ⇒ Object

Without arguments, constructs a zero marking vector for all places. Optionally, places can be specified, for which the zero vector is returned.



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

def zero( place_ids=nil )
  starting( place_ids ) * 0
end

Instance Method Details

#annotationObject

Annotation.



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

def annotation
  self.class.annotation
end

#fetch(id) ⇒ Object

Access of the vector elements.



109
110
111
# File 'lib/y_petri/simulation/marking_vector.rb', line 109

def fetch id
  self[ index( id ), 0  ]
end

#increment_closureObject

Builds the assignment closure.



162
163
164
165
# File 'lib/y_petri/simulation/marking_vector.rb', line 162

def increment_closure
  indices_of_free_places = annotation.free.map { |p| annotation.index p }
  increment_at_indices_closure( indices: indices_of_free_places )
end

#index(id) ⇒ Object

Index of a place.



121
122
123
124
125
126
127
128
# File 'lib/y_petri/simulation/marking_vector.rb', line 121

def index id
  if id.is_a? Numeric then
    fail RangeError, "Numeric index must be within 0..#{size}" unless
      ( 0..size ) === id
  else
    annotation.index place( id )
  end
end

#reset!(arg = self.class.starting) ⇒ Object

Whole vector is reset to a given collection of values. If no argument is given, starting vector is used.



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/y_petri/simulation/marking_vector.rb', line 94

def reset! arg=self.class.starting
  case arg
  when Hash then
    mp = simulation.PlaceMapping().load( arg )
    updated = mp.each_with_object self.class.starting do |(place, value), mv|
      mv.set place, value
    end
    reset! updated.column_to_a
  else # array arg assumed
    arg.each.to_a.zip( annotation ).map { |value, place| set place, value }
  end
end

#select(place_ids, &block) ⇒ Object

Creates a subset of this marking vector.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/y_petri/simulation/marking_vector.rb', line 73

def select place_ids, &block
  if block_given? then
    msg = "If block is given, arguments are not allowed!"
    fail ArgumentError, msg unless place_ids.empty?
    select annotation.select( &block )
  else
    pp = places( place_ids )
    annotated_subcl = self.class.annotated_with( pp )
    annotated_subcl[ pp.map { |p| fetch p } ]
  end
end

#set(id, value) ⇒ Object

Modifying the vector elements.



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

def set id, value
  self[ index( id ), 0 ] = value
end

#sizeObject

Marking vector size – depends on the annotation.



132
133
134
# File 'lib/y_petri/simulation/marking_vector.rb', line 132

def size
  annotation.size
end

#to_aObject

Converts the marking vector (which is a column vector) into an array.



138
139
140
# File 'lib/y_petri/simulation/marking_vector.rb', line 138

def to_a
  ( 0..size - 1 ).map { |i| self[ i, 0 ] }
end

#to_hObject

Converts the marking vector into a hash annotation_names >> values.



150
151
152
# File 'lib/y_petri/simulation/marking_vector.rb', line 150

def to_h
  annotation.names( true ) >> to_a
end

#to_hashObject

Converts the marking vector into a hash annotation >> values.



144
145
146
# File 'lib/y_petri/simulation/marking_vector.rb', line 144

def to_hash
  annotation >> to_a
end

#to_hash_with_source_placesObject

Converts the marking vector into a hash source places >> values.



156
157
158
# File 'lib/y_petri/simulation/marking_vector.rb', line 156

def to_hash_with_source_places
  annotation.sources >> to_a
end