Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

simplereactionhandler.cpp

Go to the documentation of this file.
00001 // PTF - Psychological Test Framework 00002 // http://ptf.sourceforge.net 00003 // 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Library General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 00018 #include <ptf/core/simplereactionhandler.hpp> 00019 #include <ptf/core/ptfcore.hpp> 00020 #include <iostream> 00021 00022 // bring stuff into scope 00023 using std::cout; 00024 using std::endl; 00025 00029 SimpleReactionHandler::SimpleReactionHandler() { 00030 lastRRec = 0; 00031 } 00032 00036 SimpleReactionHandler::~SimpleReactionHandler() { 00037 // nothing to do here 00038 } 00039 00043 void SimpleReactionHandler::handle() { 00044 // get refernce to PtfCore 00045 PtfCore& ptf = PtfCore::getSingleton(); 00046 // get latest task record 00047 TaskRecord* taskRec = ptf.getTaskRecords().back(); 00048 // get buttons 00049 const vector<bool> buttonsSuccess = ptf.getTasks()[taskRec->getId()]->getSuccessfulReaction(); 00050 // calculate window in which reaction has to happen 00051 timespec executeStart, executeEnd, taskStart, taskEnd, dummy, reactionWindowStart, reactionWindowEnd; 00052 taskRec->getTimestampE().getInterval(executeStart, executeEnd); 00053 ptf.getTasks()[taskRec->getId()]->getReactionWindow().getInterval(reactionWindowStart, reactionWindowEnd); 00054 timespec windowStart = TimeInterval::addTimespecs(executeEnd, reactionWindowStart); 00055 timespec windowEnd = TimeInterval::addTimespecs(executeEnd, reactionWindowEnd); 00056 // get timespec of task start/end 00057 taskRec->getTimestampP().getInterval(taskStart, dummy); 00058 taskRec->getTimestampF().getInterval(dummy, taskEnd); 00059 // enter reaction record queue mutex while handling 00060 ptf.getReactionRecordsMutex().enterMutex(); 00061 // check for reactions 00062 if (lastRRec == (int)ptf.getReactionRecords().size()) { 00063 // no reactions 00064 if ((int)buttonsSuccess.size() == 0) { 00065 // no reaction is right reaction 00066 ptf.getHandlerLog() << "Reaction handler:\n" 00067 << "\tNo reactions. Correct.\n"; 00068 // machine log 00069 ptf.getMachineLog() << lastRRec << " " 00070 << (int)ptf.getTaskRecords().size() << " " << taskRec->getId() << " " 00071 << taskStart << " " << taskEnd << " " 00072 << windowStart << " "<< windowEnd 00073 << " 0 " << boolVectorToString(buttonsSuccess) << " 7 -1 -1 -1 -1\n"; 00074 } 00075 else { 00076 // no reaction is wrong reaction 00077 ptf.getHandlerLog() << "Reaction handler:\n" 00078 << "\tNo reactions. Incorrect.\n"; 00079 // machine log 00080 ptf.getMachineLog() << lastRRec << " " 00081 << (int)ptf.getTaskRecords().size() << " " << taskRec->getId() << " " 00082 << taskStart << " " << taskEnd << " " 00083 << windowStart << " "<< windowEnd 00084 << " 0 " << boolVectorToString(buttonsSuccess) << " 8 -1 -1 -1 -1\n"; 00085 } 00086 } 00087 else { 00088 // handle available reactions 00089 while (lastRRec < (int)ptf.getReactionRecords().size()) { 00090 // get latest unhandled reaction record 00091 ReactionRecord* reactRec = ptf.getReactionRecords()[lastRRec++]; 00092 // leave mutex during handling 00093 ptf.getReactionRecordsMutex().leaveMutex(); 00094 // get pressed buttons 00095 const vector<bool>& buttonsPressed = reactRec->getButtonStates(); 00096 // get relevant timespec (later timestamp of interval) 00097 timespec reactionTime; 00098 reactRec->getTimestamp().getInterval(dummy, reactionTime); 00099 // get delta of end of reaction interval to begin of reaction window 00100 timespec delta; 00101 delta = TimeInterval::subTimespecs(reactionTime, windowStart); 00102 // check timing 00103 if (TimeInterval::compareTimespecs(reactionTime, taskStart) == -1) { 00104 // reaction does not belong to task - too early 00105 ptf.getHandlerLog() << "Reaction handler:\n" 00106 << "\tReaction at " << reactRec->getTimestamp().toString() << "\n" 00107 << "\tTask at [" << taskStart << " | " << taskEnd << "]\n" 00108 << "\tWindow at [" << windowStart << " | " << windowEnd << "]\n" 00109 << "\tDelta to start of reaction window: " << delta << "\n" 00110 << "\tReaction was before start of task.\n"; 00111 // check buttons 00112 if (buttonsPressed == buttonsSuccess) { 00113 ptf.getHandlerLog() << "\tReaction was with right input.\n"; 00114 // machine log 00115 timespec a, b; 00116 reactRec->getTimestamp().getInterval(a, b); 00117 ptf.getMachineLog() << lastRRec << " " 00118 << (int)ptf.getTaskRecords().size() << " " << taskRec->getId() << " " 00119 << taskStart << " " << taskEnd << " " 00120 << windowStart << " " << windowEnd << " " 00121 << boolVectorToString(buttonsPressed) << " " 00122 << boolVectorToString(buttonsSuccess) << " 9 " 00123 << a << " " << b << "\n"; 00124 } 00125 else { 00126 ptf.getHandlerLog() << "\tReaction was with wrong input.\n"; 00127 // machine log 00128 timespec a, b; 00129 reactRec->getTimestamp().getInterval(a, b); 00130 ptf.getMachineLog() << lastRRec << " " 00131 << (int)ptf.getTaskRecords().size() << " " << taskRec->getId() << " " 00132 << taskStart << " " << taskEnd << " " 00133 << windowStart << " " << windowEnd << " " 00134 << boolVectorToString(buttonsPressed) << " " 00135 << boolVectorToString(buttonsSuccess) << " 10 " 00136 << a << " " << b << "\n"; 00137 } 00138 // re-enter mutex for next fetch 00139 ptf.getReactionRecordsMutex().enterMutex(); 00140 continue; 00141 } 00142 if (TimeInterval::compareTimespecs(reactionTime, taskEnd) == 1) { 00143 // reaction does not belong to task - too late 00144 ptf.getHandlerLog() << "Reaction handler:\n" 00145 << "\tReaction at " << reactRec->getTimestamp().toString() << "\n" 00146 << "\tTask at [" << taskStart << " | " << taskEnd << "]\n" 00147 << "\tWindow at [" << windowStart << " | " << windowEnd << "]\n" 00148 << "\tDelta to start of reaction window: " << delta << "\n" 00149 << "\tReaction was after end of task.\n"; 00150 // check buttons 00151 if (buttonsPressed == buttonsSuccess) { 00152 ptf.getHandlerLog() << "\tReaction was with right input.\n"; 00153 // machine log 00154 timespec a, b; 00155 reactRec->getTimestamp().getInterval(a, b); 00156 ptf.getMachineLog() << lastRRec << " " 00157 << (int)ptf.getTaskRecords().size() << " " << taskRec->getId() << " " 00158 << taskStart << " " << taskEnd << " " 00159 << windowStart << " " << windowEnd << " " 00160 << boolVectorToString(buttonsPressed) << " " 00161 << boolVectorToString(buttonsSuccess) << " 11 " 00162 << a << " " << b << "\n"; 00163 } 00164 else { 00165 ptf.getHandlerLog() << "\tReaction was with wrong input.\n"; 00166 // machine log 00167 timespec a, b; 00168 reactRec->getTimestamp().getInterval(a, b); 00169 ptf.getMachineLog() << lastRRec << " " 00170 << (int)ptf.getTaskRecords().size() << " " << taskRec->getId() << " " 00171 << taskStart << " " << taskEnd << " " 00172 << windowStart << " " << windowEnd << " " 00173 << boolVectorToString(buttonsPressed) << " " 00174 << boolVectorToString(buttonsSuccess) << " 12 " 00175 << a << " " << b << "\n"; 00176 } 00177 // re-enter mutex for next fetch 00178 ptf.getReactionRecordsMutex().enterMutex(); 00179 continue; 00180 } 00181 if (TimeInterval::compareTimespecs(reactionTime, windowStart) == -1) { 00182 // reaction does belong to task, but out of reaction window - too early 00183 ptf.getHandlerLog() << "Reaction handler:\n" 00184 << "\tReaction at " << reactRec->getTimestamp().toString() << "\n" 00185 << "\tTask at [" << taskStart << " | " << taskEnd << "]\n" 00186 << "\tWindow at [" << windowStart << " | " << windowEnd << "]\n" 00187 << "\tDelta to start of reaction window: " << delta << "\n" 00188 << "\tReaction was before start of reaction window.\n"; 00189 // check buttons 00190 if (buttonsPressed == buttonsSuccess) { 00191 ptf.getHandlerLog() << "\tReaction was with right input.\n"; 00192 // machine log 00193 timespec a, b; 00194 reactRec->getTimestamp().getInterval(a, b); 00195 ptf.getMachineLog() << lastRRec << " " 00196 << (int)ptf.getTaskRecords().size() << " " << taskRec->getId() << " " 00197 << taskStart << " " << taskEnd << " " 00198 << windowStart << " " << windowEnd << " " 00199 << boolVectorToString(buttonsPressed) << " " 00200 << boolVectorToString(buttonsSuccess) << " 3 " 00201 << a << " " << b << "\n"; 00202 } 00203 else { 00204 ptf.getHandlerLog() << "\tReaction was with wrong input.\n"; 00205 // machine log 00206 timespec a, b; 00207 reactRec->getTimestamp().getInterval(a, b); 00208 ptf.getMachineLog() << lastRRec << " " 00209 << (int)ptf.getTaskRecords().size() << " " << taskRec->getId() << " " 00210 << taskStart << " " << taskEnd << " " 00211 << windowStart << " " << windowEnd << " " 00212 << boolVectorToString(buttonsPressed) << " " 00213 << boolVectorToString(buttonsSuccess) << " 4 " 00214 << a << " " << b << "\n"; 00215 } 00216 // re-enter mutex for next fetch 00217 ptf.getReactionRecordsMutex().enterMutex(); 00218 continue; 00219 } 00220 if (TimeInterval::compareTimespecs(reactionTime, windowEnd) == 1) { 00221 // reaction does belong to task, but out of reaction window - too late 00222 ptf.getHandlerLog() << "Reaction handler:\n" 00223 << "\tReaction at " << reactRec->getTimestamp().toString() << "\n" 00224 << "\tTask at [" << taskStart << " | " << taskEnd << "]\n" 00225 << "\tWindow at [" << windowStart << " | " << windowEnd << "]\n" 00226 << "\tDelta to start of reaction window: " << delta << "\n" 00227 << "\tReaction was after end of reaction window.\n"; 00228 // check buttons 00229 if (buttonsPressed == buttonsSuccess) { 00230 ptf.getHandlerLog() << "\tReaction was with right input.\n"; 00231 // machine log 00232 timespec a, b; 00233 reactRec->getTimestamp().getInterval(a, b); 00234 ptf.getMachineLog() << lastRRec << " " 00235 << (int)ptf.getTaskRecords().size() << " " << taskRec->getId() << " " 00236 << taskStart << " " << taskEnd << " " 00237 << windowStart << " " << windowEnd << " " 00238 << boolVectorToString(buttonsPressed) << " " 00239 << boolVectorToString(buttonsSuccess) << " 5 " 00240 << a << " " << b << "\n"; 00241 } 00242 else { 00243 ptf.getHandlerLog() << "\tReaction was with wrong input.\n"; 00244 // machine log 00245 timespec a, b; 00246 reactRec->getTimestamp().getInterval(a, b); 00247 ptf.getMachineLog() << lastRRec << " " 00248 << (int)ptf.getTaskRecords().size() << " " << taskRec->getId() << " " 00249 << taskStart << " " << taskEnd << " " 00250 << windowStart << " " << windowEnd << " " 00251 << boolVectorToString(buttonsPressed) << " " 00252 << boolVectorToString(buttonsSuccess) << " 6 " 00253 << a << " " << b << "\n"; 00254 } 00255 // re-enter mutex for next fetch 00256 ptf.getReactionRecordsMutex().enterMutex(); 00257 continue; 00258 } 00259 // reaction does belong to task and is in reaction window 00260 ptf.getHandlerLog() << "Reaction handler:\n" 00261 << "\tReaction at " << reactRec->getTimestamp().toString() << "\n" 00262 << "\tTask at [" << taskStart << " | " << taskEnd << "]\n" 00263 << "\tWindow at [" << windowStart << " | " << windowEnd << "]\n" 00264 << "\tDelta to start of reaction window: " << delta << "\n" 00265 << "\tReaction was in reaction window.\n"; 00266 // check buttons 00267 if (buttonsPressed == buttonsSuccess) { 00268 ptf.getHandlerLog() << "\tReaction was with right input.\n"; 00269 // machine log 00270 timespec a, b; 00271 reactRec->getTimestamp().getInterval(a, b); 00272 ptf.getMachineLog() << lastRRec << " " 00273 << (int)ptf.getTaskRecords().size() << " " << taskRec->getId() << " " 00274 << taskStart << " " << taskEnd << " " 00275 << windowStart << " " << windowEnd << " " 00276 << boolVectorToString(buttonsPressed) << " " 00277 << boolVectorToString(buttonsSuccess) << " 1 " 00278 << a << " " << b << "\n"; 00279 } 00280 else { 00281 ptf.getHandlerLog() << "\tReaction was with wrong input.\n"; 00282 // machine log 00283 timespec a, b; 00284 reactRec->getTimestamp().getInterval(a, b); 00285 ptf.getMachineLog() << lastRRec << " " 00286 << (int)ptf.getTaskRecords().size() << " " << taskRec->getId() << " " 00287 << taskStart << " " << taskEnd << " " 00288 << windowStart << " " << windowEnd << " " 00289 << boolVectorToString(buttonsPressed) << " " 00290 << boolVectorToString(buttonsSuccess) << " 2 " 00291 << a << " " << b << "\n"; 00292 } 00293 // re-enter mutex for next fetch 00294 ptf.getReactionRecordsMutex().enterMutex(); 00295 } 00296 } 00297 // finally leave mutex 00298 ptf.getReactionRecordsMutex().leaveMutex(); 00299 } 00300 00305 string SimpleReactionHandler::boolVectorToString(const vector<bool>& in) { 00306 string result = ""; 00307 if ((int)in.size() == 0) { 00308 result = "0"; 00309 return result; 00310 } 00311 for (int i = 0; i < (int)in.size(); i++) { 00312 in[i] ? result += "1" : result += "0"; 00313 } 00314 return result; 00315 } 00316

Generated on Fri Dec 17 14:54:23 2004 for Psychological Test Framework by doxygen 1.3.8