source: XIOS/trunk/src/node/scalar.cpp @ 1111

Last change on this file since 1111 was 1106, checked in by mhnguyen, 7 years ago

Fixing bug of referencing to an object.

+) From now on, two objects of a same grid element (domain, axis, scalar) are equal if
they have the same non-empty attributes and the same transformations.
(This is very common case with inheritance by *_ref).

Test
+) On Curie
+) Ok with toy_cmip6

File size: 5.6 KB
Line 
1#include "scalar.hpp"
2
3#include "attribute_template.hpp"
4#include "object_template.hpp"
5#include "group_template.hpp"
6#include "object_factory.hpp"
7#include "xios_spl.hpp"
8#include "type.hpp"
9
10namespace xios {
11
12   /// ////////////////////// Définitions ////////////////////// ///
13
14   CScalar::CScalar(void)
15      : CObjectTemplate<CScalar>()
16      , CScalarAttributes()
17      , relFiles()
18   { /* Ne rien faire de plus */ }
19
20   CScalar::CScalar(const StdString & id)
21      : CObjectTemplate<CScalar>(id)
22      , CScalarAttributes()
23      , relFiles()
24   { /* Ne rien faire de plus */ }
25
26   CScalar::~CScalar(void)
27   { /* Ne rien faire de plus */ }
28
29   std::map<StdString, ETranformationType> CScalar::transformationMapList_ = std::map<StdString, ETranformationType>();
30   bool CScalar::dummyTransformationMapList_ = CScalar::initializeTransformationMap(CScalar::transformationMapList_);
31   bool CScalar::initializeTransformationMap(std::map<StdString, ETranformationType>& m)
32   {
33     m["reduce_axis"]   = TRANS_REDUCE_AXIS_TO_SCALAR;
34     m["extract_axis"]  = TRANS_EXTRACT_AXIS_TO_SCALAR;
35     m["reduce_domain"] = TRANS_REDUCE_DOMAIN_TO_SCALAR;
36   }
37
38   StdString CScalar::GetName(void)   { return (StdString("scalar")); }
39   StdString CScalar::GetDefName(void){ return (CScalar::GetName()); }
40   ENodeType CScalar::GetType(void)   { return (eScalar); }
41
42   CScalar* CScalar::createScalar()
43   {
44     CScalar* scalar = CScalarGroup::get("scalar_definition")->createChild();
45     return scalar;
46   }
47
48   bool CScalar::IsWritten(const StdString & filename) const
49   {
50      return (this->relFiles.find(filename) != this->relFiles.end());
51   }
52
53   void CScalar::addRelFile(const StdString& filename)
54   {
55      this->relFiles.insert(filename);
56   }
57
58   void CScalar::checkAttributes(void)
59   {
60   }
61
62  void CScalar::checkAttributesOnClient()
63  {
64
65  }
66
67  /*!
68    Compare two scalar objects.
69    They are equal if only if they have identical attributes as well as their values.
70    Moreover, they must have the same transformations.
71  \param [in] scalar Compared scalar
72  \return result of the comparison
73  */
74  bool CScalar::isEqual(CScalar* obj)
75  {
76    bool objEqual = SuperClass::isEqual(obj);
77    if (!objEqual) return objEqual;
78
79    TransMapTypes thisTrans = this->getAllTransformations();
80    TransMapTypes objTrans  = obj->getAllTransformations();
81
82    TransMapTypes::const_iterator it, itb, ite;
83    std::vector<ETranformationType> thisTransType, objTransType;
84    for (it = thisTrans.begin(); it != thisTrans.end(); ++it)
85      thisTransType.push_back(it->first);
86    for (it = objTrans.begin(); it != objTrans.end(); ++it)
87      objTransType.push_back(it->first);
88
89    if (thisTransType.size() != objTransType.size()) return false;
90    for (int idx = 0; idx < thisTransType.size(); ++idx)
91      objEqual &= (thisTransType[idx] == objTransType[idx]);
92
93    return objEqual;
94  }
95
96  CTransformation<CScalar>* CScalar::addTransformation(ETranformationType transType, const StdString& id)
97  {
98    transformationMap_.push_back(std::make_pair(transType, CTransformation<CScalar>::createTransformation(transType,id)));
99    return transformationMap_.back().second;
100  }
101
102  bool CScalar::hasTransformation()
103  {
104    return (!transformationMap_.empty());
105  }
106
107  void CScalar::setTransformations(const TransMapTypes& scalarTrans)
108  {
109    transformationMap_ = scalarTrans;
110  }
111
112  CScalar::TransMapTypes CScalar::getAllTransformations(void)
113  {
114    return transformationMap_;
115  }
116
117  void CScalar::duplicateTransformation(CScalar* src)
118  {
119    if (src->hasTransformation())
120    {
121      this->setTransformations(src->getAllTransformations());
122    }
123  }
124
125  /*!
126   * Go through the hierarchy to find the scalar from which the transformations must be inherited
127   */
128  void CScalar::solveInheritanceTransformation()
129  {
130    if (hasTransformation() || !hasDirectScalarReference())
131      return;
132
133    CScalar* scalar = this;
134    std::vector<CScalar*> refScalar;
135    while (!scalar->hasTransformation() && scalar->hasDirectScalarReference())
136    {
137      refScalar.push_back(scalar);
138      scalar = scalar->getDirectScalarReference();
139    }
140
141    if (scalar->hasTransformation())
142      for (size_t i = 0; i < refScalar.size(); ++i)
143        refScalar[i]->setTransformations(scalar->getAllTransformations());
144  }
145
146  /*!
147    Parse children nodes of a scalar in xml file.
148    \param node child node to process
149  */
150  void CScalar::parse(xml::CXMLNode & node)
151  {
152    SuperClass::parse(node);
153
154    if (node.goToChildElement())
155    {
156      StdString nodeElementName;
157      do
158      {
159        StdString nodeId("");
160        if (node.getAttributes().end() != node.getAttributes().find("id"))
161        { nodeId = node.getAttributes()["id"]; }
162
163        nodeElementName = node.getElementName();
164        std::map<StdString, ETranformationType>::const_iterator ite = transformationMapList_.end(), it;
165        it = transformationMapList_.find(nodeElementName);
166        if (ite != it)
167        {
168          transformationMap_.push_back(std::make_pair(it->second, CTransformation<CScalar>::createTransformation(it->second,
169                                                                                                                 nodeId,
170                                                                                                                 &node)));
171        }
172        else
173        {
174          ERROR("void CScalar::parse(xml::CXMLNode & node)",
175                << "The transformation " << nodeElementName << " has not been supported yet.");
176        }
177      } while (node.goToNextElement()) ;
178      node.goToParentElement();
179    }
180  }
181
182  // Definition of some macros
183  DEFINE_REF_FUNC(Scalar,scalar)
184
185} // namespace xios
Note: See TracBrowser for help on using the repository browser.