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,
87 const std::vector<AugmentedToken>& tokens);
88
93struct Petri;
94
99using Reducer = std::function<void(Petri&)>;
100
106void deductMarking(std::vector<AugmentedToken>& tokens,
107 const SmallVectorInput& inputs);
108
116struct Petri {
130 explicit Petri(const Net& _net, const PriorityTable& _priority,
131 const Marking& _initial_tokens, const Marking& _final_marking,
132 const std::string& _case_id,
133 std::shared_ptr<TaskSystem> threadpool);
134 ~Petri() noexcept = default;
135 Petri(Petri const&) = delete;
136 Petri(Petri&&) noexcept = delete;
137 Petri& operator=(Petri const&) = delete;
138 Petri& operator=(Petri&&) noexcept = delete;
139
147 std::vector<AugmentedToken> toTokens(const Marking& marking) const noexcept;
148
157 Marking getMarking() const;
158
163 std::vector<Transition> getActiveTransitions() const;
164
171 Eventlog getLogInternal() const;
172
179 void fireTransitions();
180
181 struct PTNet {
186 std::vector<std::string> transition;
187
192 std::vector<std::string> place;
193
199 std::vector<SmallVectorInput> input_n;
200
206 std::vector<SmallVectorInput> output_n;
207
213 std::vector<SmallVector> p_to_ts_n;
214
220 std::vector<int8_t> priority;
221
227 std::vector<Callback> store;
228
229 void registerCallback(const std::string& t, Callback&& callback) noexcept {
230 if (std::find(transition.begin(), transition.end(), t) !=
231 transition.end()) {
232 store[toIndex(transition, t)] = std::move(callback);
233 }
234 }
235 } net;
236
237 std::vector<AugmentedToken> initial_tokens;
238 std::vector<AugmentedToken> tokens;
239 std::vector<AugmentedToken> final_marking;
240 std::vector<size_t> scheduled_callbacks;
243 std::string case_id;
244 std::atomic<std::optional<unsigned int>>
246
247 std::shared_ptr<moodycamel::BlockingConcurrentQueue<Reducer>>
252 std::shared_ptr<TaskSystem>
254
260 void fireAsynchronous(const size_t t);
261
262 private:
268 void fireSynchronous(const size_t t);
269};
270
271std::tuple<std::vector<std::string>, std::vector<std::string>,
272 std::vector<Callback>>
273convert(const Net& _net);
274std::tuple<std::vector<SmallVectorInput>, std::vector<SmallVectorInput>>
275populateIoLookups(const Net& _net, const std::vector<Place>& ordered_places);
276std::vector<SmallVector> createReversePlaceToTransitionLookup(
277 size_t place_count, size_t transition_count,
278 const std::vector<SmallVectorInput>& input_transitions);
279
280std::vector<int8_t> createPriorityLookup(
281 const std::vector<Transition> transition, const PriorityTable& _priority);
282} // 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:74
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:35
std::vector< SmallEvent > SmallLog
a list of events
Definition petri.h:38
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
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
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
Definition petri.h:181
std::vector< SmallVectorInput > input_n
list of list of inputs to transitions. This vector is indexed like transition.
Definition petri.h:199
std::vector< Callback > store
This is the same 'lookup table', only index using transition so it is compatible with index lookup.
Definition petri.h:227
std::vector< std::string > transition
(ordered) list of string representation of transitions
Definition petri.h:186
std::vector< SmallVectorInput > output_n
list of list of outputs of transitions. This vector is indexed like transition.
Definition petri.h:206
std::vector< std::string > place
(ordered) list of string representation of places
Definition petri.h:192
std::vector< int8_t > priority
This vector holds priorities for all transitions. This vector is index like transition.
Definition petri.h:220
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:213
Petri is a data structure that encodes the Petri net and holds pointers to the thread-pool and the re...
Definition petri.h:116
std::shared_ptr< TaskSystem > pool
A pointer to the threadpool used to defer Callbacks.
Definition petri.h:253
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:237
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:241
std::vector< size_t > scheduled_callbacks
List of active transitions.
Definition petri.h:240
std::atomic< std::optional< unsigned int > > thread_id_
The id of the thread from which the Petri is fired.
Definition petri.h:245
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:214
std::vector< AugmentedToken > tokens
The current marking.
Definition petri.h:238
std::string case_id
The unique identifier for this Petri-run.
Definition petri.h:243
Token state
The current state of the Petri.
Definition petri.h:242
std::vector< AugmentedToken > final_marking
The final marking.
Definition petri.h:239
Eventlog getLogInternal() const
get the current eventlog, also copies in all child eventlogs of active petri nets.
Definition petri.cpp:236
std::shared_ptr< moodycamel::BlockingConcurrentQueue< Reducer > > reducer_queue
Definition petri.h:248
void fireAsynchronous(const size_t t)
Schedules the Callback associated with t on the threadpool.
Definition petri.cpp:123
std::vector< Transition > getActiveTransitions() const
Get the list of active transitions.
Definition petri.cpp:225
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:167
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