Symmetri
Loading...
Searching...
No Matches
symmetri.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <functional>
6#include <memory>
7#include <optional>
8#include <set>
9#include <string>
10#include <utility>
11#include <vector>
12
13#include "symmetri/callback.h"
14#include "symmetri/tasks.h"
15#include "symmetri/types.h"
16
17namespace symmetri {
18
25struct Petri;
26class Token;
27
34class PetriNet final {
35 public:
47 PetriNet(const std::set<std::string> &petri_net_xmls,
48 const std::string &case_id, std::shared_ptr<TaskSystem> threadpool,
49 const Marking &goal_marking = {},
50 const PriorityTable &priorities = {});
51
62 PetriNet(const Net &net, const std::string &case_id,
63 std::shared_ptr<TaskSystem> threadpool,
64 const Marking &initial_marking, const Marking &goal_marking = {},
65 const PriorityTable &priorities = {});
66
76 std::function<void()> getInputTransitionHandle(
77 const std::string &transition) const noexcept;
78
86 void registerCallback(const std::string &transition,
87 Callback &&callback) const noexcept;
88
99 bool tryRegisterCallback(const std::string &transition,
100 Callback &&callback) const noexcept;
101
109 std::vector<Transition> getUnregisteredTransitions() const noexcept;
110
120 template <typename T, typename... Args>
121 void registerCallbackInPlace(const std::string &transition,
122 Args &&...args) const noexcept {
123 if (impl == nullptr || s.empty()) {
124 return;
125 }
126 s.emplace_back(std::in_place, identity<T>{}, std::forward<Args>(args)...);
127 *getCallbackItr(transition) = std::move(s.back());
128 s.pop_back();
129 }
130
137 Marking getMarking() const noexcept;
138
144 std::vector<Transition> getActiveTransitions() const noexcept;
145
155 bool reuseApplication(const std::string &case_id);
156
157 friend Token(symmetri::fire)(const PetriNet &);
158 friend void(symmetri::cancel)(const PetriNet &);
159 friend void(symmetri::pause)(const PetriNet &);
160 friend void(symmetri::resume)(const PetriNet &);
161 friend Eventlog(symmetri::getLog)(const PetriNet &);
162
163 private:
171 std::vector<std::optional<Callback>>::iterator getCallbackItr(
172 const std::string &transition_name) const;
173
174 const std::shared_ptr<Petri> impl;
176 std::vector<std::optional<Callback>> &s;
177};
178
179} // namespace symmetri
Token fire(const T &callback)
Generates a Token based on what kind of information the Callback returns.
Definition callback.h:65
Eventlog getLog(const T &)
Get the Log object. By default it returns an empty vector.
Definition callback.h:83
void resume(const T &)
Templates a resume action for a Callback. By default it does nothing and not resume the Callback.
Definition callback.h:53
void cancel(const T &)
The default cancel implementation is naive. It only returns a user-exit state and does nothing to the...
Definition callback.h:35
void pause(const T &)
Implements a pause action for a Callback. By default it does nothing and not pause the Callback.
Definition callback.h:44
Callback is a wrapper around any type that you to tie to a transition. Typically this is an invokable...
Definition callback.h:104
PetriNet exposes the possible constructors to create PetriNets. It also allows the user to register a...
Definition symmetri.h:34
Marking getMarking() const noexcept
Get the Marking object. This function is thread-safe and be called during PetriNet execution.
Definition symmetri.cpp:100
bool tryRegisterCallback(const std::string &transition, Callback &&callback) const noexcept
Same as registerCallback, but reports whether the Callback was actually registered.
Definition symmetri.cpp:72
std::function< void()> getInputTransitionHandle(const std::string &transition) const noexcept
By registering a input transition you get a handle to manually force a transition to fire....
Definition symmetri.cpp:48
std::vector< Transition > getUnregisteredTransitions() const noexcept
Get the transitions that still have the default payload (DirectMutation), i.e. transitions for which ...
Definition symmetri.cpp:83
PetriNet(const std::set< std::string > &petri_net_xmls, const std::string &case_id, std::shared_ptr< TaskSystem > threadpool, const Marking &goal_marking={}, const PriorityTable &priorities={})
Construct a new PetriNet object from a set of paths to PNML- or GRML-files. Since PNML-files do not h...
Definition symmetri.cpp:20
bool reuseApplication(const std::string &case_id)
reuseApplication resets the PetriNet such that the same net can be used again after a cancel call or ...
Definition symmetri.cpp:124
void registerCallback(const std::string &transition, Callback &&callback) const noexcept
The default transition payload (DirectMutation) is overloaded by the Callback supplied for a specific...
Definition symmetri.cpp:67
void registerCallbackInPlace(const std::string &transition, Args &&...args) const noexcept
Construct a Callback of type T in place. This is required for type that are not moveable;.
Definition symmetri.h:121
std::vector< Transition > getActiveTransitions() const noexcept
Get the list of active transitions. This function is thread-safe and be called during PetriNet execut...
Definition symmetri.cpp:112
Tokens are elements that can reside in places. Tokens can have a color which makes them distinguishab...
Definition colors.hpp:74
Petri is a data structure that encodes the Petri net and holds pointers to the thread-pool and the re...
Definition petri.h:127
Definition callback.h:88
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
std::string Transition
Definition types.h:18