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
162 std::vector<Transition> getActiveTransitions() const;
163
170 Eventlog getLogInternal() const;
171
178 void fireTransitions();
179
180 struct PTNet {
185 std::vector<std::string> transition;
186
191 std::vector<std::string> place;
192
198 std::vector<SmallVectorInput> input_n;
199
205 std::vector<SmallVectorInput> output_n;
206
212 std::vector<SmallVector> p_to_ts_n;
213
219 std::vector<int8_t> priority;
220
226 std::vector<Callback> store;
227
228 void registerCallback(const std::string& t, Callback&& callback) noexcept {
229 if (std::find(transition.begin(), transition.end(), t) !=
230 transition.end()) {
231 store[toIndex(transition, t)] = std::move(callback);
232 }
233 }
234 } net;
235
236 std::vector<AugmentedToken> initial_tokens;
237 std::vector<AugmentedToken> tokens;
238 std::vector<AugmentedToken> final_marking;
239 std::vector<size_t> scheduled_callbacks;
242 std::string case_id;
243 std::atomic<std::optional<unsigned int>>
245
246 std::shared_ptr<moodycamel::BlockingConcurrentQueue<Reducer>>
251 std::shared_ptr<TaskSystem>
253
259 void fireAsynchronous(const size_t t);
260
261 private:
267 void fireSynchronous(const size_t t);
268};
269
270std::tuple<std::vector<std::string>, std::vector<std::string>,
271 std::vector<Callback>>
272convert(const Net& _net);
273std::tuple<std::vector<SmallVectorInput>, std::vector<SmallVectorInput>>
274populateIoLookups(const Net& _net, const std::vector<Place>& ordered_places);
275std::vector<SmallVector> createReversePlaceToTransitionLookup(
276 size_t place_count, size_t transition_count,
277 const std::vector<SmallVectorInput>& input_transitions);
278
279std::vector<int8_t> createPriorityLookup(
280 const std::vector<Transition> transition, const PriorityTable& _priority);
281} // 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:84
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
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
gch::small_vector< AugmentedToken, 4 > SmallVectorInput
General purpose stack-allocated mini vector for colored markings.
Definition petri.h:50
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:180
std::vector< SmallVectorInput > input_n
list of list of inputs to transitions. This vector is indexed like transition.
Definition petri.h:198
std::vector< Callback > store
This is the same 'lookup table', only index using transition so it is compatible with index lookup.
Definition petri.h:226
std::vector< std::string > transition
(ordered) list of string representation of transitions
Definition petri.h:185
std::vector< SmallVectorInput > output_n
list of list of outputs of transitions. This vector is indexed like transition.
Definition petri.h:205
std::vector< std::string > place
(ordered) list of string representation of places
Definition petri.h:191
std::vector< int8_t > priority
This vector holds priorities for all transitions. This vector is index like transition.
Definition petri.h:219
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:212
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:252
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:236
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:240
std::vector< size_t > scheduled_callbacks
List of active transitions.
Definition petri.h:239
std::atomic< std::optional< unsigned int > > thread_id_
The id of the thread from which the Petri is fired.
Definition petri.h:244
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:237
std::string case_id
The unique identifier for this Petri-run.
Definition petri.h:242
Token state
The current state of the Petri.
Definition petri.h:241
std::vector< AugmentedToken > final_marking
The final marking.
Definition petri.h:238
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:247
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