DataFactory.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003 
00004 This file is part of MeRMaID.
00005 
00006 MeRMaID is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation, either version 3 of the License, or
00009 (at your option) any later version.
00010 
00011 MeRMaID 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
00014 GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with MeRMaID.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 
00022 /**
00023  * @Filename DataFactory.cpp
00024  * @Description DataFactory class implementation.
00025  * @Status Implementing
00026  * @Version $Id: DataFactory.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer Marco Barbosa + Nelson Ramos
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include <sstream>
00033 #include <boost/pointer_cast.hpp>
00034 
00035 #include "DataFactory.hpp"
00036 
00037 #include <XmlAttribute.hpp>
00038 #include <XmlAttributeVector.hpp>
00039 #include <XmlElement.hpp>
00040 #include <XmlElementVector.hpp>
00041 #include <XmlDocument.hpp>
00042 
00043 #include "DataArray.hpp"
00044 #include "DataType.hpp"
00045 
00046 #include <Exception.hpp>
00047 
00048 #include <iostream>
00049 #include <sstream>
00050 
00051 using namespace mermaid::support::data;
00052 
00053 using mermaid::support::errorhandling::Exception;
00054 using boost::shared_ptr;
00055 using boost::static_pointer_cast;
00056 
00057 using mermaid::support::xml::XmlAttribute;
00058 using mermaid::support::xml::XmlAttributeVector;
00059 using mermaid::support::xml::XmlDocument;
00060 using mermaid::support::xml::XmlElement;
00061 using mermaid::support::xml::XmlElementVector;
00062 
00063 using yarp::os::Value;
00064 
00065 std::map<std::string, shared_ptr<DataStructureDescription> > DataFactory::dataStructureDescriptionMap = std::map<std::string, shared_ptr<DataStructureDescription> >();
00066 
00067 shared_ptr<Integer> DataFactory::buildInteger (int value)
00068 {
00069   return shared_ptr<Integer> (new Integer (value));
00070 };
00071 
00072 shared_ptr<IntegerArray> DataFactory::buildDynamicIntegerArray()
00073 {
00074   return shared_ptr<IntegerArray> (new IntegerArray());
00075 };
00076 
00077 shared_ptr<IntegerArray> DataFactory::buildStaticIntegerArray (int size)
00078 {
00079   return shared_ptr<IntegerArray> (new IntegerArray (size));
00080 };
00081 
00082 shared_ptr<Double> DataFactory::buildDouble (double value)
00083 {
00084   return shared_ptr<Double> (new Double (value));
00085 };
00086 
00087 shared_ptr<DoubleArray> DataFactory::buildDynamicDoubleArray()
00088 {
00089   return shared_ptr<DoubleArray> (new DoubleArray());
00090 };
00091 
00092 shared_ptr<DoubleArray> DataFactory::buildStaticDoubleArray (int size)
00093 {
00094   return shared_ptr<DoubleArray> (new DoubleArray (size));
00095 };
00096 
00097 shared_ptr<String> DataFactory::buildString (std::string value)
00098 {
00099   return shared_ptr<String> (new String (value));
00100 };
00101 
00102 shared_ptr<StringArray> DataFactory::buildDynamicStringArray()
00103 {
00104   return shared_ptr<StringArray> (new StringArray());
00105 };
00106 
00107 shared_ptr<StringArray> DataFactory::buildStaticStringArray (int size)
00108 {
00109   return shared_ptr<StringArray> (new StringArray (size));
00110 };
00111 
00112 
00113 shared_ptr<DataValueVector> DataFactory::buildDataValueVector()
00114 {
00115   //std::cerr << "DataFactory::buildDataValueVector()" << std::endl;
00116   DataValueVector * v = new DataValueVector();
00117   return shared_ptr<DataValueVector> (v);
00118 };
00119 
00120 shared_ptr<DataValueVector> DataFactory::buildDataValueVector (Bottle& b)
00121 {
00122   shared_ptr<DataValueVector> v (new DataValueVector());
00123   
00124   for (int i = 0; i < (int) b.size(); i++) {
00125   
00126     //std::cerr << "DataFactory::buildDataValueVector : [" << i << "]: ";
00127     
00128     Value& element = b.get (i);
00129     
00130     if (element.isInt()) {
00131       shared_ptr<DataValue> i = DataFactory::buildInteger (element.asInt());
00132       v->pushBack (i);
00133       //std::cerr << "int " << element.asInt() << std::endl;
00134     }
00135     else if (element.isDouble()) {
00136       shared_ptr<DataValue> f = DataFactory::buildDouble (element.asDouble());
00137       v->pushBack (f);
00138       //std::cerr << "double " << element.asDouble() << std::endl;
00139     }
00140     else if (element.isString()) {
00141       shared_ptr<DataValue> s = DataFactory::buildString (std::string (element.asString().c_str()));
00142       v->pushBack (s);
00143       //std::cerr << "string \"" << element.asString() << "\"" << std::endl;
00144     }
00145     else if (element.isList()) {
00146       throw Exception ("DataFactory::buildDataValueVector : unable to read list");
00147       /*Bottle * b = element.asList();
00148       shared_ptr<DataValueVector> newVector(buildDataValueVector(*b).get());
00149       v->pushBack(newVector);*/
00150     }
00151     else {
00152       throw Exception ("DataFactory::buildDataValueVector : unrecognized type");
00153     }
00154   }
00155   
00156   return v;
00157 };
00158 
00159 
00160 void DataFactory::loadDataDescription (shared_ptr<XmlDocument> document)
00161 {
00162   std::cerr << "DataFactory::loadDataDescription" << std::endl;
00163   
00164   shared_ptr<XmlElement> rootElement = document->getRootElement();
00165   XmlElementVector dataElements = rootElement->getChildrenElements();
00166   
00167   XmlElementVector::iterator it;
00168   
00169   for (it = dataElements.begin(); it != dataElements.end(); it++) {
00170     shared_ptr<XmlElement> e = *it;
00171     
00172     shared_ptr<DataStructureDescription> d (new DataStructureDescription (e));
00173     std::string dataStructureName = d->getName();
00174     dataStructureDescriptionMap[dataStructureName] = d;
00175     std::cerr << "DataFactory::loadDataDescription : loaded: " << dataStructureName << std::endl;
00176   };
00177 }; // loadDataDescription()
00178 
00179 shared_ptr<DataBox> DataFactory::buildDataBox (std::string typeName)
00180 {
00181 
00182   /*if(typeName == "")
00183   {
00184       return shared_ptr<DataBox>(new DataBox());
00185   }*/
00186   
00187   shared_ptr<DataStructureDescription> dsd = dataStructureDescriptionMap[typeName];
00188   
00189   if (dsd == false) {
00190     throw Exception ("DataFactory::buildDataBoxFromStructure : invalid type name: " + typeName);
00191   }
00192   
00193   shared_ptr<DataBox> dbox (new DataBox (dsd));
00194   
00195   return dbox;
00196 };//buildDataFromStructure()
00197 
00198 shared_ptr<DataBox> DataFactory::buildDataBoxFromStructureAndBottle (std::string structureTypeName, Bottle &bottle)
00199 {
00200   //std::cerr << "DataFactory::buildDataBoxFromStructureAndBottle : structureTypeName=" << structureTypeName << ", bottle=" << bottle.toString() << std::endl;
00201   shared_ptr<DataValueVector> valueVector = buildDataValueVector (bottle);
00202   return buildDataBoxFromStructureAndValueVector (structureTypeName, valueVector);
00203   
00204 }; // buildDataBoxFromStructureAndBottle()
00205 
00206 
00207 
00208 shared_ptr<DataBox> DataFactory::buildDataBoxFromStructureAndValueVector (std::string structureTypeName, shared_ptr<DataValueVector> vv)
00209 {
00210   //throw Exception("DataFactory::buildDataBoxFromStructureAndValueVector : not up to date");
00211   
00212   shared_ptr<DataBox> dbox = buildDataBox (structureTypeName);
00213   dbox->setDataFromValueVector (vv);
00214   return dbox;
00215   
00216 }; // buildDataFromStructureAndValueVector()
00217 
00218 shared_ptr<DataBoxArray> DataFactory::buildDynamicDataBoxArray (std::string typeName)
00219 {
00220   return shared_ptr<DataBoxArray> (new DataBoxArray (typeName));
00221 }; // buildDynamicDataBoxArray()
00222 
00223 shared_ptr<DataBoxArray> DataFactory::buildStaticDataBoxArray (std::string typeName, int size)
00224 {
00225   return shared_ptr<DataBoxArray> (new DataBoxArray (typeName, size));
00226 }; // buildStaticDataBoxArray()
00227 
00228 
00229 void DataFactory::writeDataBoxToBottle (shared_ptr<DataBox> box, Bottle &bottle)
00230 {
00231 //    std::cerr << "DataFactory::writeDataBoxToBottle : box=" << (std::string)((*box)) << std::endl;
00232 
00233   shared_ptr<DataValueVector>  vv = box->getDataValueVector();
00234   
00235   int vectorSize = vv->getSize();
00236   
00237   for (int i = 0; i < vectorSize; i++) {
00238     shared_ptr<DataValue> d = vv->getValue (i);
00239     DataType t = d->getType();
00240     
00241     if (t == INTEGER) {
00242       Integer * i = (Integer*) d.get();
00243       bottle.addInt (i->getValue());
00244     }
00245     else if (t == DOUBLE) {
00246       Double * f = (Double*) d.get();
00247       bottle.addDouble (f->getValue());
00248     }
00249     else if (t == STRING) {
00250       String * s = (String*) d.get();
00251       bottle.addString (s->getValue().c_str());
00252     }
00253     else if (t == INTEGERARRAY) {
00254       IntegerArray * a = (IntegerArray*) d.get();
00255       int size = a->getSize();
00256       
00257       if (a->isDynamic() == true) {
00258         bottle.addInt (size);
00259       }
00260       
00261       for (int i = 0; i < size; i++) {
00262         int integer = (*a) [i]->getValue();
00263         bottle.addInt (integer);
00264       }
00265     }
00266     else if (t == DOUBLEARRAY) {
00267       DoubleArray * a = (DoubleArray*) d.get();
00268       int size = a->getSize();
00269       
00270       if (a->isDynamic() == true) {
00271         bottle.addInt (size);
00272       }
00273       
00274       for (int i = 0; i < size; i++) {
00275         double d = (*a) [i]->getValue();
00276         bottle.addDouble (d);
00277       }
00278     }
00279     else if (t == STRINGARRAY) {
00280       StringArray * a = (StringArray*) d.get();
00281       int size = a->getSize();
00282       
00283       if (a->isDynamic() == true) {
00284         bottle.addInt (size);
00285       }
00286       
00287       for (int i = 0; i < size; i++) {
00288         std::string s = (*a) [i]->getValue();
00289         bottle.addString (s.c_str());
00290       }
00291     }
00292     else if (t == DATABOX) {
00293       shared_ptr<DataBox> box = static_pointer_cast<DataBox> (d);
00294       writeDataBoxToBottle (box, bottle);
00295     }
00296     else if (t == DATABOXARRAY) {
00297       DataBoxArray * a = (DataBoxArray*) d.get();
00298       int size = a->getSize();
00299       
00300       if (a->isDynamic() == true) {
00301         bottle.addInt (size);
00302       }
00303       
00304       for (int i = 0; i < size; i++) {
00305         shared_ptr<DataBox> b = (*a) [i];
00306         writeDataBoxToBottle (b, bottle);
00307       }
00308     }
00309     else {
00310       throw Exception ("DataFactory::writeDataBoxToBottle: unknown type conversion");
00311     }
00312   };
00313 }; // writeDataBoxToBottle()
00314 
00315 shared_ptr<DataStructureDescription> DataFactory::getDataStructureDescription (std::string dataStructureName)
00316 {
00317   return dataStructureDescriptionMap[dataStructureName];
00318 }
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3