Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

TupleTranslator.cpp

Go to the documentation of this file.
00001  /**********************************************************************
00002   *                                                                    *
00003   * Copyright (c) 2002 Lorenzo Moneta, CERN/IT                       *
00004   *                   <Lorenzo.Moneta.cern.ch>                       *
00005   *                                                                    *
00006   * This library is free software; you can redistribute it and/or      *
00007   * modify it under the terms of the GNU Lesser General Public         *
00008   * License as published by the Free Software Foundation; either       *
00009   * version 2.1 of the License, or (at your option) any later version. *
00010   *                                                                    *
00011   * This library is distributed in the hope that it will be useful,    *
00012   * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
00013   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
00014   * Lesser General Public License for more details.                    *
00015   *                                                                    *
00016   * You should have received a copy of the GNU Lesser General Public   *
00017   * License along with this library (see file COPYING); if not, write  *
00018   * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
00019   * 330, Boston, MA 02111-1307 USA, or contact the author.             *
00020   *                                                                    *
00021   **********************************************************************/
00022 
00023 // Implementation file for class TupleTranslator
00024 // 
00025 // Created by: moneta  at Thu Sep 12 10:41:06 2002
00026 // 
00027 // Last update: Thu Sep 12 10:41:06 2002
00028 // 
00029 
00030 #include "TupleTranslator.h"
00031 #include "AIDA/ITuple.h"
00032 #include "AIDA_Dev/IDevTuple.h"
00033 #include "AIDA_Dev/IDevTupleFactory.h"
00034 
00035 #include "DataXML/DataObject.h"
00036 
00037 static const std::string emptyString = "";
00038 
00040 Anaphe::AIDA_XMLStore::TupleTranslator::TupleTranslator(AIDA::ITuple * tuple, const std::string & name, const std::string & path ) : 
00041   m_tuple(tuple), m_name(name), m_path(path)
00042 {
00043 }
00045 Anaphe::AIDA_XMLStore::TupleTranslator::TupleTranslator(const DataXML::DataObject * xmlObject) : 
00046   m_element(*xmlObject)
00047 {
00048 }
00049 
00050 
00051 Anaphe::AIDA_XMLStore::TupleTranslator::~TupleTranslator() 
00052 {  // no op
00053 }
00054 
00055 Anaphe::AIDA_XMLStore::TupleTranslator::TupleTranslator(const Anaphe::AIDA_XMLStore::TupleTranslator &)  
00056 {
00057 }
00058 
00059 Anaphe::AIDA_XMLStore::TupleTranslator & Anaphe::AIDA_XMLStore::TupleTranslator::operator = (const Anaphe::AIDA_XMLStore::TupleTranslator &rhs) 
00060 {
00061    if (this == &rhs) return *this;  // time saving self-test
00062 
00063    return *this;
00064 }
00065 
00066 bool Anaphe::AIDA_XMLStore::TupleTranslator::toXML() 
00067 { 
00068   // write name and title
00069   appendObjectHeader(m_element,"tuple",m_name,m_tuple->title(),m_path); 
00070   // here if some options to be added 
00071   //m_element.setAttribute("options", m_tuple->options() ); 
00072   
00073   appendAnnotation(m_element,m_tuple->annotation()); 
00074 
00075   if (!setHeader() ) return false; 
00076   if (!setData () ) return false;
00077 
00078   return true;
00079 
00080 }
00081 
00082 // set columns header
00083 
00084 bool Anaphe::AIDA_XMLStore::TupleTranslator::setHeader()
00085 { 
00086   DataXML::DataObject colsElement;
00087   colsElement.setName("columns"); 
00088   for (int i = 0; i < m_tuple->columns(); ++i) { 
00089     DataXML::DataObject cElement;
00090     cElement.setName("column"); 
00091     cElement.setAttribute("name",m_tuple->columnName(i)); 
00092     cElement.setAttribute("type",m_tuple->columnType(i)); 
00093     colsElement.appendChild(cElement);
00094   }
00095 
00096   m_element.appendChild(colsElement);
00097   return true; 
00098 }
00099 
00100 // write tuple data (rows) 
00101 
00102 bool Anaphe::AIDA_XMLStore::TupleTranslator::setData()
00103 { 
00104   // write data element and append to the dataobject
00105 
00106   DataXML::DataObject rowsElement;
00107   rowsElement.setName("rows");
00108   
00109   appendRow(rowsElement, m_tuple); 
00110 
00111   m_element.appendChild(rowsElement);
00112   return true;
00113 }
00114 
00115 // append row of a tuple
00116 
00117 void Anaphe::AIDA_XMLStore::TupleTranslator::appendRow(DataXML::DataObject & parElement, AIDA::ITuple * tuple)
00118 { 
00119   // loop on the tuple rows
00120   tuple->start(); 
00121   while (tuple->next() ) { 
00122     DataXML::DataObject dataElement;
00123     dataElement.setName("row");    
00124     for (int j = 0; j < tuple->columns(); ++ j) { 
00125       // get element according to type
00126       if (tuple->columnType(j) == "double" )              
00127         appendTupleEntry<double>(dataElement, tuple->getDouble(j) ); 
00128       else if (tuple->columnType(j) == "float" )
00129         appendTupleEntry<float>(dataElement, tuple->getFloat(j) ); 
00130       else if (tuple->columnType(j) == "int" )
00131         appendTupleEntry(dataElement, tuple->getInt(j) ); 
00132       else if (tuple->columnType(j) == "short" )
00133         appendTupleEntry(dataElement, tuple->getShort(j) ); 
00134       else if (tuple->columnType(j) == "long" )
00135         appendTupleEntry(dataElement, tuple->getLong(j) ); 
00136       else if (tuple->columnType(j) == "char" )
00137         appendTupleEntry(dataElement, tuple->getChar(j) ); 
00138       else if (tuple->columnType(j) == "char" )
00139         appendTupleEntry(dataElement, tuple->getChar(j) ); 
00140       else if (tuple->columnType(j) == "bool" || tuple->columnType(j) == "boolean")
00141         appendTupleEntry(dataElement, tuple->getBoolean(j) ); 
00142       else if (tuple->columnType(j) == "string")
00143         appendTupleEntry(dataElement, tuple->getString(j) );       
00144       // case of Tuple in a Tuple (check if type contains "uple" )
00145       else if (tuple->columnType(j).find("uple") != std::string::npos ) { 
00146         DataXML::DataObject subTupleElement;
00147         subTupleElement.setName("entryITuple"); 
00148         AIDA::ITuple * subTuple = tuple->getTuple(j); 
00149         // append row
00150         appendRow(subTupleElement,subTuple); 
00151         dataElement.appendChild(subTupleElement); 
00152       }
00153     }
00154     // append row element to parent      
00155     parElement.appendChild(dataElement); 
00156   }
00157 }
00158 
00159 // append tuple entry (template on all possible types )
00160 
00161 template <class T> 
00162 bool Anaphe::AIDA_XMLStore::TupleTranslator::appendTupleEntry(DataXML::DataObject & dataElement,  T value) 
00163 { 
00164 
00165   DataXML::DataObject entryElement;
00166   entryElement.setName("entry");
00167   entryElement.setAttribute("value",  toString(value) );
00168 
00169   dataElement.appendChild(entryElement);  
00170 
00171   return true;
00172 }
00173 
00174 
00175 
00176 
00178 
00179 AIDA::Dev::IDevTuple * Anaphe::AIDA_XMLStore::TupleTranslator::createFromXML(AIDA::Dev::IDevTupleFactory& factory)
00180 { 
00181   // read header 
00182   std::string title,options = emptyString;
00183   getObjectHeader(m_element,m_name,title,m_path,options); 
00184 
00185 
00186   // read annotation
00187   AnnotationData annoData; 
00188   getAnnotation(m_element,annoData); 
00189 
00190   std::vector<std::string> colNames; 
00191   std::vector<std::string> colTypes; 
00192 
00193   // Now I can create the cloud 
00194 
00195   AIDA::Dev::IDevTuple * tuple  = 0;
00196      //  factory.createTuple( title, colNames, colTypes, options );
00197 
00198   if (!tuple) { 
00199     std::cerr << " AIDA_XMLStore::TupleTranslator - Cannot create Tuple " << std::endl; 
00200     return 0; 
00201   }
00202 
00203   //no need to  get statistics since for AIDA 
00204 
00205   // set annotation
00206   AIDA::IAnnotation & anno = tuple->annotation(); 
00207   setAnnotation(&anno,annoData); 
00208 
00209   // get the data 
00210  
00211   const DataXML::DataObject * dataElement = m_element.getChild("entries1d");
00212   if (!dataElement) return tuple; 
00213   //int i = 0; 
00214   //cout << " Number of elements " << dataElement->children().size() << endl; 
00215   for (std::vector<DataXML::DataObject>::const_iterator entryElement = dataElement->children().begin(); entryElement != dataElement->children().end(); ++entryElement) {  
00216     // need to check on the name to eliminate white spaces
00217     if (entryElement->name() == "entry1d") { 
00218       double xval,yval,zval = 0;    
00219       double weight = 1; 
00220       getCloudEntryData(*entryElement,1,xval,yval,zval,weight);
00221       // fill the cloud with the contents 
00222       //cout << " entry " << entryElement->name() << " " << i << " x " << xval << " w " << weight << endl; 
00223       tuple->fill(xval,weight); 
00224       //i++;
00225     }
00226   }
00227 
00228   //  std::cout << " cloud entries = " << cloud->entries() << endl; 
00229 
00230   return tuple; 
00231 }

Generated on Tue Nov 19 12:32:57 2002 for AIDA_XMLStore by doxygen1.2.16