y_petri 2.2.4 → 2.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +675 -0
  3. data/README.md +6 -3
  4. data/Rakefile +1 -1
  5. data/lib/y_petri/agent/{petri_net_related.rb → petri_net_aspect.rb} +34 -10
  6. data/lib/y_petri/agent/{simulation_related.rb → simulation_aspect.rb} +49 -34
  7. data/lib/y_petri/agent.rb +5 -5
  8. data/lib/y_petri/core/guarded.rb +24 -0
  9. data/lib/y_petri/core/timed/euler.rb +4 -8
  10. data/lib/y_petri/core/timed/gillespie.rb +11 -17
  11. data/lib/y_petri/core/timed/methods.rb +23 -0
  12. data/lib/y_petri/core/timed/pseudo_euler.rb +10 -13
  13. data/lib/y_petri/core/timed/quasi_euler.rb +9 -8
  14. data/lib/y_petri/core/timed/runge_kutta.rb +10 -18
  15. data/lib/y_petri/core/timed.rb +6 -14
  16. data/lib/y_petri/core/timeless/methods.rb +15 -0
  17. data/lib/y_petri/core/timeless/pseudo_euler.rb +4 -8
  18. data/lib/y_petri/core/timeless.rb +9 -4
  19. data/lib/y_petri/core.rb +44 -42
  20. data/lib/y_petri/net/data_set.rb +246 -142
  21. data/lib/y_petri/net/node_access.rb +282 -0
  22. data/lib/y_petri/net/own_state.rb +14 -4
  23. data/lib/y_petri/net/state/feature/assignment.rb +123 -0
  24. data/lib/y_petri/net/state/feature/delta.rb +55 -35
  25. data/lib/y_petri/net/state/feature/firing.rb +68 -25
  26. data/lib/y_petri/net/state/feature/flux.rb +9 -2
  27. data/lib/y_petri/net/state/feature/gradient.rb +36 -19
  28. data/lib/y_petri/net/state/feature/marking.rb +10 -5
  29. data/lib/y_petri/net/state/feature.rb +105 -11
  30. data/lib/y_petri/net/state/features/record.rb +144 -99
  31. data/lib/y_petri/net/state/features.rb +327 -200
  32. data/lib/y_petri/net/state.rb +48 -82
  33. data/lib/y_petri/net/visualization.rb +1 -1
  34. data/lib/y_petri/net.rb +62 -47
  35. data/lib/y_petri/place/arcs.rb +44 -0
  36. data/lib/y_petri/place/features.rb +115 -0
  37. data/lib/y_petri/place.rb +62 -29
  38. data/lib/y_petri/simulation/dependency.rb +31 -67
  39. data/lib/y_petri/simulation/feature_set.rb +1 -1
  40. data/lib/y_petri/simulation/initial_marking/access.rb +42 -26
  41. data/lib/y_petri/simulation/marking_clamps/access.rb +22 -17
  42. data/lib/y_petri/simulation/marking_clamps.rb +0 -2
  43. data/lib/y_petri/simulation/marking_vector/access.rb +102 -40
  44. data/lib/y_petri/simulation/marking_vector.rb +35 -37
  45. data/lib/y_petri/simulation/matrix.rb +1 -1
  46. data/lib/y_petri/simulation/node_representation.rb +25 -0
  47. data/lib/y_petri/simulation/nodes/access.rb +78 -0
  48. data/lib/y_petri/simulation/{elements.rb → nodes.rb} +14 -13
  49. data/lib/y_petri/simulation/place_mapping.rb +2 -2
  50. data/lib/y_petri/simulation/place_representation.rb +8 -7
  51. data/lib/y_petri/simulation/places/access.rb +89 -70
  52. data/lib/y_petri/simulation/places/free.rb +1 -1
  53. data/lib/y_petri/simulation/places/types.rb +20 -22
  54. data/lib/y_petri/simulation/places.rb +23 -18
  55. data/lib/y_petri/simulation/recorder.rb +23 -18
  56. data/lib/y_petri/simulation/timed/recorder.rb +19 -11
  57. data/lib/y_petri/simulation/timed.rb +93 -29
  58. data/lib/y_petri/simulation/timeless/recorder.rb +11 -6
  59. data/lib/y_petri/simulation/timeless.rb +13 -3
  60. data/lib/y_petri/simulation/transition_representation/A.rb +24 -4
  61. data/lib/y_petri/simulation/transition_representation/S.rb +11 -1
  62. data/lib/y_petri/simulation/transition_representation/T.rb +1 -1
  63. data/lib/y_petri/simulation/transition_representation/Ts.rb +1 -1
  64. data/lib/y_petri/simulation/transition_representation/a.rb +1 -1
  65. data/lib/y_petri/simulation/transition_representation/s.rb +12 -1
  66. data/lib/y_petri/simulation/transition_representation/t.rb +1 -1
  67. data/lib/y_petri/simulation/transition_representation/tS.rb +1 -1
  68. data/lib/y_petri/simulation/transition_representation/ts.rb +1 -1
  69. data/lib/y_petri/simulation/transition_representation/types.rb +1 -1
  70. data/lib/y_petri/simulation/transition_representation.rb +4 -11
  71. data/lib/y_petri/simulation/transitions/A.rb +17 -2
  72. data/lib/y_petri/simulation/transitions/S.rb +1 -1
  73. data/lib/y_petri/simulation/transitions/T.rb +1 -1
  74. data/lib/y_petri/simulation/transitions/Ts.rb +6 -5
  75. data/lib/y_petri/simulation/transitions/a.rb +1 -1
  76. data/lib/y_petri/simulation/transitions/access.rb +195 -168
  77. data/lib/y_petri/simulation/transitions/s.rb +1 -1
  78. data/lib/y_petri/simulation/transitions/t.rb +1 -1
  79. data/lib/y_petri/simulation/transitions/tS.rb +1 -1
  80. data/lib/y_petri/simulation/transitions/ts.rb +1 -1
  81. data/lib/y_petri/simulation/transitions/types.rb +1 -1
  82. data/lib/y_petri/simulation/transitions.rb +5 -7
  83. data/lib/y_petri/simulation.rb +84 -90
  84. data/lib/y_petri/transition/A.rb +8 -2
  85. data/lib/y_petri/transition/T.rb +25 -2
  86. data/lib/y_petri/transition/arcs.rb +19 -3
  87. data/lib/y_petri/transition/construction_convenience.rb +11 -10
  88. data/lib/y_petri/transition/t.rb +14 -1
  89. data/lib/y_petri/transition/types.rb +6 -1
  90. data/lib/y_petri/transition.rb +9 -12
  91. data/lib/y_petri/version.rb +1 -1
  92. data/lib/y_petri/world/dependency.rb +3 -3
  93. data/lib/y_petri/world/{petri_net_related.rb → petri_net_aspect.rb} +4 -4
  94. data/lib/y_petri/world/simulation_aspect.rb +352 -0
  95. data/lib/y_petri/world.rb +4 -4
  96. data/lib/y_petri.rb +1 -1
  97. data/test/agent_test.rb +2 -1
  98. data/test/examples/demonstrator.rb +4 -1
  99. data/test/examples/demonstrator_2.rb +5 -0
  100. data/test/examples/demonstrator_4.rb +6 -5
  101. data/test/examples/example_2.rb +2 -0
  102. data/test/examples/manual_examples.rb +4 -4
  103. data/test/net_test.rb +457 -54
  104. data/test/place_test.rb +11 -7
  105. data/test/simulation_test.rb +358 -331
  106. data/test/transition_test.rb +11 -10
  107. data/test/world_test.rb +2 -0
  108. data/test/y_petri_test.rb +2 -1
  109. data/y_petri.gemspec +24 -18
  110. metadata +71 -17
  111. data/LICENSE +0 -22
  112. data/lib/y_petri/net/element_access.rb +0 -239
  113. data/lib/y_petri/simulation/element_representation.rb +0 -20
  114. data/lib/y_petri/simulation/elements/access.rb +0 -57
  115. data/lib/y_petri/transition/type.rb +0 -103
  116. data/lib/y_petri/transition/type_information.rb +0 -103
  117. data/lib/y_petri/world/simulation_related.rb +0 -176
