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

FunctionTranslator.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 FunctionTranslator
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 "FunctionTranslator.h"
00031 #include "AIDA/IFunction.h"
00032 #include "AIDA/IModelFunction.h"
00033 #include "AIDA_Dev/IDevFunction.h"
00034 #include "AIDA_Dev/IDevFunctionFactory.h"
00035 
00036 #include "DataXML/DataObject.h"
00037 
00038 static const std::string emptyString = "";
00039 
00041 Anaphe::AIDA_XMLStore::FunctionTranslator::FunctionTranslator(const AIDA::IFunction * f, const std::string & name, const std::string & path ) : 
00042   m_function(f), m_name(name), m_path(path)
00043 {
00044 }
00046 Anaphe::AIDA_XMLStore::FunctionTranslator::FunctionTranslator(const DataXML::DataObject * xmlObject) : 
00047   m_element(*xmlObject)
00048 {
00049 }
00050 
00051 
00052 Anaphe::AIDA_XMLStore::FunctionTranslator::~FunctionTranslator() 
00053 {  // no op
00054 }
00055 
00056 Anaphe::AIDA_XMLStore::FunctionTranslator::FunctionTranslator(const Anaphe::AIDA_XMLStore::FunctionTranslator &)  
00057 {
00058 }
00059 
00060 Anaphe::AIDA_XMLStore::FunctionTranslator & Anaphe::AIDA_XMLStore::FunctionTranslator::operator = (const Anaphe::AIDA_XMLStore::FunctionTranslator &rhs) 
00061 {
00062    if (this == &rhs) return *this;  // time saving self-test
00063 
00064    return *this;
00065 }
00066 
00067 bool Anaphe::AIDA_XMLStore::FunctionTranslator::toXML() 
00068 { 
00069   // write name (no need for title is in annotation)
00070   appendObjectHeader(m_element,"function",m_name,emptyString,m_path); 
00071   // extra stuff for functions (is normalized)
00072   std::string norm = "false"; 
00073   AIDA::IModelFunction* mf = 
00074     dynamic_cast< AIDA::IModelFunction* >( const_cast<AIDA::IFunction * > (m_function ));
00075   if (mf) { 
00076     if (mf->isNormalized()) 
00077            norm = "true"; 
00078   }
00079   m_element.setAttribute("isNormalized", norm );  
00080   
00081   appendAnnotation(m_element,m_function->annotation()); 
00082 
00083   if (!setData () ) return false;
00084   return true;
00085 
00086 }
00087 
00088 
00089 bool Anaphe::AIDA_XMLStore::FunctionTranslator::setData()
00090 { 
00091   // write data element and append to the dataobject
00092 
00093   DataXML::DataObject codeletElement;
00094   codeletElement.setName("codelet");
00095   codeletElement.setAttribute("name",m_function->codeletString());
00096   m_element.appendChild(codeletElement);
00097 
00098   DataXML::DataObject argsElement;
00099   argsElement.setName("arguments");
00100   // put here the ranges
00101   m_element.appendChild(argsElement);
00102   
00103   DataXML::DataObject parsElement;
00104   parsElement.setName("parameters");
00105   // put here the params
00106   std::vector<std::string> parNames = m_function->parameterNames(); 
00107   std::vector<double> params = m_function->parameters(); 
00108   for (int i = 0; i < m_function->numberOfParameters() ; ++ i) { 
00109     DataXML::DataObject pElement;
00110     pElement.setName("parameter");
00111     pElement.setAttribute("name",parNames[i]); 
00112     pElement.setAttribute("value",toString(params[i]) ); 
00113     parsElement.appendChild(pElement);
00114   }
00115   m_element.appendChild(parsElement); 
00116 
00117   return true;
00118 }
00119 
00121 
00122 AIDA::Dev::IDevFunction * Anaphe::AIDA_XMLStore::FunctionTranslator::createFromXML(AIDA::Dev::IDevFunctionFactory& factory)
00123 { 
00124   // read header 
00125   std::string title,options = emptyString;
00126   getObjectHeader(m_element,m_name,title,m_path,options); 
00127   bool norm = false; 
00128   if (m_element.getAttributeValue("isNormalized") == "true") 
00129     norm = true;
00130 
00131   // if title is empty try with name
00132   if (title.empty()) title = m_name; 
00133 
00134   const DataXML::DataObject * codElement = m_element.getChild("codelet");
00135   std::string codelet = ""; 
00136   if (codElement) 
00137     codelet = codElement->getAttributeValue("name");
00138 
00139   // read annotation
00140   AnnotationData annoData; 
00141   getAnnotation(m_element,annoData); 
00142 
00143   // Now I can create the function 
00144 
00145   std::string model = codelet; 
00146   // then try with title
00147   if (model.empty()) model = title; 
00148 
00149 
00150   AIDA::Dev::IDevFunction * f  = 
00151     factory.createFunctionByName( model);
00152 
00153   if (!f) return 0; 
00154 
00155   //no need to  get statistics since for AIDA 
00156 
00157   // set annotation
00158   AIDA::IAnnotation & anno = f->annotation(); 
00159   setAnnotation(&anno,annoData); 
00160 
00161   // forget about the range.. (to be done..)
00162 
00163   // get the data 
00164  
00165   const DataXML::DataObject * dataElement = m_element.getChild("parameters");
00166   if (!dataElement) return f;
00167   std::vector<std::string> parNames; 
00168   std::vector<double> params; 
00169   for (std::vector<DataXML::DataObject>::const_iterator entryElement = dataElement->children().begin(); entryElement != dataElement->children().end(); ++entryElement) {  
00170     if (entryElement->name() == "parameter") { 
00171       double val;   
00172       std::string name = entryElement->getAttributeValue("name"); 
00173       toValue(entryElement->getAttributeValue("value"), val);
00174       parNames.push_back(name); 
00175       params.push_back(val); 
00176     }
00177   }
00178   // check .. 
00179   if (f->numberOfParameters() != static_cast<int>(params.size() )) { 
00180     std::cerr << " FunctionTranslator: Error wrong number of parameters " << std::endl;
00181     return 0; 
00182 
00183   }
00184 
00185   f->setParameters(params); 
00186   f->setParameterNames(parNames); 
00187 
00188   return f; 
00189 }

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