FinanceSim 0.1.0
Financial Simulation Library
Loading...
Searching...
No Matches
bindings.cpp
Go to the documentation of this file.
1#include <pybind11/pybind11.h>
2#include <pybind11/stl.h>
3#include <pybind11/functional.h>
4
5#include "core/time.hpp"
6#include "core/event.hpp"
7#include "core/event_bus.hpp"
8#include "core/model.hpp"
9#include "core/log_level.hpp"
10#include "core/log_writer.hpp"
11#include "core/logger.hpp"
20
21namespace py = pybind11;
22using namespace financesim;
23
24PYBIND11_MODULE(financesim_cpp, m) {
25 m.doc() = "Financial simulation C++ models";
26
27 // Time types
28 py::enum_<ExecutionTiming>(m, "ExecutionTiming")
29 .value("StartOfPeriod", ExecutionTiming::StartOfPeriod)
30 .value("EndOfPeriod", ExecutionTiming::EndOfPeriod);
31
32 py::class_<Schedule>(m, "Schedule")
33 .def(py::init<>())
34 .def_readwrite("start_time", &Schedule::start_time)
35 .def_readwrite("stop_time", &Schedule::stop_time)
36 .def_readwrite("rate", &Schedule::rate)
37 .def_readwrite("timing", &Schedule::timing);
38
39 // Event base class
40 py::class_<Event, std::shared_ptr<Event>>(m, "Event")
41 .def("type_name", &Event::type_name)
42 .def("timestamp", &Event::timestamp)
43 .def("source_id", &Event::source_id);
44
45 // Typed events
46 py::class_<IncomeEvent, Event, std::shared_ptr<IncomeEvent>>(m, "IncomeEvent")
47 .def(py::init<SimTime, std::string, double, std::string, std::string>(),
48 py::arg("timestamp"), py::arg("source_id"), py::arg("amount"),
49 py::arg("category"), py::arg("target_account") = "")
50 .def("amount", &IncomeEvent::amount)
51 .def("category", &IncomeEvent::category)
52 .def("target_account", &IncomeEvent::target_account);
53
54 py::class_<ExpenseEvent, Event, std::shared_ptr<ExpenseEvent>>(m, "ExpenseEvent")
55 .def(py::init<SimTime, std::string, double, std::string, std::string>(),
56 py::arg("timestamp"), py::arg("source_id"), py::arg("amount"),
57 py::arg("category"), py::arg("target_account") = "")
58 .def("amount", &ExpenseEvent::amount)
59 .def("category", &ExpenseEvent::category)
60 .def("target_account", &ExpenseEvent::target_account);
61
62 py::class_<AssetEvent, Event, std::shared_ptr<AssetEvent>>(m, "AssetEvent")
63 .def(py::init<SimTime, std::string, std::string, double, double>())
64 .def("asset_id", &AssetEvent::asset_id)
65 .def("value", &AssetEvent::value)
66 .def("delta", &AssetEvent::delta);
67
68 py::class_<LiabilityEvent, Event, std::shared_ptr<LiabilityEvent>>(m, "LiabilityEvent")
69 .def(py::init<SimTime, std::string, std::string, double, double>())
70 .def("liability_id", &LiabilityEvent::liability_id)
71 .def("value", &LiabilityEvent::value)
72 .def("delta", &LiabilityEvent::delta);
73
74 py::class_<AccountEvent, Event, std::shared_ptr<AccountEvent>>(m, "AccountEvent")
75 .def(py::init<SimTime, std::string, std::string, double, double, std::string>(),
76 py::arg("timestamp"), py::arg("source_id"), py::arg("account_id"),
77 py::arg("balance"), py::arg("delta"), py::arg("reason"))
78 .def("account_id", &AccountEvent::account_id)
79 .def("balance", &AccountEvent::balance)
80 .def("delta", &AccountEvent::delta)
81 .def("reason", &AccountEvent::reason);
82
83 py::class_<TransferEvent, Event, std::shared_ptr<TransferEvent>>(m, "TransferEvent")
84 .def(py::init<SimTime, std::string, std::string, std::string, double, std::string>(),
85 py::arg("timestamp"), py::arg("source_id"), py::arg("from_account"),
86 py::arg("to_account"), py::arg("amount"), py::arg("reason"))
87 .def("from_account", &TransferEvent::from_account)
88 .def("to_account", &TransferEvent::to_account)
89 .def("amount", &TransferEvent::amount)
90 .def("reason", &TransferEvent::reason);
91
92 // Event bus
93 py::class_<EventBus>(m, "EventBus")
94 .def(py::init<>())
95 .def("subscribe_all", &EventBus::subscribe_all)
96 .def("unsubscribe", &EventBus::unsubscribe)
97 .def("event_log", &EventBus::event_log, py::return_value_policy::reference)
98 .def("clear_log", &EventBus::clear_log)
99 .def("reset", &EventBus::reset);
100
101 // Model base class
102 py::class_<Model, std::shared_ptr<Model>>(m, "Model")
103 .def("id", &Model::id)
104 .def("name", &Model::name)
105 .def("schedule", &Model::schedule, py::return_value_policy::reference)
106 .def("initialize", &Model::initialize)
107 .def("update", &Model::update)
108 .def("finalize", &Model::finalize)
109 .def("reset", &Model::reset);
110
111 // Base model classes
112 py::class_<IncomeBase, Model, std::shared_ptr<IncomeBase>>(m, "IncomeBase")
113 .def(py::init<std::string, std::string, Schedule>());
114
115 py::class_<CareerJob, IncomeBase, std::shared_ptr<CareerJob>>(m, "CareerJob")
116 .def(py::init<std::string, std::string, double, SimTime>(),
117 py::arg("id"), py::arg("name"), py::arg("annual_salary"), py::arg("start_day") = 0.0)
118 .def("annual_salary", &CareerJob::annual_salary)
119 .def("payment_amount", &CareerJob::payment_amount);
120
121 py::class_<ExpensesBase, Model, std::shared_ptr<ExpensesBase>>(m, "ExpensesBase")
122 .def(py::init<std::string, std::string, Schedule>());
123
124 py::class_<AssetsBase, Model, std::shared_ptr<AssetsBase>>(m, "AssetsBase")
125 .def(py::init<std::string, std::string, Schedule>());
126
127 py::class_<LiabilitiesBase, Model, std::shared_ptr<LiabilitiesBase>>(m, "LiabilitiesBase")
128 .def(py::init<std::string, std::string, Schedule>());
129
130 // Logging framework
131 py::enum_<LogLevel>(m, "LogLevel")
132 .value("DEBUG", LogLevel::DEBUG)
133 .value("INFO", LogLevel::INFO)
134 .value("WARN", LogLevel::WARN)
135 .value("ERROR", LogLevel::ERROR);
136
137 py::class_<LogWriter, LogWriterPtr>(m, "LogWriter")
138 .def("flush", &LogWriter::flush)
139 .def("close", &LogWriter::close);
140
141 py::class_<ConsoleWriter, LogWriter, std::shared_ptr<ConsoleWriter>>(m, "ConsoleWriter")
142 .def(py::init<>());
143
144 py::class_<JsonWriter, LogWriter, std::shared_ptr<JsonWriter>>(m, "JsonWriter")
145 .def(py::init<const std::string&>(), py::arg("filename"));
146
147 py::class_<Logger>(m, "Logger")
148 .def(py::init<>())
149 .def("set_level", &Logger::set_level)
150 .def("level", &Logger::level)
151 .def("add_writer", &Logger::add_writer)
152 .def("clear_writers", &Logger::clear_writers)
153 .def("add_type_filter", &Logger::add_type_filter)
154 .def("clear_type_filters", &Logger::clear_type_filters)
155 .def("add_source_filter", &Logger::add_source_filter)
156 .def("clear_source_filters", &Logger::clear_source_filters)
157 .def("set_time_range", &Logger::set_time_range)
158 .def("clear_time_range", &Logger::clear_time_range)
159 .def("attach", &Logger::attach)
160 .def("detach", &Logger::detach)
161 .def("flush", &Logger::flush)
162 .def("is_attached", &Logger::is_attached);
163
164 // Account models
165 py::class_<AccountBase, Model, std::shared_ptr<AccountBase>>(m, "AccountBase")
166 .def(py::init<std::string, std::string, std::string, double, Schedule>(),
167 py::arg("id"), py::arg("name"), py::arg("routing_tag"),
168 py::arg("initial_balance") = 0.0,
169 py::arg("schedule") = make_account_schedule())
170 .def("routing_tag", &AccountBase::routing_tag)
171 .def("balance", &AccountBase::balance);
172
173 py::class_<CheckingAccount, AccountBase, std::shared_ptr<CheckingAccount>>(m, "CheckingAccount")
174 .def(py::init<std::string, std::string, double, std::string, Schedule>(),
175 py::arg("id"), py::arg("name"),
176 py::arg("initial_balance") = 0.0,
177 py::arg("routing_tag") = "default",
178 py::arg("schedule") = make_account_schedule());
179
180 py::class_<SavingsAccount, AccountBase, std::shared_ptr<SavingsAccount>>(m, "SavingsAccount")
181 .def(py::init<std::string, std::string, double, double, std::string, Schedule>(),
182 py::arg("id"), py::arg("name"), py::arg("apy"),
183 py::arg("initial_balance") = 0.0,
184 py::arg("routing_tag") = "savings",
185 py::arg("schedule") = make_savings_schedule())
186 .def("apy", &SavingsAccount::apy);
187}
PYBIND11_MODULE(financesim_cpp, m)
Definition bindings.cpp:24
const std::string & routing_tag() const
Get the routing tag used to match income/expense events.
double balance() const
Get current balance.
double delta() const
Definition event.hpp:144
const std::string & reason() const
Definition event.hpp:145
double balance() const
Definition event.hpp:143
const std::string & account_id() const
Definition event.hpp:142
double delta() const
Definition event.hpp:98
double value() const
Definition event.hpp:97
const std::string & asset_id() const
Definition event.hpp:96
double annual_salary() const
Get the annual salary.
double payment_amount() const
Get the per-paycheck amount (annual / 24)
SubscriptionId subscribe_all(EventCallback callback)
Subscribe to all events.
Definition event_bus.hpp:45
void reset()
Reset the bus (clear all subscriptions and log)
Definition event_bus.hpp:92
void clear_log()
Clear the event log.
Definition event_bus.hpp:89
void unsubscribe(SubscriptionId id)
Unsubscribe using subscription ID.
Definition event_bus.hpp:52
const std::vector< EventPtr > & event_log() const
Get all events in chronological order (for replay/debugging)
Definition event_bus.hpp:86
const std::string & source_id() const
Source model ID that generated this event.
Definition event.hpp:21
virtual const char * type_name() const =0
Returns the type name of this event (for debugging/logging)
SimTime timestamp() const
Time at which this event was generated.
Definition event.hpp:18
double amount() const
Definition event.hpp:74
const std::string & category() const
Definition event.hpp:75
const std::string & target_account() const
Definition event.hpp:76
const std::string & target_account() const
Definition event.hpp:53
const std::string & category() const
Definition event.hpp:52
double amount() const
Definition event.hpp:51
double delta() const
Definition event.hpp:120
double value() const
Definition event.hpp:119
const std::string & liability_id() const
Definition event.hpp:118
virtual void flush()=0
Flush any buffered output.
virtual void close()=0
Close the writer and release resources.
void add_type_filter(const std::string &type_name)
Definition logger.cpp:30
void add_writer(LogWriterPtr writer)
Add a writer for output (Logger takes ownership via shared_ptr)
Definition logger.cpp:19
void detach()
Detach from the current EventBus (stops logging)
Definition logger.cpp:66
void add_source_filter(const std::string &source_id)
Definition logger.cpp:38
void clear_writers()
Remove all writers.
Definition logger.cpp:23
bool is_attached() const
Check if attached to an EventBus.
Definition logger.hpp:66
void set_time_range(SimTime start, SimTime end)
Set time range filter.
Definition logger.cpp:46
LogLevel level() const
Get the current log level.
Definition logger.hpp:28
void set_level(LogLevel level)
Set the minimum log level (events below this level are ignored)
Definition logger.cpp:15
void clear_type_filters()
Clear all type filters (log all types)
Definition logger.cpp:34
void attach(EventBus &bus)
Attach to an EventBus (starts logging)
Definition logger.cpp:56
void clear_source_filters()
Clear all source filters (log all sources)
Definition logger.cpp:42
void flush()
Flush all writers.
Definition logger.cpp:74
void clear_time_range()
Clear time range filter.
Definition logger.cpp:52
virtual const std::string & name() const =0
Human-readable name/description.
virtual void update(SimTime time)=0
virtual void initialize(EventBus &bus)=0
virtual const Schedule & schedule() const =0
Get the model's execution schedule.
virtual void reset()=0
Reset model to initial state (for replay)
virtual void finalize()=0
virtual const std::string & id() const =0
Unique identifier for this model instance.
double apy() const
Get the APY (annual percentage yield)
const std::string & reason() const
Definition event.hpp:171
const std::string & from_account() const
Definition event.hpp:168
const std::string & to_account() const
Definition event.hpp:169
double amount() const
Definition event.hpp:170
Schedule make_savings_schedule()
Helper to create default schedule for savings accounts (monthly interest)
Schedule make_account_schedule()
Helper to create default schedule for accounts (no periodic updates)
ExecutionTiming timing
Definition time.hpp:20
SimTime start_time
Definition time.hpp:17
SimTime stop_time
Definition time.hpp:18