Skip to content

Commit

Permalink
Feature/single run (#29)
Browse files Browse the repository at this point in the history
* refactor and added option to run 1 transition or a specific transition.

* expose api

* little typo

* thread sanitized

* improve flaky test

* more ci

* less deps

* catch from apt

* catch from source

* remove more web

* little update to readme

* add mermaid drawing

* fix bug and bump version

---------

Co-authored-by: Thomas Horstink <thomas@mainblades.com>
  • Loading branch information
thorstink and Thomas Horstink authored Feb 6, 2023
1 parent 46c7853 commit 1d01f8a
Show file tree
Hide file tree
Showing 23 changed files with 441 additions and 369 deletions.
16 changes: 12 additions & 4 deletions .github/workflows/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: apt dependencies
run: |
sudo apt update
sudo apt install -y build-essential lcov gcovr ninja-build libeigen3-dev git libtool pkg-config libspdlog-dev
sudo apt install -y ninja-build libeigen3-dev libspdlog-dev
- name: catch2
run: |
Expand All @@ -25,11 +25,19 @@ jobs:
git submodule update --init --recursive
mkdir build
cd build
bash -c 'cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON -DBUILD_EXAMPLES=ON -DCODE_COVERAGE=ON ..'
bash -c 'cmake -GNinja -DCMAKE_BUILD_TYPE=Release -DBUILD_EXAMPLES=ON ..'
ninja
- name: "test and coverage"
- name: "tests with ASAN"
run: |
cd build
bash -c 'cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON -DASAN_BUILD=ON -DTSAN_BUILD=OFF ..'
ninja
ctest -VV
- name: "tests with TSAN"
run: |
cd build
bash -c 'cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON -DASAN_BUILD=OFF -DTSAN_BUILD=ON ..'
ninja
ctest -VV
# gcovr -r ../ .
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-uninitialized -Wno-psabi -Wno-unused-parameter -Werror -Wall -Wextra -O2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-uninitialized -Wno-psabi -Wno-unused-parameter -Werror -Wall -Wextra -O3")

if(BUILD_TESTING)
enable_testing()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address -fno-omit-frame-pointer -O0")
if(ASAN_BUILD AND NOT TSAN_BUILD)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address,undefined -fno-omit-frame-pointer -O0")
elseif(TSAN_BUILD AND NOT ASAN_BUILD)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=thread -fno-omit-frame-pointer -O0")
endif()
endif()

# the lib
Expand Down
45 changes: 37 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
# Petri-net-parser
# Symmetri

Parses a Petri net and runs it
A C++-17 library that takes a Petri net and turns it into a program. This is done by mapping *[transitions](https://en.wikipedia.org/wiki/Petri_net#Petri_net_basics)* to *functions* and calling the functions for which their transition counterpart is *[fireable](https://en.wikipedia.org/wiki/Petri_net#Execution_semantics)*. Petri nets are a graphical language that naturally can model concurrent and distributed systems [wikipedia](https://en.wikipedia.org/wiki/Petri_net#Petri_net_basics).

```mermaid
graph LR
classDef inactive fill:#ffc0;
classDef active fill:#0050cb;
classDef fake_tok_place color:#ffc0;
classDef place color:#00c0cb;
B-->D[foo]:::inactive;
Z(("#9679;")):::place-->A
A:::inactive-->B(("#9679;" )):::fake_tok_place;
A[bar]:::active-->C(("#9679;")):::fake_tok_place;
C-->D;
D-->Z
D-->B;
```

```cpp
using namespace symmetri;
StateNet net = {{"foo", {{"B", "C"}, {"Z"}}},
{"bar", {{"Z"}, {"B", "C"}}}};
Store store = {{"foo", &foo}, {"bar", &bar}};
std::vector<std::pair<symmetri::Transition, int8_t>> priority = {};
NetMarking m0 = {{"Z", 1}, {"B", 0}, {"C", 0}};
StoppablePool stp(1);
symmetri::Application app(net, m0, {}, store, priority,
"test_net_without_end", stp);
auto [ev, res] = app(); // run untill done.
stp.stop();
```
## Build
Expand All @@ -9,17 +39,16 @@ Clone the repository and make sure you also initialize the submodules.
```bash
mkdir build
cd build
cmake .. -DBUILD_TESTING=0 -DBUILD_EXAMPLES=0 -DCMAKE_INSTALL_PREFIX:PATH=../install
cmake .. -DBUILD_TESTING=1 -DBUILD_EXAMPLES=0 -DCMAKE_INSTALL_PREFIX:PATH=../install
cmake .. -DBUILD_TESTING=1 -DBUILD_EXAMPLES=1 -DCMAKE_INSTALL_PREFIX:PATH=../install
make install
cmake .. -DBUILD_TESTING=0 -DBUILD_EXAMPLES=0
cmake .. -DBUILD_TESTING=1 -DBUILD_EXAMPLES=0
cmake .. -DBUILD_TESTING=1 -DBUILD_EXAMPLES=1
```

## Run

```
cd ../install
./Symmetri_flight ../nets/PT1.pnml ../nets/PT2.pnml ../nets/PT3.pnml
cd ../build
./examples/flight/symmetri_flight ../nets/PT1.pnml ../nets/PT2.pnml ../nets/PT3.pnml
# or
./Symmetri_hello_world ../nets/passive_n1.pnml ../nets/T50startP0.pnml
```
Expand Down
5 changes: 1 addition & 4 deletions examples/flight/flight.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ int main(int, char *argv[]) {
symmetri::Application bignet(net, final_marking, store, priority, "pluto",
pool);

std::atomic<bool> running(true);

auto [el, result] = bignet(); // infinite loop
running.store(false);
for (const auto &[caseid, t, s, c, tid] : el) {
spdlog::info("{0}, {1}, {2}, {3}", caseid, t, printState(s),
c.time_since_epoch().count());
}

pool.stop();
spdlog::info("Result of this net: {0}", printState(result));

return result == symmetri::TransitionState::Completed ? 0 : -1;
Expand Down
4 changes: 2 additions & 2 deletions package.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0"?>
<package format="2">
<name>symmetri</name>
<version>0.4.0</version>
<description>A C++ library for interfacing with petri nets</description>
<version>0.5.0</version>
<description>A C++ library for executing Petri nets as programs</description>
<maintainer email="thomas@mainblades.com">Thomas Horstink</maintainer>
<author>Thomas Horstink</author>
<license>Mainblades internal</license>
Expand Down
2 changes: 2 additions & 0 deletions symmetri/.clocignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
includes
submodules
tests/assets
small_vector.hpp
6 changes: 0 additions & 6 deletions symmetri/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ install(
NAMESPACE ${PROJECT_NAME}::
)

set(ui_files
${CMAKE_CURRENT_SOURCE_DIR}/web
)

install(DIRECTORY ${ui_files} DESTINATION share)

if(BUILD_TESTING)
add_subdirectory(tests)
endif()
3 changes: 2 additions & 1 deletion symmetri/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void StoppablePool::stop() {
if (t.joinable()) {
t.join();
}
}}
}
}

} // namespace symmetri
13 changes: 7 additions & 6 deletions symmetri/include/symmetri/symmetri.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ using Store = std::map<std::string, PolyAction>;
struct Impl;

