y_petri 2.2.4 → 2.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -1,103 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module YPetri::Transition::TypeInformation
4
- # Is this a timed stoichiometric transition?
5
- #
6
- def TS?
7
- type == :TS
8
- end
9
-
10
- # Is this a timed non-stoichiometric transition?
11
- #
12
- def Ts?
13
- type == :Ts
14
- end
15
-
16
- # Is this a timeless stoichiometric transition?
17
- #
18
- def tS?
19
- type == :tS
20
- end
21
-
22
- # Is this a timeless non-stoichiometric transition?
23
- #
24
- def ts?
25
- type == :ts
26
- end
27
-
28
- # Is this a stoichiometric transition?
29
- #
30
- def stoichiometric?; @stoichiometric end
31
- alias S? stoichiometric?
32
-
33
- # Is this a non-stoichiometric transition?
34
- #
35
- def nonstoichiometric?
36
- ! stoichiometric?
37
- end
38
- alias s? nonstoichiometric?
39
-
40
- # Does the transition's action depend on delta time?
41
- #
42
- def timed?
43
- @timed
44
- end
45
- alias T? timed?
46
-
47
- # Is the transition timeless? (Opposite of #timed?)
48
- #
49
- def timeless?
50
- not timed?
51
- end
52
- alias t? timeless?
53
-
54
- # Is the transition functional?
55
- #
56
- # Explanation: If rate or action closure is supplied, a transition is always
57
- # considered 'functional'. Otherwise, it is considered not 'functional'.
58
- # Note that even transitions that are not functional still have standard
59
- # action acc. to Petri's definition. Also note that a timed transition is
60
- # necessarily functional.
61
- #
62
- def functional?
63
- @functional
64
- end
65
-
66
- # Opposite of #functional?
67
- #
68
- def functionless?
69
- not functional?
70
- end
71
-
72
- # Reports the transition's membership in one of the 4 basic types:
73
- #
74
- # 1. TS .... timed stoichiometric
75
- # 2. tS .... timeless stoichiometric
76
- # 3. Ts .... timed nonstoichiometric
77
- # 4. ts .... timeless nonstoichiometric
78
- #
79
- # plus the fifth type
80
- #
81
- # 5. A .... assignment transitions
82
- #
83
- def type
84
- return :A if assignment_action?
85
- timed? ? ( stoichiometric? ? :TS : :Ts ) : ( stoichiometric? ? :tS : :ts )
86
- end
87
-
88
- # Is this a transition with assignment action? (Transitions with assignment
89
- # action, or "assignment transitions", completely replace the marking of their
90
- # codomain with their action closure result, like in spreadsheets.)
91
- #
92
- def assignment_action?
93
- @assignment_action
94
- end
95
- alias assignment? assignment_action?
96
- alias A? assignment_action?
97
-
98
- # Is this a non-assignment transition? (Opposite of +#A?+)
99
- #
100
- def a?
101
- ! assignment_action?
102
- end
103
- end # class YPetri::Transition::Type
@@ -1,103 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module YPetri::Transition::Types
4
- # Is this a timed stoichiometric transition?
5
- #
6
- def TS?
7
- type == :TS
8
- end
9
-
10
- # Is this a timed non-stoichiometric transition?
11
- #
12
- def Ts?
13
- type == :Ts
14
- end
15
-
16
- # Is this a timeless stoichiometric transition?
17
- #
18
- def tS?
19
- type == :tS
20
- end
21
-
22
- # Is this a timeless non-stoichiometric transition?
23
- #
24
- def ts?
25
- type == :ts
26
- end
27
-
28
- # Is this a stoichiometric transition?
29
- #
30
- def stoichiometric?; @stoichiometric end
31
- alias S? stoichiometric?
32
-
33
- # Is this a non-stoichiometric transition?
34
- #
35
- def nonstoichiometric?
36
- ! stoichiometric?
37
- end
38
- alias s? nonstoichiometric?
39
-
40
- # Does the transition's action depend on delta time?
41
- #
42
- def timed?
43
- @timed
44
- end
45
- alias T? timed?
46
-
47
- # Is the transition timeless? (Opposite of #timed?)
48
- #
49
- def timeless?
50
- not timed?
51
- end
52
- alias t? timeless?
53
-
54
- # Is the transition functional?
55
- #
56
- # Explanation: If rate or action closure is supplied, a transition is always
57
- # considered 'functional'. Otherwise, it is considered not 'functional'.
58
- # Note that even transitions that are not functional still have standard
59
- # action acc. to Petri's definition. Also note that a timed transition is
60
- # necessarily functional.
61
- #
62
- def functional?
63
- @functional
64
- end
65
-
66
- # Opposite of #functional?
67
- #
68
- def functionless?
69
- not functional?
70
- end
71
-
72
- # Reports the transition's membership in one of the 4 basic types:
73
- #
74
- # 1. TS .... timed stoichiometric
75
- # 2. tS .... timeless stoichiometric
76
- # 3. Ts .... timed nonstoichiometric
77
- # 4. ts .... timeless nonstoichiometric
78
- #
79
- # plus the fifth type
80
- #
81
- # 5. A .... assignment transitions
82
- #
83
- def type
84
- return :A if assignment_action?
85
- timed? ? ( stoichiometric? ? :TS : :Ts ) : ( stoichiometric? ? :tS : :ts )
86
- end
87
-
88
- # Is this a transition with assignment action? (Transitions with assignment
89
- # action, or "assignment transitions", completely replace the marking of their
90
- # codomain with their action closure result, like in spreadsheets.)
91
- #
92
- def assignment_action?
93
- @assignment_action
94
- end
95
- alias assignment? assignment_action?
96
- alias A? assignment_action?
97
-
98
- # Is this a non-assignment transition? (Opposite of +#A?+)
99
- #
100
- def a?
101
- ! assignment_action?
102
- end
103
- end # class YPetri::Transition::Type
@@ -1,176 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # Instance methods of the World class related to simulation (initial marking
4
- # collections, clamp collections, inital marking collections, management of
5
- # simulations...)
6
- #
7
- module YPetri::World::SimulationRelated
8
- # Collections of clamps, initial marking vectors, and simulation settings.
9
- #
10
- attr_reader :clamp_collections,
11
- :initial_marking_collections,
12
- :simulation_settings_collections
13
-
14
- # Instance initialization.
15
- #
16
- def initialize
17
- @simulations = {} # { simulation => its settings }
18
- @clamp_collections = { Base: {} } # { collection name => clamp hash }
19
- @initial_marking_collections = { Base: {} } # { collection name => im hash }
20
- @simulation_settings_collections = # { collection name => ss hash }
21
- { Base: ( YPetri::Simulation::DEFAULT_SETTINGS.call
22
- .update YPetri::Simulation::Timed::DEFAULT_SETTINGS.call ) }
23
- super
24
- end
25
-
26
- # Hash of simulation instances and their settings.
27
- #
28
- def simulations
29
- @simulations
30
- end
31
-
32
- # Clamp collection names.
33
- #
34
- def clamp_collection_names
35
- @clamp_collections.keys
36
- end
37
- alias ncc clamp_collection_names
38
-
39
- # Initial marking collection names.
40
- #
41
- def initial_marking_collection_names
42
- @initial_marking_collections.keys
43
- end
44
- alias nimc initial_marking_collection_names
45
-
46
- # Simulation settings collection names.
47
- #
48
- def simulation_settings_collection_names
49
- @simulation_settings_collections.keys
50
- end
51
- alias nssc simulation_settings_collection_names
52
-
53
- # Clamp collection identified by the argument.
54
- #
55
- def clamp_collection name=:Base
56
- @clamp_collections[name]
57
- end
58
- alias cc clamp_collection
59
-
60
- # Marking collection identified by the argument.
61
- #
62
- def initial_marking_collection name=:Base
63
- @initial_marking_collections[name]
64
- end
65
- alias imc initial_marking_collection
66
-
67
- # Simulation settings collection specified by the argument.
68
- #
69
- def simulation_settings_collection name=:Base
70
- @simulation_settings_collections[name]
71
- end
72
- alias ssc simulation_settings_collection
73
-
74
- # Creates a new clamp collection. If collection identifier is not given,
75
- # resets :Base clamp collection to new values.
76
- #
77
- def set_clamp_collection( name=:Base, clamp_hash )
78
- @clamp_collections[name] = clamp_hash
79
- end
80
- alias set_cc set_clamp_collection
81
-
82
- # Creates a new initial marking collection. If collection identifier is not
83
- # given, resets :Base initial marking collection to new values.
84
- #
85
- def set_initial_marking_collection( name=:Base, initial_marking_hash )
86
- @initial_marking_collections[name] = initial_marking_hash
87
- end
88
- alias set_imc set_initial_marking_collection
89
-
90
- # Creates a new simulation settings collection. If collection identifier is
91
- # not given, resets :Base simulation settings collection to new values.
92
- #
93
- def set_simulation_settings_collection( name=:Base, sim_set_hash )
94
- @simulation_settings_collections[name] = sim_set_hash
95
- end
96
- alias set_ssc set_simulation_settings_collection
97
-
98
- # Presents a simulation specified by the argument, which must be a hash with
99
- # four items: :net, :clamp_collection, :inital_marking_collection and
100
- # :simulation_settings_collection.
101
- #
102
- def simulation settings={}
103
- key = case settings
104
- when ~:may_have then # it is a hash or equivalent
105
- settings.may_have :net
106
- settings.may_have :cc, syn!: :clamp_collection
107
- settings.may_have :imc, syn!: :initial_marking_collection
108
- settings.may_have :ssc, syn!: :simulation_settings_collection
109
- { net: net( settings[:net] || self.Net::Top ), # the key
110
- cc: settings[:cc] || :Base,
111
- imc: settings[:imc] || :Base,
112
- ssc: settings[:ssc] || :Base }
113
- else # use the unprocessed argument itself as the key
114
- settings
115
- end
116
- @simulations[ key ]
117
- end
118
-
119
- # Makes a new timed simulation. Named arguments for this method are the same
120
- # as for TimedSimulation#new, but in addition, :name can be supplied.
121
- #
122
- # To create a simulation, simulation settings collection, initial marking
123
- # collection, and clamp collection have to be specified. A <em>place clamp</em>,
124
- # is a fixed value, at which the marking is held. Similarly, <em>initial
125
- # marking</em> is the marking, which a free place receives at the beginning.
126
- # Free places are those, that are not clamped. After initialization, marking
127
- # of free places is allowed to change as the transition fire.
128
- #
129
- # For example, having places :P1..:P5, clamped :P1, :P2 can be written as eg.:
130
- #
131
- # * clamps = { P1: 4, P2: 5 }
132
- #
133
- # Places :P3, :P4, :P5 are <em>free</em>. Their initial marking has to be
134
- # specified, which can be written as eg.:
135
- #
136
- # * initial_markings = { P3: 1, P4: 2, P5: 3 }
137
- #
138
- # As for simulation settings, their exact nature depends on the simulation
139
- # method. For default Euler method, there are 3 important parameters:
140
- # - <em>step_size</em>,
141
- # - <em>sampling_period</em>,
142
- # - <em>target_time</em>
143
- #
144
- # For example, default simulation settings are:
145
- #
146
- # * default_ss = { step_size: 0.1, sampling_period: 5, target_time: 60 }
147
- #
148
- def new_simulation( net: Net()::Top, **nn )
149
- net_inst = net( net )
150
- nn.may_have :cc, syn!: :clamp_collection
151
- nn.may_have :imc, syn!: :initial_marking_collection
152
- nn.may_have :ssc, syn!: :simulation_settings_collection
153
- cc_id = nn.delete( :cc ) || :Base
154
- imc_id = nn.delete( :imc ) || :Base
155
- ssc_id = nn.delete( :ssc ) || :Base
156
- # Construct the simulation key:
157
- key = if nn.has? :name, syn!: :ɴ then # explicit key (name)
158
- nn[:name]
159
- else # constructed key
160
- {}.merge( net: net_inst,
161
- cc: cc_id,
162
- imc: imc_id,
163
- ssc: ssc_id )
164
- .merge( nn )
165
- end
166
- # Let's clarify what we got so far.
167
- sim_settings = ssc( ssc_id )
168
- mc_hash = cc( cc_id )
169
- im_hash = imc( imc_id )
170
- # Create and return the simulation
171
- sim = net_inst.simulation **sim_settings.merge( initial_marking: im_hash,
172
- marking_clamps: mc_hash
173
- ).merge( nn )
174
- @simulations[ key ] = sim
175
- end
176
- end # module YPetri::World::SimulationRelated