OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
GeoPackageOutputPlugin.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2//
3// Copyright 2026 Caleb Buahin
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
26
27#ifndef OPENSWMM_GEOPACKAGE_OUTPUT_PLUGIN_HPP
28#define OPENSWMM_GEOPACKAGE_OUTPUT_PLUGIN_HPP
29
30#include "GpkgUtils.hpp"
32#include <string>
33#include <unordered_map>
34#include <vector>
35
36namespace openswmm::gpkg {
37
46public:
51
56 PluginState state() const noexcept override { return state_; }
57
70 int initialize(const std::vector<std::string>& init_args,
71 const IPluginComponentInfo* info) override;
72
79 int validate(const SimulationContext& ctx) override;
93 int prepare(const SimulationContext& ctx) override;
106 int update(const SimulationSnapshot& snapshot) override;
117 int finalize(const SimulationContext& ctx) override;
118
126 const char* last_error_message() const noexcept override { return error_msg_.c_str(); }
127
128private:
130 std::string error_msg_;
131 std::string db_path_;
132 std::string simulation_id_;
133 DbPtr db_;
134 StmtPtr insert_stmt_;
135
136 // Cached variable IDs
137 std::unordered_map<std::string, int> variable_ids_;
138
139 // Batch buffer
140 struct ResultRow {
141 std::string object_type;
142 std::string object_id;
143 int variable_id;
144 double elapsed_time;
145 double value;
146 };
147 std::vector<ResultRow> buffer_;
148 static constexpr size_t FLUSH_THRESHOLD = 5000;
149
157 void flush_buffer();
167 int lookup_variable(const std::string& name, const std::string& obj_type);
168
182 int register_species_variables(const SimulationContext& ctx);
183
184 // Per-timestep results arrive pre-converted to display units (engine
185 // boundary), so no plugin-side unit-conversion factors are required.
186
187 // Per-object report flags, captured during prepare() from SimulationContext
188 std::vector<char> subcatch_rpt_flag_;
189 std::vector<char> node_rpt_flag_;
190 std::vector<char> link_rpt_flag_;
191};
192
193} // namespace openswmm::gpkg
194
195#endif // OPENSWMM_GEOPACKAGE_OUTPUT_PLUGIN_HPP
RAII wrappers and utilities for SQLite operations in GeoPackage I/O.
Interface for output-writing plugins.
Interface for output-writing plugins.
Definition IOutputPlugin.hpp:77
Describes a plugin component: metadata, capabilities, and factory methods.
Definition IPluginComponentInfo.hpp:201
GeoPackageOutputPlugin()=default
Create an unloaded output adapter with no open database.
int update(const SimulationSnapshot &snapshot) override
Copy a snapshot's report-selected values into buffered database rows.
Definition GeoPackageOutputPlugin.cpp:222
int initialize(const std::vector< std::string > &init_args, const IPluginComponentInfo *info) override
Copy output adapter configuration without opening the database.
Definition GeoPackageOutputPlugin.cpp:137
int prepare(const SimulationContext &ctx) override
Open/create the results database, write model metadata and prepare result insertion.
Definition GeoPackageOutputPlugin.cpp:154
int validate(const SimulationContext &ctx) override
Mark the adapter validated without inspecting the model.
Definition GeoPackageOutputPlugin.cpp:149
int finalize(const SimulationContext &ctx) override
Flush buffered rows, mark the run completed and release database resources.
Definition GeoPackageOutputPlugin.cpp:469
PluginState state() const noexcept override
Read the stored adapter lifecycle marker.
Definition GeoPackageOutputPlugin.hpp:56
const char * last_error_message() const noexcept override
Borrow the most recently stored failure diagnostic.
Definition GeoPackageOutputPlugin.hpp:126
PluginState
Plugin lifecycle states.
Definition PluginState.hpp:86
@ UNLOADED
Library not yet loaded (or was closed).
Definition PluginState.hpp:87
Definition ExternalContentReader.cpp:50
std::unique_ptr< sqlite3_stmt, StmtDeleter > StmtPtr
Definition GpkgUtils.hpp:156
std::unique_ptr< sqlite3, DbDeleter > DbPtr
Definition GpkgUtils.hpp:87
Central, reentrant simulation context.
Definition SimulationContext.hpp:390
Complete simulation state snapshot at one output time step.
Definition SimulationSnapshot.hpp:123
Central, reentrant simulation context.
Definition SimulationContext.hpp:390