preCICE v3.1.2
Loading...
Searching...
No Matches
XMLTag.cpp
Go to the documentation of this file.
1#include "xml/XMLTag.hpp"
2#include <Eigen/Core>
3#include <ostream>
4#include <utility>
6#include "utils/Helpers.hpp"
7#include "utils/assertion.hpp"
9
10namespace precice::xml {
11
17 : _listener(listener),
18 _name(std::move(tagName)),
19 _namespace(std::move(xmlNamespace)),
20 _occurrence(occurrence)
21{
22 if (not _namespace.empty()) {
23 _fullName = _namespace + ":" + _name;
24 } else {
26 }
27}
28
34
36{
37 _namespaces.push_back(namespaceName);
38 return *this;
39}
40
42{
45 if (not tag._namespace.empty()) {
47 }
48
49 _subtags.push_back(std::make_shared<XMLTag>(tag));
50 return *this;
51}
52
62
72
82
92
103
105{
108 _attributeHints.emplace(std::move(name), std::move(message));
109}
110
115
117{
120 if (iter != _doubleAttributes.end()) {
121 return iter->second.getValue();
122 }
123 if (default_value) {
124 return default_value.value();
125 }
126 PRECICE_UNREACHABLE("The XMLAttribute doesn't exist, check its default.");
127}
128
130{
132 iter = _intAttributes.find(name);
133 if (iter != _intAttributes.end()) {
134 return iter->second.getValue();
135 }
136 if (default_value) {
137 return default_value.value();
138 }
139 PRECICE_UNREACHABLE("The XMLAttribute doesn't exist, check its default.");
140}
141
143{
146 if (iter != _stringAttributes.end()) {
147 return iter->second.getValue();
148 }
149 if (default_value) {
150 return default_value.value();
151 }
152 PRECICE_UNREACHABLE("The XMLAttribute doesn't exist, check its default.");
153}
154
156{
159 if (iter != _booleanAttributes.end()) {
160 return iter->second.getValue();
161 }
162 if (default_value) {
163 return default_value.value();
164 }
165 PRECICE_UNREACHABLE("The XMLAttribute doesn't exist, check its default.");
166}
167
169{
171 // std::map<std::string, XMLAttribute<utils::DynVector> >::const_iterator iter;
174 const auto size = iter->second.getValue().size();
176 "Vector attribute \"{}\" of tag <{}> is {}D, "
177 "which does not match the dimension of the {}D precice-configuration.",
178 name, getFullName(), size, dimensions);
179
180 // Read only first "dimensions" components of the parsed vector values
181 Eigen::VectorXd result(dimensions);
182 const Eigen::VectorXd &parsed = iter->second.getValue();
183 for (int i = 0; i < dimensions; i++) {
184 result[i] = parsed[i];
185 }
186 PRECICE_DEBUG("Returning value = {}", result);
187 return result;
188}
189
191{
193
194 for (auto &element : aAttributes) {
195 auto name = element.first;
196
198 // check existing hints
199 if (auto pos = _attributeHints.find(name);
200 pos != _attributeHints.end()) {
201 PRECICE_ERROR("The tag <{}> in the configuration contains the attribute \"{}\". {}", _fullName, name, pos->second);
202 }
203
205 if (!matches.empty() && matches.front().distance < 3) {
206 matches.erase(std::remove_if(matches.begin(), matches.end(), [](auto &m) { return m.distance > 2; }), matches.end());
208 std::transform(matches.begin(), matches.end(), std::back_inserter(stringMatches), [](auto &m) { return m.name; });
209 PRECICE_ERROR("The tag <{}> in the configuration contains an unknown attribute \"{}\". Did you mean \"{}\"?", _fullName, name, fmt::join(stringMatches, ", "));
210 }
211 PRECICE_ERROR("The tag <{}> in the configuration contains an unknown attribute \"{}\". Expected attributes are {}.", _fullName, name, fmt::join(_attributes, ", "));
212 }
213 }
214
215 for (auto &pair : _doubleAttributes) {
216 pair.second.readValue(aAttributes);
217 }
218
219 for (auto &pair : _intAttributes) {
220 pair.second.readValue(aAttributes);
221 }
222
223 for (auto &pair : _stringAttributes) {
224 pair.second.readValue(aAttributes);
225 }
226
227 for (auto &pair : _booleanAttributes) {
228 pair.second.readValue(aAttributes);
229 }
230
231 for (auto &pair : _eigenVectorXdAttributes) {
232 pair.second.readValue(aAttributes);
233 }
234}
235
236/*void XMLTag:: readAttributes
237(
238 XMLReader* xmlReader )
239{
240 PRECICE_TRACE();
241// using utils::contained;
242// std::set<std::string> readNames;
243 for (int i=0; i < xmlReader->getAttributeCount(); i++){
244 std::string name = xmlReader->getAttributeName(i);
245 if (not utils::contained(name, _attributes)){
246 std::string error = "Wrong attribute \"" + name + "\"";
247 throw std::runtime_error{error};
248 }
249// else if (contained(name, _doubleAttributes)){
250// XMLAttribute<double>& attr = _doubleAttributes[name];
251// attr.readValue(xmlReader);
252// }
253// else if (contained(name, _intAttributes)){
254// XMLAttribute<int>& attr = _intAttributes[name];
255// attr.readValue(xmlReader);
256// }
257// else if (contained(name, _stringAttributes)){
258// XMLAttribute<std::string>& attr = _stringAttributes[name];
259// attr.readValue(xmlReader);
260// }
261// else if (contained(name, _booleanAttributes)){
262// XMLAttribute<bool>& attr = _booleanAttributes[name];
263// attr.readValue(xmlReader);
264// }
265// else if (contained(name, _vector2DAttributes)){
266// XMLAttribute<Vector2D>& attr = _vector2DAttributes[name];
267// attr.readValue(xmlReader);
268// }
269// else if (contained(name, _vector3DAttributes)){
270// XMLAttribute<Vector3D>& attr = _vector3DAttributes[name];
271// attr.readValue(xmlReader);
272// }
273// else if (contained(name, _dynVectorAttributes)){
274// XMLAttribute<DynVector>& attr = _dynVectorAttributes[name];
275// attr.readValue(xmlReader);
276// }
277// else {
278// throw std::runtime_error{"Internal error in readAttributes"};
279// }
280// readNames.insert(name);
281 }
282
283// // Check if all attributes are read
284// for (const std::string& name : _attributes){
285// if (not contained(name, readNames)){
286//
287// std::ostringstream stream;
288// stream << "Attribute \"" << name << "\" is not defined";
289// throw std::runtime_error{stream.str()};
290// }
291// }
292
293 for (auto & pair : _doubleAttributes){
294 pair.second.readValue(xmlReader);
295 }
296
297 for ( auto & pair : _intAttributes){
298 pair.second.readValue(xmlReader);
299 }
300
301 for ( auto & pair : _stringAttributes ){
302 pair.second.readValue(xmlReader);
303 }
304
305 for ( auto & pair : _booleanAttributes ){
306 pair.second.readValue(xmlReader);
307 }
308
309 for ( auto & pair : _eigenVectorXdAttributes ){
310 pair.second.readValue(xmlReader);
311 }
312}*/
313
315{
316 for (const auto &tag : _subtags) {
317 std::string ns = tag->_namespace;
318 bool configured = tag->isConfigured();
319
320 bool occurOnce = tag->getOccurrence() == OCCUR_ONCE;
321 bool occurOnceOrMore = tag->getOccurrence() == OCCUR_ONCE_OR_MORE;
322
323 if (not ns.empty()) {
326 configured |= nsIter->second;
327 }
328
329 if ((not configured) && (occurOnce || occurOnceOrMore)) {
330
331 if (tag->getNamespace().empty()) {
332 PRECICE_ERROR("Tag <{}> was not found but is required to occur at least once.", tag->getName());
333 } else {
334 PRECICE_ERROR("Tag <{}:... > was not found but is required to occur at least once.", tag->getNamespace());
335 }
336 }
337 }
338}
339
341{
342 _configured = false;
343
344 for (auto &pair : _configuredNamespaces) {
345 pair.second = false;
346 }
347
348 for (auto &pair : _doubleAttributes) {
349 pair.second.setRead(false);
350 }
351
352 for (auto &pair : _intAttributes) {
353 pair.second.setRead(false);
354 }
355
356 for (auto &pair : _stringAttributes) {
357 pair.second.setRead(false);
358 }
359
360 for (auto &pair : _booleanAttributes) {
361 pair.second.setRead(false);
362 }
363
364 for (auto &pair : _eigenVectorXdAttributes) {
365 pair.second.setRead(false);
366 }
367
368 for (auto &tag : _subtags) {
369 tag->_configured = false;
370 tag->resetAttributes();
371 }
372}
373
375{
376 _doubleAttributes.clear();
377 _intAttributes.clear();
378 _stringAttributes.clear();
379 _booleanAttributes.clear();
380 _subtags.clear();
381}
382
383//NoPListener& getNoPListener()
384//{
385// static NoPListener listener;
386// return listener;
387//}
388
390{
391 static NoPListener listener;
392 return XMLTag(listener, "configuration", XMLTag::OCCUR_ONCE);
393}
394
410
412{
414 return std::string("0..*");
416 return std::string("0..1");
417 } else if (occurrence == XMLTag::OCCUR_ONCE) {
418 return std::string("1");
420 return std::string("1..*");
421 }
422 return "";
423}
424} // namespace precice::xml
425
426//std::ostream& operator<<
427//(
428// std::ostream& os,
429// const precice::xml::XMLTag& tag )
430//{
431// os << tag.printDocumentation(80, 0);
432// return os;
433//}
#define PRECICE_ERROR(...)
Definition LogMacros.hpp:15
#define PRECICE_DEBUG(...)
Definition LogMacros.hpp:64
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:95
#define PRECICE_CHECK(check,...)
Definition LogMacros.hpp:35
std::string name
#define PRECICE_ASSERT(...)
Definition assertion.hpp:87
#define PRECICE_UNREACHABLE(...)
Definition assertion.hpp:95
T back_inserter(T... args)
This class provides a lightweight logger.
Definition Logger.hpp:16
const ATTRIBUTE_T & getValue() const
const std::string & getName() const
Represents an XML tag to be configured automatically.
Definition XMLTag.hpp:31
Eigen::VectorXd getEigenVectorXdAttributeValue(const std::string &name, int dimensions) const
Returns Eigen vector attribute value with given dimensions.
Definition XMLTag.cpp:168
std::string _fullName
Combination of name and namespace: _namespace + ":" + _name.
Definition XMLTag.hpp:249
AttributeMap< double > _doubleAttributes
Definition XMLTag.hpp:265
std::map< std::string, bool > _configuredNamespaces
Definition XMLTag.hpp:261
bool hasAttribute(const std::string &attributeName)
Definition XMLTag.cpp:111
std::string getStringAttributeValue(const std::string &name, std::optional< std::string > default_value=std::nullopt) const
Definition XMLTag.cpp:142
bool getBooleanAttributeValue(const std::string &name, std::optional< bool > default_value=std::nullopt) const
Definition XMLTag.cpp:155
AttributeMap< std::string > _stringAttributes
Definition XMLTag.hpp:269
void readAttributes(const std::map< std::string, std::string > &aAttributes)
reads all attributes of this tag
Definition XMLTag.cpp:190
XMLTag & setDocumentation(const std::string &documentation)
Adds a description of the purpose of this XML tag.
Definition XMLTag.cpp:29
Namespaces _namespaces
Definition XMLTag.hpp:257
std::string _namespace
XML namespace of the tag.
Definition XMLTag.hpp:246
void clear()
Removes all attributes and subtags.
Definition XMLTag.cpp:374
std::set< std::string > _attributes
Definition XMLTag.hpp:263
static std::string getOccurrenceString(Occurrence occurrence)
Definition XMLTag.cpp:411
const std::string & getFullName() const
Returns full name consisting of xml namespace + ":" + name.
Definition XMLTag.hpp:170
Occurrence
Types of occurrences of an XML tag.
Definition XMLTag.hpp:67
AttributeMap< int > _intAttributes
Definition XMLTag.hpp:267
void addAttributeHint(std::string name, std::string message)
Adds a hint for missing attributes, which will be displayed along the error message.
Definition XMLTag.cpp:104
XMLTag(Listener &listener, std::string name, Occurrence occurrence, std::string xmlNamespace="")
Standard constructor.
Definition XMLTag.cpp:12
std::map< std::string, std::string > _attributeHints
Definition XMLTag.hpp:275
std::string _name
Name of the tag.
Definition XMLTag.hpp:243
int getIntAttributeValue(const std::string &name, std::optional< int > default_value=std::nullopt) const
Definition XMLTag.cpp:129
std::string _doc
Definition XMLTag.hpp:251
XMLTag & addNamespace(const std::string &namespaceName)
Adds a namespace to the tag.
Definition XMLTag.cpp:35
AttributeMap< bool > _booleanAttributes
Definition XMLTag.hpp:271
double getDoubleAttributeValue(const std::string &name, std::optional< double > default_value=std::nullopt) const
Definition XMLTag.cpp:116
XMLTag & addAttribute(const XMLAttribute< double > &attribute)
Removes the XML subtag with given name.
Definition XMLTag.cpp:53
XMLTag & addSubtag(const XMLTag &tag)
Adds an XML tag as subtag by making a copy of the given tag.
Definition XMLTag.cpp:41
void areAllSubtagsConfigured() const
Definition XMLTag.cpp:314
AttributeMap< Eigen::VectorXd > _eigenVectorXdAttributes
Definition XMLTag.hpp:273
T count(T... args)
T emplace(T... args)
T empty(T... args)
T end(T... args)
T find(T... args)
T insert(T... args)
bool contained(const ELEMENT_T &element, const std::vector< ELEMENT_T > &vec)
Returns true, if given element is in vector, otherwise false.
Definition Helpers.hpp:39
std::vector< StringMatch > computeMatches(std::string_view given, const Container &expected)
Definition String.hpp:95
contains the XML configuration parser.
XMLTag getRootTag()
Returns an XMLTag::Listener that does nothing on callbacks.
Definition XMLTag.cpp:389
void configure(XMLTag &tag, const precice::xml::ConfigurationContext &context, std::string_view configurationFilename)
Configures the given configuration from file configurationFilename.
Definition XMLTag.cpp:395
STL namespace.
static precice::logging::Logger _log("precicec")
T remove_if(T... args)
Tightly coupled to the parameters of Participant()
Definition XMLTag.hpp:24
No operation listener for tests.
Definition XMLTag.hpp:285
Callback interface for configuration classes using XMLTag.
Definition XMLTag.hpp:43
T transform(T... args)