Skip to content

Commit

Permalink
move traits to symmetri namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
thorstink committed Nov 12, 2023
1 parent c2fde8b commit e8062df
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 66 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ add_subdirectory(symmetri)

# some examples using the lib
if(BUILD_EXAMPLES)
# add_subdirectory(examples/flight)
add_subdirectory(examples/flight)
add_subdirectory(examples/hello_world)
add_subdirectory(examples/combinations)
add_subdirectory(examples/performance)
Expand Down
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ RECURSIVE = YES
# Note that relative paths are relative to the directory from which doxygen is
# run.

EXCLUDE = symmetri/submodules symmetri/small_vector.hpp symmetri/include/symmetri/maxplus.hpp symmetri/tests symmetri/queue
EXCLUDE = symmetri/submodules symmetri/small_vector.hpp symmetri/include/symmetri/maxplus.hpp symmetri/tests symmetri/queue symmetri/gui

# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
# directories that are symbolic links (a Unix file system feature) are excluded
Expand Down
30 changes: 11 additions & 19 deletions examples/flight/flight.cc
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include <spdlog/spdlog.h>
#include <symmetri/parsers.h>
#include <symmetri/symmetri.h>

#include <fstream>
#include <iostream>
#include <symmetri/utilities.hpp>

#include "symmetri/parsers.h"
#include "symmetri/symmetri.h"
#include "symmetri/utilities.hpp"
#include "transition.hpp"

using namespace symmetri;

/**
* @brief We want to use the Foo class with Symmetri; Foo has nice
* functionalities such as Pause and Resume and it can also get
Expand All @@ -19,10 +17,11 @@ using namespace symmetri;
* all - if nothing is defined, a default version is used.
*
*/
const static symmetri::Token FooFail(symmetri::Color::registerToken("FooFail"));

const static Token FooFail(Color::registerToken("FooFail"));

Token fire(const Foo &f) { return f.fire() ? FooFail : Color::Success; }
symmetri::Token fire(const Foo &f) {
return f.fire() ? FooFail : symmetri::Color::Success;
}

void cancel(const Foo &f) { f.cancel(); }

Expand All @@ -37,21 +36,14 @@ void resume(const Foo &f) { f.resume(); }
*/
void printLog(const symmetri::Eventlog &eventlog) {
for (const auto &[caseid, t, s, c] : eventlog) {
spdlog::info("EventLog: {0}, {1}, {2}, {3}", caseid, t, Color::toString(s),
c.time_since_epoch().count());
spdlog::info("EventLog: {0}, {1}, {2}, {3}", caseid, t,
symmetri::Color::toString(s), c.time_since_epoch().count());
}
}

void writeMermaidHtmlToFile(const std::string &mermaid) {
std::ofstream mermaid_file;
mermaid_file.open("examples/flight/mermaid.html");
mermaid_file << "<div class=\"mermaid\">" + mermaid + "</div>";
mermaid_file.close();

return;
}

int main(int, char *argv[]) {
using namespace symmetri;

spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%f] [%^%l%$] [thread %t] %v");

// We get some paths to PNML-files
Expand Down
2 changes: 1 addition & 1 deletion examples/performance/performance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ symmetri::Token fire(const Simple &) {
i++;
return symmetri::Color::Success;
}
using namespace symmetri;