@@ -41,7 +41,7 @@ class YPetri::Net::State::Features::Record < Array
41
41
  super begin
42
42
  Integer( feature )
43
43
  rescue TypeError
44
- feat = State().feature( feature )
44
+ feat = net.State.Feature( feature )
45
45
  features.index( feat )
46
46
  end
47
47
  end
@@ -56,13 +56,17 @@ class YPetri::Net::State::Features::Record < Array
56
56
  when ~:call then v.call
57
57
  else v end
58
58
  end
59
- msg = "Marking clamps given in the argument taken together with this " +
60
- "record's markings must complete the full state of the net!"
61
- fail TypeError, msg unless
62
- features.marking.map( &:place ) + net.places( cc.keys ) == net.places
63
- State().new net.places.map do |place|
64
- begin; cc.fetch place; rescue IndexError; fetch marking( place ) end
59
+ own = features.marking.map &:place
60
+ from_clamps = net.Places cc.keys
61
+ fail TypeError, "Marking clamps supplied in the argument together with " +
62
+ "this record's markings must complete the full state of the net!" unless
63
+ net.places - own - from_clamps == []
64
+ array = net.places.map do |place|
65
+ begin; cc.fetch place; rescue IndexError
66
+ fetch place
67
+ end
65
68
  end
69
+ State().new array
66
70
  end
