Class: YPetri::Net::State::Features::Record

Inherits:
Array
  • Object
show all
Defined in:
lib/y_petri/net/state/features/record.rb

Overview

A collection of values for a given set of state features.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load(values) ⇒ Object

Constructs a new Record object from a given collection of values.



13
14
15
# File 'lib/y_petri/net/state/features/record.rb', line 13

def load values
  new( values.dup )
end

Instance Method Details

#assignment(*ids, transition: L! ) ⇒ Object

Returns the values for a set of assignment features selected from this record’s feature set. Expects an arbitrary number of arguments, optinally qualified by :transition named argument. Without arguments, returns values for all the assignment features.



203
204
205
206
207
208
209
210
211
# File 'lib/y_petri/net/state/features/record.rb', line 203

def assignment *ids, transition: L!
  if transition.local_object? then
    return Assignment( ids ) unless ids.empty?
    Assignment features.assignment
  else
    return Assignment( ids, transition: transition ) unless ids.empty?
    Assignment features.assignment, transition: transition
  end
end

#Assignment(array, transition: L! ) ⇒ Object

Returns the values for a set of assignment features selected from this record’s feature set. Expects a single array argument, optionally qualified by :transition named argument.



190
191
192
193
194
195
196
# File 'lib/y_petri/net/state/features/record.rb', line 190

def Assignment array, transition: L!
  return array.map { |id| fetch net.State.Feature.Assignment( id ) } if
    transition.local_object?
  array.map { |id|
    fetch net.State.Feature.Assignment( id, transition: transition )
  }
end

#Delta(array, transitions: nil) ⇒ Object

Returns the values for a set of delta features selected from this record’s feature set. Expects a single array argument, optionally qualified by by :transitions named argument, defaulting to all the transitions in the net.



166
167
168
169
170
# File 'lib/y_petri/net/state/features/record.rb', line 166

def Delta array, transitions: nil
  array.map { |id|
    fetch( net.State.Feature.Delta id, transitions: net.tt( transitions ) )
  }
end

#delta(*delta_features, transitions: nil) ⇒ Object

Returns the values for a set of delta features selected from this record’s feature set. Expects an arbitrary number of arguments, optionally qualified by :transitions named argument, defaulting to all the transitions in the net. Without arguments, returns values for all the delta features.



177
178
179
180
181
182
183
184
# File 'lib/y_petri/net/state/features/record.rb', line 177

def delta *delta_features, transitions: nil
  return Delta( delta_features, transitions: transitions ) unless
    delta_features.empty?
  return Delta( features.delta ) if transitions.nil?
  Delta( features.delta.select do |f|
           f.transitions == transitions.map { |t| net.transition t }
         end )
end

#dump(precision: nil) ⇒ Object

Outputs the record as a plain array.



26
27
28
29
# File 'lib/y_petri/net/state/features/record.rb', line 26

def dump precision: nil
  return features.map &method( :fetch ) if precision.nil?
  features.map { |f| fetch( f ).round( precision ) }
end

#euclidean_distance(other) ⇒ Object

Computes the Euclidean distance from another record.



215
216
217
218
219
220
221
222
# File 'lib/y_petri/net/state/features/record.rb', line 215

def euclidean_distance( other )
  fail TypeError unless features == other.features
  sum_of_squares = zip( other )
    .map { |a, b| a - b }
    .map { |d| d * d }
    .reduce( 0.0, :+ )
  sum_of_squares ** 0.5
end

#fetch(feature) ⇒ Object

Returns an identified feature, or fails.



40
41
42
43
44
45
46
47
# File 'lib/y_petri/net/state/features/record.rb', line 40

def fetch feature
  super begin
          Integer( feature )
        rescue TypeError
          feat = net.State.Feature( feature )
          features.index( feat )
        end
end

#firing(*firing_features) ⇒ Object

Expects an arbitrary number of firing feature identifiers and returns the corresponding values from the reciever record. If no arguments are given, values for all the firing features are returned.



132
133
134
135
# File 'lib/y_petri/net/state/features/record.rb', line 132

def firing *firing_features
  return Firing( features.firing ) if firing_features.empty?
  Firing( firing_features )
end

#Firing(array) ⇒ Object

Expects a single aarray of firing feature identifiers, and selects the corresponding values from the reciever record.



124
125
126
# File 'lib/y_petri/net/state/features/record.rb', line 124

def Firing array
  array.map { |id| fetch( net.State.Feature.Firing id ) }
