somdataxmlreader.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by root                                            *
00003  *   root@aquiles                                                          *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #ifndef SOMDATAXMLREADER_H
00021 #define SOMDATAXMLREADER_H
00022 
00023 #include "xmlsomreader.h"
00024 #include "Data.h"
00025 
00026 
00032 class SOMDataXMLReader : public xmlsom::XMLSOMReader {
00033 
00034   public:
00035   
00036     SOMDataXMLReader(std::string filename);
00037     
00038     virtual SOMData* load() throw(std::exception);
00039 
00040     virtual ~SOMDataXMLReader() {};
00041 
00042   protected:
00043   
00044     virtual void processElement(const std::string& name,
00045                                 const std::string& value,
00046                                 const std::map<std::string,std::string>& attrs) 
00047       throw(std::exception);
00048     
00049   private:
00050     
00051     SOMData* data;
00052     
00053     TMatrix entries;
00054     
00055     TvecLabel labels;
00056     
00057     int dimension;
00058     
00059     int size;
00060     
00061     int actualNode;    
00062     
00063     int actualWeight;
00064 };
00065 
00066 
00067 
00068 //default constructor
00069 SOMDataXMLReader::SOMDataXMLReader(std::string filename) 
00070     : xmlsom::XMLSOMReader(filename) {
00071 
00072   dimension = 1;
00073   size = 1;
00074   actualNode = 0;    
00075   actualWeight = 0;
00076 };
00077 
00078 
00079 
00080 //main method, must call XMLReader::doParse()
00081 SOMData* SOMDataXMLReader::load() throw(std::exception) {
00082 
00083   doParse();
00084   
00085   data = new SOMData();  
00086   data->setEntries(entries);
00087   data->setLabel(labels);
00088   data->setDataSize(size);
00089   data->setDimension(dimension);
00090 
00091   return data;
00092 };
00093 
00094 
00095 
00096 //method for elements processing
00097 void SOMDataXMLReader::processElement( const std::string& name,
00098                                        const std::string& value,
00099                                        const std::map<std::string,std::string>& attrs ) 
00100   throw(std::exception) {
00101 
00102   if(name == "size")
00103     size = atoi(value.c_str());
00104   
00105   else if(name == "dimension")
00106     dimension = atoi(value.c_str());
00107 
00108   else if(name == "data") {
00109     entries = create_matrix( 1, size, 1, dimension );
00110   
00111   } else if(name == "row") {
00112     ++actualNode;
00113     actualWeight = 0;
00114     
00115   } else if(name == "label")
00116     labels.push_back(value);  
00117   
00118   else if(name == "weight")
00119     entries[actualNode][actualWeight] = atof(value.c_str());
00120 };
00121 
00122 
00123 
00124 #endif
00125 

Generated on Tue Aug 7 16:03:33 2007 for SOMCode by  doxygen 1.5.3