SpikeStream Library  0.2
NetworkDao.cpp
Go to the documentation of this file.
00001 //SpikeStream includes
00002 #include "GlobalVariables.h"
00003 #include "NetworkDao.h"
00004 #include "SpikeStreamDBException.h"
00005 #include "Util.h"
00006 #include "XMLParameterParser.h"
00007 using namespace spikestream;
00008 
00009 //Other includes
00010 #include <iostream>
00011 using namespace std;
00012 
00013 
00015 NetworkDao::NetworkDao(const DBInfo& dbInfo) : AbstractDao(dbInfo){
00016 }
00017 
00018 
00020 NetworkDao::~NetworkDao(){
00021         closeDatabaseConnection();
00022 }
00023 
00024 
00025 /*----------------------------------------------------------*/
00026 /*-----                 PUBLIC METHODS                 -----*/
00027 /*----------------------------------------------------------*/
00028 
00030 void NetworkDao::addNetwork(NetworkInfo& netInfo){
00031         QSqlQuery query = getQuery("INSERT INTO Networks (Name, Description) VALUES ('" + netInfo.getName() + "', '" + netInfo.getDescription() + "')");
00032         executeQuery(query);
00033 
00034         //Check id is correct and add to network info if it is
00035         int lastInsertID = query.lastInsertId().toInt();
00036         if(lastInsertID >= START_NEURALNETWORK_ID)
00037                 netInfo.setID(lastInsertID);
00038         else
00039                 throw SpikeStreamDBException("Insert ID for Network is invalid: " + QString::number(lastInsertID));
00040 }
00041 
00042 
00044 void NetworkDao::addWeightlessConnection(unsigned int connectionID, unsigned int patternIndex){
00045         QSqlQuery query = getQuery("INSERT INTO WeightlessConnections (ConnectionID, PatternIndex) VALUES (" + QString::number(connectionID) + ", " + QString::number(patternIndex) + ")");
00046         executeQuery(query);
00047 }
00048 
00049 
00051 unsigned int NetworkDao::addWeightlessNeuronTrainingPattern(unsigned int neuronID, const unsigned char* patternArray, bool output, unsigned int patternArraySize){
00052         //Create a byte array that does not copy data
00053         QByteArray tmpByteArray = QByteArray::fromRawData((char*) patternArray, patternArraySize);
00054 
00055         //Add data to database
00056         QSqlQuery query = getQuery("INSERT INTO WeightlessNeuronTrainingPatterns (NeuronID, Pattern, Output) VALUES (" + QString::number(neuronID) + ", :PATTERNARRAY, " + QString::number(output) + ")");
00057         query.bindValue(":PATTERNARRAY", tmpByteArray);
00058         executeQuery(query);
00059 
00060         //Check id is correct and return it if it is
00061         int lastInsertID = query.lastInsertId().toInt();
00062         if(lastInsertID >= START_WEIGHTLESS_TRAINING_PATTERN_ID)
00063                 return lastInsertID;
00064 
00065         //Throw exception if the last insert id is not in the valid range
00066         throw SpikeStreamDBException("Insert ID for WeightlessNeuronTrainingPatterns is invalid: " + QString::number(lastInsertID));
00067 }
00068 
00069 
00071 unsigned int NetworkDao::getConnectionCount(unsigned int networkID){
00072         QSqlQuery query = getQuery("SELECT COUNT(*) FROM Connections WHERE ConnectionGroupID IN (SELECT ConnectionGroupID FROM ConnectionGroups WHERE NetworkID=" + QString::number(networkID) + ")");
00073         executeQuery(query);
00074         query.next();
00075         return Util::getUInt(query.value(0).toString());
00076 }
00077 
00078 
00080 unsigned int NetworkDao::getConnectionCount(ConnectionGroup* conGrp){
00081         //Build query
00082         QString queryStr = "SELECT COUNT(*) FROM Connections WHERE ConnectionGroupID=" + QString::number(conGrp->getID());
00083 
00084         //Execute query and return result
00085         QSqlQuery query = getQuery(queryStr);
00086         executeQuery(query);
00087         query.next();
00088         return Util::getUInt(query.value(0).toString());
00089 }
00090 
00091 
00093 unsigned int NetworkDao::getConnectionCount(const QList<ConnectionGroup*>& conGrpList){
00094         if(conGrpList.isEmpty())
00095                 return 0;
00096 
00097         //Build query
00098         QString queryStr = "SELECT COUNT(*) FROM Connections WHERE 1=0";
00099         foreach(ConnectionGroup* conGrp, conGrpList){
00100                 queryStr += " OR ConnectionGroupID=" + QString::number(conGrp->getID());
00101         }
00102 
00103         //Execute query and return result
00104         QSqlQuery query = getQuery(queryStr);
00105         executeQuery(query);
00106         query.next();
00107         return Util::getUInt(query.value(0).toString());
00108 }
00109 
00110 
00112 unsigned int NetworkDao::getConnectionCount(const QList<unsigned>& conGrpIDList){
00113         if(conGrpIDList.isEmpty())
00114                 return 0;
00115 
00116         //Build query
00117         QString queryStr = "SELECT COUNT(*) FROM Connections WHERE 1=0";
00118         foreach(unsigned conGrpID, conGrpIDList){
00119                 queryStr += " OR ConnectionGroupID=" + QString::number(conGrpID);
00120         }
00121 
00122         //Execute query and return result
00123         QSqlQuery query = getQuery(queryStr);
00124         executeQuery(query);
00125         query.next();
00126         return Util::getUInt(query.value(0).toString());
00127 }
00128 
00129 
00131 unsigned int NetworkDao::getConnectionCount(const ConnectionGroupInfo& conGrpInfo){
00132         //Build and execute query
00133         QString queryStr = "SELECT COUNT(*) FROM Connections WHERE ConnectionGroupID=" + QString::number(conGrpInfo.getID());
00134         QSqlQuery query = getQuery(queryStr);
00135         executeQuery(query);
00136         query.next();
00137         return Util::getUInt(query.value(0).toString());
00138 }
00139 
00140 
00142 QList< QPair<unsigned, Connection> > NetworkDao::getConnections(unsigned int fromNeuronID, unsigned int toNeuronID){
00143         QSqlQuery query = getQuery("SELECT ConnectionID, Delay, Weight FROM Connections WHERE FromNeuronID=" + QString::number(fromNeuronID) + " AND ToNeuronID="+ QString::number(toNeuronID));
00144         executeQuery(query);
00145         QList< QPair<unsigned, Connection> > conPairList;
00146         unsigned tmpID;
00147         while ( query.next() ) {
00148                 tmpID = query.value(0).toUInt();
00149                 Connection tmpCon(
00150                                 fromNeuronID,//FromNeuronID
00151                                 toNeuronID,//ToNeuronID
00152                                 query.value(1).toString().toFloat(),//Delay
00153                                 query.value(2).toString().toFloat()//Weight
00154                 );
00155                 conPairList.append(QPair<unsigned, Connection>(tmpID, tmpCon));
00156         }
00157         return conPairList;
00158 }
00159 
00160 
00162 void NetworkDao::getConnectionGroupsInfo(unsigned int networkID, QList<ConnectionGroupInfo>& conGrpInfoList){
00163         conGrpInfoList.clear();
00164         QSqlQuery query = getQuery("SELECT ConnectionGroupID, Description, FromNeuronGroupID, ToNeuronGroupID, Parameters, SynapseTypeID FROM ConnectionGroups WHERE NetworkID=" + QString::number(networkID));
00165         executeQuery(query);
00166         XMLParameterParser parameterParser;
00167         for(int i=0; i<query.size(); ++i){
00168                 query.next();
00169                 conGrpInfoList.append(
00170                                 ConnectionGroupInfo(
00171                                                 query.value(0).toUInt(),//Connection group id
00172                                                 query.value(1).toString(),//Description
00173                                                 query.value(2).toUInt(),//From group id
00174                                                 query.value(3).toUInt(),//To group id
00175                                                 parameterParser.getParameterMap(query.value(4).toString()),//Parameters
00176                                                 getSynapseType(query.value(5).toUInt())//Synapse type
00177                                 )
00178                 );
00179         }
00180 }
00181 
00182 
00186 void NetworkDao::getAllFromConnections(unsigned int networkID, QHash<unsigned int, QHash<unsigned int, bool> >& connMap){
00187         //Reset the map
00188         connMap.clear();
00189 
00190         //Get a list of all the neuron ids in the network
00191         QList<unsigned int> neuronIDList = getNeuronIDs(networkID);
00192 
00193         //Work through all of the neurons in the network
00194         foreach(unsigned int neuronID, neuronIDList){
00195                 //Add the connections FROM this neuron id TO other neurons
00196                 QHash<unsigned int, bool> tmpFromConMap;
00197                 QList<unsigned int> fromConList = getFromConnections(neuronID);
00198                 foreach(unsigned int fromConNeurID, fromConList)
00199                         tmpFromConMap[fromConNeurID] = true;
00200                 connMap.insert(neuronID, tmpFromConMap);
00201         }
00202 }
00203 
00204 
00208 void NetworkDao::getAllToConnections(unsigned int networkID, QHash<unsigned int, QHash<unsigned int, bool> >& connMap){
00209         //Reset the map
00210         connMap.clear();
00211 
00212         //Get a list of all the neuron ids in the network
00213         QList<unsigned int> neuronIDList = getNeuronIDs(networkID);
00214 
00215         //Work through all of the neurons in th network
00216         foreach(unsigned int neuronID, neuronIDList){
00217                 //Add the connections FROM this neuron id TO other neurons
00218                 QHash<unsigned int, bool> tmpToConMap;
00219                 QList<unsigned int> toConList = getToConnections(neuronID);
00220                 foreach(unsigned int toConNeurID, toConList)
00221                         tmpToConMap[toConNeurID] = true;
00222                 connMap.insert(neuronID, tmpToConMap);
00223         }
00224 }
00225 
00226 
00228 QList<unsigned int> NetworkDao::getFromConnections(unsigned int fromNeuronID){
00229         QSqlQuery query = getQuery("SELECT ToNeuronID FROM Connections WHERE FromNeuronID=" + QString::number(fromNeuronID));
00230         executeQuery(query);
00231 
00232         //Add neuron ids to list
00233         QList<unsigned int> conList;
00234         while(query.next()){
00235                 unsigned int neurID = Util::getUInt(query.value(0).toString());
00236                 conList.append(neurID);
00237         }
00238 
00239         //Return list
00240         return conList;
00241 }
00242 
00243 
00245 QList<unsigned int> NetworkDao::getToConnections(unsigned int toNeuronID){
00246         QSqlQuery query = getQuery("SELECT FromNeuronID FROM Connections WHERE ToNeuronID=" + QString::number(toNeuronID));
00247         executeQuery(query);
00248 
00249         //Add neuron ids to list
00250         QList<unsigned int> conList;
00251         while(query.next()){
00252                 unsigned int neurID = Util::getUInt(query.value(0).toString());
00253                 conList.append(neurID);
00254         }
00255 
00256         //Return list
00257         return conList;
00258 }
00259 
00260 
00262 QHash<QString, double> NetworkDao::getDefaultNeuronParameters(unsigned int neuronTypeID){
00263         NeuronType neuronType = getNeuronType(neuronTypeID);
00264         QHash<QString, double> paramMap;
00265 
00266         //Check for case where there are no parameters.
00267         if(neuronType.getParameterInfoList().isEmpty())
00268                 return paramMap;
00269 
00270         //Extract default values from parameter table
00271         QSqlQuery query = getQuery("SHOW COLUMNS FROM " + neuronType.getParameterTableName());
00272         executeQuery(query);
00273         int defaultCol = query.record().indexOf("Default");
00274         int variableNameCol = query.record().indexOf("Field");
00275         while(query.next()){
00276                 QString variableName = query.value(variableNameCol).toString();
00277                 if(variableName != "NeuronGroupID"){
00278                         paramMap[variableName] = Util::getDouble( query.value(defaultCol).toString() );
00279                 }
00280         }
00281 
00282         //Check it matches the parameters stored in the neuron type
00283         foreach(ParameterInfo info, neuronType.getParameterInfoList()){
00284                 if(!paramMap.contains(info.getName()))
00285                         throw SpikeStreamException("Parameter missing: " + info.getName() + " for neuron type " + QString::number(neuronType.getID()));
00286         }
00287 
00288         //Return map
00289         return paramMap;
00290 }
00291 
00292 
00294 QHash<QString, double> NetworkDao::getDefaultSynapseParameters(unsigned int synapseTypeID){
00295         SynapseType synapseType = getSynapseType(synapseTypeID);
00296         QHash<QString, double> paramMap;
00297 
00298         //Check for case where there are no parameters.
00299         if(synapseType.getParameterInfoList().isEmpty())
00300                 return paramMap;
00301 
00302         //Extract default values from parameter table
00303         QSqlQuery query = getQuery("SHOW COLUMNS FROM " + synapseType.getParameterTableName());
00304         executeQuery(query);
00305         int defaultCol = query.record().indexOf("Default");
00306         int variableNameCol = query.record().indexOf("Field");
00307         while(query.next()){
00308                 QString variableName = query.value(variableNameCol).toString();
00309                 if(variableName != "ConnectionGroupID"){
00310                         paramMap[variableName] = Util::getDouble( query.value(defaultCol).toString() );
00311                 }
00312         }
00313 
00314         //Check it matches the parameters stored in the synapse type
00315         foreach(ParameterInfo info, synapseType.getParameterInfoList()){
00316                 if(!paramMap.contains(info.getName()))
00317                         throw SpikeStreamException("Parameter missing: " + info.getName() + " for synapse type " + QString::number(synapseType.getID()));
00318         }
00319 
00320         //Return map
00321         return paramMap;
00322 }
00323 
00324 
00326 unsigned int NetworkDao::getNeuronCount(const QList<NeuronGroup*>& neurGrpList){
00327         if(neurGrpList.isEmpty())
00328                 return 0;
00329 
00330         //Build query
00331         QString queryStr = "SELECT COUNT(*) FROM Neurons WHERE 1=0";
00332         foreach(NeuronGroup* neurGrp, neurGrpList){
00333                 queryStr += " OR NeuronGroupID=" + QString::number(neurGrp->getID());
00334         }
00335 
00336         //Execute query and return result
00337         QSqlQuery query = getQuery(queryStr);
00338         executeQuery(query);
00339         query.next();
00340         return Util::getUInt(query.value(0).toString());
00341 }
00342 
00343 
00345 unsigned int NetworkDao::getNeuronCount(unsigned int networkID){
00346         QSqlQuery query = getQuery("SELECT COUNT(*) FROM Neurons WHERE NeuronGroupID IN (SELECT NeuronGroupID FROM NeuronGroups WHERE NetworkID=" + QString::number(networkID) + ")");
00347         executeQuery(query);
00348         query.next();
00349         return Util::getUInt(query.value(0).toString());
00350 }
00351 
00352 
00354 unsigned NetworkDao::getNeuronCount(const NeuronGroupInfo& neurGrpInfo){
00355         QSqlQuery query = getQuery("SELECT COUNT(*) FROM Neurons WHERE NeuronGroupID=" + QString::number(neurGrpInfo.getID()) );
00356         executeQuery(query);
00357         query.next();
00358         return Util::getUInt(query.value(0).toString());
00359 }
00360 
00361 
00364 unsigned int NetworkDao::getNeuronCount(unsigned int networkID, const Box& box){
00365         //Build query
00366         QString queryStr = "SELECT COUNT(*) FROM Neurons ";
00367         queryStr += " WHERE NeuronGroupID IN (SELECT NeuronGroupID FROM NeuronGroups WHERE NetworkID=" + QString::number(networkID) + ") AND ";
00368         queryStr += " X >= " + QString::number(box.x1) + " AND X <= " + QString::number(box.x2) + " AND ";
00369         queryStr += " Y >= " + QString::number(box.y1) + " AND Y <= " + QString::number(box.y2) + " AND ";
00370         queryStr += " Z >= " + QString::number(box.z1) + " AND Z <= " + QString::number(box.z2);
00371 
00372         //Execute query and return result
00373         QSqlQuery query = getQuery(queryStr);
00374         executeQuery(query);
00375         query.next();
00376         return(Util::getUInt(query.value(0).toString()));
00377 }
00378 
00379 
00381 Box NetworkDao::getNeuronGroupBoundingBox(unsigned int neurGrpID){
00382         QSqlQuery query = getQuery("SELECT MIN(X), MIN(Y), MIN(Z), MAX(X), MAX(Y), MAX(Z) FROM Neurons WHERE NeuronGroupID = " + QString::number(neurGrpID));
00383         executeQuery(query);
00384         query.next();
00385         Box box(
00386                         query.value(0).toInt(),//x1
00387                         query.value(1).toInt(),//y1
00388                         query.value(2).toInt(),//z1
00389                         query.value(3).toInt(),//x2
00390                         query.value(4).toInt(),//y2
00391                         query.value(5).toInt()//z2
00392                         );
00393         return box;
00394 }
00395 
00396 
00398 unsigned int NetworkDao::getConnectionGroupSize(unsigned int connGrpID){
00399         QSqlQuery query = getQuery("SELECT COUNT(*) FROM Connections WHERE ConnectionGroupID=" + QString::number(connGrpID));
00400         executeQuery(query);
00401         query.next();
00402         return query.value(0).toUInt();
00403 }
00404 
00405 
00407 QList<NetworkInfo> NetworkDao::getNetworksInfo(){
00408         QSqlQuery query = getQuery("SELECT NetworkID, Name, Description FROM Networks");
00409         executeQuery(query);
00410         QList<NetworkInfo> tmpList;
00411         for(int i=0; i<query.size(); ++i){
00412                 query.next();
00413                 tmpList.append( NetworkInfo(query.value(0).toUInt(), query.value(1).toString(), query.value(2).toString()));
00414         }
00415         return tmpList;
00416 }
00417 
00418 
00420 void NetworkDao::getNeuronGroupsInfo(unsigned int networkID, QList<NeuronGroupInfo>& neurGrpInfoList){
00421         QSqlQuery query = getQuery("SELECT NeuronGroupID, Name, Description, Parameters, NeuronTypeID FROM NeuronGroups WHERE NetworkID=" + QString::number(networkID));
00422         executeQuery(query);
00423         neurGrpInfoList.clear();
00424         XMLParameterParser parameterParser;
00425         for(int i=0; i<query.size(); ++i){
00426                 query.next();
00427                 neurGrpInfoList.append(
00428                                 NeuronGroupInfo(
00429                                                 query.value(0).toUInt(),
00430                                                 query.value(1).toString(),
00431                                                 query.value(2).toString(),
00432                                                 parameterParser.getParameterMap(query.value(3).toString()),
00433                                                 getNeuronType(query.value(4).toUInt())
00434                                 )
00435                 );
00436         }
00437 }
00438 
00439 
00441 QList<unsigned int> NetworkDao::getNeuronIDs(unsigned int networkID){
00442         QList<unsigned int> neurIDList;
00443         QSqlQuery query = getQuery("SELECT NeuronID FROM Neurons WHERE NeuronGroupID IN (SELECT NeuronGroupID FROM NeuronGroups WHERE NetworkID=" + QString::number(networkID) + ")");
00444         executeQuery(query);
00445         for(int i=0; i<query.size(); ++i){
00446                 query.next();
00447                 neurIDList.append(Util::getUInt(query.value(0).toString()));
00448         }
00449         return neurIDList;
00450 }
00451 
00452 
00454 QHash<QString, double> NetworkDao::getNeuronParameters(const NeuronGroupInfo& neurGrpInfo){
00455         /* Get class describing the type of neuron in this group - this contains the parameter
00456                 table name and info about the parameters */
00457         NeuronType neuronType = getNeuronType(neurGrpInfo.getNeuronTypeID());
00458 
00459         //Return empty map if there are no parameters
00460         if(neuronType.getParameterInfoList().isEmpty())
00461                 return QHash<QString, double>();
00462 
00463         //Build query to extract values of parameters for this neuron group
00464         QString queryStr = "SELECT ";
00465         QList<ParameterInfo> parameterInfoList = neuronType.getParameterInfoList();
00466         foreach(ParameterInfo paramInfo, parameterInfoList){
00467                 queryStr += paramInfo.getName() + ",";
00468         }
00469         queryStr.truncate(queryStr.length() - 1);
00470         queryStr += " FROM " + neuronType.getParameterTableName() + " WHERE NeuronGroupID=" + QString::number(neurGrpInfo.getID());
00471         QSqlQuery query = getQuery(queryStr);
00472         executeQuery(query);
00473 
00474         //Check only one row exists
00475         if(query.size() != 1)
00476                 throw SpikeStreamException("Zero or multiple parameter entries for neuron group in table " + neuronType.getParameterTableName() + ": " + QString::number(query.size()));
00477 
00478         //Extract parameters
00479         query.next();
00480         QHash<QString, double> paramMap;
00481         int indx = 0;
00482         foreach(ParameterInfo paramInfo, parameterInfoList){
00483                 if(paramMap.contains(paramInfo.getName()))
00484                         throw SpikeStreamException("Duplicate entries in parameter map!");
00485 
00486                 paramMap[paramInfo.getName()] = Util::getDouble(query.value(indx).toString());
00487                 ++indx;
00488         }
00489 
00490         //Return the finished map
00491         return paramMap;
00492 }
00493 
00494 
00496 NeuronType NetworkDao::getNeuronType(unsigned int neuronTypeID){
00497         //Resuse other method - not efficient, but more likely to be reliable than duplicating code.
00498         QList<NeuronType> neurTypesList = getNeuronTypes();
00499         foreach(NeuronType neurType, neurTypesList){
00500                 if(neurType.getID() == neuronTypeID)
00501                         return neurType;
00502         }
00503         throw SpikeStreamException("Neuron type with ID " + QString::number(neuronTypeID) + " not found.");
00504 }
00505 
00506 
00508 NeuronType NetworkDao::getNeuronType(const QString& neuronTypeDescription){
00509         //Resuse other method - not efficient, but more likely to be reliable than duplicating code.
00510         QList<NeuronType> neurTypesList = getNeuronTypes();
00511         foreach(NeuronType neurType, neurTypesList){
00512                 if(neurType.getDescription().toUpper() == neuronTypeDescription.toUpper())
00513                         return neurType;
00514         }
00515         throw SpikeStreamException("Neuron type with description " + neuronTypeDescription + " not found.");
00516 }
00517 
00518 
00521 QList<NeuronType> NetworkDao::getNeuronTypes(){
00522         QList<NeuronType> neuronTypesList;
00523         QSqlQuery query = getQuery("SELECT NeuronTypeID, Description, ParameterTableName, ClassLibrary FROM NeuronTypes ORDER BY NeuronTypeID");
00524         executeQuery(query);
00525         for(int i=0; i<query.size(); ++i){
00526                 query.next();
00527                 NeuronType tmpNeurType(
00528                         Util::getUInt(query.value(0).toString()),//ID
00529                         query.value(1).toString(),//Description
00530                         query.value(2).toString(),//ParameterTableName
00531                         query.value(3).toString()//ClassLibrary
00532                 );
00533                 neuronTypesList.append(tmpNeurType);
00534         }
00535 
00536         //Add the information about parameters for each neuron type
00537         for(int i=0; i<neuronTypesList.size(); ++i){
00538                 QList<ParameterInfo> paramInfoList = getNeuronParameterInfo(neuronTypesList.at(i));
00539                 neuronTypesList[i].setParameterInfoList(paramInfoList);
00540         }
00541         return neuronTypesList;
00542 }
00543 
00544 
00546 unsigned NetworkDao::getStartNeuronID(unsigned neuronGroupID){
00547         QSqlQuery query = getQuery("SELECT MIN(NeuronID) FROM Neurons WHERE NeuronGroupID=" + QString::number(neuronGroupID));
00548         executeQuery(query);
00549         query.next();
00550         return Util::getUInt(query.value(0).toString());
00551 }
00552 
00553 
00555 SynapseType NetworkDao::getSynapseType(unsigned int synapseTypeID){
00556         //Resuse other method - not efficient, but more likely to be reliable than duplicating code.
00557         QList<SynapseType> synTypesList = getSynapseTypes();
00558         foreach(SynapseType synType, synTypesList){
00559                 if(synType.getID() == synapseTypeID)
00560                         return synType;
00561         }
00562         throw SpikeStreamException("Synapse type with ID " + QString::number(synapseTypeID) + " not found.");
00563 }
00564 
00565 
00567 SynapseType NetworkDao::getSynapseType(const QString& synapseTypeDescription){
00568         //Resuse other method - not efficient, but more likely to be reliable than duplicating code.
00569         QList<SynapseType> synTypesList = getSynapseTypes();
00570         foreach(SynapseType synType, synTypesList){
00571                 if(synType.getDescription().toUpper() == synapseTypeDescription.toUpper())
00572                         return synType;
00573         }
00574         throw SpikeStreamException("Synapse type with description " + synapseTypeDescription + " not found.");
00575 }
00576 
00577 
00579 QHash<QString, double> NetworkDao::getSynapseParameters(const ConnectionGroupInfo& conGrpInfo){
00580         /* Get class describing the type of synapse in this group - this contains the parameter
00581                 table name and info about the parameters */
00582         SynapseType synapseType = getSynapseType(conGrpInfo.getSynapseTypeID());
00583 
00584         //Return empty map if there are no parameters
00585         if(synapseType.getParameterInfoList().isEmpty())
00586                 return QHash<QString, double>();
00587 
00588         //Build query to extract values of parameters for this connection group
00589         QString queryStr = "SELECT ";
00590         QList<ParameterInfo> parameterInfoList = synapseType.getParameterInfoList();
00591         foreach(ParameterInfo paramInfo, parameterInfoList){
00592                 queryStr += paramInfo.getName() + ",";
00593         }
00594         queryStr.truncate(queryStr.length() - 1);
00595         queryStr += " FROM " + synapseType.getParameterTableName() + " WHERE ConnectionGroupID=" + QString::number(conGrpInfo.getID());
00596         QSqlQuery query = getQuery(queryStr);
00597         executeQuery(query);
00598 
00599         //Check only one row exists
00600         if(query.size() != 1)
00601                 throw SpikeStreamException("Zero or multiple parameter entries for connection group in table " + synapseType.getParameterTableName() + ": " + QString::number(query.size()));
00602 
00603         //Extract parameters
00604         query.next();
00605         QHash<QString, double> paramMap;
00606         int indx = 0;
00607         foreach(ParameterInfo paramInfo, parameterInfoList){
00608                 if(paramMap.contains(paramInfo.getName()))
00609                         throw SpikeStreamException("Duplicate entries in parameter map!");
00610 
00611                 paramMap[paramInfo.getName()] = Util::getDouble(query.value(indx).toString());
00612                 ++indx;
00613         }
00614 
00615         //Return the finished map
00616         return paramMap;
00617 }
00618 
00619 
00622 QList<SynapseType> NetworkDao::getSynapseTypes(){
00623         QList<SynapseType> synapseTypesList;
00624         QSqlQuery query = getQuery("SELECT SynapseTypeID, Description, ParameterTableName, ClassLibrary FROM SynapseTypes ORDER BY SynapseTypeID");
00625         executeQuery(query);
00626         for(int i=0; i<query.size(); ++i){
00627                 query.next();
00628                 SynapseType tmpSynType(
00629                         Util::getUInt(query.value(0).toString()),//ID
00630                         query.value(1).toString(),//Description
00631                         query.value(2).toString(),//ParameterTableName
00632                         query.value(3).toString()//ClassLibrary
00633                 );
00634                 synapseTypesList.append(tmpSynType);
00635         }
00636 
00637         //Add the information about parameters for each synapse type
00638         for(int i=0; i<synapseTypesList.size(); ++i){
00639                 QList<ParameterInfo> paramInfoList = getSynapseParameterInfo(synapseTypesList.at(i));
00640                 synapseTypesList[i].setParameterInfoList(paramInfoList);
00641         }
00642 
00643         return synapseTypesList;
00644 }
00645 
00646 
00648 WeightlessNeuron* NetworkDao::getWeightlessNeuron(unsigned int neuronID){
00649         //Query to select neurons connected to weightless neuron and the pattern index of each neuron
00650         QSqlQuery query = getQuery("SELECT cons.FromNeuronID, weiCons.PatternIndex FROM Connections cons INNER JOIN WeightlessConnections weiCons ON cons.ConnectionID=weiCons.ConnectionID WHERE cons.ToNeuronID=" + QString::number(neuronID));
00651         executeQuery(query);
00652 
00653         /* Store the pattern index of each neuron that connects to
00654         There may be several connections between neurons, so connection map contains a list of connections for each neuron
00655         instead of a single entry */
00656         QHash<unsigned int, QList<unsigned int> > tmpConMap;
00657         unsigned int tmpFromNeurID, tmpPatternIndex;
00658         for(int i=0; i<query.size(); ++i){
00659                 query.next();
00660                 tmpFromNeurID = Util::getUInt(query.value(0).toString());
00661                 tmpPatternIndex = Util::getUInt(query.value(1).toString());
00662                 tmpConMap[tmpFromNeurID].append(tmpPatternIndex);
00663         }
00664 
00665         //Create the weightless neuron
00666         WeightlessNeuron* tmpWeightlessNeuron = new WeightlessNeuron(tmpConMap, neuronID);
00667 
00668         //Query to get the training patterns
00669         query = getQuery("SELECT Pattern, Output FROM WeightlessNeuronTrainingPatterns WHERE NeuronID = " + QString::number(neuronID));
00670         executeQuery(query);
00671 
00672         //Add training patterns to neuron
00673         for(int i=0; i<query.size(); ++i){
00674                 query.next();
00675                 QByteArray tmpByteArray = query.value(0).toByteArray();
00676                 tmpWeightlessNeuron->addTraining(tmpByteArray, Util::getUInt(query.value(1).toString()));
00677         }
00678 
00679         //Create and return the weigthless neuron
00680         return tmpWeightlessNeuron;
00681 }
00682 
00683 
00685 unsigned int NetworkDao::getNeuronGroupID(unsigned int neuronID){
00686         QSqlQuery query = getQuery("SELECT NeuronGroupID FROM Neurons WHERE NeuronID = " + QString::number(neuronID));
00687         executeQuery(query);
00688         query.next();
00689         return Util::getUInt(query.value(0).toString());
00690 }
00691 
00692 
00695 bool NetworkDao::isWeightlessNeuron(unsigned int neuronID){
00696         unsigned int tmpNeurGrpID = getNeuronGroupID(neuronID);
00697 
00698         //Query to select neuron type description
00699         QSqlQuery query = getQuery("SELECT neurType.Description FROM NeuronTypes neurType INNER JOIN NeuronGroups neurGrps ON neurType.NeuronTypeID=neurGrps.NeuronTypeID WHERE neurGrps.NeuronGroupID=" + QString::number(tmpNeurGrpID));
00700         executeQuery(query);
00701 
00702         //Check to see if it is weightless
00703         query.next();
00704         if(query.value(0).toString().toUpper() == "WEIGHTLESS NEURON")
00705                 return true;
00706         return false;
00707 }
00708 
00709 
00712 bool NetworkDao::isWeightlessNetwork(unsigned int networkID){
00713 
00714         //Query to select neuron type description
00715         QSqlQuery query = getQuery("SELECT neurType.Description FROM NeuronTypes neurType INNER JOIN NeuronGroups neurGrps ON neurType.NeuronTypeID=neurGrps.NeuronTypeID WHERE neurGrps.NetworkID=" + QString::number(networkID));
00716         executeQuery(query);
00717 
00718         //Check to see if it is weightless
00719         while(query.next()){
00720                 if(query.value(0).toString().toUpper() != "WEIGHTLESS NEURON")
00721                         return false;
00722         }
00723         return true;
00724 }
00725 
00726 
00728 void NetworkDao::setConnectionGroupProperties(unsigned connectionGroupID, const QString& description){
00729         QString queryStr = "UPDATE ConnectionGroups SET Description='" + description + "' ";
00730         queryStr += "WHERE ConnectionGroupID=" + QString::number(connectionGroupID);
00731         QSqlQuery query = getQuery(queryStr);
00732         executeQuery(query);
00733         if(query.numRowsAffected() != 1)
00734                 throw SpikeStreamDBException("Error in connection group update. Exactly one row should be affected: " + QString::number(query.numRowsAffected()) + ", ID: " + QString::number(connectionGroupID));
00735 }
00736 
00737 
00740 void NetworkDao::setNetworkProperties(unsigned networkID, const QString& name, const QString& description){
00741         QString queryStr = "UPDATE Networks SET Name='" + name + "', Description='" + description + "' ";
00742         queryStr += "WHERE NetworkID=" + QString::number(networkID);
00743         QSqlQuery query = getQuery(queryStr);
00744         executeQuery(query);
00745         if(query.numRowsAffected() != 1)
00746                 throw SpikeStreamDBException("Error in network update. Exactly one row should be affected: " + QString::number(query.numRowsAffected()));
00747 }
00748 
00749 
00751 void NetworkDao::setNeuronGroupProperties(unsigned neuronGroupID, const QString& name, const QString& description){
00752         QString queryStr = "UPDATE NeuronGroups SET Name='" + name + "', Description='" + description + "' ";
00753         queryStr += "WHERE NeuronGroupID=" + QString::number(neuronGroupID);
00754         QSqlQuery query = getQuery(queryStr);
00755         executeQuery(query);
00756         if(query.numRowsAffected() != 1)
00757                 throw SpikeStreamDBException("Error in neuron group update. Exactly one row should be affected: " + QString::number(query.numRowsAffected()) + ", ID: " + QString::number(neuronGroupID));
00758 }
00759 
00760 
00762 void NetworkDao::setNeuronParameters(const NeuronGroupInfo& neurGrpInfo, QHash<QString, double>& paramMap){
00763         /* Get class describing the type of neuron in this group - this contains the parameter
00764                 table name and info about the parameters */
00765         NeuronType neuronType = getNeuronType(neurGrpInfo.getNeuronTypeID());
00766 
00767         //Do nothing if there are no parameters
00768         if(neuronType.getParameterCount() == 0)
00769                 return;
00770 
00771         //Build query to update parameters for this neuron group
00772         QString queryStr = "UPDATE " + neuronType.getParameterTableName() + " SET ";
00773         foreach(ParameterInfo paramInfo, neuronType.getParameterInfoList()){
00774                 if(!paramMap.contains(paramInfo.getName()))
00775                         throw SpikeStreamException("Parameters in neuron type and parameters in paramter map do not match.");
00776 
00777                 queryStr += paramInfo.getName() + "=" + QString::number(paramMap[paramInfo.getName()]) + ",";
00778         }
00779         queryStr.truncate(queryStr.length() - 1);//Take off the last trailing comma
00780         queryStr += " WHERE NeuronGroupID=" + QString::number(neurGrpInfo.getID());
00781         executeQuery(queryStr);
00782 }
00783 
00784 
00786 void NetworkDao::setSynapseParameters(const ConnectionGroupInfo& conGrpInfo, QHash<QString, double>& paramMap){
00787         /* Get class describing the type of synapse in this group - this contains the parameter
00788                 table name and info about the parameters */
00789         SynapseType synapseType = getSynapseType(conGrpInfo.getSynapseTypeID());
00790 
00791         //Do nothing if there are no parameters
00792         if(synapseType.getParameterCount() == 0)
00793                 return;
00794 
00795         //Build query to update parameters for this neuron group
00796         QString queryStr = "UPDATE " + synapseType.getParameterTableName() + " SET ";
00797         foreach(ParameterInfo paramInfo, synapseType.getParameterInfoList()){
00798                 if(!paramMap.contains(paramInfo.getName()))
00799                         throw SpikeStreamException("Parameters in synapse type and parameters in paramter map do not match.");
00800 
00801                 queryStr += paramInfo.getName() + "=" + QString::number(paramMap[paramInfo.getName()]) + ",";
00802         }
00803         queryStr.truncate(queryStr.length() - 1);//Take off the last trailing comma
00804         queryStr += " WHERE ConnectionGroupID=" + QString::number(conGrpInfo.getID());
00805         executeQuery(queryStr);
00806 }
00807 
00808 
00810 void NetworkDao::setWeight(unsigned connectionID, double newWeight){
00811         QSqlQuery query = getQuery("UPDATE Connections SET Weight=" + QString::number(newWeight) + " WHERE ConnectionID=" + QString::number(connectionID));
00812         executeQuery(query);
00813 }
00814 
00815 
00816 /*----------------------------------------------------------*/
00817 /*-----                PRIVATE METHODS                 -----*/
00818 /*----------------------------------------------------------*/
00819 
00822 QList<ParameterInfo> NetworkDao::getNeuronParameterInfo(const NeuronType& neuronType){
00823         QList<ParameterInfo> paramInfoList;
00824 
00825         //Extract variable names and descriptions (as comments) from parameter table
00826         QSqlQuery query = getQuery("SHOW FULL COLUMNS FROM " + neuronType.getParameterTableName());
00827         executeQuery(query);
00828         int variableNameCol = query.record().indexOf("Field");
00829         int commentCol = query.record().indexOf("Comment");
00830         int typeCol = query.record().indexOf("Type");
00831         while(query.next()){
00832                 QString variableName = query.value(variableNameCol).toString();
00833                 if(variableName != "NeuronGroupID"){
00834                         if(query.value(typeCol).toString() == "tinyint(1)")
00835                                 paramInfoList.append(ParameterInfo(variableName, query.value(commentCol).toString(), ParameterInfo::BOOLEAN));
00836                         else
00837                                 paramInfoList.append(ParameterInfo(variableName, query.value(commentCol).toString(), ParameterInfo::DOUBLE));
00838                 }
00839         }
00840         return paramInfoList;
00841 }
00842 
00843 
00846 QList<ParameterInfo> NetworkDao::getSynapseParameterInfo(const SynapseType& synapseType){
00847         QList<ParameterInfo> paramInfoList;
00848 
00849         //Extract variable names and descriptions (as comments) from parameter table
00850         QSqlQuery query = getQuery("SHOW FULL COLUMNS FROM " + synapseType.getParameterTableName());
00851         executeQuery(query);
00852         int variableNameCol = query.record().indexOf("Field");
00853         int commentCol = query.record().indexOf("Comment");
00854         int typeCol = query.record().indexOf("Type");
00855         while(query.next()){
00856                 QString variableName = query.value(variableNameCol).toString();
00857                 if(variableName != "ConnectionGroupID"){
00858                         if(query.value(typeCol).toString() == "tinyint(1)")
00859                                 paramInfoList.append(ParameterInfo(variableName, query.value(commentCol).toString(), ParameterInfo::BOOLEAN));
00860                         else
00861                                 paramInfoList.append(ParameterInfo(variableName, query.value(commentCol).toString(), ParameterInfo::DOUBLE));
00862                 }
00863         }
00864         return paramInfoList;
00865 }
00866 
 All Classes Files Functions Variables Typedefs Defines