end

#Flux(array) ⇒ Object

Expects a single aarray of flux feature identifiers, and selects the corresponding values from the reciever record.



108
109
110
# File 'lib/y_petri/net/state/features/record.rb', line 108

def Flux array
  array.map { |id| fetch( net.State.Feature.Flux id ) }
end

#flux(*flux_features) ⇒ Object

Expects an arbitrary number of flux feature identifiers and returns the corresponding values from the reciever record. If no arguments are given, values for all the flux features are returned.



116
117
118
119
# File 'lib/y_petri/net/state/features/record.rb', line 116

def flux *flux_features
  return Flux( features.flux ) if flux_features.empty?
  Flux( flux_features )
end

#Gradient(array, transitions: nil) ⇒ Object

Expects a single array of gradient feature identifiers, optionally qualified by the :transitions named argument, defaulting to all T transitions in the net.



141
142
143
144
145
# File 'lib/y_petri/net/state/features/record.rb', line 141

def Gradient array, transitions: nil
 array.map { |id|
    fetch( net.State.Feature.Gradient id, transitions: transitions )
  }
end

#gradient(*gradient_features, transitions: nil) ⇒ Object

Expects an arbitrary number of gradient feature identifiers, optionally qualified by the :transitions named argument, defaulting to all T transitions in the net. If no arguments are given, values for all the gradient features are defined.



152
153
154
155
156
157
158
159
# File 'lib/y_petri/net/state/features/record.rb', line 152

def gradient *gradient_features, transitions: nil
  return Gradient( gradient_features, transitions: transitions ) unless
    gradient_features.empty?
  return Gradient( features.gradient ) if transitions.nil?
  Gradient( features.gradient.select do |f|
              f.transitions == transitions.map { |t| net.transition t }
            end )
end

#Marking(array) ⇒ Object

Expects a single array of marking feture identifiers, and selects the corresponding values from the reciever record.



90
91
92
93
94
# File 'lib/y_petri/net/state/features/record.rb', line 90

def Marking array
  array.map { |id| fetch( net.State.Feature.Marking id ) }
  # possible TODO - maybe a new feature instance and reloading the record
  # through it woud be in place. Not doing now.
end

#marking(*marking_features) ⇒ Object

Expects an arbitrary number of marking feature identifiers and returns the corresponding values from the reciever record. If no arguments are given, values for all the marking features are returned.



100
101
102
103
# File 'lib/y_petri/net/state/features/record.rb', line 100

def marking *marking_features
  return Marking( features.marking ) if marking_features.empty?
  Marking( marking_features )
end

Pretty prints the record with feature names.



33
34
35
36
# File 'lib/y_petri/net/state/features/record.rb', line 33

def print gap: 4, precision: 4
  hsh = features.labels >> dump( precision: precision )
  hsh.pretty_print_numeric_values gap: gap, precision: precision
end

#reconstruct(marking_clamps: {}, **settings) ⇒ Object

Given a set of marking clamps complementary to the marking features of this record, reconstructs a Simulation instance with the corresponding state. If the net is timed, or if the construction of the simulation from the net has need for any special settings, these must be supplied to this method. (Timed nets eg. require :time named argument for successful construction.)



78
79
80
81
82
83
84
85
# File 'lib/y_petri/net/state/features/record.rb', line 78

def reconstruct marking_clamps: {}, **settings
  clamped_places = net.Places( marking_clamps.keys )
  ff = features.marking - net.State.Features.Marking( clamped_places )
  m_hsh =
    ff.map { |f| f.place } >>
    marking
  net.simulation marking_clamps: marking_clamps, marking: m_hsh, **settings
end

#state(marking_clamps: {}) ⇒ Object

Returns the state instance implied by the receiver record, and a set of complementary marking clamps supplied as the argument.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/y_petri/net/state/features/record.rb', line 52

def state marking_clamps: {}
  cc = marking_clamps.with_keys { |k| net.place k }.with_values! do |v|
    case v
    when YPetri::Place then v.marking
    when ~:call then v.call
    else v end
  end
  own = features.marking.map &:place
  from_clamps = net.Places cc.keys
  fail TypeError, "Marking clamps supplied in the argument together with " +
    "this record's markings must complete the full state of the net!" unless
    net.places - own - from_clamps == []
  array = net.places.map do |place|
    begin; cc.fetch place; rescue IndexError
      fetch place
    end
  end
  State().new array
end