SpikeStream Application Library
0.2
|
00001 //SpikeStream includes 00002 #include "Globals.h" 00003 #include "NetworkDao.h" 00004 #include "TruthTableModel.h" 00005 using namespace spikestream; 00006 00007 //Qt includes 00008 #include <QDebug> 00009 00010 //Other includes 00011 #include <iostream> 00012 using namespace std; 00013 00014 00016 TruthTableModel::TruthTableModel() : QAbstractTableModel(){ 00017 } 00018 00019 00021 TruthTableModel::~TruthTableModel(){ 00022 } 00023 00024 00025 /*-------------------------------------------------------------*/ 00026 /*------- PUBLIC METHODS ------*/ 00027 /*-------------------------------------------------------------*/ 00028 00030 int TruthTableModel::columnCount(const QModelIndex&) const{ 00031 return headerList.size() + 1; 00032 } 00033 00034 00037 QVariant TruthTableModel::data(const QModelIndex & index, int role) const{ 00038 //Return invalid index if index is invalid or no network loaded 00039 if (!index.isValid()) 00040 return QVariant(); 00041 if(!Globals::networkLoaded()) 00042 return QVariant(); 00043 00044 //Check rows and columns are in range 00045 if (index.row() < 0 || index.row() >= rowCount() || index.column() < 0 || index.column() >= columnCount()) 00046 return QVariant(); 00047 00048 //Return appropriate data 00049 if (role == Qt::DisplayRole){ 00050 return dataList.at(index.row()).at(index.column()); 00051 } 00052 00053 00054 //If we have reached this point ignore request 00055 return QVariant(); 00056 } 00057 00058 00060 QVariant TruthTableModel::headerData(int section, Qt::Orientation orientation, int role) const{ 00061 if (role != Qt::DisplayRole) 00062 return QVariant(); 00063 00064 if (orientation == Qt::Horizontal){ 00065 if(section < headerList.size()) 00066 return headerList.at(section); 00067 if(section == headerList.size()) 00068 return "Output"; 00069 } 00070 00071 return QVariant(); 00072 00073 } 00074 00075 00077 int TruthTableModel::rowCount(const QModelIndex&) const{ 00078 return dataList.size(); 00079 } 00080 00081 00084 void TruthTableModel::setNeuronID(unsigned int neuronID){ 00085 //Reset the class 00086 clearModelData(); 00087 00088 WeightlessNeuron* neuron = Globals::getNetworkDao()->getWeightlessNeuron(neuronID); 00089 00090 //Build empty header list 00091 for(int i=0; i<neuron->getNumberOfConnections(); ++i) 00092 headerList.append(0); 00093 00094 /* Store the headers 00095 Each position in the header list corresponds to a particular neuron id */ 00096 QHash<unsigned int, QList<unsigned int> > connectionMap = neuron->getConnectionMap(); 00097 for(QHash<unsigned int, QList<unsigned int> >::iterator mapIter = connectionMap.begin(); mapIter != connectionMap.end(); ++mapIter){ 00098 //Work through connections for the neuron 00099 foreach(unsigned int index, mapIter.value()){ 00100 headerList[index] = mapIter.key(); 00101 } 00102 } 00103 00104 //Get training data 00105 QList<unsigned char*> trainingDataList = neuron->getTrainingData(); 00106 00107 //Add training data to list 00108 for(int i=0; i<trainingDataList.size(); ++i){//Work through the arrays of training data 00109 QList<unsigned int> tmpList; 00110 for(int j=0; j<neuron->getNumberOfConnections(); ++j){//Work through all of the connections 00111 if(trainingDataList.at(i)[j/8 + 1] & 1<<(j%8)) 00112 tmpList.append(1); 00113 else 00114 tmpList.append(0); 00115 } 00116 00117 //Add output 00118 tmpList.append(trainingDataList.at(i)[0]); 00119 00120 //Store list 00121 dataList.append(tmpList); 00122 } 00123 00124 //Clean up 00125 delete neuron; 00126 00127 //Inform view about changes 00128 reset(); 00129 00130 } 00131 00132 00134 void TruthTableModel::clearModelData(){ 00135 dataList.clear(); 00136 headerList.clear(); 00137 } 00138 00139