int main(int argc, char *argv[]) {
using namespace symmetri;
auto pool = std::make_shared<TaskSystem>(1);
PetriNet petri = [=] {
if (argc > 1) {
Expand Down
39 changes: 24 additions & 15 deletions symmetri/include/symmetri/callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "symmetri/types.h"

namespace symmetri {

/**
* @brief Checks if the callback is synchronous. Synchronous callbacks are
* immediately executed inside the Petri net executor. Asynchronous callbacks
Expand Down Expand Up @@ -81,8 +83,6 @@ symmetri::Eventlog getLog(const T &) {
return {};
}

namespace symmetri {

/**
* @brief Callback is a wrapper around any type that you to tie to a
* transition. Typically this is an invokable object, such as a function, that
Expand All @@ -101,21 +101,30 @@ class Callback {
/**
* @brief Construct a new Callback object
*
* @param cb is the Callback instance
* @param callback is the Callback instance
*/
Callback(Transition cb)
: self_(std::make_shared<model<Transition>>(std::move(cb))) {}

Token operator()() const { return this->self_->fire_(); }
void operator()(Eventlog &el) const { el = this->self_->get_log_(); }
friend Token fire(const Callback &cb) { return cb.self_->fire_(); }
friend Eventlog getLog(const Callback &cb) { return cb.self_->get_log_(); }
friend bool isSynchronous(const Callback &cb) {
return cb.self_->is_synchronous_();
Callback(Transition callback)
: self_(std::make_shared<model<Transition>>(std::move(callback))) {}

// void operator()(Eventlog &el) const { el = this->self_->get_log_(); }
friend Token fire(const Callback &callback) {
return callback.self_->fire_();
}
friend Eventlog getLog(const Callback &callback) {
return callback.self_->get_log_();
}
friend bool isSynchronous(const Callback &callback) {
return callback.self_->is_synchronous_();
}
friend void cancel(const Callback &callback) {
return callback.self_->cancel_();
}
friend void pause(const Callback &callback) {
return callback.self_->pause_();
}
friend void resume(const Callback &callback) {
return callback.self_->resume_();
}
friend void cancel(const Callback &cb) { return cb.self_->cancel_(); }
friend void pause(const Callback &cb) { return cb.self_->pause_(); }
friend void resume(const Callback &cb) { return cb.self_->resume_(); }

private:
struct concept_t {
Expand Down
2 changes: 1 addition & 1 deletion symmetri/include/symmetri/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ std::tuple<symmetri::Net, symmetri::Marking, symmetri::PriorityTable> readGrml(
* register tokens for the color attributes that are in the arcs that go from a
* place to a transition.
*
* @param files
* @param pnml-files
* @return std::tuple<symmetri::Net, symmetri::Marking>
*/
std::tuple<symmetri::Net, symmetri::Marking> readPnml(
Expand Down
14 changes: 8 additions & 6 deletions symmetri/include/symmetri/symmetri.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ namespace symmetri {
struct Petri;

/**
* @brief PetriNet exposes the possible constructors to create PetriNets.
* @brief PetriNet exposes the possible constructors to create PetriNets. It
* also allows the user to register a Callback to a transition, or to get a
* handle for input-transitions.
*
*/
class PetriNet final {
Expand Down Expand Up @@ -98,11 +100,11 @@ class PetriNet final {
*/
bool reuseApplication(const std::string &case_id);

friend symmetri::Token(::fire)(const PetriNet &);
friend void(::cancel)(const PetriNet &);
friend void(::pause)(const PetriNet &);
friend void(::resume)(const PetriNet &);
friend symmetri::Eventlog(::getLog)(const PetriNet &);
friend symmetri::Token(symmetri::fire)(const PetriNet &);
friend void(symmetri::cancel)(const PetriNet &);
friend void(symmetri::pause)(const PetriNet &);
friend void(symmetri::resume)(const PetriNet &);
friend symmetri::Eventlog(symmetri::getLog)(const PetriNet &);

private:
const std::shared_ptr<symmetri::Petri>
Expand Down
2 changes: 1 addition & 1 deletion symmetri/include/symmetri/tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TaskSystem {
TaskSystem& operator=(TaskSystem&&) noexcept = delete;

/**
* @brief push tasks the the queue for later execution on the thread pool.
* @brief push tasks the queue for later execution on the thread pool.
*
* @param p
*/
Expand Down
9 changes: 8 additions & 1 deletion symmetri/include/symmetri/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@ using PriorityTable =
std::vector<std::pair<Transition, int8_t>>; ///< Priority is limited from
///< -128 to 127

/**
* @brief A DirectMutation is a synchronous no-operation function. It simply
* mutates the mutation on the petri net executor loop. This way the deferring
* to the threadpool and back to the petri net loop is avoided.
*
*/
struct DirectMutation {};
class PetriNet;

} // namespace symmetri
/**
* @brief A DirectMutation is a synchronous Callback that always
* completes.
Expand Down Expand Up @@ -108,3 +113,5 @@ void resume(const symmetri::PetriNet &);
* @return symmetri::Eventlog
*/
symmetri::Eventlog getLog(const symmetri::PetriNet &);

} // namespace symmetri
9 changes: 2 additions & 7 deletions symmetri/petri.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#include "petri.h"

bool isSynchronous(const symmetri::DirectMutation &) { return true; }
symmetri::Token fire(const symmetri::DirectMutation &) {
return symmetri::Color::Success;
}

namespace symmetri {
std::tuple<std::vector<std::string>, std::vector<std::string>,
std::vector<std::string>, std::vector<Callback>>
Expand Down Expand Up @@ -242,8 +237,8 @@ Eventlog Petri::getLogInternal() const {
}

// get event log from parent nets:
for (const auto &cb : net.store) {
Eventlog sub_el = getLog(cb);
for (const auto &callback : net.store) {
Eventlog sub_el = getLog(callback);
if (!sub_el.empty()) {
eventlog.insert(eventlog.end(), sub_el.begin(), sub_el.end());
}
Expand Down
6 changes: 3 additions & 3 deletions symmetri/petri.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ inline bool operator>(const AugmentedToken &lhs, const AugmentedToken &rhs) {
}

/**
* @brief a minimal event representation.
* @brief a minimal Event representation.
*
*/
struct SmallEvent {
Expand Down Expand Up @@ -256,10 +256,10 @@ struct Petri {
std::vector<Callback> store;

void registerCallback(const std::string &t,
const symmetri::Callback &cb) noexcept {
const symmetri::Callback &callback) noexcept {
if (std::find(transition.begin(), transition.end(), t) !=
transition.end()) {
store[toIndex(transition, t)] = cb;
store[toIndex(transition, t)] = callback;
}
}
} net; ///< Is a data-oriented design of a Petri net
Expand Down
13 changes: 6 additions & 7 deletions symmetri/symmetri.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ std::function<void()> PetriNet::getInputTransitionHandle(
}
}

void PetriNet::registerCallback(const std::string &transition,
const symmetri::Callback &cb) const noexcept {
void PetriNet::registerCallback(
const std::string &transition,
const symmetri::Callback &callback) const noexcept {
if (!impl->thread_id_.load().has_value()) {
impl->net.registerCallback(transition, cb);
impl->net.registerCallback(transition, callback);
}
}

Expand All @@ -98,10 +99,6 @@ bool PetriNet::reuseApplication(const std::string &new_case_id) {
return false;
}

} // namespace symmetri

using namespace symmetri;

symmetri::Token fire(const PetriNet &app) {
if (app.impl->thread_id_.load().has_value()) {
return symmetri::Color::Failed;
Expand Down Expand Up @@ -193,3 +190,5 @@ Eventlog getLog(const PetriNet &app) {
return app.impl->getLogInternal();
}
}

} // namespace symmetri
10 changes: 10 additions & 0 deletions symmetri/tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@

namespace symmetri {

/**
* @brief An alias for the implementation. In this case
* moodycamel::BlockingConcurrentQueue.
*
*/
using Queue = moodycamel::BlockingConcurrentQueue<TaskSystem::Task>;

/**
* @brief TaskQueue is an inheritance based alias for a lock-free queue.
*
*/
class TaskQueue : public Queue {
using Queue::Queue;
};
Expand Down
6 changes: 3 additions & 3 deletions symmetri/tests/test_callback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include "symmetri/callback.h"

using namespace symmetri;

class Foo {
public:
Foo(std::string name) : name(name), constructor(1), copy_constructor(0) {
Expand All @@ -23,13 +21,15 @@ class Foo {
const int copy_constructor;
};

Token fire(const Foo&) { return Color::Success; }
symmetri::Token fire(const Foo&) { return symmetri::Color::Success; }

void resume(const Foo& f) {
CHECK(f.constructor == 1);
CHECK(f.copy_constructor == 0);
}

using namespace symmetri;

TEST_CASE("Constructing is just as you expect") {
Foo o("one_constructor");
CHECK(o.constructor == 1);
Expand Down
5 changes: 5 additions & 0 deletions symmetri/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#include "symmetri/colors.hpp"
namespace symmetri {

bool isSynchronous(const symmetri::DirectMutation&) { return true; }
symmetri::Token fire(const symmetri::DirectMutation&) {
return symmetri::Color::Success;
}

const std::string& Color::toString(symmetri::Token r) { return Color::map[r]; }

const Token& Color::registerToken(const std::string& s) {
Expand Down

0 comments on commit e8062df

Please sign in to comment.