00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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 {
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;
00062
00063 return *this;
00064 }
00065
00066 bool Anaphe::AIDA_XMLStore::TupleTranslator::toXML()
00067 {
00068
00069 appendObjectHeader(m_element,"tuple",m_name,m_tuple->title(),m_path);
00070
00071
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
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
00101
00102 bool Anaphe::AIDA_XMLStore::TupleTranslator::setData()
00103 {
00104
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
00116
00117 void Anaphe::AIDA_XMLStore::TupleTranslator::appendRow(DataXML::DataObject & parElement, AIDA::ITuple * tuple)
00118 {
00119
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
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
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
00150 appendRow(subTupleElement,subTuple);
00151 dataElement.appendChild(subTupleElement);
00152 }
00153 }
00154
00155 parElement.appendChild(dataElement);
00156 }
00157 }
00158
00159
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
00182 std::string title,options = emptyString;
00183 getObjectHeader(m_element,m_name,title,m_path,options);
00184
00185
00186
00187 AnnotationData annoData;
00188 getAnnotation(m_element,annoData);
00189
00190 std::vector<std::string> colNames;
00191 std::vector<std::string> colTypes;
00192
00193
00194
00195 AIDA::Dev::IDevTuple * tuple = 0;
00196
00197
00198 if (!tuple) {
00199 std::cerr << " AIDA_XMLStore::TupleTranslator - Cannot create Tuple " << std::endl;
00200 return 0;
00201 }
00202
00203
00204
00205
00206 AIDA::IAnnotation & anno = tuple->annotation();
00207 setAnnotation(&anno,annoData);
00208
00209
00210
00211 const DataXML::DataObject * dataElement = m_element.getChild("entries1d");
00212 if (!dataElement) return tuple;
00213
00214
00215 for (std::vector<DataXML::DataObject>::const_iterator entryElement = dataElement->children().begin(); entryElement != dataElement->children().end(); ++entryElement) {
00216
00217 if (entryElement->name() == "entry1d") {
00218 double xval,yval,zval = 0;
00219 double weight = 1;
00220 getCloudEntryData(*entryElement,1,xval,yval,zval,weight);
00221
00222
00223 tuple->fill(xval,weight);
00224
00225 }
00226 }
00227
00228
00229
00230 return tuple;
00231 }