00001 /*************************************************************************** 00002 * SpikeStream Application * 00003 * Copyright (C) 2007 by David Gamez * 00004 * david@davidgamez.eu * 00005 * Version 0.1 * 00006 * * 00007 * This program is free software; you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation; either version 2 of the License, or * 00010 * (at your option) any later version. * 00011 * * 00012 * This program is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU General Public License * 00018 * along with this program; if not, write to the * 00019 * Free Software Foundation, Inc., * 00020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00021 ***************************************************************************/ 00022 00023 //SpikeStream includes 00024 #include "NetworkModelXmlHandler.h" 00025 #include "Utilities.h" 00026 #include "Debug.h" 00027 00028 //Other includes 00029 #include <iostream> 00030 using namespace std; 00031 00032 00033 /*! Constructor. */ 00034 NetworkModelXmlHandler::NetworkModelXmlHandler(MonitorArea *monArea) : QXmlDefaultHandler() { 00035 //Store reference to the monitor area 00036 monitorArea = monArea; 00037 } 00038 00039 00040 /*! Destructor. */ 00041 NetworkModelXmlHandler::~NetworkModelXmlHandler(){ 00042 #ifdef MEMORY_DEBUG 00043 cout<<"DELETING NETWORK MODEL XML HANDLER"<<endl; 00044 #endif//MEMORY_DEBUG 00045 } 00046 00047 00048 //------------------------------------------------------------------------- 00049 //-------------------------- PUBLIC METHODS ----------------------------- 00050 //------------------------------------------------------------------------- 00051 00052 /*! Returns a nicely formatted version of the network model XML. */ 00053 QString NetworkModelXmlHandler::getNetworkModelString(){ 00054 return networkModelString; 00055 } 00056 00057 00058 /*! Returns a vector containing details about the neuron groups that have been 00059 extracted from the XML. */ 00060 vector<NeuronGroup*> NetworkModelXmlHandler::getNeuronGrpVector(){ 00061 return neurGrpVect; 00062 } 00063 00064 00065 /*! Returns true if an error has been encountered during parsing. */ 00066 bool NetworkModelXmlHandler::getParseError(){ 00067 return parseError; 00068 } 00069 00070 00071 /*! Returns a string describing the parsing error. */ 00072 QString NetworkModelXmlHandler::getParseErrorString(){ 00073 return parseErrorString; 00074 } 00075 00076 00077 //------------------------------------------------------------------------- 00078 //------------------------- PROTECTED METHODS ----------------------------- 00079 //------------------------------------------------------------------------- 00080 00081 /*! Called when parser encounters characters. */ 00082 bool NetworkModelXmlHandler::characters(const QString& chars){ 00083 //Add chars to network model string 00084 networkModelString += chars; 00085 00086 //Process the string 00087 try{ 00088 if(currentElement == "name") 00089 neuronGrp->name = chars; 00090 else if(currentElement == "start_neuron_id") 00091 neuronGrp->startNeuronID = Utilities::getUInt(chars.ascii()); 00092 else if(currentElement == "width") 00093 neuronGrp->width = Utilities::getUInt(chars.ascii()); 00094 else if(currentElement == "length") 00095 neuronGrp->length = Utilities::getUInt(chars.ascii()); 00096 else if(currentElement == "location"){ 00097 QStringList tempStringList = QStringList::split(",", chars); 00098 neuronGrp->xPos = Utilities::getInt(tempStringList[0].ascii()); 00099 neuronGrp->yPos = Utilities::getInt(tempStringList[1].ascii()); 00100 neuronGrp->zPos = Utilities::getInt(tempStringList[2].ascii()); 00101 } 00102 else if(currentElement == "spacing") 00103 neuronGrp->spacing = Utilities::getUInt(chars.ascii()); 00104 else if(currentElement == "neuron_type") 00105 neuronGrp->neuronType = Utilities::getUInt(chars.ascii()); 00106 else{ 00107 cerr<<"NetworkModelXmlHandler: UNRECOGNIZED ELEMENT"<<endl; 00108 parseError = true; 00109 parseErrorString += "Unrecognized element."; 00110 } 00111 } 00112 catch (std::exception& er) {// Catch-all for std exceptions 00113 parseError = true; 00114 parseErrorString += er.what(); 00115 } 00116 return true; 00117 } 00118 00119 00120 /*! Called when the parser encounters the end of an element. */ 00121 bool NetworkModelXmlHandler::endElement( const QString&, const QString&, const QString& qName){ 00122 //Add to the network model string 00123 if(qName == "neural_network"){ 00124 networkModelString += "\n</"; 00125 networkModelString += qName + ">"; 00126 } 00127 else if(qName == "neuron_group"){ 00128 networkModelString += "\n\t</"; 00129 networkModelString += qName + ">"; 00130 } 00131 else{ 00132 networkModelString += "</"; 00133 networkModelString += qName + ">"; 00134 } 00135 00136 //Process the name 00137 if(qName == "neuron_group"){ 00138 monitorArea->addMonitorWindow(*neuronGrp);//Struct passed by value, so monitor area should get copy 00139 neurGrpVect.push_back(neuronGrp); 00140 } 00141 return true; 00142 } 00143 00144 00145 /*! Called when the parser generates an error. */ 00146 bool NetworkModelXmlHandler::error ( const QXmlParseException& parseEx){ 00147 cerr<<"NetworkModelXmlHandler: PARSING ERROR"<<endl; 00148 parseError = true; 00149 parseErrorString += parseEx.message(); 00150 return true; 00151 } 00152 00153 00154 /*! Returns a default error string. */ 00155 QString NetworkModelXmlHandler::errorString (){ 00156 return QString("NetworkModelXmlHandler: Default error string"); 00157 00158 } 00159 00160 00161 /*! Called when the parser generates a fatal error. */ 00162 bool NetworkModelXmlHandler::fatalError ( const QXmlParseException& parseEx){ 00163 cerr<<"NetworkModelXmlHandler: PARSING FATAL ERROR"<<endl; 00164 parseError = true; 00165 parseErrorString += parseEx.message(); 00166 return true; 00167 } 00168 00169 00170 /*! Called when parser reaches the start of the document. */ 00171 bool NetworkModelXmlHandler::startDocument(){ 00172 parseError = false; 00173 parseErrorString = ""; 00174 return true; 00175 } 00176 00177 00178 /*! Called when parser reaches the start of an element. */ 00179 bool NetworkModelXmlHandler::startElement(const QString&, const QString&, const QString& qName, const QXmlAttributes& xmlAttributes){ 00180 //Add to the network model string 00181 if(qName == "neural_network"){ 00182 networkModelString += "<"; 00183 networkModelString += qName + ">"; 00184 } 00185 else if(qName == "neuron_group"){ 00186 networkModelString += "\n\t<"; 00187 networkModelString += qName + ">"; 00188 } 00189 else { 00190 networkModelString += "\n\t\t<"; 00191 networkModelString += qName + ">"; 00192 } 00193 00194 //Process name 00195 if(qName == "neuron_group"){ 00196 neuronGrp = new NeuronGroup; 00197 00198 /*Set the neuron group id */ 00199 try{ 00200 bool neuronGrpIDFound = false; 00201 for(int i=0; i<xmlAttributes.length(); ++i) 00202 if(xmlAttributes.localName(i) == "id"){ 00203 neuronGrp->neuronGrpID = Utilities::getUInt(xmlAttributes.value(i).ascii()); 00204 neuronGrpIDFound = true; 00205 } 00206 00207 if(!neuronGrpIDFound){ 00208 cerr<<"NetworkDataXmlHandler: Cannot find neuron group ID"<<endl; 00209 parseError = true; 00210 parseErrorString += "Cannot find neuron group ID."; 00211 return true; 00212 } 00213 } 00214 catch (std::exception& er) {// Catch-all for std exceptions 00215 parseError = true; 00216 parseErrorString += er.what(); 00217 return true; 00218 } 00219 } 00220 else 00221 currentElement = qName; 00222 return true; 00223 } 00224 00225 00226 /*! Called when the parser generates a warning. */ 00227 bool NetworkModelXmlHandler::warning ( const QXmlParseException& ){ 00228 cerr<<"NetworkModelXmlHandler: PARSING WARNING"<<endl; 00229 return true; 00230 } 00231 00232 00233
1.4.4