Mantid
Loading...
Searching...
No Matches
MDGeometryXMLParser.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#include <algorithm>
8#include <functional>
9#include <utility>
10
14
15#include <Poco/AutoPtr.h>
16#include <Poco/DOM/DOMParser.h>
17#include <Poco/DOM/Document.h>
18#include <Poco/DOM/Element.h>
19#include <Poco/DOM/NodeList.h>
20
21namespace Mantid::Geometry {
23struct findID {
24 const std::string m_id;
25 explicit findID(std::string id) : m_id(std::move(id)) {}
26
27 bool operator()(const Mantid::Geometry::IMDDimension_sptr &obj) const { return m_id == obj->getDimensionId(); }
28};
29
32 bool operator()(const Mantid::Geometry::IMDDimension_sptr &obj) const { return obj->getIsIntegrated(); }
33};
34
39 if (!m_executed) {
40 throw std::runtime_error("Attempting to get dimension information from "
41 "MDGeometryXMLParser, before calling ::execute()");
42 }
43}
44
49 Poco::XML::DOMParser pParser;
50 Poco::AutoPtr<Poco::XML::Document> pDoc = pParser.parseString(m_xmlToProcess);
51 Poco::XML::Element *pRootElem = pDoc->documentElement();
52 // Apply root node checking if supplied.
53 Poco::XML::Element *geometryXMLElement = nullptr;
54 if (!m_rootNodeName.empty()) {
55 Poco::XML::Element *temp = pRootElem->getChildElement(m_rootNodeName);
56 geometryXMLElement = temp;
57 if (geometryXMLElement == nullptr) {
58 std::string message = "Root node was not found to be the expected value of " + m_rootNodeName;
59 throw std::runtime_error(message);
60 }
61 } else {
62 // The default is to take the root node to be the geometry xml element.
63 geometryXMLElement = pRootElem;
64 }
65
66 Poco::AutoPtr<Poco::XML::NodeList> dimensionsXML =
67 geometryXMLElement->getElementsByTagName(MDGeometryXMLDefinitions::workspaceDimensionElementName());
68 size_t nDimensions = dimensionsXML->length();
69 VecIMDDimension_sptr vecAllDims(nDimensions);
70
72 for (size_t i = 0; i < nDimensions; i++) {
73 auto *dimensionXML = static_cast<Poco::XML::Element *>(dimensionsXML->item(static_cast<unsigned long>(i)));
74 vecAllDims[i] = createDimension(*dimensionXML);
75 }
76 VecIMDDimension_sptr vecNonMappedDims = vecAllDims;
77 Poco::XML::Element *xDimensionElement =
78 geometryXMLElement->getChildElement(MDGeometryXMLDefinitions::workspaceXDimensionElementName());
79 std::string xDimId =
80 xDimensionElement->getChildElement(MDGeometryXMLDefinitions::workspaceRefDimensionElementName())->innerText();
81 if (!xDimId.empty()) {
82 auto xDimensionIt = find_if(vecAllDims.begin(), vecAllDims.end(), findID(xDimId));
83 if (xDimensionIt == vecAllDims.end()) {
84 throw std::invalid_argument("Cannot determine x-dimension mapping.");
85 }
86 m_xDimension = *xDimensionIt;
87 vecNonMappedDims.erase(std::remove_if(vecNonMappedDims.begin(), vecNonMappedDims.end(), findID(xDimId)),
88 vecNonMappedDims.end());
89 }
90
91 Poco::XML::Element *yDimensionElement =
92 geometryXMLElement->getChildElement(MDGeometryXMLDefinitions::workspaceYDimensionElementName());
93 std::string yDimId =
94 yDimensionElement->getChildElement(MDGeometryXMLDefinitions::workspaceRefDimensionElementName())->innerText();
95
96 if (!yDimId.empty()) {
97 auto yDimensionIt = find_if(vecAllDims.begin(), vecAllDims.end(), findID(yDimId));
98 if (yDimensionIt == vecAllDims.end()) {
99 throw std::invalid_argument("Cannot determine y-dimension mapping.");
100 }
101 m_yDimension = *yDimensionIt;
102 vecNonMappedDims.erase(std::remove_if(vecNonMappedDims.begin(), vecNonMappedDims.end(), findID(yDimId)),
103 vecNonMappedDims.end());
104 }
105
106 Poco::XML::Element *zDimensionElement =
107 geometryXMLElement->getChildElement(MDGeometryXMLDefinitions::workspaceZDimensionElementName());
108 std::string zDimId =
109 zDimensionElement->getChildElement(MDGeometryXMLDefinitions::workspaceRefDimensionElementName())->innerText();
110
111 if (!zDimId.empty()) {
112 auto zDimensionIt = find_if(vecAllDims.begin(), vecAllDims.end(), findID(zDimId));
113 if (zDimensionIt == vecAllDims.end()) {
114 throw std::invalid_argument("Cannot determine z-dimension mapping.");
115 }
116 m_zDimension = *zDimensionIt;
117 vecNonMappedDims.erase(std::remove_if(vecNonMappedDims.begin(), vecNonMappedDims.end(), findID(zDimId)),
118 vecNonMappedDims.end());
119 }
120
121 Poco::XML::Element *tDimensionElement =
122 geometryXMLElement->getChildElement(MDGeometryXMLDefinitions::workspaceTDimensionElementName());
123 std::string tDimId =
124 tDimensionElement->getChildElement(MDGeometryXMLDefinitions::workspaceRefDimensionElementName())->innerText();
125 if (!tDimId.empty()) {
126 auto tDimensionIt = find_if(vecAllDims.begin(), vecAllDims.end(), findID(tDimId));
127 if (tDimensionIt == vecAllDims.end()) {
128 throw std::invalid_argument("Cannot determine t-dimension mapping.");
129 }
130 m_tDimension = *tDimensionIt;
131 if (!vecNonMappedDims.empty()) {
132 vecNonMappedDims.erase(std::remove_if(vecNonMappedDims.begin(), vecNonMappedDims.end(), findID(tDimId)),
133 vecNonMappedDims.end());
134 }
135 }
136 m_vecNonMappedDims = vecNonMappedDims; // Copy with strong guarantee.
137 m_vecAllDims = vecAllDims;
138 m_executed = true;
139}
140
146 : m_executed(false), m_xmlToProcess(std::move(xmlToProcess)) {}
147
151MDGeometryXMLParser::MDGeometryXMLParser() : m_executed(false), m_xmlToProcess("") {}
152
158 validate();
159 return m_xDimension;
160}
161
167 validate();
168 return m_yDimension;
169}
170
176 validate();
177 return m_zDimension;
178}
179
185 validate();
186 return m_tDimension;
187}
188
194 validate();
195 return m_vecNonMappedDims;
196}
197
203 validate();
205 temp.erase(std::remove_if(temp.begin(), temp.end(), findIntegrated()), temp.end());
206 return temp;
207}
208
214 validate();
216 temp.erase(std::remove_if(temp.begin(), temp.end(), std::not_fn(findIntegrated())), temp.end());
217 return temp;
218}
219
225 validate();
226 return m_vecAllDims;
227}
228
234 validate();
235 return nullptr != m_xDimension.get();
236}
237
243 validate();
244 return nullptr != m_yDimension.get();
245}
246
252 validate();
253 return nullptr != m_zDimension.get();
254}
255
261 validate();
262 return nullptr != m_tDimension.get();
263}
264
270void MDGeometryXMLParser::SetRootNodeCheck(std::string elementName) { m_rootNodeName = std::move(elementName); }
271
277 if (this != &other) {
278 m_executed = other.m_executed;
279 m_rootNodeName = other.m_rootNodeName;
280 m_vecNonMappedDims = other.m_vecNonMappedDims;
281 m_vecAllDims = other.m_vecAllDims;
282 m_xDimension = other.m_xDimension;
283 m_yDimension = other.m_yDimension;
284 m_zDimension = other.m_zDimension;
285 m_tDimension = other.m_tDimension;
286 }
287 return *this;
288}
289
295 : m_executed(other.m_executed), m_rootNodeName(other.m_rootNodeName), m_vecNonMappedDims(other.m_vecNonMappedDims),
296 m_vecAllDims(other.m_vecAllDims), m_xDimension(other.m_xDimension), m_yDimension(other.m_yDimension),
297 m_zDimension(other.m_zDimension), m_tDimension(other.m_tDimension) {}
298
305 validate();
306 bool bResult = false;
307 if (hasXDimension()) {
308 if (candidate->getDimensionId() == m_xDimension->getDimensionId()) {
309 bResult = true;
310 }
311 }
312 return bResult;
313}
314
321 validate();
322 bool bResult = false;
323 if (hasYDimension()) {
324 if (candidate->getDimensionId() == m_yDimension->getDimensionId()) {
325 bResult = true;
326 }
327 }
328 return bResult;
329}
330
337 validate();
338 bool bResult = false;
339 if (hasZDimension()) {
340 if (candidate->getDimensionId() == m_zDimension->getDimensionId()) {
341 bResult = true;
342 }
343 }
344 return bResult;
345}
346
353 validate();
354 bool bResult = false;
355 if (hasTDimension()) {
356 if (candidate->getDimensionId() == m_tDimension->getDimensionId()) {
357 bResult = true;
358 }
359 }
360 return bResult;
361}
362} // namespace Mantid::Geometry
double obj
the value of the quadratic function
static const std::string workspaceXDimensionElementName()
static const std::string workspaceYDimensionElementName()
static const std::string workspaceTDimensionElementName()
static const std::string workspaceDimensionElementName()
static const std::string workspaceRefDimensionElementName()
static const std::string workspaceZDimensionElementName()
Handles the extraction of dimensions from a xml xml string to determine how mappings have been formed...
Mantid::Geometry::VecIMDDimension_sptr m_vecAllDims
void validate() const
Validate the current object.
Mantid::Geometry::IMDDimension_sptr getZDimension() const
Getter for z dimension.
bool hasYDimension() const
Determine wheter y dimension is present.
MDGeometryXMLParser & operator=(const MDGeometryXMLParser &)
Assignement operator.
bool isZDimension(const Mantid::Geometry::IMDDimension_sptr &) const
Determines whether query dimension is the z dimension.
Mantid::Geometry::IMDDimension_sptr getTDimension() const
Getter for t dimension.
void SetRootNodeCheck(std::string elementName)
Setter for the root element.
Mantid::Geometry::VecIMDDimension_sptr getNonMappedDimensions() const
Getter for all those dimensions which are not mapped.
Mantid::Geometry::IMDDimension_sptr getXDimension() const
Getter for x dimension.
bool hasZDimension() const
Determine wheter z dimension is present.
bool isTDimension(const Mantid::Geometry::IMDDimension_sptr &) const
Determines whether query dimension is the t dimension.
Mantid::Geometry::IMDDimension_sptr m_yDimension
virtual void execute()
Peforms the processing associated with these transformations.
Mantid::Geometry::IMDDimension_sptr m_tDimension
Mantid::Geometry::IMDDimension_sptr m_zDimension
Mantid::Geometry::VecIMDDimension_sptr getNonIntegratedDimensions() const
Getter for all those dimensions which are not integrated.
bool hasTDimension() const
Determine wheter t dimension is present.
bool isXDimension(const Mantid::Geometry::IMDDimension_sptr &) const
Determines whether query dimension is the x dimension.
bool hasXDimension() const
Determine wheter x dimension is present.
Mantid::Geometry::VecIMDDimension_sptr getAllDimensions() const
Getter for all dimensions parsed.
Mantid::Geometry::VecIMDDimension_sptr m_vecNonMappedDims
Mantid::Geometry::IMDDimension_sptr getYDimension() const
Getter for y dimension.
Mantid::Geometry::VecIMDDimension_sptr getIntegratedDimensions() const
Getter for all those dimensions which are integrated.
Mantid::Geometry::IMDDimension_sptr m_xDimension
bool isYDimension(const Mantid::Geometry::IMDDimension_sptr &) const
Determines whether query dimension is the y dimension.
MANTID_GEOMETRY_DLL IMDDimension_sptr createDimension(const std::string &dimensionXMLString)
Creates IMDDimension objects based on input XML.
std::shared_ptr< IMDDimension > IMDDimension_sptr
Shared Pointer for IMDDimension. Frequently used type in framework.
Definition: IMDDimension.h:98
std::vector< IMDDimension_sptr > VecIMDDimension_sptr
Vector of shared pointers to IMDDimensions.
Definition: IMDDimension.h:105
STL namespace.
Helper unary comparison type for finding IMDDimensions by a specified id.
bool operator()(const Mantid::Geometry::IMDDimension_sptr &obj) const
Helper unary comparison type for finding non-integrated dimensions.
bool operator()(const Mantid::Geometry::IMDDimension_sptr &obj) const