Skip to content

Commit

Permalink
wip remove web (#24)
Browse files Browse the repository at this point in the history
* wip remove web

* fix bug of running same transition twice and discarding both from active transitions if only one succeeded

* run all possible transitions

* priority test added

* implemented priorities

* trigger simplification

* more cleanup firiing

* added test for nullopts

* new marking

* Feature/macos comp/beta (#26)

* wip

* transition trigger is added to active transition list

Co-authored-by: Thomas Horstink <thomas@mainblades.com>

* reset cmake flags

* add new marking reached

Co-authored-by: Thomas Horstink <thomas@mainblades.com>
  • Loading branch information
thorstink and Thomas Horstink authored Jan 6, 2023
1 parent e2d290c commit 9926abf
Show file tree
Hide file tree
Showing 26 changed files with 642 additions and 346 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ jobs:
run: |
sudo apt update
sudo apt install -y build-essential lcov gcovr ninja-build libeigen3-dev git libtool pkg-config libspdlog-dev
- name: seasocks
run: |
git clone https://github.com/mattgodbolt/seasocks.git
cd seasocks && mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Debug -DUNITTESTS=OFF -DSEASOCKS_SHARED=ON ..
sudo make install
- name: catch2
run: |
Expand Down
7 changes: 3 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-maybe-uninitialized -Wno-psabi -Wno-unused-parameter -Werror -Wall -Wextra -O2")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address -fno-omit-frame-pointer -Wno-maybe-uninitialized -Wno-psabi -Wno-unused-parameter -Werror -Wall -Wextra -O0")
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} -g -fsanitize=address -fno-omit-frame-pointer -Wno-uninitialized -Wno-psabi -Wno-unused-parameter -Werror -Wall -Wextra -O0")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=thread -fno-omit-frame-pointer -Wno-maybe-uninitialized -Wno-psabi -Wno-unused-parameter -Werror -Wall -Wextra -O0")

if(BUILD_TESTING)
Expand All @@ -26,5 +26,4 @@ add_subdirectory(symmetri)
if(BUILD_EXAMPLES)
add_subdirectory(examples/flight)
add_subdirectory(examples/hello_world)
endif()

endif()
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Clone the repository and make sure you also initialize the submodules.

```bash
mkdir build
cd 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
Expand All @@ -33,6 +33,18 @@ and look at `http://localhost:2222/` for a live view of the activity.
- single webiface server
- replace maps with vectors (?)

rangify firing once apple clang is ready...

```cpp
auto possible_transition_list2 =
model.tokens | std::ranges::views::filter([&](const auto &place) {
const auto ptr = std::lower_bound(
std::begin(model.reverse_loopup), std::end(model.reverse_loopup),place
[](const auto &u, const auto& place) { return u.first == place; });
return ptr != model.reverse_loopup.end() || ptr->second.empty();
});
```
https://www.youtube.com/watch?v=2KGkcGtGVM4
https://stlab.cc/tip/2017/12/23/small-object-optimizations.html
Expand Down
4 changes: 3 additions & 1 deletion examples/flight/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#an app
find_package(spdlog REQUIRED)

add_executable(${PROJECT_NAME}_flight flight.cc)
target_link_libraries(${PROJECT_NAME}_flight symmetri)
target_link_libraries(${PROJECT_NAME}_flight symmetri spdlog::spdlog)

#install
install(TARGETS ${PROJECT_NAME}_flight DESTINATION ${PROJECT_SOURCE_DIR}/install)
30 changes: 5 additions & 25 deletions examples/flight/flight.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "symmetri/retry.h"
#include "symmetri/symmetri.h"
#include "symmetri/ws_interface.h"

symmetri::Eventlog getNewEvents(const symmetri::Eventlog &el,
symmetri::clock_s::time_point t) {
Expand All @@ -24,7 +23,7 @@ std::function<void()> helloT(std::string s) {
};
};

