NGen
Loading...
Searching...
No Matches
deserialization.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.2
18Interface of the BMI state restore protocol. Pairs with
19`NgenSerializationProtocol` (save); shares the `Record` value type
20defined in `include/utilities/serialization/record.hpp`.
21
22Storage is delegated to a `RecordBackend` acquired at `initialize()`
23time, optionally scoped by a caller-provided `id_subset` so that a
24run restoring only a known subset of features can give the backend
25a chance to build a smaller index.
26
27Each `run()` opens a short-lived `Reader` for the current
28`ctx.id` and asks the configured match mode (`latest`, by step, or
29by timestamp) for the corresponding record.
30*/
31#pragma once
32
33#include "utilities/serialization/record_backend.hpp"
34#include <memory>
35#include <nonstd/expected.hpp>
36#include <protocol.hpp>
37
38namespace models {
39namespace bmi {
40namespace protocols {
41using nonstd::expected;
42
44constexpr const char* const DESERIALIZATION_RESTORE_KEY = "restore";
46constexpr const char* const DESERIALIZATION_STEP_KEY = "step";
48constexpr const char* const DESERIALIZATION_STEP_LATEST = "latest";
54constexpr const char* const DESERIALIZATION_TIMESTAMP_KEY = "timestamp";
59constexpr const char* const DESERIALIZATION_ID_SUBSET_KEY = "id_subset";
60
100 public:
101 NgenDeserializationProtocol(const ModelPtr& model, const Properties& properties);
104
105 private:
106 auto run(const ModelPtr& model, const Context& ctx) const
107 -> expected<void, ProtocolError> override;
108 nsel_NODISCARD auto check_support(const ModelPtr& model)
109 -> expected<void, ProtocolError> override;
110
124 auto initialize(const ModelPtr& model, const Properties& properties)
125 -> expected<void, ProtocolError> override;
126
127 bool is_supported() const override final;
128
129 private:
130 // Match mode. When `by_timestamp` is true,
131 // `target_simulation_timestamp` is the sole matching criterion.
132 // Otherwise `step_latest` (if true) finds the highest step for
133 // the id, else `target_step` must match exactly.
134 bool by_timestamp = false;
136 bool step_latest = true;
137 int target_step = 0;
138 // Optional set of ids the caller wants indexed, parsed from
139 // config in initialize() into an
140 // `any_of(primary_prefix(...))` predicate. Registered with the
141 // backend as this caller's contribution to the index scope —
142 // records whose id fails this predicate are not indexed.
143 //
144 // This is the *backend-level* scope. The *per-Reader* scope
145 // used inside `run()` is a tighter `exact_id(ctx.id)`
146 // predicate constructed per-call so each Reader returns
147 // exactly the one feature's record.
148 //
149 // Empty / default-constructed means "no filter".
150 ::ngen::serialization::IdPredicate id_scope;
151 bool supported = false;
152 bool check;
154
155 // Storage backend, acquired in initialize() via a
156 // `RecordBackend`-typed factory.
157 std::shared_ptr<::ngen::serialization::RecordBackend> backend_;
158};
159
160} // namespace protocols
161} // namespace bmi
162} // namespace models
Definition protocol.hpp:110
auto run(const ModelPtr &model, const Context &ctx) const -> expected< void, ProtocolError > override
Run the BMI protocol against the given model.
Definition deserialization.cpp:69
bool by_timestamp
Definition deserialization.hpp:134
bool is_fatal
Definition deserialization.hpp:153
auto initialize(const ModelPtr &model, const Properties &properties) -> expected< void, ProtocolError > override
Parse top-level path and the restore sub-block.
Definition deserialization.cpp:194
bool supported
Definition deserialization.hpp:151
std::shared_ptr<::ngen::serialization::RecordBackend > backend_
Definition deserialization.hpp:157
int target_step
Definition deserialization.hpp:137
NgenDeserializationProtocol()
Definition deserialization.cpp:62
bool check
Definition deserialization.hpp:152
nsel_NODISCARD auto check_support(const ModelPtr &model) -> expected< void, ProtocolError > override
Check if the BMI protocol is supported by the model.
Definition deserialization.cpp:144
bool step_latest
Definition deserialization.hpp:136
bool is_supported() const override final
Whether the protocol is supported by the model.
Definition deserialization.cpp:348
::ngen::serialization::IdPredicate id_scope
Definition deserialization.hpp:150
int64_t target_simulation_timestamp
Definition deserialization.hpp:135
Definition deserialization.hpp:40
constexpr const char *const DESERIALIZATION_RESTORE_KEY
Restore-side sub-block key under the top-level serialization block.
Definition deserialization.hpp:44
std::shared_ptr< models::bmi::Bmi_Adapter > ModelPtr
Definition protocol.hpp:107
constexpr const char *const DESERIALIZATION_STEP_LATEST
Sentinel value for step meaning "latest step present for this id".
Definition deserialization.hpp:48
constexpr const char *const DESERIALIZATION_STEP_KEY
Restore-side "which step to load" key.
Definition deserialization.hpp:46
geojson::PropertyMap Properties
Definition protocol.hpp:108
constexpr const char *const DESERIALIZATION_ID_SUBSET_KEY
Optional restore-side list of ids to index.
Definition deserialization.hpp:59
constexpr const char *const DESERIALIZATION_TIMESTAMP_KEY
Restore-side "match by simulation timestamp" key.
Definition deserialization.hpp:54
Definition AbstractCLibBmiAdapter.hpp:6
Definition DomainLayer.hpp:13
STL namespace.
Definition protocol.hpp:100