67
71
 
68
72
  # Given a set of marking clamps complementary to the marking features of this
@@ -71,108 +75,149 @@ class YPetri::Net::State::Features::Record < Array
71
75
  # has need for any special settings, these must be supplied to this method.
72
76
  # (Timed nets eg. require +:time+ named argument for successful construction.)
73
77
  #
74
- def reconstruct **settings
75
- marking_clamps = settings[:marking_clamps] || {}
76
- clamped_places = net.places( marking_clamps.keys )
77
- ff = features.marking - net.State.marking( clamped_places )
78
- m_hsh = ff.map { |feat| feat.place } >> marking
78
+ def reconstruct marking_clamps: {}, **settings
79
+ clamped_places = net.Places( marking_clamps.keys )
80
+ ff = features.marking - net.State.Features.Marking( clamped_places )
81
+ m_hsh =
82
+ ff.map { |f| f.place } >>
83
+ marking
79
84
  net.simulation marking_clamps: marking_clamps, marking: m_hsh, **settings
80
85
  end
81
86
 
82
- # Expects a marking feature identifier (place identifier or Marking instance),
83
- # and returns the value for that feature in this record. If an array of
84
- # marking feature identifiers is supplied, it is mapped to the array of
85
- # corresponding values. If no argument is given, values from this record for
86
- # all the present marking features are returned.
87
- #
88
- def marking arg=nil
89
- return marking( features.marking ) if arg.nil?
90
- case arg
91
- when Array then arg.map { |id| marking id }
92
- else fetch( features.marking arg ) end
93
- end
94
-
95
- # Expects a flux feature identifier (transition identifier or Flux instance),
96
- # and returns the value for that feature in this record. If an array of flux
97
- # feature identifiers is supplied, it is mapped to the array of corresponding
98
- # values. If no argument is given, values from this record for all the present
99
- # flux features are returned.
100
- #
101
- def flux id=nil
102
- return flux( features.flux ) if id.nil?
103
- case id
104
- when Array then id.map { |id| flux net.transition( id ) }
105
- else fetch( features.flux id ) end
106
- end
107
-
108
- # Expects a firing feature identifier (transition identifier or Firing
109
- # instance), and returns the value for that feature in this record. If an
110
- # array of firing feature identifiers is supplied, it is mapped to the array
111
- # of corresponding values. If no argument is given, values from this record
112
- # for all the present flux features are returned.
113
- #
114
- def firing id=nil
115
- return firing( features.firing ) if id.nil?
116
- case id
117
- when Array then id.map { |id| firing net.transition( id ) }
118
- else fetch( features.firing id ) end
119
- end
120
-
121
- # Expects a gradient feature identifier (place identifier, or Gradient
122
- # instance), qualified by an array of transitions (named argument
123
- # +:transitions+, defaults to all timed transitions in the net), and returns
124
- # the value for that feature in this record. If an array of gradient feature
125
- # identifiers is supplied, it is mapped to the array of corresponding values.
126
- # If no gradient feature identifier is given, values from this record for all
127
- # the present gradient features are returned.
128
- #
129
- def gradient id=nil, transitions: nil
130
- if id.nil? then
131
- return gradient( features.gradient ) if transitions.nil?
132
- gradient( features.gradient.select do |f|
133
- f.transitions == transitions.map { |t| net.transition t }
134
- end )
135
- else
136
- return gradient( id, transitions: net.T_tt ) if transitions.nil?
137
- case id
138
- when Array then
139
- pl.map { |id| gradient id, transitions: transitions }
140
- else
141
- fetch( features.gradient id, transitions: transitions )
142
- end
143
- end
87
+ # Expects a single array of marking feture identifiers, and selects the
88
+ # corresponding values from the reciever record.
89
+ #
90
+ def Marking array
91
+ array.map { |id| fetch( net.State.Feature.Marking id ) }
92
+ # possible TODO - maybe a new feature instance and reloading the record
93
+ # through it woud be in place. Not doing now.
144
94
  end
