CYCLUS
Loading...
Searching...
No Matches
pyhooks.cc
Go to the documentation of this file.
1#include "Python.h"
2#include "pyhooks.h"
3
4#ifdef CYCLUS_WITH_PYTHON
5#include <stdlib.h>
6
7#include "eventhooks_api.h"
8#include "pyinfile_api.h"
9#include "pymodule_api.h"
10
11namespace cyclus {
12int PY_INTERP_COUNT = 0;
13bool PY_INTERP_INIT = false;
14
15
16void PyStart(void) {
17 if (!PY_INTERP_INIT) {
20 PY_INTERP_INIT = true;
21 };
23};
24
25void PyStop(void) {
27
28 // PY_INTERP_COUNT should only be negative when called atexit()
31 };
32};
33
34void EventLoop(void) { import_eventhooks(); eventloophook(); };
35
36std::string PyFindModule(std::string lib) { import_pymodule(); return py_find_module(lib); };
37
38Agent* MakePyAgent(std::string lib, std::string agent, void* ctx) {
40 return make_py_agent(lib, agent, ctx);
41};
42
43void InitFromPyAgent(Agent* src, Agent* dst, void* ctx) {
46};
47
49
50void PyDelAgent(int i) { import_pymodule(); py_del_agent(i); };
51
52namespace toolkit {
53std::string PyToJson(std::string infile) { import_pyinfile(); return py_to_json(infile); };
54
55std::string JsonToPy(std::string infile) { import_pyinfile(); return json_to_py(infile); };
56
57void PyCallListeners(std::string tstype, Agent* agent, void* cpp_ctx, int time, boost::spirit::hold_any value){
59 py_call_listeners(tstype, agent, cpp_ctx, time, value);
60};
61
62} // namespace toolkit
63} // namespace cyclus
64#else // else CYCLUS_WITH_PYTHON
65#include "error.h"
66
67namespace cyclus {
69bool PY_INTERP_INIT = false;
70
71void PyStart(void) {};
72
73void PyStop(void) {};
74
75void EventLoop(void) {};
76
77std::string PyFindModule(std::string lib) { return std::string(""); };
78
79Agent* MakePyAgent(std::string lib, std::string agent, void* ctx) { return NULL; };
80
81void InitFromPyAgent(Agent* src, Agent* dst, void* ctx) {};
82
83void ClearPyAgentRefs(void) {};
84
85void PyDelAgent(int i) {};
86
87namespace toolkit {
88std::string PyToJson(std::string infile) {
89 throw cyclus::ValidationError("Cannot convert from Python input files since "
90 "Cyclus was not built with Python bindings.");
91 return "";
92};
93
94std::string JsonToPy(std::string infile) {
95 throw cyclus::ValidationError("Cannot convert to Python input files since "
96 "Cyclus was not built with Python bindings.");
97 return "";
98};
99
100void PyCallListeners(std::string tsname, Agent* agent, void* cpp_ctx, int time, boost::spirit::hold_any value) {};
101
102} // namespace toolkit
103} // namespace cyclus
104#endif // ends CYCLUS_WITH_PYTHON
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:49
For validating files received via IO.
Definition error.h:71
static int import_eventhooks(void)
#define eventloophook
void PyCallListeners(std::string tsname, Agent *agent, void *cpp_ctx, int time, boost::spirit::hold_any value)
Calls the Python listeners.
Definition pyhooks.cc:100
std::string PyToJson(std::string infile)
Convert Python simulation string to JSON.
Definition pyhooks.cc:88
std::string JsonToPy(std::string infile)
Convert JSON string to Python simulation string.
Definition pyhooks.cc:94
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
bool PY_INTERP_INIT
Whether or not the Python interpreter has been initilized.
Definition pyhooks.cc:69
void EventLoop(void)
Definition pyhooks.cc:75
void InitFromPyAgent(Agent *src, Agent *dst, void *ctx)
Initializes a Python agent fron another agent.
Definition pyhooks.cc:81
void ClearPyAgentRefs(void)
Removes all Python agents from the internal cache.
Definition pyhooks.cc:83
int PY_INTERP_COUNT
Because of NumPy #7595, we can only initialize & finalize the Python interpreter once.
Definition pyhooks.cc:68
void PyDelAgent(int i)
Removes a single Python agent from the reference cache.
Definition pyhooks.cc:85
std::string PyFindModule(std::string lib)
Finds a Python module and returns its filename.
Definition pyhooks.cc:77
void PyStop(void)
Closes the current Python session.
Definition pyhooks.cc:73
Agent * MakePyAgent(std::string lib, std::string agent, void *ctx)
Finds a Python module and returns an agent pointer from it.
Definition pyhooks.cc:79
void PyStart(void)
Initialize Python functionality, this is a no-op if Python was not installed along with Cyclus.
Definition pyhooks.cc:71
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
#define py_to_json
static int import_pyinfile(void)
#define json_to_py
#define py_find_module
#define clear_pyagent_refs
static int import_pymodule(void)
#define py_del_agent
#define init_from_py_agent
#define py_call_listeners
#define make_py_agent