Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYTrafic.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 /*
17  *
18  */
19 
20 #if TY_USE_IHM
22 #endif
23 
24 #include "TYTrafic.h"
25 
26 #include "Tympan/core/logging.h"
27 
29 
31 {
33 
34  lv.vehicleType = VehicleType_VL;
35  hgv.vehicleType = VehicleType_PL;
36  for (unsigned i = 0; i < NB_VEHICLE_TYPES; ++i)
37  {
38  arr[i].flowType = FlowType_CONST;
39  arr[i].trafficFlow = 0;
40  arr[i].trafficSpeed = 0;
41  assert((arr[i].vehicleType == i + 1) && "Bad consistency assumption wrt vehicles types in Ctor");
42  }
43 }
44 
46 {
47  *this = other;
48 }
49 
51 
53 {
54  if (this != &other)
55  {
56  TYElement::operator=(other);
57  for (unsigned i = 0; i < NB_VEHICLE_TYPES; ++i)
58  {
59  arr[i].flowType = other.arr[i].flowType;
60  arr[i].trafficFlow = other.arr[i].trafficFlow;
61  arr[i].trafficSpeed = other.arr[i].trafficSpeed;
62  }
63  }
64  return *this;
65 }
66 
67 bool TYTrafic::operator==(const TYTrafic& other) const
68 {
69  if (this != &other)
70  {
71  for (unsigned i = 0; i < NB_VEHICLE_TYPES; ++i)
72  {
73  if (arr[i].flowType != other.arr[i].flowType)
74  {
75  return false;
76  };
77  if (arr[i].trafficFlow != other.arr[i].trafficFlow)
78  {
79  return false;
80  };
81  if (arr[i].trafficSpeed != other.arr[i].trafficSpeed)
82  {
83  return false;
84  };
85  }
86  }
87  return true;
88 }
89 
90 bool TYTrafic::operator!=(const TYTrafic& other) const
91 {
92  return !operator==(other);
93 }
94 
95 bool TYTrafic::deepCopy(const TYElement* pOther, bool copyId /*=true*/, bool pUseCopyTag /*=false*/)
96 {
97  if (!TYElement::deepCopy(pOther, copyId))
98  {
99  return false;
100  }
101 
102  const TYTrafic* pOtherTrafic = dynamic_cast<const TYTrafic*>(pOther);
103  assert(pOtherTrafic && "invalid cast to TYTrafic*");
104 
105  *this = *pOtherTrafic;
106  return true;
107 }
108 
109 std::string TYTrafic::toString() const
110 {
111  return "TYTrafic";
112 }
113 
115 {
116  DOM_Element domTrafficElem = TYElement::toXML(domElement);
117 
118  for (unsigned i = 0; i < NB_VEHICLE_TYPES; ++i)
119  {
120  const RoadTrafficComponent& rtc = arr[i];
121  QDomDocument domDoc = domTrafficElem.ownerDocument();
122  QDomElement componentElem = domDoc.createElement("TrafficComponent");
123  componentElem.setAttribute("flowType", rtc.flowType);
124  componentElem.setAttribute("trafficFlow", rtc.trafficFlow);
125  componentElem.setAttribute("trafficSpeed", rtc.trafficSpeed);
126  assert((rtc.vehicleType == i + 1) && "Bad consistency assumption wrt vehicles types");
127  componentElem.setAttribute("vehicleType", rtc.vehicleType);
128  domTrafficElem.appendChild(componentElem);
129  }
130  return domTrafficElem;
131 }
132 
134 {
135  TYElement::fromXML(domElement);
136 
137  QDomNodeList children = domElement.elementsByTagName("TrafficComponent");
138  if (children.size() != NB_VEHICLE_TYPES)
139  {
140  OMessageManager::get()->error("Loading TYTrafic element %s, "
141  "%u TrafficComponent child elements were found but %u were expected",
142  str_qt2c(getStringID()), children.size(), NB_VEHICLE_TYPES);
143  return 0;
144  }
145 
146  for (unsigned i = 0; i < NB_VEHICLE_TYPES; ++i)
147  {
148  QDomElement elem = children.item(i).toElement();
149  if (elem.isNull())
150  {
151  debugXml(children.item(i));
152  return 0;
153  }
154  if (!fromXML_TrafficComponent(elem, arr[i]))
155  {
156  return 0;
157  }
158  assert((arr[i].vehicleType == i + 1) && "Inconsistent vehicle types in traffic components");
159  }
160 
161  return 1;
162 }
163 
164 int TYTrafic::fromXML_TrafficComponent(DOM_Element domElement, RoadTrafficComponent& rtc)
165 {
166  QString s;
167  bool ok = false;
168 
169  // Deserialise the trafficFlow
170  s = domElement.attribute("trafficFlow", QString());
171  if (s.isEmpty()) // Attribute not found
172  {
174  "Can not read the TrafficComponent `trafficFlow` attribute for element %s.",
175  str_qt2c(getStringID()));
176  return 0;
177  }
178  double trafficFlow = s.toDouble(&ok);
179  if (!ok)
180  {
182  "Floating point number expected for attribute `trafficFlow` on element %s, not %s",
184  return 0;
185  }
186 
187  // Deserialise the trafficSpeed
188  s = domElement.attribute("trafficSpeed", QString());
189  if (s.isEmpty()) // Attribute not found
190  {
192  "Can not read the TrafficComponent `trafficSpeed` attribute for element %s.",
193  str_qt2c(getStringID()));
194  return 0;
195  }
196  double trafficSpeed = s.toDouble(&ok);
197  if (!ok)
198  {
200  "Floating point number expected for attribute `trafficSpeed` on element %s, not %s",
202  return 0;
203  }
204 
205  // Deserialise the flowType
206  s = domElement.attribute("flowType", QString());
207  if (s.isEmpty()) // Attribute not found
208  {
210  "Can not read the TrafficComponent `flowType` attribute for element %s.",
211  str_qt2c(getStringID()));
212  return 0;
213  }
214  unsigned flowType = s.toUInt(&ok);
215  if (!ok)
216  {
217  OMessageManager::get()->error("Integer expected for attribute `flowType` on element %s, not %s",
219  return 0;
220  }
221 
222  // Deserialise the vehicleType
223  s = domElement.attribute("vehicleType", QString());
224  if (s.isEmpty()) // Attribute not found
225  {
227  "Can not read the TrafficComponent `vehicleType` attribute for element %s.",
228  str_qt2c(getStringID()));
229  return 0;
230  }
231  unsigned vehicleType = s.toUInt(&ok);
232  if (!ok)
233  {
234  OMessageManager::get()->error("Integer expected for attribute `vehicleType` on element %s, not %s",
236  return 0;
237  }
238 
239  rtc.trafficFlow = trafficFlow;
240  rtc.trafficSpeed = trafficSpeed;
241  rtc.flowType = static_cast<RoadFlowType>(flowType);
242  rtc.vehicleType = static_cast<RoadVehicleType>(vehicleType);
243  return 1;
244 }
NxReal s
Definition: NxVec3.cpp:317
QDomElement DOM_Element
Definition: QT2DOM.h:30
#define debugXml(expr)
Definition: TYElement.h:1036
const char * str_qt2c(const QString &qstr)
Definition: TYElement.h:1012
outil IHM pour un traffic (fichier header)
TY_EXTENSION_INST(TYTrafic)
virtual void error(const char *message,...)
Definition: logging.cpp:127
static OMessageManager * get()
Definition: logging.cpp:108
virtual const char * getClassName() const
Definition: TYElement.h:249
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYElement.cpp:307
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYElement.cpp:368
QString _name
Nom courant de l'element.
Definition: TYElement.h:966
TYElement & operator=(const TYElement &other)
Definition: TYElement.cpp:265
virtual int fromXML(DOM_Element domElement)
Definition: TYElement.cpp:381
QString getStringID() const
Definition: TYElement.h:653
QString generateName(const char *classname)
Retourne le nom de la classe associe a un nombre.
static TYNameManager * get()
Retourne l'instance singleton.
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYTrafic.cpp:95
int fromXML_TrafficComponent(DOM_Element domElement, RoadTrafficComponent &rtc)
Auxilliary methods used to read a RoadTrafficComponent from XML.
Definition: TYTrafic.cpp:164
@ NB_VEHICLE_TYPES
Definition: TYTrafic.h:41
virtual ~TYTrafic()
Definition: TYTrafic.cpp:50
RoadTrafficComponent arr[NB_VEHICLE_TYPES]
Definition: TYTrafic.h:125
RoadTrafficComponent lv
Definition: TYTrafic.h:121
bool operator!=(const TYTrafic &other) const
Operateur !=.
Definition: TYTrafic.cpp:90
TYTrafic()
Definition: TYTrafic.cpp:30
virtual std::string toString() const
Definition: TYTrafic.cpp:109
RoadTrafficComponent hgv
Definition: TYTrafic.h:122
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYTrafic.cpp:114
TYTrafic & operator=(const TYTrafic &other)
Operateur =.
Definition: TYTrafic.cpp:52
virtual int fromXML(DOM_Element domElement)
Definition: TYTrafic.cpp:133
bool operator==(const TYTrafic &other) const
Operateur ==.
Definition: TYTrafic.cpp:67