1414//
1515// =====================================================================================
1616
17- #include < vector>
18- #include < utility>
19- #include < typeinfo>
20- #include < typeindex>
2117#include < map>
18+ #include < typeindex>
19+ #include < typeinfo>
20+ #include < utility>
21+ #include < vector>
2222
23+ #include " ../external/pybind11/include/pybind11/functional.h"
24+ #include " ../external/pybind11/include/pybind11/numpy.h"
2325#include " ../external/pybind11/include/pybind11/pybind11.h"
2426#include " ../external/pybind11/include/pybind11/stl.h"
25- #include " ../external/pybind11/include/pybind11/numpy.h"
26- #include " ../external/pybind11/include/pybind11/functional.h"
2727
2828// See
2929// https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#binding-stl-containers
3030// #include "../external/pybind11/include/pybind11/stl_bind.h"
3131
32- #include " ../basecode/header.h"
3332#include " ../basecode/global.h"
33+ #include " ../basecode/header.h"
3434#include " ../basecode/Cinfo.h"
35-
35+ #include " ../builtins/Variable.h"
36+ #include " ../shell/Neutral.h"
3637#include " ../shell/Shell.h"
3738#include " ../shell/Wildcard.h"
38- #include " ../shell/Neutral.h"
39- #include " ../builtins/Variable.h"
40-
4139#include " ../utility/strutil.h"
42-
4340#include " helper.h"
4441#include " pymoose.h"
4542
4643using namespace std ;
4744namespace py = pybind11;
4845
49- Id initModule (py::module & m)
50- {
51- return initShell ();
52- }
46+ Id initModule (py::module & m) { return initShell (); }
5347
5448template <typename T = double >
5549void setProperty (const ObjId& id, const string& fname, T val)
@@ -72,7 +66,8 @@ py::array_t<T> getFieldNumpy(const ObjId& id, const string& fname)
7266 return py::array_t <T>(v.size (), v.data ());
7367}
7468
75- py::object getValueFinfo (const ObjId& oid, const string& fname, const string& rttType)
69+ py::object getValueFinfo (const ObjId& oid, const string& fname,
70+ const string& rttType)
7671{
7772 py::object r = py::none ();
7873 if (rttType == " double" )
@@ -103,9 +98,9 @@ py::object getValueFinfo(const ObjId& oid, const string& fname, const string& rt
10398 r = py::cast (getProp<vector<Id>>(oid, fname));
10499 else if (rttType == " vector<ObjId>" )
105100 r = py::cast (getProp<vector<ObjId>>(oid, fname));
106- else
107- {
108- py::print ( " Warning: pymoose::getProperty::Warning: Unsupported type " + rttType);
101+ else {
102+ py::print ( " Warning: pymoose::getProperty::Warning: Unsupported type " +
103+ rttType);
109104 r = py::none ();
110105 }
111106 return r;
@@ -116,27 +111,41 @@ inline ObjId getElementFinfoItem(const ObjId& oid, const size_t& i)
116111 return ObjId (oid.path (), oid.dataIndex , i);
117112}
118113
119- py::list getElementFinfo (const ObjId& objid, const string& fname)
114+ py::list getElementFinfo (const ObjId& objid, const string& fname,
115+ const string& rttType)
120116{
121- auto oid = ObjId (objid.path () + ' /' + fname);
117+ auto oid = ObjId (objid.path () + ' /' + fname);
122118 auto len = Field<unsigned int >::get (oid, " numField" );
123119 vector<ObjId> res (len);
124- for (size_t i = 0 ; i < len; i++)
120+ for (size_t i = 0 ; i < len; i++)
125121 res[i] = ObjId (oid.path (), oid.dataIndex , i);
126122 return py::cast (res);
127123}
128124
129- py::object getLookupValueFinfoItem (const ObjId& oid, const string& fname, const string& k)
125+ py::object getLookupValueFinfoItem (const ObjId& oid, const string& fname,
126+ const string& k, const string& rttType)
130127{
128+ vector<string> srcDestType;
129+ moose::tokenize (rttType, " ," , srcDestType);
130+ string srcType = srcDestType[0 ];
131+ string tgtType = srcDestType[1 ];
132+
131133 py::object r;
132- r = py::cast (LookupField<string, bool >::get (oid, fname, k));
134+ if (tgtType == " bool" )
135+ r = py::cast (LookupField<string, bool >::get (oid, fname, k));
136+ else if (tgtType == " vector<Id>" )
137+ r = py::cast (LookupField<string, vector<Id>>::get (oid, fname, k));
138+ else
139+ cerr << " Unsupported types: " << rttType << endl;
133140 return r;
134141}
135142
136- py::function getLookupValueFinfo (const ObjId& oid, const string& fname)
143+ py::function getLookupValueFinfo (const ObjId& oid, const string& fname,
144+ const string& rttType)
137145{
138- std::function<py::object (const string&)> f = [oid, fname](const string& key) {
139- return getLookupValueFinfoItem (oid, fname, key);
146+ std::function<py::object (const string&)> f = [oid, fname,
147+ rttType](const string& key) {
148+ return getLookupValueFinfoItem (oid, fname, key, rttType);
140149 };
141150 return py::cast (f);
142151}
@@ -154,28 +163,26 @@ py::object getProperty(const ObjId& oid, const string& fname)
154163 string rttType = finfo->rttiType ();
155164 string finfoType = cinfo->getFinfoType (finfo);
156165
157- if (finfoType == " ValueFinfo" )
166+ if (finfoType == " ValueFinfo" )
158167 // return value.
159168 return getValueFinfo (oid, fname, rttType);
160- else if (finfoType == " FieldElementFinfo" ) {
169+ else if (finfoType == " FieldElementFinfo" ) {
161170 // Return list.
162- return getElementFinfo (oid, fname);
171+ return getElementFinfo (oid, fname, rttType );
163172 }
164- else if (finfoType == " LookupValueFinfo" ) {
173+ else if (finfoType == " LookupValueFinfo" ) {
165174 // Return function.
166- return getLookupValueFinfo (oid, fname);
175+ return getLookupValueFinfo (oid, fname, rttType );
167176 }
168177
169- cout << " Searching for " << fname << " with rttType "
170- << rttType << " and type: " << finfoType << endl;
171-
178+ cout << " Searching for " << fname << " with rttType " << rttType
179+ << " and type: " << finfoType << endl;
172180
173- py::print (" Warning: pymoose::getProperty::Warning: Unsupported type " + rttType);
181+ py::print (" Warning: pymoose::getProperty::Warning: Unsupported type " +
182+ rttType);
174183 return pybind11::none ();
175184}
176185
177-
178-
179186PYBIND11_MODULE (_cmoose, m)
180187{
181188 m.doc () = R"moosedoc( moose module.)moosedoc" ;
@@ -191,15 +198,16 @@ PYBIND11_MODULE(_cmoose, m)
191198 .def_property_readonly (" numIds" , &Id::numIds)
192199 .def_property_readonly (" path" , &Id::path)
193200 .def_property_readonly (" id" , &Id::value)
194- .def_property_readonly (" cinfo" ,
195- [](Id& id) { return id.element ()->cinfo (); },
196- py::return_value_policy::reference)
197201 .def_property_readonly (
198- " type" , [](Id& id) { return id.element ()->cinfo ()->name (); })
202+ " cinfo" , [](Id& id) { return id.element ()->cinfo (); },
203+ py::return_value_policy::reference)
204+ .def_property_readonly (
205+ " type" , [](Id& id) { return id.element ()->cinfo ()->name (); })
199206 .def (" __repr__" , [](const Id& id) {
200- return " <Id id=" + std::to_string (id.value ()) + " path=" +
201- id.path () + " class=" + id.element ()->cinfo ()->name () + " >" ;
202- });
207+ return " <Id id=" + std::to_string (id.value ()) +
208+ " path=" + id.path () +
209+ " class=" + id.element ()->cinfo ()->name () + " >" ;
210+ });
203211
204212 py::class_<ObjId>(m, " _ObjId" )
205213 .def (py::init<>())
@@ -215,10 +223,12 @@ PYBIND11_MODULE(_cmoose, m)
215223 [](const ObjId oid) { return oid.id .value (); })
216224 .def_property_readonly (" path" , &ObjId::path)
217225 .def_property_readonly (" name" , &ObjId::name)
218- .def_property_readonly (" className" , [](const ObjId& oid){ return oid.element ()->cinfo ()->name (); })
226+ .def_property_readonly (
227+ " className" ,
228+ [](const ObjId& oid) { return oid.element ()->cinfo ()->name (); })
219229 .def_property_readonly (" id" , [](ObjId& oid) { return oid.id ; })
220230 .def_property_readonly (
221- " type" , [](ObjId& oid) { return oid.element ()->cinfo ()->name (); })
231+ " type" , [](ObjId& oid) { return oid.element ()->cinfo ()->name (); })
222232 // --------------------------------------------------------------------
223233 // Set/Get
224234 // --------------------------------------------------------------------
@@ -236,7 +246,6 @@ PYBIND11_MODULE(_cmoose, m)
236246 .def (" getElementFieldItem" , &getElementFieldItem)
237247 .def (" getNumpy" , &getFieldNumpy<double >)
238248
239-
240249 // ---------------------------------------------------------------------
241250 // Connect
242251 // ---------------------------------------------------------------------
@@ -246,10 +255,10 @@ PYBIND11_MODULE(_cmoose, m)
246255 // Extra
247256 // ---------------------------------------------------------------------
248257 .def (" __repr__" , [](const ObjId& oid) {
249- return " <" + oid.element ()->cinfo ()->name () + " id= " +
250- std::to_string (oid.id .value ()) + " path= " + oid. path ( ) +
251- " >" ;
252- });
258+ return " <" + oid.element ()->cinfo ()->name () +
259+ " id= " + std::to_string (oid.id .value ()) +
260+ " path= " + oid. path () + " >" ;
261+ });
253262
254263 py::class_<Variable>(m, " _Variable" ).def (py::init<>());
255264
@@ -270,14 +279,15 @@ PYBIND11_MODULE(_cmoose, m)
270279 .def (" setClock" , &Shell::doSetClock)
271280 .def (" reinit" , &Shell::doReinit)
272281 .def (" delete" , &Shell::doDelete)
273- .def (" start" , &Shell::doStart
274- , py::arg ( " runtime " ), py::arg (" notify" ) = false )
282+ .def (" start" , &Shell::doStart, py::arg ( " runtime " ),
283+ py::arg (" notify" ) = false )
275284 .def (" quit" , &Shell::doQuit);
276285
277286 // Module functions.
278- m.def (" getShell" ,
279- []() { return reinterpret_cast <Shell*>(Id ().eref ().data ()); },
280- py::return_value_policy::reference);
287+ m.def (
288+ " getShell" ,
289+ []() { return reinterpret_cast <Shell*>(Id ().eref ().data ()); },
290+ py::return_value_policy::reference);
281291
282292 m.def (" wildcardFind" , &wildcardFind2);
283293 m.def (" delete" , &mooseDelete);
@@ -289,7 +299,8 @@ PYBIND11_MODULE(_cmoose, m)
289299 m.def (" getCwe" , &mooseGetCwe);
290300 m.def (" setClock" , &mooseSetClock);
291301 m.def (" loadModelInternal" , &loadModelInternal);
292- m.def (" getFieldDict" , &mooseGetFieldDict, py::arg (" className" ), py::arg (" finfoType" )=" " );
302+ m.def (" getFieldDict" , &mooseGetFieldDict, py::arg (" className" ),
303+ py::arg (" finfoType" ) = " " );
293304
294305 // Attributes.
295306 m.attr (" NA" ) = NA;
0 commit comments