145
95
 
146
- # Expects a gradient feature identifier (place identifier, or Delta instance),
147
- # qualified by an array of transitions (named argument +:transitions+,
148
- # defaults to all timed transitions in the net), and returns the value for
149
- # that feature in this record. If an array of delta feature identifiers is
150
- # supplied, it is mapped to the array of corresponding values. If no delta
151
- # feature identifier is given, values from this record for all the present
152
- # delta features are returned.
153
- #
154
- def delta pl=nil, transitions: nil
155
- if id.nil? then
156
- return delta( features.delta ) if transitions.nil?
157
- delta( features.delta.select do |f|
158
- f.transitions == transitions.map { |t| net.transition t }
159
- end )
96
+ # Expects an arbitrary number of marking feature identifiers and returns
97
+ # the corresponding values from the reciever record. If no arguments are
98
+ # given, values for all the marking features are returned.
99
+ #
100
+ def marking *marking_features
101
+ return Marking( features.marking ) if marking_features.empty?
102
+ Marking( marking_features )
103
+ end
104
+
105
+ # Expects a single aarray of flux feature identifiers, and selects the
106
+ # corresponding values from the reciever record.
107
+ #
108
+ def Flux array
109
+ array.map { |id| fetch( net.State.Feature.Flux id ) }
110
+ end
111
+
112
+ # Expects an arbitrary number of flux feature identifiers and returns the
113
+ # corresponding values from the reciever record. If no arguments are given,
114
+ # values for all the flux features are returned.
115
+ #
116
+ def flux *flux_features
117
+ return Flux( features.flux ) if flux_features.empty?
118
+ Flux( flux_features )
119
+ end
120
+
121
+ # Expects a single aarray of firing feature identifiers, and selects the
122
+ # corresponding values from the reciever record.
123
+ #
124
+ def Firing array
125
+ array.map { |id| fetch( net.State.Feature.Firing id ) }
126
+ end
127
+
128
+ # Expects an arbitrary number of firing feature identifiers and returns the
129
+ # corresponding values from the reciever record. If no arguments are given,
130
+ # values for all the firing features are returned.
131
+ #
132
+ def firing *firing_features
133
+ return Firing( features.firing ) if firing_features.empty?
134
+ Firing( firing_features )
135
+ end
136
+
137
+ # Expects a single array of gradient feature identifiers, optionally qualified
138
+ # by the +:transitions+ named argument, defaulting to all T transitions in the
139
+ # net.
140
+ #
141
+ def Gradient array, transitions: nil
142
+ array.map { |id|
143
+ fetch( net.State.Feature.Gradient id, transitions: transitions )
144
+ }
145
+ end
146
+
147
+ # Expects an arbitrary number of gradient feature identifiers, optionally
148
+ # qualified by the +:transitions+ named argument, defaulting to all T
149
+ # transitions in the net. If no arguments are given, values for all the
150
+ # gradient features are defined.
151
+ #
152
+ def gradient *gradient_features, transitions: nil
153
+ return Gradient( gradient_features, transitions: transitions ) unless
154
+ gradient_features.empty?
155
+ return Gradient( features.gradient ) if transitions.nil?
156
+ Gradient( features.gradient.select do |f|
157
+ f.transitions == transitions.map { |t| net.transition t }
158
+ end )
159
+ end
160
+
161
+ # Returns the values for a set of delta features selected from this record's
162
+ # feature set. Expects a single array argument, optionally qualified by
163
+ # by +:transitions+ named argument, defaulting to all the transitions in the
164
+ # net.
165
+ #
166
+ def Delta array, transitions: nil
167
+ array.map { |id|
168
+ fetch( net.State.Feature.Delta id, transitions: net.tt( transitions ) )
169
+ }
170
+ end
171
+
172
+ # Returns the values for a set of delta features selected from this record's
173
+ # feature set. Expects an arbitrary number of arguments, optionally qualified
174
+ # by +:transitions+ named argument, defaulting to all the transitions in the
175
+ # net. Without arguments, returns values for all the delta features.
176
+ #
177
+ def delta *delta_features, transitions: nil
178
+ return Delta( delta_features, transitions: transitions ) unless
179
+ delta_features.empty?
180
+ return Delta( features.delta ) if transitions.nil?
181
+ Delta( features.delta.select do |f|
182
+ f.transitions == transitions.map { |t| net.transition t }
183
+ end )
184
+ end
185
+
186
+ # Returns the values for a set of assignment features selected from this
187
+ # record's feature set. Expects a single array argument, optionally qualified
188
+ # by +:transition+ named argument.
189
+ #
190
+ def Assignment array, transition: L!
191
+ return array.map { |id| fetch net.State.Feature.Assignment( id ) } if
192
+ transition.local_object?
193
+ array.map { |id|
194
+ fetch net.State.Feature.Assignment( id, transition: transition )
195
+ }
196
+ end
197
+
198
+ # Returns the values for a set of assignment features selected from this
199
+ # record's feature set. Expects an arbitrary number of arguments, optinally
200
+ # qualified by +:transition+ named argument. Without arguments, returns
201
+ # values for all the assignment features.
202
+ #
203
+ def assignment *ids, transition: L!
204
+ if transition.local_object? then
205
+ return Assignment( ids ) unless ids.empty?
206
+ Assignment features.assignment
160
207
  else
161
- return delta( id, transitions: net.tt ) if transitions.nil?
162
- case id
163
- when Array then
164
- id.map { |id| delta id, transitions: transitions }
165
- else
166
- fetch( features.delta id, transitions: net.tt( transitions ) )
167
- end
208
+ return Assignment( ids, transition: transition ) unless ids.empty?
209
+ Assignment features.assignment, transition: transition
168
210
  end
169
211
  end
170
212
 
171
213
  # Computes the Euclidean distance from another record.
172
214
  #
173
- def euclidean_distance other
174
- sum_of_sq = features.map { |f|
175
- fetch( f ) - other.fetch( f )
176
- }.map { |dist| dist * dist }.reduce( :+ )
215
+ def euclidean_distance( other )
216
+ fail TypeError unless features == other.features
217
+ sum_of_squares = zip( other )
218
+ .map { |a, b| a - b }
219
+ .map { |d| d * d }
220
+ .reduce( 0.0, :+ )
221
+ sum_of_squares ** 0.5
177
222
  end
178
223
  end # class YPetri::Net::State::Features::Record