int main(int argc, char *argv[]) {
int main(int, char *argv[]) {
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%f] [%^%l%$] [thread %t] %v");

auto pnml1 = std::string(argv[1]);
Expand All @@ -37,40 +36,21 @@ int main(int argc, char *argv[]) {
symmetri::Store s2 = {{"T0", helloT("T01")}, {"T1", helloT("T02")}};
auto snet = {pnml1, pnml2};

symmetri::Application subnet(snet, final_marking2, s2, "charon", pool);
symmetri::Application subnet(snet, final_marking2, s2, {}, "charon", pool);

symmetri::Store store = {
{"T0", subnet}, {"T1", helloT("T1")}, {"T2", helloT("T2")}};

symmetri::NetMarking final_marking = {{"P3", 5}};
auto net = {pnml1, pnml2, pnml3};
symmetri::Application bignet(net, final_marking, store, "pluto", pool);
std::vector<std::pair<symmetri::Transition, int8_t>> priority;
symmetri::Application bignet(net, final_marking, store, priority, "pluto",
pool);

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

// some thread to poll the net and send it away through a server
auto wt = std::thread([&bignet, &running] {
auto server = WsServer(2222, [&]() { bignet.togglePause(); });
auto previous_stamp = symmetri::clock_s::now();
do {
bignet.doMeData([&] {
auto [t, el, state_net, marking, at] =
bignet.get(); // not _thread safe_
auto new_events = getNewEvents(el, previous_stamp);
if (!new_events.empty()) {
server.sendNet(t, state_net, marking, at);
server.sendLog(new_events);
previous_stamp = t;
}
});
std::this_thread::sleep_for(std::chrono::milliseconds(500));
} while (running.load());
server.stop();
});

auto [el, result] = bignet(); // infinite loop
running.store(false);
wt.join();
for (const auto &[caseid, t, s, c, tid] : el) {
spdlog::info("{0}, {1}, {2}, {3}", caseid, t, printState(s),
c.time_since_epoch().count());
Expand Down
4 changes: 3 additions & 1 deletion examples/hello_world/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#an app
find_package(spdlog REQUIRED)

add_executable(${PROJECT_NAME}_hello_world main.cc)
target_link_libraries(${PROJECT_NAME}_hello_world symmetri)
target_link_libraries(${PROJECT_NAME}_hello_world symmetri spdlog::spdlog)

#install
install(TARGETS ${PROJECT_NAME}_hello_world DESTINATION ${PROJECT_SOURCE_DIR}/install)
32 changes: 7 additions & 25 deletions examples/hello_world/main.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// This is an example of how to use the petri-net library: In this example we
// have a net where transition t50 can be triggered by inputing the character
// '1' through std::cin (the keyboard). When t50 is triggered, the net becomes
// live. It's progress can be visualized through the webserver which is also
// launched. This example has no ' final marking ' (goal marking), so it' ll run
// live. This example has no ' final marking ' (goal marking), so it' ll run
// forever until the user hits ctrl-c.
#include <spdlog/spdlog.h>

#include <iostream>
#include <thread>

#include "symmetri/symmetri.h"
#include "symmetri/ws_interface.h"

// Before main, we define a bunch of functions that can be bound to
// transitions in the petri net.
Expand All @@ -32,7 +30,7 @@ symmetri::TransitionState helloResult() {

// The main is simply the body of a cpp program. It has to start somewhere, so
// that's here.
int main(int argc, char *argv[]) {
int main(int, char *argv[]) {
// Through argc and argv you can gather input arguments. E.g. when you launch
// this petri net application you execute something like
// "./Symmetri_hello_world ../nets/passive_n1.pnml ../nets/T50startP0.pnml"
Expand All @@ -58,7 +56,7 @@ int main(int argc, char *argv[]) {
// of threads it can use (maximum amount of stuff it can do in parallel) and a
// name so the net is easy to identifiy in a log.
symmetri::Application net({pnml_path_start, pnml_path_passive}, std::nullopt,
store, "CASE_X", pool);
store, {}, "CASE_X", pool);

// We use a simple boolean flag to terminate the threads once the net
// finishes. Without it, these threads would prevent the program from cleanly
Expand All @@ -67,7 +65,7 @@ int main(int argc, char *argv[]) {
// We create a little thread that listens to events that allow us to manually
// trigger transitions. E.g. collision avoidance or manual take-over
std::thread input_thread(
[&running, t50 = net.registerTransitionCallback<float>("t50")] {
[&running, t50 = net.registerTransitionCallback("t50")] {
char input_char;
do {
// cin is a way to listen to the keyboard in c++. It hangs until the
Expand All @@ -76,7 +74,7 @@ int main(int argc, char *argv[]) {
std::cin >> input_char;
switch (input_char) {
case '1': {
t50(input_char);
t50();
break;
}
default:
Expand All @@ -86,27 +84,11 @@ int main(int argc, char *argv[]) {
// becomes false.
});

// and we launch a thread in which we run a webserver. This is optional.
std::thread web_thread([&net, &running] {
auto server = WsServer(2222, [&]() { net.togglePause(); });
do {
net.doMeData([&] {
auto [t, el, state_net, marking, at] = net.get();
server.sendNet(t, state_net, marking, at);
});
std::this_thread::sleep_for(std::chrono::milliseconds(500));
} while (running.load()); // this exits the loop once the running-flag
// becomes false.
server.stop(); // and we have to sign the webserver it should terminate.
});

auto [el, result] =
net(); // This function blocks until either the net completes, deadlocks
// or user requests exit (ctrl-c)
running.store(
false); // We set this to false so the two threads that we launched (for
// the web-server and keyboard input get interrupted.)
web_thread.join();
running.store(false); // We set this to false so the thread that we launched
// gets interrupted.
input_thread.join();

// this simply prints the event log
Expand Down
8 changes: 3 additions & 5 deletions symmetri/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
find_package(Seasocks REQUIRED)
find_package(spdlog REQUIRED)

include(FetchContent)
Expand All @@ -17,7 +16,7 @@ if(NOT concurrentqueue_POPULATED)
add_subdirectory(${concurrentqueue_SOURCE_DIR} ${concurrentqueue_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

include_directories(include submodules ${SEASOCKS_INCLUDE_DIRS})
include_directories(include submodules)

#lib
add_library(${PROJECT_NAME} SHARED
Expand All @@ -26,12 +25,11 @@ add_library(${PROJECT_NAME} SHARED
model.cc
pnml_parser.cc
retry.cpp
ws_interface.cpp
types.cpp
submodules/tinyxml2/tinyxml2.cpp
)
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
target_link_libraries(${PROJECT_NAME} PUBLIC pthread Seasocks::seasocks PRIVATE concurrentqueue spdlog::spdlog)
target_link_libraries(${PROJECT_NAME} PUBLIC pthread PRIVATE concurrentqueue spdlog::spdlog)

include(GenerateExportHeader)
generate_export_header(${PROJECT_NAME})
Expand Down Expand Up @@ -97,5 +95,5 @@ set(ui_files
install(DIRECTORY ${ui_files} DESTINATION share)

if(BUILD_TESTING)
# add_subdirectory(tests)
add_subdirectory(tests)
endif()
1 change: 0 additions & 1 deletion symmetri/cmake/symmetriConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
include(CMakeFindDependencyMacro)

# Find dependencies
find_dependency(Seasocks REQUIRED)
find_dependency(spdlog REQUIRED)

# Our library dependencies (contains definitions for IMPORTED targets)
Expand Down
34 changes: 19 additions & 15 deletions symmetri/include/symmetri/symmetri.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace symmetri {

size_t calculateTrace(const Eventlog& event_log) noexcept;
size_t calculateTrace(const Eventlog &event_log) noexcept;
std::string printState(symmetri::TransitionState s) noexcept;

using Store = std::vector<std::pair<std::string, PolyAction>>;
Expand All @@ -29,30 +29,34 @@ struct Application {
void createApplication(
const symmetri::StateNet &net, const symmetri::NetMarking &m0,
const std::optional<symmetri::NetMarking> &final_marking,
const Store &store, const std::string &case_id,
const symmetri::StoppablePool &stp);
const Store &store,
const std::vector<std::pair<symmetri::Transition, int8_t>> &priority,
const std::string &case_id, const symmetri::StoppablePool &stp);

public:
Application(const std::set<std::string> &path_to_petri,
const std::optional<symmetri::NetMarking> &final_marking,
const Store &store, const std::string &case_id,
const symmetri::StoppablePool &stp);
Application(
const std::set<std::string> &path_to_petri,
const std::optional<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(const symmetri::StateNet &net, const symmetri::NetMarking &m0,
const std::optional<symmetri::NetMarking> &final_marking,
const Store &store, const std::string &case_id,
const symmetri::StoppablePool &stp);
Application(
const symmetri::StateNet &net, const symmetri::NetMarking &m0,
const std::optional<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);

template <typename T>
inline std::function<void(T)> registerTransitionCallback(
inline std::function<void()> registerTransitionCallback(
const std::string &transition) const {
return [transition, this](T) { p(transition); };
return [transition, this]() { p(transition); };
}

void doMeData(std::function<void()> f) const;

std::tuple<clock_s::time_point, symmetri::Eventlog, symmetri::StateNet,
symmetri::NetMarking, std::set<std::string>>
std::vector<symmetri::Place>, std::vector<std::string>>
get() const noexcept;

const symmetri::StateNet &getNet();
Expand Down
9 changes: 6 additions & 3 deletions symmetri/include/symmetri/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace symmetri {
using Place = std::string;
using Transition = std::string;
using clock_s = std::chrono::system_clock;
using clock_s = std::chrono::steady_clock;

enum class TransitionState { Started, Completed, Deadlock, UserExit, Error };

Expand All @@ -28,7 +28,7 @@ using StateNet =
using NetMarking = std::unordered_map<Place, uint16_t>;

template <typename T>
constexpr TransitionResult runTransition(const T& x) {
TransitionResult runTransition(const T& x) {
if constexpr (std::is_invocable_v<T>) {
if constexpr (std::is_same_v<TransitionState, decltype(x())>) {
return {{}, x()};
Expand All @@ -43,6 +43,9 @@ constexpr TransitionResult runTransition(const T& x) {
}
}

bool MarkingReached(const NetMarking& marking, const NetMarking& final_marking);
bool MarkingEquality(const std::vector<Place>& m1,
const std::vector<Place>& m2);
bool MarkingReached(const std::vector<Place>& marking,
const std::vector<Place>& final_marking);
bool StateNetEquality(const StateNet& net1, const StateNet& net2);
} // namespace symmetri
26 changes: 0 additions & 26 deletions symmetri/include/symmetri/ws_interface.h

This file was deleted.

Loading

0 comments on commit 9926abf

Please sign in to comment.