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
15void PyStart(void) {
16 if (!PY_INTERP_INIT) {
17 Py_Initialize();
18 atexit(PyStop);
19 PY_INTERP_INIT = true;
20 };
22};
23
24void PyStop(void) {
26
27 // PY_INTERP_COUNT should only be negative when called atexit()
29 Py_Finalize();
30 };
31};
32
33void EventLoop(void) {
36};
37
38std::string PyFindModule(std::string lib) {
40 return py_find_module(lib);
41};
42
43Agent* MakePyAgent(std::string lib, std::string agent, void* ctx) {
45 return make_py_agent(lib, agent, ctx);
46};
47
48void InitFromPyAgent(Agent* src, Agent* dst, void* ctx) {
50 init_from_py_agent(src, dst, ctx);
51};
52
53void ClearPyAgentRefs(void) {
56};
57
58void PyDelAgent(int i) {
60 py_del_agent(i);
61};
62
63namespace toolkit {
64std::string PyToJson(std::string infile) {
66 return py_to_json(infile);
67};
68
69std::string JsonToPy(std::string infile) {
71 return json_to_py(infile);
72};
73
74void PyCallListeners(std::string tstype, Agent* agent, void* cpp_ctx, int time,
77 py_call_listeners(tstype, agent, cpp_ctx, time, value);
78};
79
80} // namespace toolkit
81} // namespace cyclus
82#else // else CYCLUS_WITH_PYTHON
83#include "error.h"
84
85namespace cyclus {
87bool PY_INTERP_INIT = false;
88
89void PyStart(void) {};
90
91void PyStop(void) {};
92
93void EventLoop(void) {};
94
95std::string PyFindModule(std::string lib) {
96 return std::string("");
97};
98
99Agent* MakePyAgent(std::string lib, std::string agent, void* ctx) {
100 return NULL;
101};
102
103void InitFromPyAgent(Agent* src, Agent* dst, void* ctx) {};
104
105void ClearPyAgentRefs(void) {};
106
107void PyDelAgent(int i) {};
108
109namespace toolkit {
110std::string PyToJson(std::string infile) {
112 "Cannot convert from Python input files since "
113 "Cyclus was not built with Python bindings.");
114 return "";
115};
116
117std::string JsonToPy(std::string infile) {
119 "Cannot convert to Python input files since "
120 "Cyclus was not built with Python bindings.");
121 return "";
122};
123
124void PyCallListeners(std::string tsname,
125 Agent* agent,
126 void* cpp_ctx,
127 int time,
128 boost::spirit::hold_any value) {};
129
130} // namespace toolkit
131} // namespace cyclus
132#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:50
For validating files received via IO.
Definition error.h:67
static int import_eventhooks(void)
#define eventloophook
basic_hold_any< char > hold_any
Definition any.hpp:345
void PyCallListeners(std::string tsname, Agent *agent, void *cpp_ctx, int time, boost::spirit::hold_any value)
Calls the Python listeners.
Definition pyhooks.cc:124
std::string PyToJson(std::string infile)
Convert Python simulation string to JSON.
Definition pyhooks.cc:110
std::string JsonToPy(std::string infile)
Convert JSON string to Python simulation string.
Definition pyhooks.cc:117
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:87
void EventLoop(void)
Definition pyhooks.cc:93
void InitFromPyAgent(Agent *src, Agent *dst, void *ctx)
Initializes a Python agent fron another agent.
Definition pyhooks.cc:103
void ClearPyAgentRefs(void)
Removes all Python agents from the internal cache.
Definition pyhooks.cc:105
int PY_INTERP_COUNT
Because of NumPy #7595, we can only initialize & finalize the Python interpreter once.
Definition pyhooks.cc:86
void PyDelAgent(int i)
Removes a single Python agent from the reference cache.
Definition pyhooks.cc:107
std::string PyFindModule(std::string lib)
Finds a Python module and returns its filename.
Definition pyhooks.cc:95
void PyStop(void)
Closes the current Python session.
Definition pyhooks.cc:91
Agent * MakePyAgent(std::string lib, std::string agent, void *ctx)
Finds a Python module and returns an agent pointer from it.
Definition pyhooks.cc:99
void PyStart(void)
Initialize Python functionality, this is a no-op if Python was not installed along with Cyclus.
Definition pyhooks.cc:89
#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