Skip to content

Commit

Permalink
update Agent communication protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
FyS committed Nov 17, 2023
1 parent 9add945 commit e5469a6
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 113 deletions.
8 changes: 5 additions & 3 deletions fabko/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ target_sources(core
PUBLIC
core/blackboard.cpp
core/blackboard.hh
core/boardcom.cpp
core/boardcom.hh
core/acl.hh
core/protocol/fap_request.hh
core/protocol/boardcom.cpp
core/protocol/boardcom.hh
core/common/logging.cpp
core/common/logging.hh
core/common/exception.hh
Expand Down Expand Up @@ -49,13 +50,14 @@ add_library(agent OBJECT)
target_sources(agent
PRIVATE
agent/agent.cpp
agent/action.hh
PUBLIC
agent/agent.hh)
target_include_directories(agent PUBLIC
agent
)
target_link_libraries(agent PRIVATE fabko::core)
target_compile_definitions(agent PUBLIC cxx_std_23)
target_link_libraries(agent PRIVATE fabko::core)
add_library(fabko::agent ALIAS agent)


Expand Down
41 changes: 41 additions & 0 deletions fabko/agent/action.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Dual Licensing Either :
// - AGPL
// or
// - Subscription license for commercial usage (without requirement of licensing propagation).
// please contact [email protected] for additional information about this subscription commercial licensing.
//
// Created by FyS on 13/11/2023. License 2022-2023
//
// In the case no license has been purchased for the use (modification or distribution in any way) of the software stack
// the APGL license is applying.
//

#pragma once

#include <variant>

#include <protocol/fap_request.hh>

namespace fabko {

enum class action_source {
automatic,
manual
};

/**
* Represent an action that an agent is doing / going to do.
* This is
*/
struct agent_action {

unsigned action_id{};

action_source source{action_source::automatic};

agent_protocol::request initial_request;

agent_protocol::status status;
};

} // namespace fabko
19 changes: 19 additions & 0 deletions fabko/agent/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,26 @@
// In the case no license has been purchased for the use (modification or distribution in any way) of the software stack
// the APGL license is applying.
//

#include <array>

#include "agent.hh"

#include "protocol/fap_request.hh"

namespace fabko {

constexpr std::size_t MAX_QUEUED_SIZE = 10;

//! implementation details of the agent
struct impl {


/**
* list of actions that the agent is going to do. The list is ordered by priority
*/
std::array<agent_protocol::request, MAX_QUEUED_SIZE> action_list; // implementation as a ring buffer may prove beneficial

};

}
28 changes: 28 additions & 0 deletions fabko/agent/agent.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,37 @@

#pragma once

#include <concepts>
#include <memory>
#include <expected>

namespace fabko {

template<typename action>
class agent {
struct impl;

public:
~agent() = default;

// extract requirement of the type on a static assertion as agent symbol is not available to the compiler at the
// template declaration level.
static_assert(
std::is_invocable_v<action, agent&>,
"The action function provided to the agent has to be with the following signature: void(agent&)");


[[nodiscard]] std::expected<>

private:
void execute_action() {
_callback_on_action(*this);
}

action _callback_on_action;

std::unique_ptr<impl> _pimpl;

};

} // namespace fabko
2 changes: 1 addition & 1 deletion fabko/core/acl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ constexpr bool is_error_handling(message_type type) {
}

struct acc {
unsigned identifier;
unsigned identifier{};

// communication protocol : which can be used in other to define different ways to communicate between the agents
std::string protocol;
Expand Down
14 changes: 7 additions & 7 deletions fabko/core/blackboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ namespace fabko {

struct blackboard::blackboard_impl {

explicit blackboard_impl(com::c_board_com auto&& bc, blackboard_data data)
explicit blackboard_impl(agent_protocol::c_board_com auto&& bc, blackboard_data data)
: bc(std::forward<decltype(bc)>(bc)), data(std::move(data)) {}

blackboard_data instantiate_black_board(const std::string& request) {
data.id = std::visit(
std::visit(
overloaded{[&request](auto& b) -> std::string { return b.instantiate_black_board(request); }}, bc);
return data;
}

com::board_protocol bc;
agent_protocol::board_protocol bc;
blackboard_data data;
};

blackboard::~blackboard() = default;

template<com::c_board_com BoardCommunication>
blackboard::blackboard(BoardCommunication&& bc, com::request initial_request)
template<agent_protocol::c_board_com BoardCommunication>
blackboard::blackboard(BoardCommunication&& bc, agent_protocol::request initial_request)
: _pimpl(std::forward<BoardCommunication>(bc), {.initial_request = std::move(initial_request)}) {
}

com::propositions blackboard::request_propositions(const com::request& request) {
agent_protocol::propositions blackboard::request_propositions(const agent_protocol::request& request) {
return {};
}

com::decision_status blackboard::submit_decision(const std::string& decision) {
agent_protocol::decision_status blackboard::submit_decision(const std::string& decision) {
return {};
}

Expand Down
16 changes: 8 additions & 8 deletions fabko/core/blackboard.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#include <string>
#include <vector>

#include "boardcom.hh"
#include "protocol/boardcom.hh"
#include "protocol/fap_request.hh"

namespace fabko {

struct blackboard_data {
std::string id{};
com::request initial_request;
com::propositions propositions{};
agent_protocol::request initial_request;
agent_protocol::propositions propositions{};
};

/**
Expand All @@ -40,11 +40,11 @@ class blackboard {
public:
~blackboard();

template<com::c_board_com BoardCommunication>
explicit blackboard(BoardCommunication&& bc, com::request initial_request);
template<agent_protocol::c_board_com BoardCommunication>
explicit blackboard(BoardCommunication&& bc, agent_protocol::request initial_request);

[[nodiscard]] com::propositions request_propositions(const com::request& request);
[[nodiscard]] com::decision_status submit_decision(const std::string& decision);
[[nodiscard]] agent_protocol::propositions request_propositions(const agent_protocol::request& request);
[[nodiscard]] agent_protocol::decision_status submit_decision(const std::string& decision);

private:
std::unique_ptr<blackboard_impl> _pimpl;
Expand Down
4 changes: 2 additions & 2 deletions fabko/core/common/string_utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
namespace fabko {

template<typename Handler>
void split_string(const std::string& input, const std::string& separator, Handler&& handler, int limitation = -1) {
void split_string(std::string_view input, std::string_view separator, Handler&& handler, int limitation = -1) {

std::size_t pos_start = 0;
std::size_t pos_end;
Expand All @@ -37,7 +37,7 @@ void split_string(const std::string& input, const std::string& separator, Handle
}

template<typename Handler>
void split_string(const std::string& input, const std::vector<std::string>& separators, Handler&& handler, int limitation = -1) {
void split_string(std::string_view input, const std::vector<std::string_view>& separators, Handler&& handler, int limitation = -1) {

std::size_t pos_start = 0;
std::size_t pos_end;
Expand Down
33 changes: 33 additions & 0 deletions fabko/core/protocol/boardcom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Dual Licensing Either :
// - AGPL
// or
// - Subscription license for commercial usage (without requirement of licensing propagation).
// please contact [email protected] for additional information about this subscription commercial licensing.
//
// Created by FyS on 23/04/23. License 2022-2023
//
// In the case no license has been purchased for the use (modification or distribution in any way) of the software stack
// the APGL license is applying.
//

#include "boardcom.hh"

namespace fabko {

std::string agent_protocol::p2p::instantiate_black_board(const std::string&) {
return {};
}

std::string agent_protocol::online::instantiate_black_board(const std::string&) {
return {};
}

std::future<agent_protocol::propositions> agent_protocol::online::request_propositions(const std::string&) {
return {};
}

agent_protocol::decision_status agent_protocol::online::commit_decision(const std::string&) {
return agent_protocol::decision_status::RETRY;
}

} // namespace fabko
70 changes: 70 additions & 0 deletions fabko/core/protocol/boardcom.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Dual Licensing Either :
// - AGPL
// or
// - Subscription license for commercial usage (without requirement of licensing propagation).
// please contact [email protected] for additional information about this subscription commercial licensing.
//
// Created by FyS on 23/04/23. License 2022-2023
//
// In the case no license has been purchased for the use (modification or distribution in any way) of the software stack
// the APGL license is applying.
//

#pragma once

#include <concepts>
#include <future>
#include <optional>
#include <string>
#include <variant>
#include <vector>

#include "fap_request.hh"

namespace fabko::agent_protocol {

struct proposition {
std::string id;
};

struct propositions {
std::optional<std::vector<proposition>> props;
status status{status::awaiting};
};

enum class decision_status {
SUCCESS,
RETRY,
CANCELLED,
};


/**
*
* @tparam T
*/
template<typename T>
concept c_board_com =
requires(T a) {
{ T::make_board(request{}) } -> std::convertible_to<std::string>;
{ a.propositions(std::string{}) } -> std::convertible_to<std::future<propositions>>;
{ a.commit_decision(std::string{}) } -> std::convertible_to<decision_status>;
} && std::movable<T>;

class p2p {
public:
static std::string instantiate_black_board(const std::string&);
std::future<propositions> request_propositions(const std::string&);
decision_status commit_decision(const std::string&);
};

class online {
public:
static std::string instantiate_black_board(const std::string&);
std::future<propositions> request_propositions(const std::string&);
decision_status commit_decision(const std::string&);
};

using board_protocol = std::variant<p2p, online>;

} // namespace fabko::com
Loading

0 comments on commit e5469a6

Please sign in to comment.