Datum ComponentRegistry::getNumber(std::string expression, GenericDataType type){ Datum value; switch(type){ case M_FLOAT: case M_INTEGER: case M_BOOLEAN: case M_STRING: value = getValue(expression, type); if (value.getDataType() == type) { return value; } break; default: throw FatalParserException("Attempt to cast a number of invalid type"); } switch (type){ case M_FLOAT: return Datum(value.getFloat()); case M_INTEGER: return Datum(value.getInteger()); case M_STRING: return Datum(value.getString()); case M_BOOLEAN: return Datum(value.getBool()); default: return value; } }
// overridable base class method // assumes data have already been checked for proper dictionary, name. CalibratorRequestedAction Calibrator::getRequestedAction(Datum dictionaryData) { // check what action is requested (e.g. update parameters) if (!(dictionaryData.hasKey(R_CALIBRATOR_ACTION))) { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Request sent to calibrator %s that did not contain an action field was ignored.", uniqueCalibratorName.c_str()); return(CALIBRATOR_NO_ACTION); } Datum actionData = dictionaryData.getElement(R_CALIBRATOR_ACTION); if (!(actionData.getDataType() == M_STRING)) { // check if name field is a string mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Request sent to calibrator %s that did not contain a string in the action field was ignored.", uniqueCalibratorName.c_str()); return(CALIBRATOR_NO_ACTION); } if (actionData.getString() == R_CALIBRATOR_ACTION_SET_PARAMETERS) { // check is name field matches the name of this calibrator if (VERBOSE_EYE_CALIBRATORS>1) mprintf( "Calibrator %s successfully received request for to update its parameters to contained values.", uniqueCalibratorName.c_str()); return CALIBRATOR_ACTION_SET_PARAMS_TO_CONTAINED; } else if (actionData.getString() == R_CALIBRATOR_ACTION_SET_PARAMETERS_TO_DEFAULTS) { if (VERBOSE_EYE_CALIBRATORS>1) mprintf("Calibrator %s successfully received request for to update its parameters to defaults.", uniqueCalibratorName.c_str()); return CALIBRATOR_ACTION_SET_PARAMS_TO_DEFAULTS; } else { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Calibrator %s received a request, but action was unknown. Request ignored", uniqueCalibratorName.c_str()); return CALIBRATOR_NO_ACTION; } return (CALIBRATOR_NO_ACTION); }
void StimulusDisplay::getDisplayBounds(const Datum &display_info, GLdouble &left, GLdouble &right, GLdouble &bottom, GLdouble &top) { if(display_info.getDataType() == M_DICTIONARY && display_info.hasKey(M_DISPLAY_WIDTH_KEY) && display_info.hasKey(M_DISPLAY_HEIGHT_KEY) && display_info.hasKey(M_DISPLAY_DISTANCE_KEY)){ GLdouble width_unknown_units = display_info.getElement(M_DISPLAY_WIDTH_KEY); GLdouble height_unknown_units = display_info.getElement(M_DISPLAY_HEIGHT_KEY); GLdouble distance_unknown_units = display_info.getElement(M_DISPLAY_DISTANCE_KEY); GLdouble half_width_deg = (180. / M_PI) * atan((width_unknown_units/2.)/distance_unknown_units); GLdouble half_height_deg = half_width_deg * height_unknown_units / width_unknown_units; //GLdouble half_height_deg = (180. / M_PI) * atan((height_unknown_units/2.)/distance_unknown_units); left = -half_width_deg; right = half_width_deg; top = half_height_deg; bottom = -half_height_deg; } else { left = M_STIMULUS_DISPLAY_LEFT_EDGE; right = M_STIMULUS_DISPLAY_RIGHT_EDGE; top = M_STIMULUS_DISPLAY_TOP_EDGE; bottom = M_STIMULUS_DISPLAY_BOTTOM_EDGE; } }
bool ExperimentUnpackager::createFile(Datum filename, Datum buffer) { if(buffer.getDataType() != M_STRING || filename.getDataType() != M_STRING) return false; boost::filesystem::path filePath(filename.getString()); boost::filesystem::create_directories(filePath.parent_path()); // create an output file. std::ofstream outFile(filePath.string().c_str()); //, ios::binary); //write the data outFile << buffer.getString(); // flush buffer outFile.flush(); // close the handle outFile.close(); return true; }
boost::python::object convert_datum_to_python(const Datum &datum) { ScopedRecursionGuard srg(" while converting MWorks datum to Python object"); if (datum.isUndefined()) { return boost::python::object(); } switch (datum.getDataType()) { case M_INTEGER: return convert_longlong_to_python(datum.getInteger()); case M_FLOAT: return manageNewRef( PyFloat_FromDouble(datum.getFloat()) ); case M_BOOLEAN: return manageNewRef( PyBool_FromLong(datum.getBool()) ); case M_STRING: { auto &str = datum.getString(); return manageNewRef( PyString_FromStringAndSize(str.c_str(), str.size()) ); } case M_LIST: { auto &listValue = datum.getList(); boost::python::object list = manageNewRef( PyList_New(listValue.size()) ); for (Py_ssize_t i = 0; i < listValue.size(); i++) { boost::python::object item = convert_datum_to_python(listValue.at(i)); // PyList_SetItem "steals" the item reference, so we need to INCREF it Py_INCREF(item.ptr()); if (PyList_SetItem(list.ptr(), i, item.ptr())) throw_error_already_set(); } return list; } case M_DICTIONARY: { boost::python::object dict = manageNewRef( PyDict_New() ); for (auto &item : datum.getDict()) { if (PyDict_SetItem(dict.ptr(), convert_datum_to_python(item.first).ptr(), convert_datum_to_python(item.second).ptr())) { throw_error_already_set(); } } return dict; } default: PyErr_Format(PyExc_TypeError, "Cannot convert Datum of unknown type (%d)", datum.getDataType()); throw_error_already_set(); return boost::python::object(); // Never reached } }
// this routine checks that the request is a dictionary and that it contains a name that matches the calibrator bool Calibrator::checkRequest(Datum dictionaryData) { Datum data; // to hold field data for checking // check if this is a dictionary if (!(dictionaryData.getDataType() == M_DICTIONARY)) { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Request sent to calibrator %s that was not expected dictionary type was ignored.", uniqueCalibratorName.c_str()); return(false); } // check if there is a name field and if this calibrator should respond (i.e. if it has the correct name) if (!(dictionaryData.hasKey(R_CALIBRATOR_NAME))) { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Request sent to calibrator %s that did not contain name field was ignored.", uniqueCalibratorName.c_str()); return(false); } Datum nameData = dictionaryData.getElement(R_CALIBRATOR_NAME); if (!(nameData.getDataType() == M_STRING)) { // check if name field is a string mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Request sent to calibrator %s that did not contain a string in the name field was ignored.", uniqueCalibratorName.c_str()); return(false); } if (uniqueCalibratorName == nameData.getString()) { // check is name field matches the name of this calibrator if (VERBOSE_EYE_CALIBRATORS) mprintf("Calibrator %s successfully received a properly named request.", uniqueCalibratorName.c_str()); } else { if (VERBOSE_EYE_CALIBRATORS) mprintf("Calibrator %s received a request, but name did not match.", uniqueCalibratorName.c_str()); return(false); // request not meant for this calibrator (can be normal behavior -- no warnings) } return true; }
// this routine handles both "requests" and loading of private data (stored params) // if a request, then priuvate values are probably in need of update // if a load of private, then private values are OK, but I can check this. void EyeCalibrator::tryToUseDataToSetParameters(Datum dictionaryData) { // check if this is a dictionary if (!(dictionaryData.getDataType() == M_DICTIONARY)) { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Data processed by calibrator %s that was not expected dictionary type was ignored.", uniqueCalibratorName.c_str()); return; } // try to perform the requested action bool paramsChanged = false; Datum paramData; // if appropriate param fields are present and have expected length, then use the data // to try to update the parameters // H params ================================================ if (!(dictionaryData.hasKey(R_CALIBRATOR_PARAMS_H))) { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Data processed to update params of calibrator %s without proper params filed was ignored.", uniqueCalibratorName.c_str()); return; } paramData = dictionaryData.getElement(R_CALIBRATOR_PARAMS_H); // check if vector and correct length if (paramData.getDataType() != M_LIST) { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Data processed to update params of calibrator %s that did not contain vector in params field was ignored.", uniqueCalibratorName.c_str()); return; } Datum paramsH = paramData; if (paramsH.getNElements() != (fitableFunctions->getElement(HfunctionIndex))->getNumParameters() ) { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Data processed to update params of calibrator %s that did not contain expected number of params was ignored.", uniqueCalibratorName.c_str()); return; } bool noErr = (fitableFunctions->getElement(HfunctionIndex))->setParameters(paramsH); if (noErr) paramsChanged = true; // params have been updated // V params ================================================ if (!(dictionaryData.hasKey(R_CALIBRATOR_PARAMS_V))) { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Data processed to update params of calibrator %s without proper params filed was ignored.", uniqueCalibratorName.c_str()); return; } paramData = dictionaryData.getElement(R_CALIBRATOR_PARAMS_V); // check if vector and correct length if (paramData.getDataType() != M_LIST) { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Data processed to update params of calibrator %s that did not contain vector in params field was ignored.", uniqueCalibratorName.c_str()); return; } Datum paramsV = paramData; if (paramsV.getNElements() != (fitableFunctions->getElement(VfunctionIndex))->getNumParameters() ) { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Data processed to update params of calibrator %s that did not contain expected number of params was ignored.", uniqueCalibratorName.c_str()); return; } noErr = (fitableFunctions->getElement(VfunctionIndex))->setParameters(paramsV); if (noErr) paramsChanged = true; // params have been updated // if any params were updated, announce the full set of current parameters // if this change was triggered by an external change to the private variable, then we do not need to update that private variable (and we cannot anyway -- it is locked) if (paramsChanged) reportParameterUpdate(); }
bool ExperimentUnpackager::unpackageExperiment(Datum payload) { namespace bf = boost::filesystem; if(payload.getDataType() != M_DICTIONARY) { merror(M_NETWORK_MESSAGE_DOMAIN, "Invalid payload type for experiment package"); return false; } Datum experimentFilePackage = payload.getElement(M_PACKAGER_EXPERIMENT_STRING); if(experimentFilePackage.getNElements() != M_EXPERIMENT_PACKAGE_NUMBER_ELEMENTS_PER_UNIT || experimentFilePackage.getDataType() != M_DICTIONARY) return false; Datum experimentFileName = experimentFilePackage.getElement(M_PACKAGER_FILENAME_STRING); if(experimentFileName.getDataType() != M_STRING || experimentFileName.getString().empty()) return false; bf::path experimentName(experimentFileName.getString()); loadedExperimentFilename = prependExperimentInstallPath(removeFileExtension(experimentName.string()), experimentName.string()); bf::path experimentPath = loadedExperimentFilename.branch_path(); createExperimentInstallDirectoryStructure(experimentName.string()); // create the XML file Datum experimentFileBuffer = experimentFilePackage.getElement(M_PACKAGER_CONTENTS_STRING); if(experimentFileBuffer.getDataType() != M_STRING || experimentFileBuffer.getString().empty()) return false; if(!(createFile(Datum(loadedExperimentFilename.string().c_str()), experimentFileBuffer))) { // failed to create experiment file merror(M_FILE_MESSAGE_DOMAIN, "Failed to create server side experiment file %s", loadedExperimentFilename.string().c_str()); return false; } // create all of the other media files Datum mediaFileList = payload.getElement(M_PACKAGER_MEDIA_BUFFERS_STRING); if(mediaFileList.isList()) { for(int i=0; i<mediaFileList.getNElements(); ++i) { Datum mediaFilePackage = mediaFileList.getElement(i); if(mediaFilePackage.getDataType() != M_DICTIONARY | mediaFilePackage.getNElements() != 2) { merror(M_FILE_MESSAGE_DOMAIN, "incorrectly packaged media files"); return false; } Datum mediaFileName = mediaFilePackage.getElement(M_PACKAGER_FILENAME_STRING); Datum mediaFileBuffer = mediaFilePackage.getElement(M_PACKAGER_CONTENTS_STRING); if(mediaFileName.getDataType() != M_STRING || mediaFileName.getString().empty() || mediaFileBuffer.getDataType() != M_STRING) return false; std::string filename(mediaFileName.getString()); std::string filenameWPath = experimentPath.string() + "/" + filename; if(!(createFile(Datum(filenameWPath.c_str()), mediaFileBuffer))) { // failed to create experiment file merror(M_FILE_MESSAGE_DOMAIN, "Failed to create server side experiment file %s", filenameWPath.c_str()); return false; } } } expandRangeReplicatorItems(loadedExperimentFilename); modifyExperimentMediaPaths(loadedExperimentFilename); return true; }