Skip to content

Commit 95c2ef4

Browse files
committed
[dev] Add execution context to example application
1 parent ad392ee commit 95c2ef4

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.vscode
2+
.venv
23
**/__pycache__
34
**/build

examples/simple-embedding/src/SimpleEmbedding.cc

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,45 @@ PYBIND11_EMBEDDED_MODULE(example_interface, m) { }
55

66
namespace fs = std::filesystem;
77

8+
struct SimpleExecutionContext : MoniLog::MoniLogExecutionContext
9+
{
10+
const pybind11::object get_foo() const { if (foo != nullptr) return pybind11::cast(*foo); else return pybind11::cast<pybind11::none>(Py_None); }
11+
12+
double *foo = nullptr;
13+
14+
using MoniLog::MoniLogExecutionContext::MoniLogExecutionContext;
15+
};
16+
817
int main()
918
{
1019
// Retrieve the path where the Python scripts to use are located (can provide multiple locations).
1120
std::string path = fs::current_path();
1221
std::vector<std::string> python_path = { path + "/src/" };
22+
1323
// Provide the Python scripts to include.
1424
std::vector<std::string> python_scripts = {"example_moniloggers"};
25+
1526
// Name of the interface module (here, an embedded module declared above) exposing the execution context of the application.
1627
std::string interface_module = "example_interface";
28+
1729
// Initialization function for the interface module.
1830
std::function<void (pybind11::module_, pybind11::object)> interface_module_initializer =
19-
[](pybind11::module_ iterativeheatequation_module, pybind11::object context_class) { };
31+
[](pybind11::module_ interface_module, pybind11::object context_class) {
32+
pybind11::class_<SimpleExecutionContext, std::shared_ptr<SimpleExecutionContext>>(interface_module, "SimpleExecutionContext", context_class)
33+
.def_property_readonly("foo", &SimpleExecutionContext::get_foo)
34+
.def("__str__", [](SimpleExecutionContext &self)
35+
{
36+
std::ostringstream oss;
37+
oss << self.name;
38+
return oss.str();
39+
})
40+
.def("__repr__", [](SimpleExecutionContext &self)
41+
{
42+
std::ostringstream oss;
43+
oss << self.name;
44+
return oss.str();
45+
});
46+
};
2047

2148
// Define base execution events emitted by the application, to which moniloggers can register.
2249
MoniLog::register_base_events({
@@ -28,17 +55,27 @@ int main()
2855
MoniLog::register_composite_event("SomeCompositeEvent", {"SomeEvent", "SomeOtherEvent"});
2956

3057
// Instantiating the execution context accessible from Python.
31-
std::shared_ptr<MoniLog::MoniLogExecutionContext> ctx(new MoniLog::MoniLogExecutionContext());
58+
std::shared_ptr<SimpleExecutionContext> ctx(new SimpleExecutionContext("SimpleExecutionContext"));
3259

3360
// Bootstrapping monilog, consisting mainly of starting the Python interpreter, initializing
3461
// the monilog module, and evaluating the provided scripts.
3562
MoniLog::bootstrap_monilog(python_path, python_scripts, interface_module, interface_module_initializer);
3663

64+
double foo(0.0);
65+
ctx->foo = &foo;
66+
3767
// Emitting some base events, triggering the registered moniloggers.
3868
MoniLog::trigger(0, ctx);
69+
70+
foo = 17.0;
71+
3972
MoniLog::trigger(1, ctx);
4073

74+
foo = 42.0;
75+
4176
// Emitting a composite event (can also emit base event by name).
4277
MoniLog::trigger("SomeCompositeEvent", ctx);
4378

79+
foo = 17.0;
80+
4481
}

examples/simple-embedding/src/example_moniloggers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
@register("SomeEvent")
44
def monilogger1(ctx):
55
print("monilogger 1 triggered!")
6+
print("foo = " + str(ctx.foo))
67

78
@register("SomeOtherEvent")
89
def monilogger2(ctx):
910
print("monilogger 2 triggered!")
11+
print("foo = " + str(ctx.foo))
1012

1113
@register("SomeCompositeEvent")
1214
def monilogger3(ctx):
13-
print("monilogger 3 triggered!")
15+
print("monilogger 3 triggered!")
16+
print("foo = " + str(ctx.foo))

0 commit comments

Comments
 (0)