Symmetri
Loading...
Searching...
No Matches
petri.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <functional>
6#include <optional>
7#include <tuple>
8
9#include "externals/blockingconcurrentqueue.h"
10#include "externals/small_vector.hpp"
11#include "symmetri/callback.h"
12#include "symmetri/tasks.h"
13#include "symmetri/types.h"
14
15namespace symmetri {
16
22using AugmentedToken = std::tuple<size_t, Token>;
23
28struct SmallEvent {
29 size_t transition;
31 Clock::time_point stamp;
32};
33
38using SmallLog = std::vector<SmallEvent>;
39
44using SmallVector = gch::small_vector<size_t, 4>;
45
50using SmallVectorInput = gch::small_vector<AugmentedToken, 4>;
51
60size_t toIndex(const std::vector<std::string> &m, const std::string &s);
61
71gch::small_vector<size_t, 32> possibleTransitions(
72 const std::vector<AugmentedToken> &tokens,
73 const std::vector<SmallVectorInput> &input_n,
74 const std::vector<SmallVector> &p_to_ts_n);
75
86bool canFire(const SmallVectorInput &pre, std::vector<AugmentedToken> &tokens);
87
92struct Petri;
93
98using Reducer = std::function<void(Petri &)>;
99
105void deductMarking(std::vector<AugmentedToken> &tokens,
106 const SmallVectorInput &inputs);
107
115struct Petri {
129 explicit Petri(const Net &_net, const PriorityTable &_priority,
130 const Marking &_initial_tokens, const Marking &_final_marking,
131 const std::string &_case_id,
132 std::shared_ptr<TaskSystem> threadpool);
133 ~Petri() noexcept = default;
134 Petri(Petri const &) = delete;
135 Petri(Petri &&) noexcept = delete;
136 Petri &operator=(Petri const &) = delete;
137 Petri &operator=(Petri &&) noexcept = delete;
138
146 std::vector<AugmentedToken> toTokens(const Marking &marking) const noexcept;
147
156 Marking getMarking() const;
157
164 Eventlog getLogInternal() const;
165
172 void fireTransitions();
173
174 struct PTNet {
179 std::vector<std::string> transition;
180
185 std::vector<std::string> place;
186
192 std::vector<SmallVectorInput> input_n;
193
199 std::vector<SmallVectorInput> output_n;
200
206 std::vector<SmallVector> p_to_ts_n;
207
213 std::vector<int8_t> priority;
214
220 std::vector<Callback> store;
221
222 void registerCallback(const std::string &t, Callback &&callback) noexcept {
223 if (std::find(transition.begin(), transition.end(), t) !=
224 transition.end()) {
225 store[toIndex(transition, t)] = std::move(callback);
226 }
227 }
228 } net;
229
230 std::vector<AugmentedToken> initial_tokens;
231 std::vector<AugmentedToken> tokens;
232 std::vector<AugmentedToken> final_marking;
233 std::vector<size_t> scheduled_callbacks;
236 std::string case_id;
237 std::atomic<std::optional<unsigned int>>
239
240 std::shared_ptr<moodycamel::BlockingConcurrentQueue<Reducer>>
245 std::shared_ptr<TaskSystem>
247
253 void fireAsynchronous(const size_t t);
254
255 private:
261 void fireSynchronous(const size_t t);
262};
263
264std::tuple<std::vector<std::string>, std::vector<std::string>,
265 std::vector<Callback>>
266convert(const Net &_net);
267std::tuple<std::vector<SmallVectorInput>, std::vector<SmallVectorInput>>
268populateIoLookups(const Net &_net, const std::vector<Place> &ordered_places);
269std::vector<SmallVector> createReversePlaceToTransitionLookup(
270 size_t place_count, size_t transition_count,
271 const std::vector<SmallVectorInput> &input_transitions);
272
273std::vector<int8_t> createPriorityLookup(
274 const std::vector<Transition> transition, const PriorityTable &_priority);
275} // namespace symmetri
Callback is a wrapper around any type that you to tie to a transition. Typically this is an invokable...
Definition callback.h:104
Tokens are elements that can reside in places. Tokens can have a color which makes them distinguishab...
Definition colors.hpp:107
std::tuple< size_t, Token > AugmentedToken
AugmentedToken describes a token with a color in a particular place.
Definition petri.h:22
gch::small_vector< size_t, 32 > possibleTransitions(const std::vector< AugmentedToken > &tokens, const std::vector< SmallVectorInput > &input_n, const std::vector< SmallVector > &p_to_ts_n)
calculates a list of possible transitions given the current token-distribution. It returns a list of ...
Definition petri_utilities.cpp:34
std::vector< SmallEvent > SmallLog
a list of events
Definition petri.h:38
gch::small_vector< AugmentedToken, 4 > SmallVectorInput
General purpose stack-allocated mini vector for colored markings.
Definition petri.h:50
std::function< void(Petri &)> Reducer
A Reducer updates the Petri-object. Reducers are used to process the post-callback marking mutations.
Definition petri.h:98
size_t toIndex(const std::vector< std::string > &m, const std::string &s)
a small helper function to get the index representation of a place or transition.
Definition petri_utilities.cpp:5
gch::small_vector< size_t, 4 > SmallVector
General purpose stack-allocated mini vector for indices.
Definition petri.h:44
bool canFire(const SmallVectorInput &pre, std::vector< AugmentedToken > &tokens)
Takes a vector of input places (pre-conditions) and the current token distribution to determine wheth...
Definition petri_utilities.cpp:10
Definition petri.h:174
std::vector< SmallVectorInput > input_n
list of list of inputs to transitions. This vector is indexed like transition.
Definition petri.h:192
std::vector< Callback > store
This is the same 'lookup table', only index using transition so it is compatible with index lookup.
Definition petri.h:220
std::vector< std::string > transition
(ordered) list of string representation of transitions
Definition petri.h:179
std::vector< SmallVectorInput > output_n
list of list of outputs of transitions. This vector is indexed like transition.
Definition petri.h:199
std::vector< std::string > place
(ordered) list of string representation of places
Definition petri.h:185
std::vector< int8_t > priority
This vector holds priorities for all transitions. This vector is index like transition.
Definition petri.h:213
std::vector< SmallVector > p_to_ts_n
list of list of transitions that have places as inputs. This vector is index like place
Definition petri.h:206
Petri is a data structure that encodes the Petri net and holds pointers to the thread-pool and the re...
Definition petri.h:115
std::shared_ptr< TaskSystem > pool
A pointer to the threadpool used to defer Callbacks.
Definition petri.h:246
std::vector< AugmentedToken > toTokens(const Marking &marking) const noexcept
outputs the marking as a vector of tokens; e.g. [1 1 1 0 5] means 3 tokens in place 1,...
Definition petri.cpp:102
std::vector< AugmentedToken > initial_tokens
The initial marking.
Definition petri.h:230
struct symmetri::Petri::PTNet net
Is a data-oriented design of a Petri net.
SmallLog log
The most up to date event_log.
Definition petri.h:234
std::vector< size_t > scheduled_callbacks
List of active transitions.
Definition petri.h:233
std::atomic< std::optional< unsigned int > > thread_id_
The id of the thread from which the Petri is fired.
Definition petri.h:238
Marking getMarking() const
Get the current marking. It is represented by a vector of places: every occurance of a place in this ...
Definition petri.cpp:213
std::vector< AugmentedToken > tokens
The current marking.
Definition petri.h:231
std::string case_id
The unique identifier for this Petri-run.
Definition petri.h:236
Token state
The current state of the Petri.
Definition petri.h:235
std::vector< AugmentedToken > final_marking
The final marking.
Definition petri.h:232
Eventlog getLogInternal() const
get the current eventlog, also copies in all child eventlogs of active petri nets.
Definition petri.cpp:224
std::shared_ptr< moodycamel::BlockingConcurrentQueue< Reducer > > reducer_queue
Definition petri.h:241
void fireAsynchronous(const size_t t)
Schedules the Callback associated with t on the threadpool.
Definition petri.cpp:123
Petri(const Net &_net, const PriorityTable &_priority, const Marking &_initial_tokens, const Marking &_final_marking, const std::string &_case_id, std::shared_ptr< TaskSystem > threadpool)
Construct a new Petri from a multiset description of a Petri net, a lookup table for the transitions,...
Definition petri.cpp:77
void fireTransitions()
Fires all active transitions until it there are none left. Associated asynchronous Callbacks are sche...
Definition petri.cpp:166
a minimal Event representation.
Definition petri.h:28
Clock::time_point stamp
The timestamp of the event.
Definition petri.h:31
size_t transition
The transition that generated the event.
Definition petri.h:29
Token state
The result of the event.
Definition petri.h:30
std::vector< std::pair< Transition, int8_t > > PriorityTable
Definition types.h:51
std::vector< Event > Eventlog
Definition types.h:32
std::vector< std::pair< Place, Token > > Marking
Definition types.h:46
std::unordered_map< Transition, std::pair< std::vector< std::pair< Place, Token > >, std::vector< std::pair< Place, Token > > > > Net
Definition types.h:35