Symmetri
Loading...
Searching...
No Matches
petri.h
Go to the documentation of this file.
1#pragma once
2
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,
87 const std::vector<AugmentedToken> &tokens);
88
93struct Petri;
94
99using Reducer = std::function<void(Petri &)>;
100
110 size_t t_idx, const Callback &task,
111 const std::shared_ptr<moodycamel::BlockingConcurrentQueue<Reducer>>
112 &reducer_queue);
113
119void deductMarking(std::vector<AugmentedToken> &tokens,
120 const SmallVectorInput &inputs);
121
129struct Petri {
143 explicit Petri(const Net &_net, const PriorityTable &_priority,
144 const Marking &_initial_tokens, const Marking &_final_marking,
145 const std::string &_case_id,
146 std::shared_ptr<TaskSystem> threadpool);
147 ~Petri() noexcept = default;
148 Petri(Petri const &) = delete;
149 Petri(Petri &&) noexcept = delete;
150 Petri &operator=(Petri const &) = delete;
151 Petri &operator=(Petri &&) noexcept = delete;
152
160 std::vector<AugmentedToken> toTokens(const Marking &marking) const noexcept;
161
170 Marking getMarking() const;
171
178 Eventlog getLogInternal() const;
179
185 void tryFire(const Transition &t);
186
193 void fireTransitions();
194
195 struct {
200 std::vector<std::string> transition;
201
206 std::vector<std::string> place;
207
213 std::vector<SmallVectorInput> input_n;
214
220 std::vector<SmallVectorInput> output_n;
221
227 std::vector<SmallVector> p_to_ts_n;
228
234 std::vector<int8_t> priority;
235
241 std::vector<Callback> store;
242
243 void registerCallback(const std::string &t,
244 const Callback &callback) noexcept {
245 if (std::find(transition.begin(), transition.end(), t) !=
246 transition.end()) {
247 store[toIndex(transition, t)] = callback;
248 }
249 }
250 } net;
251
252 std::vector<AugmentedToken> initial_tokens;
253 std::vector<AugmentedToken> tokens;
254 std::vector<AugmentedToken> final_marking;
255 std::vector<size_t> scheduled_callbacks;
258 std::string case_id;
259 std::atomic<std::optional<unsigned int>>
261
262 std::shared_ptr<moodycamel::BlockingConcurrentQueue<Reducer>>
267 std::shared_ptr<TaskSystem>
269
270 private:
276 void fireSynchronous(const size_t t);
277
283 void fireAsynchronous(const size_t t);
284};
285
286} // namespace symmetri
Callback is a wrapper around any type that you to tie to a transition. Typically this is an invokable...
Definition callback.h:98
Tokens are elements that can reside in places. Tokens can have a color which makes them distinguishab...
Definition colors.hpp:104
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:20
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
bool canFire(const SmallVectorInput &pre, const 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
Reducer scheduleCallback(size_t t_idx, const Callback &task, const std::shared_ptr< moodycamel::BlockingConcurrentQueue< Reducer > > &reducer_queue)
Schedules the callback associated with transition t_idx/transition. It returns a reducer which is to ...
Definition petri_utilities.cpp:64
std::function< void(Petri &)> Reducer
A Reducer updates the Petri-object. Reducers are used to process the post-callback marking mutations.
Definition petri.h:99
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
Petri is a data structure that encodes the Petri net and holds pointers to the thread-pool and the re...
Definition petri.h:129
std::shared_ptr< TaskSystem > pool
A pointer to the threadpool used to defer Callbacks.
Definition petri.h:268
void tryFire(const Transition &t)
Try to fire a single transition. Does nothing if t is not active.
Definition petri.cpp:143
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< int8_t > priority
This vector holds priorities for all transitions. This vector is index like transition.
Definition petri.h:234
std::vector< AugmentedToken > initial_tokens
The initial marking.
Definition petri.h:252
std::vector< std::string > transition
(ordered) list of string representation of transitions
Definition petri.h:200
SmallLog log
The most up to date event_log.
Definition petri.h:256
std::vector< size_t > scheduled_callbacks
List of active transitions.
Definition petri.h:255
std::vector< std::string > place
(ordered) list of string representation of places
Definition petri.h:206
std::vector< SmallVectorInput > input_n
list of list of inputs to transitions. This vector is indexed like transition.
Definition petri.h:213
std::atomic< std::optional< unsigned int > > thread_id_
The id of the thread from which the Petri is fired.
Definition petri.h:260
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:205
std::vector< AugmentedToken > tokens
The current marking.
Definition petri.h:253
std::vector< Callback > store
This is the same 'lookup table', only index using transition so it is compatible with index lookup.
Definition petri.h:241
std::string case_id
The unique identifier for this Petri-run.
Definition petri.h:258
Token state
The current state of the Petri.
Definition petri.h:257
std::vector< AugmentedToken > final_marking
The final marking.
Definition petri.h:254
Eventlog getLogInternal() const
get the current eventlog, also copies in all child eventlogs of active petri nets.
Definition petri.cpp:216
struct symmetri::Petri::@0 net
Is a data-oriented design of a Petri net.
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:227
std::shared_ptr< moodycamel::BlockingConcurrentQueue< Reducer > > reducer_queue
Definition petri.h:263
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
std::vector< SmallVectorInput > output_n
list of list of outputs of transitions. This vector is indexed like transition.
Definition petri.h:220
void fireTransitions()
Fires all active transitions until it there are none left. Associated asynchronous Callbacks are sche...
Definition petri.cpp:171
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
std::string Transition
Definition types.h:14