Symmetri
Loading...
Searching...
No Matches
petri.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <stddef.h>
6#include <stdint.h>
7
8#include <algorithm>
9#include <atomic>
10#include <chrono>
11#include <functional>
12#include <memory>
13#include <optional>
14#include <string>
15#include <tuple>
16#include <utility>
17#include <vector>
18
19#include "externals/blockingconcurrentqueue.h"
20#include "externals/small_vector.hpp"
21#include "symmetri/callback.h"
22#include "symmetri/colors.hpp"
23#include "symmetri/tasks.h"
24#include "symmetri/types.h"
25
26namespace symmetri {
27
33using AugmentedToken = std::tuple<size_t, Token>;
34
39struct SmallEvent {
40 size_t transition;
42 Clock::time_point stamp;
43};
44
49using SmallLog = std::vector<SmallEvent>;
50
55using SmallVector = gch::small_vector<size_t, 4>;
56
61using SmallVectorInput = gch::small_vector<AugmentedToken, 4>;
62
71size_t toIndex(const std::vector<std::string>& m, const std::string& s);
72
82gch::small_vector<size_t, 32> possibleTransitions(
83 const std::vector<AugmentedToken>& tokens,
84 const std::vector<SmallVectorInput>& input_n,
85 const std::vector<SmallVector>& p_to_ts_n);
86
97bool canFire(const SmallVectorInput& pre,
98 const std::vector<AugmentedToken>& tokens);
99
104struct Petri;
105
110using Reducer = std::function<void(Petri&)>;
111
117void deductMarking(std::vector<AugmentedToken>& tokens,
118 const SmallVectorInput& inputs);
119
127struct Petri {
141 explicit Petri(const Net& _net, const PriorityTable& _priority,
142 const Marking& _initial_tokens, const Marking& _final_marking,
143 const std::string& _case_id,
144 std::shared_ptr<TaskSystem> threadpool);
145 ~Petri() noexcept = default;
146 Petri(Petri const&) = delete;
147 Petri(Petri&&) noexcept = delete;
148 Petri& operator=(Petri const&) = delete;
149 Petri& operator=(Petri&&) noexcept = delete;
150
158 std::vector<AugmentedToken> toTokens(const Marking& marking) const noexcept;
159
168 Marking getMarking() const;
169
174 std::vector<Transition> getActiveTransitions() const;
175
182 Eventlog getLogInternal() const;
183
190 void fireTransitions();
191
192 struct PTNet {
197 std::vector<std::string> transition;
198
203 std::vector<std::string> place;
204
210 std::vector<SmallVectorInput> input_n;
211
217 std::vector<SmallVectorInput> output_n;
218
224 std::vector<SmallVector> p_to_ts_n;
225
231 std::vector<int8_t> priority;
232
238 std::vector<Callback> store;
239 std::vector<AugmentedToken> initial_tokens;
240
241 void registerCallback(const std::string& t, Callback&& callback) noexcept {
242 if (std::find(transition.begin(), transition.end(), t) !=
243 transition.end()) {
244 store[toIndex(transition, t)] = std::move(callback);
245 }
246 }
247 } net;
248
249 std::vector<AugmentedToken> tokens;
250 std::vector<AugmentedToken> final_marking;
251 std::vector<size_t> scheduled_callbacks;
254 std::string case_id;
255 std::atomic<std::optional<unsigned int>>
257
258 std::shared_ptr<moodycamel::BlockingConcurrentQueue<Reducer>>
263 std::shared_ptr<TaskSystem>
265
271 void fireAsynchronous(const size_t t);
272
273 private:
279 void fireSynchronous(const size_t t);
280};
281
282std::tuple<std::vector<std::string>, std::vector<std::string>,
283 std::vector<Callback>>
284convert(const Net& _net);
285std::tuple<std::vector<SmallVectorInput>, std::vector<SmallVectorInput>>
286populateIoLookups(const Net& _net, const std::vector<Place>& ordered_places);
287std::vector<SmallVector> createReversePlaceToTransitionLookup(
288 size_t place_count, size_t transition_count,
289 const std::vector<SmallVectorInput>& input_transitions);
290
291std::vector<int8_t> createPriorityLookup(
292 const std::vector<Transition> transition, const PriorityTable& _priority);
293} // 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:33
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:45
std::vector< SmallEvent > SmallLog
a list of events
Definition petri.h:49
std::function< void(Petri &)> Reducer
A Reducer updates the Petri-object. Reducers are used to process the post-callback marking mutations.
Definition petri.h:110
gch::small_vector< AugmentedToken, 4 > SmallVectorInput
General purpose stack-allocated mini vector for colored markings.
Definition petri.h:61
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:20
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:15
gch::small_vector< size_t, 4 > SmallVector
General purpose stack-allocated mini vector for indices.
Definition petri.h:55
Definition petri.h:192
std::vector< AugmentedToken > initial_tokens
The initial marking.
Definition petri.h:239
std::vector< SmallVectorInput > input_n
list of list of inputs to transitions. This vector is indexed like transition.
Definition petri.h:210
std::vector< Callback > store
This is the same 'lookup table', only index using transition so it is compatible with index lookup.
Definition petri.h:238
std::vector< std::string > transition
(ordered) list of string representation of transitions
Definition petri.h:197
std::vector< SmallVectorInput > output_n
list of list of outputs of transitions. This vector is indexed like transition.
Definition petri.h:217
std::vector< std::string > place
(ordered) list of string representation of places
Definition petri.h:203
std::vector< int8_t > priority
This vector holds priorities for all transitions. This vector is index like transition.
Definition petri.h:231
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:224
Petri is a data structure that encodes the Petri net and holds pointers to the thread-pool and the re...
Definition petri.h:127
std::shared_ptr< TaskSystem > pool
A pointer to the threadpool used to defer Callbacks.
Definition petri.h:264
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:106
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:252
std::vector< size_t > scheduled_callbacks
List of active transitions.
Definition petri.h:251
std::atomic< std::optional< unsigned int > > thread_id_
The id of the thread from which the Petri is fired.
Definition petri.h:256
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:218
std::vector< AugmentedToken > tokens
The current marking.
Definition petri.h:249
std::string case_id
The unique identifier for this Petri-run.
Definition petri.h:254
Token state
The current state of the Petri.
Definition petri.h:253
std::vector< AugmentedToken > final_marking
The final marking.
Definition petri.h:250
Eventlog getLogInternal() const
get the current eventlog, also copies in all child eventlogs of active petri nets.
Definition petri.cpp:240
std::shared_ptr< moodycamel::BlockingConcurrentQueue< Reducer > > reducer_queue
Definition petri.h:259
void fireAsynchronous(const size_t t)
Schedules the Callback associated with t on the threadpool.
Definition petri.cpp:127
std::vector< Transition > getActiveTransitions() const
Get the list of active transitions.
Definition petri.cpp:229
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:81
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:39
Clock::time_point stamp
The timestamp of the event.
Definition petri.h:42
size_t transition
The transition that generated the event.
Definition petri.h:40
Token state
The result of the event.
Definition petri.h:41
std::vector< Event > Eventlog
Definition types.h:36
std::vector< std::pair< Place, Token > > Marking
Definition types.h:50
std::vector< std::pair< Transition, int8_t > > PriorityTable
Definition types.h:55
std::unordered_map< Transition, std::pair< std::vector< std::pair< Place, Token > >, std::vector< std::pair< Place, Token > > > > Net
Definition types.h:39