50 #include <structmember.h>
53 #include <boost/format.hpp>
66 #include "../basecode/header.h"
67 #include "../basecode/Id.h"
68 #include "../basecode/ObjId.h"
69 #include "../utility/utility.h"
70 #include "../utility/print_function.hpp"
71 #include "../shell/Shell.h"
79 extern PyTypeObject
IdType;
85 if (attribute ==
"vec")
89 else if (attribute ==
"dindex")
93 else if (attribute ==
"findex")
103 static const char*
const kwlist[] = {
"id",
"dataIndex",
"fieldIndex", NULL};
104 unsigned int id = 0, data = 0, field = 0;
105 PyObject * obj = NULL;
106 if (PyArg_ParseTupleAndKeywords(args, kwargs,
107 "I|II:moose_ObjId_init_from_id",
116 self->oid_ =
ObjId(
Id(
id), data, field );
117 if (self->oid_.bad())
119 PyErr_SetString(PyExc_ValueError,
"Invalid ObjId");
125 if (PyArg_ParseTupleAndKeywords(args, kwargs,
126 "O|II:moose_ObjId_init_from_id",
128 &obj, &data, &field))
138 self->oid_ =
ObjId(((
_Id*)obj)->id_, data, field );
139 if (self->oid_.bad())
141 PyErr_SetString(PyExc_ValueError,
"Invalid dataIndex/fieldIndex.");
146 else if (PyObject_IsInstance(obj, (PyObject*)&ObjIdType))
152 self->oid_ = ((
_ObjId*)obj)->oid_;
153 if (self->oid_.bad())
155 PyErr_SetString(PyExc_ValueError,
"Invalid ObjId");
167 static const char*
const kwlist[] = {
"path",
"n",
"g",
"dtype", NULL};
168 const char* parsedPath;
169 unsigned int numData = 1;
170 unsigned int isGlobal = 0;
174 PyTypeObject * mytype = Py_TYPE(
self);
175 string mytypename(mytype->tp_name);
178 bool parse_success =
false;
179 if (PyArg_ParseTupleAndKeywords(args, kwargs,
180 "s|IIs:moose_ObjId_init_from_path",
182 &parsedPath, &numData, &isGlobal, &type))
184 parse_success =
true;
194 string path(parsedPath);
200 ostringstream err, warn;
203 self->oid_ =
ObjId(path);
208 if (basetype == NULL)
210 PyErr_SetString(PyExc_TypeError
211 ,
"Unknown class. Need a valid MOOSE class or subclass thereof."
215 basetype_str = string(basetype->tp_name).substr(6);
219 basetype_str = string(type);
224 if (self->oid_.bad())
227 if ((path ==
"/") || (path ==
"/root"))
234 <<
" to " << mytypename
235 <<
"To get the existing object use `moose.element(obj)` instead.";
236 PyErr_SetString(PyExc_TypeError, err.str().c_str());
245 string className =
self->oid_.element()->cinfo()->name();
246 map <string, PyTypeObject * >::iterator ii =
249 PyTypeObject * basetype = 0;
252 basetype = ii->second;
254 basetype_str = string(basetype->tp_name).substr(6);
258 err <<
"Unknown class: " << className << endl;
266 err <<
"Accessing existing paths using object constrcutors has been deprecated. Use "
267 <<
" moose.element to access existing object. In future "
268 <<
" this will be an error." << endl;
269 PyErr_WarnEx(PyExc_DeprecationWarning, err.str().c_str(), 1);
275 err <<
"cannot convert moose." << className
276 <<
" to " << mytypename
277 <<
". To get the existing object use `moose.element(obj)` instead.";
278 PyErr_SetString(PyExc_TypeError, err.str().c_str());
286 if (new_id ==
Id() && PyErr_Occurred())
292 self->oid_ =
ObjId(new_id);
299 if (
self && !PyObject_IsInstance((PyObject*)
self, (PyObject*)Py_TYPE((PyObject*)
self)))
302 error <<
"Expected an melement or subclass. Found "
303 << Py_TYPE(
self)->tp_name;
304 PyErr_SetString(PyExc_TypeError, error.str().c_str());
317 PyErr_SetString(PyExc_ValueError,
318 "Could not parse arguments. "
319 " Call __init__(path, n, g, dtype) or"
320 " __init__(id, dataIndex, fieldIndex)");
339 long long id = (
long long)(self->oid_.id.value());
341 long fieldIndex =
self->oid_.fieldIndex;
345 if (
sizeof(
size_t) == 8)
347 return id << 48 | dataIndex << 16 | fieldIndex;
351 return id << 16 | dataIndex << 8 | fieldIndex;
363 <<
"id=" <<
self->oid_.id.value() <<
", "
364 <<
"dataIndex=" <<
self->oid_.dataIndex <<
", "
365 <<
"path=" <<
self->oid_.path() <<
">";
366 return PyString_FromString(repr.str().c_str());
377 <<
"id=" <<
self->oid_.id.value() <<
", "
378 <<
"dataIndex=" <<
self->oid_.dataIndex <<
", "
379 <<
"path=" <<
self->oid_.path() <<
">";
380 return PyString_FromString(repr.str().c_str());
386 "Returns the information of the object's classtype, Id, and path \n"
387 "in form of a vector.\n"
390 " >>> com = moose.Compartment('/com')\n"
392 " moose.vec: class=Compartment, id=481, path=/com>"
400 extern PyTypeObject
IdType;
401 _Id * ret = PyObject_New(
_Id, &IdType);
402 ret->
id_ =
self->oid_.id;
403 return (PyObject*)ret;
407 "getFieldType(fieldname)\n"
409 "Returns the type of the field `fieldname` (as a string).\n"
413 "fieldname : string\n"
414 " Name of the field to be queried.\n"
416 " >>> comp.getFieldType('neighbors')\n"
417 " >>> 'string,vector<Id>' \n"
426 char * fieldName = NULL;
427 if (!PyArg_ParseTuple(args,
"s:moose_ObjId_getFieldType", &fieldName))
433 if (typeStr.length() <= 0)
435 PyErr_SetString(PyExc_ValueError,
436 "Empty string for field type. "
437 "Field name may be incorrect.");
440 PyObject * type = PyString_FromString(typeStr.c_str());
449 "getField(fieldname)\n"
451 "Returns the value of the field `fieldname`.\n"
455 "fieldname : string\n"
456 " Name of the field.\n"
458 " >>> comp.getField('x0')\n"
467 if (!PyArg_ParseTuple(args,
"O:moose_ObjId_getField", &attr))
487 if (self->oid_.bad())
495 if (PyString_Check(attr))
497 field = PyString_AsString(attr);
501 return PyObject_GenericGetAttr((PyObject*)
self, attr);
508 string fieldName(field);
510 vector<string> valueFinfos =
getFieldNames(className,
"valueFinfo");
511 bool isValueField =
false;
512 for (
unsigned int ii = 0; ii < valueFinfos.size(); ++ii)
514 if (fieldName == valueFinfos[ii])
522 if (type.empty() || !isValueField )
525 map<string, string>::const_iterator it =
get_field_alias().find(fieldName);
528 fieldName = it->second;
529 field = fieldName.c_str();
530 isValueField =
false;
531 for (
unsigned int ii = 0; ii < valueFinfos.size(); ++ii)
533 if (fieldName == valueFinfos[ii])
542 attr = PyString_FromString(field);
546 if (type.empty() || !isValueField)
548 _ret = PyObject_GenericGetAttr((PyObject*)
self, attr);
558 _ret = PyObject_GenericGetAttr((PyObject*)
self, attr);
565 fieldName= string(field);
571 _ret = Py_BuildValue(
"s", _s.c_str());
577 _ret =
to_py(&value, ftype);
583 _ret =
to_py(&value, ftype);
589 _ret =
to_py(&value, ftype);
595 _ret =
to_py(&value, ftype);
601 _ret =
to_py(&value, ftype);
607 _ret =
to_py(&value, ftype);
613 _ret =
to_py(&value, ftype);
619 _ret =
to_py(&value, ftype);
625 _ret =
to_py(&value, ftype);
631 _ret =
to_py(&value, ftype);
636 PyErr_SetString(PyExc_NotImplementedError,
"DataId handling not implemented yet.");
643 _ret =
to_py(&value, ftype);
649 _ret =
to_py(&value, ftype);
655 _ret =
to_py(&value, ftype);
661 _ret =
to_py(&value, ftype);
667 _ret =
to_py(&value, ftype);
673 _ret =
to_py(&value, ftype);
679 _ret =
to_py(&value, ftype);
685 _ret =
to_py(&value, ftype);
691 _ret =
to_py(&value, ftype);
697 _ret =
to_py(&value, ftype);
703 _ret =
to_py(&value, ftype);
709 _ret =
to_py(&value, ftype);
715 _ret =
to_py(&value, ftype);
721 _ret =
to_py(&value, ftype);
727 _ret =
to_py(&value, ftype);
733 _ret =
to_py(&value, ftype);
739 _ret =
to_py(&value, ftype);
760 _ret = PyObject_GenericGetAttr((PyObject*)
self, attr);
774 "setField(fieldname, value)\n"
776 "Set the value of specified field.\n"
780 "fieldname : string\n"
781 " Field to be assigned value to.\n"
783 "value : python datatype compatible with the type of the field\n"
784 " The value to be assigned to the field."
787 " >>> comp.setField('x0', 45.25) \n"
788 " >>> print comp.x0\n"
795 if (!PyArg_ParseTuple(args,
"OO:moose_ObjId_setField", &field, &value))
816 if (PyString_Check(attr))
818 field = PyString_AsString(attr);
822 PyErr_SetString(PyExc_TypeError,
"Attribute name must be a string");
826 if (fieldtype.length() == 0)
833 string className = ((PyTypeObject*)PyObject_Type((PyObject*)
self))->tp_name;
836 return PyObject_GenericSetAttr((PyObject*)
self, PyString_FromString(field), value);
839 msg <<
"'" << className <<
"' class has no field '" << field <<
"'" << endl;
840 PyErr_SetString(PyExc_AttributeError, msg.str().c_str());
849 double _value = PyFloat_AsDouble(value);
855 long _value = PyInt_AsLong(value);
856 if ((_value != -1) || (!PyErr_Occurred()))
864 unsigned long _value = PyInt_AsUnsignedLongMask(value);
870 unsigned long _value = PyInt_AsUnsignedLongMask(value);
876 float _value = PyFloat_AsDouble(value);
882 char * _value = PyString_AsString(value);
897 PyErr_SetString(PyExc_ValueError,
"Null pointer passed as vec Id value.");
910 PyErr_SetString(PyExc_ValueError,
"Null pointer passed as vec Id value.");
917 if (!PySequence_Check(value))
919 PyErr_SetString(PyExc_TypeError,
"For setting vector<double> field, specified value must be a sequence." );
923 Py_ssize_t length = PySequence_Length(value);
924 vector<double> _value;
925 for (
int ii = 0; ii < length; ++ii)
927 PyObject * vo = PySequence_GetItem(value, ii);
928 double v = PyFloat_AsDouble(vo);
938 bool _value = (Py_True ==
value) || (PyInt_AsLong(value) != 0);
944 char * _value = PyString_AsString(value);
945 if (_value && _value[0])
953 int _value = PyInt_AsLong(value);
954 if ((_value != -1) || (!PyErr_Occurred()))
962 short _value = (short)PyInt_AsLong(value);
963 if ((_value != -1) || (!PyErr_Occurred()))
971 PyErr_SetString(PyExc_NotImplementedError,
"DataId handling not implemented yet.");
976 if (!PySequence_Check(value))
978 PyErr_SetString(PyExc_TypeError,
"For setting vector<int> field, specified value must be a sequence." );
980 Py_ssize_t length = PySequence_Length(value);
982 for (
int ii = 0; ii < length; ++ii)
984 PyObject * vo = PySequence_GetItem(value, ii);
985 int v = PyInt_AsLong(vo);
994 if (!PySequence_Check(value))
996 PyErr_SetString(PyExc_TypeError,
"For setting vector<short> field, specified value must be a sequence." );
1000 Py_ssize_t length = PySequence_Length(value);
1001 vector<short> _value;
1002 for (
int ii = 0; ii < length; ++ii)
1004 PyObject * vo = PySequence_GetItem(value, ii);
1005 short v = PyInt_AsLong(vo);
1007 _value.push_back(v);
1015 if (!PySequence_Check(value))
1017 PyErr_SetString(PyExc_TypeError,
1018 "For setting vector<long> field, specified value must be a sequence." );
1022 Py_ssize_t length = PySequence_Length(value);
1023 vector<long> _value;
1024 for (
int ii = 0; ii < length; ++ii)
1026 PyObject * vo = PySequence_GetItem(value, ii);
1027 long v = PyInt_AsLong(vo);
1029 _value.push_back(v);
1037 if (!PySequence_Check(value))
1039 PyErr_SetString(PyExc_TypeError,
"For setting vector<unsigned int> field, specified value must be a sequence." );
1043 Py_ssize_t length = PySequence_Length(value);
1044 vector<unsigned int> _value;
1045 for (
int ii = 0; ii < length; ++ii)
1047 PyObject * vo = PySequence_GetItem(value, ii);
1048 unsigned int v = PyInt_AsUnsignedLongMask(vo);
1050 _value.push_back(v);
1058 if (!PySequence_Check(value))
1060 PyErr_SetString(PyExc_TypeError,
"For setting vector<unsigned long> field, specified value must be a sequence." );
1064 Py_ssize_t length = PySequence_Length(value);
1065 vector<unsigned long> _value;
1066 for (
int ii = 0; ii < length; ++ii)
1068 PyObject * vo = PySequence_GetItem(value, ii);
1069 unsigned long v = PyInt_AsUnsignedLongMask(vo);
1071 _value.push_back(v);
1079 if (!PySequence_Check(value))
1081 PyErr_SetString(PyExc_TypeError,
"For setting vector<float> field, specified value must be a sequence." );
1085 Py_ssize_t length = PySequence_Length(value);
1086 vector<float> _value;
1087 for (
int ii = 0; ii < length; ++ii)
1089 PyObject * vo = PySequence_GetItem(value, ii);
1090 float v = PyFloat_AsDouble(vo);
1092 _value.push_back(v);
1100 if (!PySequence_Check(value))
1102 PyErr_SetString(PyExc_TypeError,
"For setting vector<string> field, specified value must be a sequence." );
1106 Py_ssize_t length = PySequence_Length(value);
1107 vector<string> _value;
1108 for (
int ii = 0; ii < length; ++ii)
1110 PyObject * vo = PySequence_GetItem(value, ii);
1111 char * v = PyString_AsString(vo);
1113 _value.push_back(
string(v));
1121 vector < vector <unsigned> > * _value = (vector < vector <unsigned> > *)
to_cpp(value, ftype);
1122 if (!PyErr_Occurred())
1131 vector < vector <int> > * _value = (vector < vector <int> > *)
to_cpp(value, ftype);
1132 if (!PyErr_Occurred())
1141 vector < vector <double> > * _value = (vector < vector <double> > *)
to_cpp(value, ftype);
1142 if (!PyErr_Occurred())
1151 if (!PySequence_Check(value))
1153 PyErr_SetString(PyExc_TypeError,
"For setting vector<Id> field, specified value must be a sequence." );
1157 Py_ssize_t length = PySequence_Length(value);
1159 for (
int ii = 0; ii < length; ++ii)
1161 PyObject * vo = PySequence_GetItem(value, ii);
1164 _value.push_back(v);
1172 if (!PySequence_Check(value))
1174 PyErr_SetString(PyExc_TypeError,
"For setting vector<ObjId> field, specified value must be a sequence." );
1178 Py_ssize_t length = PySequence_Length(value);
1179 vector<ObjId> _value;
1180 for (
int ii = 0; ii < length; ++ii)
1182 PyObject * vo = PySequence_GetItem(value, ii);
1185 _value.push_back(v);
1204 msg <<
"Failed to set field '" << field <<
"'";
1205 PyErr_SetString(PyExc_AttributeError,msg.str().c_str());
1218 PyErr_SetString(PyExc_IndexError,
"Index out of bounds.");
1236 ret->
oid_ =
ObjId(self->oid_.id, self->oid_.dataIndex, index);
1237 return (PyObject*)ret;
1255 return PyTuple_New(0);
1257 PyObject * ret = PyTuple_New((Py_ssize_t)(end - start));
1260 for (
int ii = start; ii < end; ++ii)
1263 value->
oid_ =
ObjId(self->oid_.id, self->oid_.dataIndex, ii);
1264 if (PyTuple_SetItem(ret, (Py_ssize_t)(ii-start), (PyObject*)value))
1268 PyErr_SetString(PyExc_RuntimeError,
"Failed to assign tuple entry.");
1278 Element * el =
self->oid_.element();
1288 return (Py_ssize_t)(fe->
numData());
1297 vector<string> type_vec;
1300 ostringstream
error;
1301 error <<
"Cannot handle key type for LookupField `" <<
Field<string>::get(target,
"className") <<
"." << fieldName <<
"`.";
1302 PyErr_SetString(PyExc_TypeError, error.str().c_str());
1305 if (type_vec.size() != 2)
1307 ostringstream
error;
1308 error <<
"LookupField type signature should be <keytype>, <valuetype>. But for `"
1309 <<
Field<string>::get(target,
"className") <<
"." << fieldName <<
"` got " << type_vec.size() <<
" components." ;
1310 PyErr_SetString(PyExc_AssertionError, error.str().c_str());
1313 PyObject * ret = NULL;
1314 char key_type_code =
shortType(type_vec[0]);
1315 char value_type_code =
shortType(type_vec[1]);
1316 switch(key_type_code)
1320 ret = lookup_value <bool> (target, string(fieldName), value_type_code, key_type_code, key);
1325 ret = lookup_value <char> (target, string(fieldName), value_type_code, key_type_code, key);
1330 ret = lookup_value <short> (target, string(fieldName), value_type_code, key_type_code, key);
1335 ret = lookup_value <unsigned short> (target, string(fieldName), value_type_code, key_type_code, key);
1340 ret = lookup_value <int> (target, string(fieldName), value_type_code, key_type_code, key);
1345 ret = lookup_value <unsigned int> (target, string(fieldName), value_type_code, key_type_code, key);
1350 ret = lookup_value <long> (target, string(fieldName), value_type_code, key_type_code, key);
1355 ret = lookup_value <unsigned long> (target, string(fieldName), value_type_code, key_type_code, key);
1360 ret = lookup_value <long long> (target, string(fieldName), value_type_code, key_type_code, key);
1365 ret = lookup_value <unsigned long long> (target, string(fieldName), value_type_code, key_type_code, key);
1370 ret = lookup_value <double> (target, string(fieldName), value_type_code, key_type_code, key);
1375 ret = lookup_value <float> (target, string(fieldName), value_type_code, key_type_code, key);
1380 ret = lookup_value <string> (target, string(fieldName), value_type_code, key_type_code, key);
1385 ret = lookup_value <Id> (target, string(fieldName), value_type_code, key_type_code, key);
1390 ret = lookup_value <ObjId> (target, string(fieldName), value_type_code, key_type_code, key);
1395 ret = lookup_value < vector <double> >(target, string(fieldName), value_type_code, key_type_code, key);
1400 ret = lookup_value < vector <string> >(target, string(fieldName), value_type_code, key_type_code, key);
1405 ret = lookup_value < vector <Id> >(target, string(fieldName), value_type_code, key_type_code, key);
1410 ret = lookup_value < vector <ObjId> >(target, string(fieldName), value_type_code, key_type_code, key);
1415 ret = lookup_value < vector <int> >(target, string(fieldName), value_type_code, key_type_code, key);
1420 ret = lookup_value < vector <unsigned int> >(target, string(fieldName), value_type_code, key_type_code, key);
1425 ret = lookup_value < vector <unsigned long> >(target, string(fieldName), value_type_code, key_type_code, key);
1430 ret = lookup_value < vector <float> >(target, string(fieldName), value_type_code, key_type_code, key);
1435 ret = lookup_value < vector <short> >(target, string(fieldName), value_type_code, key_type_code, key);
1440 ret = lookup_value < vector <char> >(target, string(fieldName), value_type_code, key_type_code, key);
1444 ostringstream
error;
1445 error <<
"Unhandled key type `" << type_vec[0] <<
"` for " <<
Field<string>::get(target,
"className") <<
"." << fieldName;
1446 PyErr_SetString(PyExc_TypeError, error.str().c_str());
1452 "getLookupField(fieldname, key) -> value type\n"
1454 "Lookup entry for `key` in `fieldName`\n"
1458 "fieldname : string\n"
1459 " Name of the lookupfield.\n"
1461 "key : appropriate type for key of the lookupfield (as in the dict "
1463 " Key for the look-up.");
1471 char * fieldName = NULL;
1472 PyObject * key = NULL;
1473 if (!PyArg_ParseTuple(args,
"sO:moose_ObjId_getLookupField", &fieldName, &key))
1482 vector<string> type_vec;
1485 ostringstream
error;
1486 error <<
"Cannot handle key type for LookupField `" <<
Field<string>::get(target,
"className") <<
"." << fieldName <<
"`.";
1487 PyErr_SetString(PyExc_TypeError, error.str().c_str());
1490 if (type_vec.size() != 2)
1492 ostringstream
error;
1493 error <<
"LookupField type signature should be <keytype>, <valuetype>. But for `"
1494 <<
Field<string>::get(target,
"className") <<
"." << fieldName <<
"` got " << type_vec.size() <<
" components." ;
1495 PyErr_SetString(PyExc_AssertionError, error.str().c_str());
1498 char key_type_code =
shortType(type_vec[0]);
1499 char value_type_code =
shortType(type_vec[1]);
1501 switch(key_type_code)
1505 ret = set_lookup_value <unsigned int> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1510 ret = set_lookup_value <unsigned long> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1515 ret = set_lookup_value <string> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1520 ret = set_lookup_value <int> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1525 ret = set_lookup_value <long> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1530 ret = set_lookup_value <long long> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1535 ret = set_lookup_value <unsigned long long> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1540 ret = set_lookup_value <bool> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1545 ret = set_lookup_value <char> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1550 ret = set_lookup_value <short> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1555 ret = set_lookup_value <unsigned short> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1560 ret = set_lookup_value <double> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1565 ret = set_lookup_value <float> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1570 ret = set_lookup_value <Id> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1575 ret = set_lookup_value <ObjId> (target, string(fieldName), value_type_code, key_type_code, key,
value);
1579 ostringstream
error;
1580 error <<
"setLookupField: invalid key type " << type_vec[0];
1581 PyErr_SetString(PyExc_TypeError, error.str().c_str());
1587 "setLookupField(fieldname, key, value)\n"
1589 "Set a lookup field entry.\n"
1594 " name of the field to be set\n"
1596 " key in the lookup field for which the value is to be set.\n"
1597 "value : value type\n"
1598 " value to be set for `key` in the lookup field.\n");
1609 if (!PyArg_ParseTuple(args,
"sOO:moose_ObjId_setLookupField", &field, &key, &value))
1621 "setDestField(arg0, arg1, ...)\n"
1623 "Set a destination field. This is for advanced uses. destFields can\n"
1624 "(and should) be directly called like functions as\n"
1625 "`element.fieldname(arg0, ...)`\n"
1629 "The number and type of paramateres depend on the destFinfo to be\n"
1630 "set. Use moose.doc('{classname}.{fieldname}') to get builtin\n"
1631 "documentation on the destFinfo `fieldname`\n"
1640 PyObject * arglist[10] = {NULL, NULL, NULL, NULL, NULL,
1641 NULL, NULL, NULL, NULL, NULL
1643 ostringstream
error;
1646 error <<
"moose.setDestField: ";
1649 &arglist[0], &arglist[1], &arglist[2],
1650 &arglist[3], &arglist[4], &arglist[5],
1651 &arglist[6], &arglist[7], &arglist[8],
1654 error <<
"At most " <<
maxArgs - 1 <<
" arguments can be handled.";
1655 PyErr_SetString(PyExc_ValueError, error.str().c_str());
1660 char * fieldName = PyString_AsString(arglist[0]);
1663 error <<
"first argument must be a string specifying field name.";
1664 PyErr_SetString(PyExc_TypeError, error.str().c_str());
1669 vector< string > argType;
1671 "destFinfo",
string(fieldName), argType) < 0)
1673 error <<
"Arguments not handled: " << fieldName <<
"(";
1674 for (
unsigned int ii = 0; ii < argType.size(); ++ii)
1676 error << argType[ii] <<
",";
1679 PyErr_SetString(PyExc_TypeError, error.str().c_str());
1682 if (argType.size() == 1)
1684 if ( arglist[1] == NULL && argType[0] ==
"void")
1696 return setDestFinfo(oid,
string(fieldName), arglist[1], argType[0]);
1698 else if (argType.size() == 2)
1704 error <<
"Can handle only up to 2 arguments" << endl;
1713 ostringstream
error;
1714 error <<
"moose.setDestFinfo: ";
1721 double param = PyFloat_AsDouble(arg);
1722 if (typecode ==
'f')
1734 char * param = PyString_AsString(arg);
1741 long param = PyInt_AsLong(arg);
1742 if (param == -1 && PyErr_Occurred())
1746 if (typecode ==
'i')
1759 unsigned long param =PyLong_AsUnsignedLong(arg);
1760 if (PyErr_Occurred())
1764 if (typecode ==
'I')
1780 error <<
"argument should be an vec or an melement";
1781 PyErr_SetString(PyExc_TypeError, error.str().c_str());
1794 error <<
"argument should be vec or an melement";
1795 PyErr_SetString(PyExc_TypeError, error.str().c_str());
1804 char * param = PyString_AsString(arg);
1807 error <<
"expected argument of type char/string";
1808 PyErr_SetString(PyExc_TypeError, error.str().c_str());
1811 else if (strlen(param) == 0)
1813 error <<
"Empty string not allowed.";
1814 PyErr_SetString(PyExc_ValueError, error.str().c_str());
1826 return _set_vector_destFinfo<int>(obj, string(fieldName), arg, typecode);
1830 return _set_vector_destFinfo<short>(obj, string(fieldName), arg, typecode);
1834 return _set_vector_destFinfo<long>(obj, string(fieldName), arg, typecode);
1838 return _set_vector_destFinfo<unsigned int>(obj, string(fieldName), arg, typecode);
1842 return _set_vector_destFinfo<unsigned long>(obj, string(fieldName), arg, typecode);
1846 return _set_vector_destFinfo<float>(obj, string(fieldName), arg, typecode);
1850 return _set_vector_destFinfo<double>(obj, string(fieldName), arg, typecode);
1854 return _set_vector_destFinfo<string>(obj, string(fieldName), arg, typecode);
1858 return _set_vector_destFinfo<Id>(obj, string(fieldName), arg, typecode);
1862 return _set_vector_destFinfo<ObjId>(obj, string(fieldName), arg, typecode);
1866 error <<
"Cannot handle argument type: " << argType;
1867 PyErr_SetString(PyExc_TypeError, error.str().c_str());
1886 PyObject*
setDestFinfo2(
ObjId obj,
string fieldName, PyObject * arg1,
char type1, PyObject * arg2,
char type2)
1888 ostringstream
error;
1889 error <<
"moose.setDestFinfo2: ";
1895 double param = PyFloat_AsDouble(arg2);
1898 return setDestFinfo1<float>(obj, fieldName, arg1, type1, (float)param);
1902 return setDestFinfo1<double>(obj, fieldName, arg1, type1, param);
1907 char * param = PyString_AsString(arg2);
1908 return setDestFinfo1<string>(obj, fieldName, arg1, type1, string(param));
1913 long param = PyInt_AsLong(arg2);
1914 if (param == -1 && PyErr_Occurred())
1920 return setDestFinfo1< int>(obj, fieldName, arg1, type1, (int)param);
1924 return setDestFinfo1< long>(obj, fieldName, arg1, type1, param);
1930 unsigned long param =PyLong_AsUnsignedLong(arg2);
1931 if (PyErr_Occurred())
1937 return setDestFinfo1< unsigned int>(obj, fieldName, arg1, type1, (
unsigned int)param);
1941 return setDestFinfo1< unsigned long>(obj, fieldName, arg1, type1, param);
1951 error <<
"argument should be an vec or an melement";
1952 PyErr_SetString(PyExc_TypeError, error.str().c_str());
1965 return setDestFinfo1< Id>(obj, fieldName, arg1, type1, param);
1982 error <<
"argument should be an vec or an melement";
1983 PyErr_SetString(PyExc_TypeError, error.str().c_str());
1988 return setDestFinfo1< ObjId>(obj, fieldName, arg1, type1, param);
1992 char * param = PyString_AsString(arg2);
1995 error <<
"expected argument of type char/string";
1996 PyErr_SetString(PyExc_TypeError, error.str().c_str());
1999 else if (strlen(param) == 0)
2001 error <<
"Empty string not allowed.";
2002 PyErr_SetString(PyExc_ValueError, error.str().c_str());
2005 return setDestFinfo1< char>(obj, fieldName, arg1, type1, param[0]);
2009 error <<
"Unhandled argument 2 type (shortType=" << type2 <<
")";
2010 PyErr_SetString(PyExc_TypeError, error.str().c_str());
2018 "getFieldNames(fieldType='') -> tuple of str\n"
2020 "Returns the names of fields of this element of fieldType kind.\n"
2025 " Type of the fields you wish to retrieve. Can be\n"
2026 " - `valueFinfo` - attributes of the object\n"
2027 " - `srcFinfo` - fields of the object which can be used as source of information for connect\n"
2028 " - `destFinfo` - fields of the object which can be used as destination of information for connect\n"
2029 " - `lookupFinfo`- fields which can be looked at through this object"
2030 ", etc. If an empty string is specified, names of all avaialable fields are returned.\n"
2034 "names : tuple of strings.\n"
2035 " names of the fields of the specified type.\n"
2039 "List names of all the source fields in PulseGen class:\n"
2041 " >>> comp.getFieldNames('lookupFinfo') \n"
2042 " ('neighbors', 'msgDests', 'msgDestFunctions', 'isA')\n"
2043 " >>> moose.getFieldNames('PulseGen', 'srcFinfo')\n"
2044 " ('childMsg', 'output')\n"
2053 char * ftype = NULL;
2054 if (!PyArg_ParseTuple(args,
"|s:moose_ObjId_getFieldNames", &ftype))
2058 string ftype_str = (ftype != NULL)?
string(ftype):
"";
2061 if (ftype_str ==
"")
2065 vector<string> fields =
getFieldNames(className,
string(*a));
2066 ret.insert(ret.end(), fields.begin(), fields.end());
2074 PyObject * pyret = PyTuple_New((Py_ssize_t)ret.size());
2076 for (
unsigned int ii = 0; ii < ret.size(); ++ ii )
2078 PyObject * fname = Py_BuildValue(
"s", ret[ii].c_str());
2085 if (PyTuple_SetItem(pyret, (Py_ssize_t)ii, fname))
2097 "getNeighbors(fieldName) -> tuple of vecs\n"
2099 "Get the objects connected to this element on specified field.\n"
2104 " name of the connection field (a destFinfo/srcFinfo/sharedFinfo)\n"
2108 "neighbors: tuple of vecs.\n"
2109 " tuple containing the ids of the neighbour vecs.\n"
2118 char * field = NULL;
2119 if (!PyArg_ParseTuple(args,
"s:moose_ObjId_getNeighbors", &field))
2125 PyObject * ret = PyTuple_New((Py_ssize_t)val.size());
2127 for (
unsigned int ii = 0; ii < val.size(); ++ ii )
2130 if (!entry || PyTuple_SetItem(ret, (Py_ssize_t)ii, (PyObject*)entry))
2137 entry->
id_ = val[ii];
2148 "connect(src, srcfield, destobj, destfield[,msgtype]) -> bool\n"
2150 "Create a message between `src_field` on `src` object to `dest_field` on `dest` object.\n"
2151 "This function is used mainly, to say, connect two entities, and to denote what kind of give-and-take relationship they share."
2152 "It enables the 'destfield' (of the 'destobj') to acquire the data, from 'srcfield'(of the 'src')."
2156 "src : element/vec/string\n"
2157 " the source object (or its path) \n"
2158 " (the one that provides information)\n"
2160 " source field on self.(type of the information)\n"
2161 "destobj : element\n"
2162 " Destination object to connect to.\n"
2163 " (The one that need to get information)\n"
2165 " field to connect to on `destobj`.\n"
2167 " type of the message. Can be \n"
2174 " Default: `Single`.\n"
2178 "msgmanager: melement\n"
2179 " message-manager for the newly created message.\n"
2183 "Connect the output of a pulse generator to the input of a spike\n"
2186 " >>> pulsegen = moose.PulseGen('pulsegen')\n"
2187 " >>> spikegen = moose.SpikeGen('spikegen')\n"
2188 " >>> pulsegen.connect('output', spikegen, 'Vm')\n"
2202 PyObject * destPtr = NULL;
2203 char * srcField = NULL, * destField = NULL, * msgType = NULL;
2204 static char default_msg_type[] =
"Single";
2205 if(!PyArg_ParseTuple(args,
2206 "sOs|s:moose_ObjId_connect",
2214 if (msgType == NULL)
2216 msgType = default_msg_type;
2226 PyErr_SetString(PyExc_NameError,
2227 "connect failed: check field names and type compatibility.");
2231 msgMgrId->
oid_ = mid;
2232 return (PyObject*)msgMgrId;
2236 "Compare two element instances. This just does a string comparison of\n"
2237 "the paths of the element instances. This function exists only to\n"
2238 "facilitate certain operations requiring sorting/comparison, like\n"
2239 "using elements for dict keys. Conceptually only equality comparison is\n"
2240 "meaningful for elements.\n");
2248 if ((
self != NULL && other == NULL) || (
self == NULL && other != NULL))
2254 else if (op == Py_NE)
2260 PyErr_SetString(PyExc_TypeError,
"Cannot compare NULL with non-NULL");
2264 if (!PyObject_IsInstance(other, (PyObject*)&ObjIdType))
2266 ostringstream
error;
2267 error <<
"Cannot compare ObjId with "
2268 << Py_TYPE(other)->tp_name;
2269 PyErr_SetString(PyExc_TypeError, error.str().c_str());
2277 string l_path =
self->oid_.path();
2278 string r_path = ((
_ObjId*)other)->oid_.path();
2279 int result = l_path.compare(r_path);
2282 if (op == Py_EQ || op == Py_LE || op == Py_GE)
2288 else if (result < 0)
2290 if (op == Py_LT || op == Py_LE || op == Py_NE)
2298 if (op == Py_GT || op == Py_GE || op == Py_NE)
2307 "getDataIndex() -> int\n"
2309 "Returns the dataIndex (position of the object in vector) "
2310 "of this object, if it belongs to a vector, otherwise returns 0.\n"
2312 " >>> comp = moose.Compartment('/comp')\n"
2313 " >>> comp.getDataIndex()\n"
2323 PyObject * ret = Py_BuildValue(
"I", self->oid_.dataIndex);
2336 PyObject * ret = Py_BuildValue(
"I", self->oid_.dataIndex);
2348 moose_ObjId_getFieldType_documentation
2352 moose_ObjId_getField_documentation
2356 moose_ObjId_setField_documentation
2360 moose_ObjId_getLookupField_documentation
2364 moose_ObjId_setLookupField_documentation
2368 moose_ObjId_getId_documentation
2372 "Return the vec this element belongs to. This is overridden by the"
2373 " attribute of the same name for quick access."
2377 moose_ObjId_getFieldNames_documenation
2381 moose_ObjId_getNeighbors_documentation
2385 moose_ObjId_connect_documentation
2389 moose_ObjId_getDataIndex_documentation
2393 "Get the index of this object as a field."
2397 moose_ObjId_setDestField_documentation
2399 {NULL, NULL, 0, NULL},
2407 "Individual moose element contained in an array-type object"
2410 "Each element has a unique path, possibly with its index in"
2411 " the vec. These are identified by three components: vec, dndex and"
2412 " findex. vec is the containing vec, which is identified by a unique"
2413 " number (field `value`). `dindex` is the index of the current"
2414 " item in the containing vec. `dindex` is 0 for single elements."
2415 " findex is field index, specifying the index of elements which exist"
2416 " as fields of another moose element.\n"
2420 "Users need not create melement directly. Instead use the named moose"
2421 " class for creating new elements. To get a reference to an existing"
2422 " element, use the :ref:`moose.element` function.\n"
2427 " path of the element to be created.\n"
2429 "n : positive int, optional\n"
2430 " defaults to 1. If greater, then a vec of that size is created and\n"
2431 " this element is a reference to the first entry of the vec.\n"
2433 "g : int, optional\n"
2434 " if 1, this is a global element, else if 0 (default), the element\n"
2435 " is local to the current node.\n"
2438 " name of the class of the element. If creating any concrete\n"
2439 " subclass, this is automatically taken from the class name and\n"
2440 " should not be specified explicitly.\n"
2445 " The vec containing this element. `vec` wraps the Id of the object in MOOSE C++ API.\n"
2448 " index of this element in the container vec\n"
2451 " if this is a tertiary object, i.e. acts as a field in another\n"
2452 " element (like synapse[0] in IntFire[1]), then the index of\n"
2453 " this field in the containing element.\n"
2458 ">>> a = Neutral('alpha') # Creates element named `alpha` under current working element\n"
2459 ">>> b = Neutral('alpha/beta') # Creates the element named `beta` under `alpha`\n"
2460 ">>> c = moose.melement(b)"
2465 PyVarObject_HEAD_INIT(NULL, 0)
2484 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
2485 moose_ObjId_documentation,
PyObject * moose_ObjId_getNeighbors(_ObjId *self, PyObject *args)
PyObject * moose_ObjId_richcompare(_ObjId *self, PyObject *other, int op)
const char ** getFinfoTypes()
finalize()
PyObject * moose_ObjId_setLookupField(_ObjId *self, PyObject *args)
PyObject * getLookupField(ObjId target, char *fieldName, PyObject *key)
PyObject * moose_ObjId_getSlice(_ObjId *self, Py_ssize_t start, Py_ssize_t end)
static double op(double x)
PyObject * moose_ObjId_setDestField(_ObjId *self, PyObject *args)
PyObject * moose_ObjId_getFieldType(_ObjId *self, PyObject *args)
map< string, PyTypeObject * > & get_moose_classes()
PyObject * moose_ObjId_setField(_ObjId *self, PyObject *args)
int moose_ObjId_init_from_id(_ObjId *self, PyObject *args, PyObject *kwargs)
int moose_ObjId_init_from_path(_ObjId *self, PyObject *args, PyObject *kwargs)
const map< string, string > & get_field_alias()
static bool set(const ObjId &dest, const string &field, A arg)
Py_ssize_t moose_ObjId_getLength(_ObjId *self)
#define RAISE_INVALID_ID(ret, msg)
PyObject * moose_ObjId_getFieldIndex(_ObjId *self)
void * to_cpp(PyObject *object, char typecode)
PyObject * get_ObjId_attr(_ObjId *oid, string attribute)
PyObject * moose_ObjId_repr(_ObjId *self)
PyObject * moose_ObjId_getDataIndex(_ObjId *self)
static PyMethodDef ObjIdMethods[]
Id create_Id_from_path(string path, unsigned int numData, unsigned int isGlobal, string type)
PyObject * moose_ObjId_getattro(_ObjId *self, PyObject *attr)
long moose_ObjId_hash(_ObjId *self)
virtual bool hasFields() const =0
PyObject * moose_ObjId_str(_ObjId *self)
int setLookupField(ObjId target, char *fieldName, PyObject *key, PyObject *value)
int moose_ObjId_init(_ObjId *self, PyObject *args, PyObject *kwargs)
PyObject * moose_ObjId_getId(_ObjId *self)
string getFieldType(string className, string fieldName)
PyObject * setDestFinfo2(ObjId obj, string fieldName, PyObject *arg1, char type1, PyObject *arg2, char type2)
static bool isValid(Id id)
int parseFinfoType(string className, string finfoType, string fieldName, vector< string > &typeVec)
static bool set(const ObjId &dest, const string &field)
PyDoc_STRVAR(moose_ObjId_getId_documentation,"getId() -> vec\n""\n""Returns the information of the object's classtype, Id, and path \n""in form of a vector.\n""\nExample\n""-------\n"" >>> com = moose.Compartment('/com')\n"" >>> com.getId()\n"" moose.vec: class=Compartment, id=481, path=/com>""\n")
std::string fix(const std::string userPath, const string &delimiters)
int PyType_IsSubtype(PyTypeObject *, PyTypeObject *)
unsigned int numData() const
Virtual: Returns number of data entries.
static bool set(const ObjId &dest, const string &field, A arg)
PyObject * moose_ObjId_getFieldNames(_ObjId *self, PyObject *args)
PyObject * to_py(void *obj, char typecode)
PyTypeObject * getBaseClass(PyObject *self)
static A get(const ObjId &dest, const string &field)
PyObject * moose_ObjId_connect(_ObjId *self, PyObject *args)
PyObject * moose_ObjId_getItem(_ObjId *self, Py_ssize_t index)
int moose_ObjId_setattro(_ObjId *self, PyObject *attr, PyObject *value)
vector< string > getFieldNames(string className, string finfoType)
const unsigned int BADINDEX
Used by ObjId and Eref.
PyObject * moose_ObjId_getLookupField(_ObjId *self, PyObject *args)
PyObject * setDestFinfo(ObjId obj, string fieldName, PyObject *arg, string argType)
PyObject * moose_ObjId_getField(_ObjId *self, PyObject *args)