NGen
Loading...
Searching...
No Matches
protocol.hpp
1/*
2Author: Nels Frazier
3Copyright (C) 2025 Lynker
4------------------------------------------------------------------------
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16------------------------------------------------------------------------
17Version 0.3
18Remove "supported" member variable and added is_supported() method to protocol interface
19
20Version 0.2
21Enumerate protocol error types and add ProtocolError exception class
22Implement error handling via expected<T, ProtocolError> and error_or_warning
23Removed model member and required model reference in run(), check_support(), and initialize()
24Minor refactoring and style changes
25
26Version 0.1
27Virtual interface for BMI protocols
28*/
29
30#pragma once
31
32#include "Bmi_Adapter.hpp"
33#include "JSONProperty.hpp"
34#include <nonstd/expected.hpp>
35
36namespace models{ namespace bmi{ namespace protocols{
37using nonstd::expected;
38using nonstd::make_unexpected;
39
47
48class ProtocolError: public std::exception {
49 public:
50 ProtocolError () = delete;
51 ProtocolError(Error err, const std::string& message="") : err(std::move(err)), message(std::move(message)) {}
52 ProtocolError(const ProtocolError& other) = default;
53 ProtocolError(ProtocolError&& other) noexcept = default;
54 ProtocolError& operator=(const ProtocolError& other) = default;
55 ProtocolError& operator=(ProtocolError&& other) noexcept = default;
56 ~ProtocolError() = default;
57
58 auto to_string() const -> std::string {
59 switch (err) {
60 case Error::UNITIALIZED_MODEL: return "Error(Uninitialized Model)::" + message;
61 case Error::UNSUPPORTED_PROTOCOL: return "Warning(Unsupported Protocol)::" + message;
62 case Error::INTEGRATION_ERROR: return "Error(Integration)::" + message;
63 case Error::PROTOCOL_ERROR: return "Error(Protocol)::" + message;
64 case Error::PROTOCOL_WARNING: return "Warning(Protocol)::" + message;
65 default: return "Unknown Error: " + message;
66 }
67 }
68
69 auto error_code() const -> const Error& { return err; }
70 auto get_message() const -> const std::string& { return message; }
71
72 char const *what() const noexcept override {
74 return message.c_str();
75 }
76
77 private:
79 mutable std::string message;
80};
81
82struct Context{
84 const int total_steps;
85 const std::string& timestamp;
86 const std::string& id;
87};
88
89using ModelPtr = std::shared_ptr<models::bmi::Bmi_Adapter>;
91
98 public:
99
100 virtual ~NgenBmiProtocol() = default;
101
102 protected:
112 static auto error_or_warning(const ProtocolError& err) -> expected<void, ProtocolError> {
113 // Log warnings, but throw errors
114 switch(err.error_code()){
116 throw err;
117 break;
122 std::cerr << err.to_string() << std::endl;
123 return make_unexpected<ProtocolError>( ProtocolError(std::move(err) ) );
124 default:
125 throw err;
126 }
127 assert (false && "Unreachable code reached in error_or_warning");
128 }
129
146 nsel_NODISCARD virtual auto run(const ModelPtr& model, const Context& ctx) const -> expected<void, ProtocolError> = 0;
147
161 nsel_NODISCARD virtual expected<void, ProtocolError> check_support(const ModelPtr& model) = 0;
162
179 virtual auto initialize(const ModelPtr& model, const Properties& properties) -> expected<void, ProtocolError> = 0;
180
185 virtual bool is_supported() const = 0;
186
196 friend class NgenBmiProtocols;
197};
198
199}}}
Definition protocol.hpp:92
virtual bool is_supported() const =0
Whether the protocol is supported by the model.
virtual nsel_NODISCARD expected< void, ProtocolError > check_support(const ModelPtr &model)=0
Check if the BMI protocol is supported by the model.
virtual auto initialize(const ModelPtr &model, const Properties &properties) -> expected< void, ProtocolError >=0
Initialize the BMI protocol from a set of key/value properties.
virtual ~NgenBmiProtocol()=default
Abstract interface for a generic BMI protocol.
static auto error_or_warning(const ProtocolError &err) -> expected< void, ProtocolError >
Handle a ProtocolError by either throwing it or logging it as a warning.
Definition protocol.hpp:112
virtual nsel_NODISCARD auto run(const ModelPtr &model, const Context &ctx) const -> expected< void, ProtocolError >=0
Run the BMI protocol against the given model.
Definition protocols.hpp:51
Definition protocol.hpp:48
std::string message
Definition protocol.hpp:79
Error err
Definition protocol.hpp:78
char const * what() const noexcept override
Definition protocol.hpp:72
auto error_code() const -> const Error &
Definition protocol.hpp:69
ProtocolError & operator=(ProtocolError &&other) noexcept=default
ProtocolError(Error err, const std::string &message="")
Definition protocol.hpp:51
ProtocolError(const ProtocolError &other)=default
ProtocolError & operator=(const ProtocolError &other)=default
auto get_message() const -> const std::string &
Definition protocol.hpp:70
ProtocolError(ProtocolError &&other) noexcept=default
auto to_string() const -> std::string
Definition protocol.hpp:58
Definition bmi.hpp:16
std::map< std::string, JSONProperty > PropertyMap
Shorthand for a mapping between strings and properties.
Definition JSONProperty.hpp:21
std::shared_ptr< models::bmi::Bmi_Adapter > ModelPtr
Definition protocol.hpp:89
geojson::PropertyMap Properties
Definition protocol.hpp:90
Error
Definition protocol.hpp:40
Definition AbstractCLibBmiAdapter.hpp:6
STL namespace.
Definition protocol.hpp:82
const std::string & id
Definition protocol.hpp:86
const std::string & timestamp
Definition protocol.hpp:85
const int total_steps
Definition protocol.hpp:84
const int current_time_step
Definition protocol.hpp:83