/**
* @brief The Application class executes the petri net, by using the
* @brief The Application class executes the Petri net, by using the
*
*/
class Application {
private:
mutable std::shared_ptr<Impl> impl;
std::function<void(const std::string &t)> p;
std::function<void(const clock_s::time_point &t)> publish;
void createApplication(
const symmetri::StateNet &net, const symmetri::NetMarking &m0,
const symmetri::NetMarking &final_marking, const Store &store,
Expand All @@ -76,13 +75,15 @@ class Application {
const symmetri::NetMarking &final_marking, const Store &store,
const std::vector<std::pair<symmetri::Transition, int8_t>> &priority,
const std::string &case_id, const symmetri::StoppablePool &stp);

~Application();
TransitionResult operator()() const noexcept;
symmetri::Eventlog getEvenLog() const noexcept;
std::vector<symmetri::Place> getMarking() const noexcept;
std::vector<std::string> getActiveTransitions() const noexcept;
std::function<void()> registerTransitionCallback(
const std::string &transition) const noexcept;
bool tryRunTransition(const std::string &s) const noexcept;
symmetri::Eventlog getEventLog() const noexcept;
std::pair<std::vector<Transition>, std::vector<Place>> getState()
const noexcept;
std::vector<std::string> getFireableTransitions() const noexcept;
void togglePause() const noexcept;
void exitEarly() const noexcept;
};
Expand Down
Loading

0 comments on commit 1d01f8a

Please sign in to comment.