Mantid
Loading...
Searching...
No Matches
MDGeometryXMLBuilder.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 <sstream>
8
11
12#include <Poco/DOM/AutoPtr.h>
13#include <Poco/DOM/DOMParser.h>
14#include <Poco/DOM/DOMWriter.h>
15#include <Poco/DOM/Document.h>
16#include <Poco/DOM/Element.h>
17#include <Poco/DOM/Text.h>
18#include <boost/algorithm/string.hpp>
19#include <boost/format.hpp>
20#include <boost/functional/hash.hpp>
21
22namespace Mantid::Geometry {
23
29template <typename CheckDimensionPolicy>
31 bool bAdded = false; // Addition fails by default.
32 if (dimensionToAdd) {
33 const std::string dimensionId = dimensionToAdd->getDimensionId();
34 auto location =
35 std::find_if(m_vecDimensions.cbegin(), m_vecDimensions.cend(),
36 [&dimensionId](const IMDDimension_const_sptr &b) { return dimensionId == b->getDimensionId(); });
37 if (location == m_vecDimensions.cend()) {
38 m_vecDimensions.emplace_back(std::move(dimensionToAdd));
39 bAdded = true;
40 m_changed = true;
41 }
42 }
43 return bAdded;
44}
45
50template <typename CheckDimensionPolicy>
52 for (auto &manyDim : manyDims) {
53 addOrdinaryDimension(manyDim);
54 }
55}
56
57template <typename CheckDimensionPolicy>
60 : m_vecDimensions(other.m_vecDimensions), m_spXDimension(other.m_spXDimension),
61 m_spYDimension(other.m_spYDimension), m_spZDimension(other.m_spZDimension), m_spTDimension(other.m_spTDimension),
62 m_changed(other.m_changed), m_lastResult(other.m_lastResult) {}
63
64template <typename CheckDimensionPolicy>
67 if (this != &other) {
68 m_vecDimensions = other.m_vecDimensions;
69 m_spXDimension = other.m_spXDimension;
70 m_spYDimension = other.m_spYDimension;
71 m_spZDimension = other.m_spZDimension;
72 m_spTDimension = other.m_spTDimension;
73 m_changed = other.m_changed;
74 m_lastResult = other.m_lastResult;
75 }
76 return *this;
77}
78
84template <typename CheckDimensionPolicy>
86 CheckDimensionPolicy policy;
87 policy(dimensionToAdd);
88}
89
95template <typename CheckDimensionPolicy>
97
98 bool bAdded = false;
99 if (dimension) {
100 applyPolicyChecking(*dimension);
101 addOrdinaryDimension(dimension);
102 m_spXDimension = dimension;
103 m_changed = true;
104 bAdded = true;
105 }
106 return bAdded;
107}
108
114template <typename CheckDimensionPolicy>
116
117 bool bAdded = false;
118 if (dimension) {
119 applyPolicyChecking(*dimension);
120 addOrdinaryDimension(dimension);
121 m_spYDimension = dimension;
122 m_changed = true;
123 bAdded = true;
124 }
125 return bAdded;
126}
127
133template <typename CheckDimensionPolicy>
135 bool bAdded = false;
136 if (dimension) {
137 applyPolicyChecking(*dimension);
138 addOrdinaryDimension(dimension);
139 m_spZDimension = dimension;
140 m_changed = true;
141 bAdded = true;
142 }
143 return bAdded;
144}
145
151template <typename CheckDimensionPolicy>
153
154 bool bAdded = false;
155 if (dimension) {
156 applyPolicyChecking(*dimension);
157 addOrdinaryDimension(dimension);
158 m_spTDimension = dimension;
159 m_changed = true;
160 bAdded = true;
161 }
162 return bAdded;
163}
164
169template <typename CheckDimensionPolicy> const std::string &MDGeometryBuilderXML<CheckDimensionPolicy>::create() const {
170 using namespace Poco::XML;
171 if (m_changed) {
172 // Create the root element for this fragment.
173 AutoPtr<Document> pDoc = new Document;
174 AutoPtr<Element> dimensionSetElement = pDoc->createElement("DimensionSet");
175 pDoc->appendChild(dimensionSetElement);
176
177 // Loop through dimensions and generate xml for each.
178 std::string dimensionXMLString;
179
180 for (auto it = m_vecDimensions.begin(); it != m_vecDimensions.end(); ++it) {
181 dimensionXMLString += (*it)->toXMLString();
182 }
183
184 // Pass dimensions to dimension set.
185 AutoPtr<Text> percents = pDoc->createTextNode("%s");
186 dimensionSetElement->appendChild(percents);
187
188 // x-dimension mapping.
189 AutoPtr<Element> xDimensionElement = pDoc->createElement("XDimension");
190 AutoPtr<Element> xDimensionIdElement = pDoc->createElement("RefDimensionId");
191 if (hasXDimension()) {
192 std::string xDimensionId = this->m_spXDimension->getDimensionId();
193 AutoPtr<Text> idXText = pDoc->createTextNode(xDimensionId);
194 xDimensionIdElement->appendChild(idXText);
195 }
196 xDimensionElement->appendChild(xDimensionIdElement);
197 dimensionSetElement->appendChild(xDimensionElement);
198
199 // y-dimension mapping.
200 AutoPtr<Element> yDimensionElement = pDoc->createElement("YDimension");
201 AutoPtr<Element> yDimensionIdElement = pDoc->createElement("RefDimensionId");
202 if (hasYDimension()) {
203 std::string yDimensionId = this->m_spYDimension->getDimensionId();
204 AutoPtr<Text> idYText = pDoc->createTextNode(yDimensionId);
205 yDimensionIdElement->appendChild(idYText);
206 }
207 yDimensionElement->appendChild(yDimensionIdElement);
208 dimensionSetElement->appendChild(yDimensionElement);
209
210 // z-dimension mapping.
211 AutoPtr<Element> zDimensionElement = pDoc->createElement("ZDimension");
212 AutoPtr<Element> zDimensionIdElement = pDoc->createElement("RefDimensionId");
213 if (hasZDimension()) {
214 std::string zDimensionId = this->m_spZDimension->getDimensionId();
215 AutoPtr<Text> idZText = pDoc->createTextNode(zDimensionId);
216 zDimensionIdElement->appendChild(idZText);
217 }
218 zDimensionElement->appendChild(zDimensionIdElement);
219 dimensionSetElement->appendChild(zDimensionElement);
220
221 // t-dimension mapping.
222 AutoPtr<Element> tDimensionElement = pDoc->createElement("TDimension");
223 AutoPtr<Element> tDimensionIdElement = pDoc->createElement("RefDimensionId");
224 if (hasTDimension()) {
225 std::string tDimensionId = this->m_spTDimension->getDimensionId();
226 AutoPtr<Text> idTText = pDoc->createTextNode(tDimensionId);
227 tDimensionIdElement->appendChild(idTText);
228 }
229 tDimensionElement->appendChild(tDimensionIdElement);
230 dimensionSetElement->appendChild(tDimensionElement);
231
232 std::stringstream xmlstream;
233 DOMWriter writer;
234 writer.writeNode(xmlstream, pDoc);
235
236 m_lastResult = boost::str(boost::format(xmlstream.str().c_str()) % dimensionXMLString.c_str());
237 m_changed = false;
238 }
239 return m_lastResult;
240}
241
242template <typename CheckDimensionPolicy> bool MDGeometryBuilderXML<CheckDimensionPolicy>::hasXDimension() const {
243 return nullptr != this->m_spXDimension.get();
244}
245
246template <typename CheckDimensionPolicy> bool MDGeometryBuilderXML<CheckDimensionPolicy>::hasYDimension() const {
247 return nullptr != this->m_spYDimension.get();
248}
249
250template <typename CheckDimensionPolicy> bool MDGeometryBuilderXML<CheckDimensionPolicy>::hasZDimension() const {
251 return nullptr != this->m_spZDimension.get();
252}
253
254template <typename CheckDimensionPolicy> bool MDGeometryBuilderXML<CheckDimensionPolicy>::hasTDimension() const {
255 return nullptr != this->m_spTDimension.get();
256}
257
258template <typename CheckDimensionPolicy>
260 return hasTDimension() && this->m_spTDimension->getIsIntegrated();
261}
262
266template <typename CheckDimensionPolicy>
268
272template <typename CheckDimensionPolicy> MDGeometryBuilderXML<CheckDimensionPolicy>::~MDGeometryBuilderXML() = default;
273
274// Create a builder that blocks the creation when a integrated dimension is used
275// in x, y, z, t mappings.
277// Create a builder that applies no blocking/checking.
279} // namespace Mantid::Geometry
The class describes one dimension of multidimensional dataset representing an orthogonal dimension an...
Definition: IMDDimension.h:39
bool addXDimension(const IMDDimension_const_sptr &dimension) const
Add x dimension.
bool hasZDimension() const
Determine whether a valid z dimension has been provided.
bool addZDimension(const IMDDimension_const_sptr &dimension) const
Add z dimension.
bool hasTDimension() const
Determine whether a valid t dimension has been provided.
bool addYDimension(const IMDDimension_const_sptr &dimension) const
Add y dimension.
void addManyOrdinaryDimensions(const VecIMDDimension_sptr &manyDims) const
Add many ordinary dimensions.
bool hasIntegratedTDimension() const
Determine whether an integrated t dimension has been provided.
bool addTDimension(const IMDDimension_const_sptr &dimension) const
Add t dimension.
void applyPolicyChecking(const IMDDimension &dimensionToAdd) const
Instantiate and apply the checking policy.
bool hasXDimension() const
Determine if a valid x dimension has been provided.
const std::string & create() const
Create the xml.
MDGeometryBuilderXML & operator=(const MDGeometryBuilderXML &)
Assignment Operator.
bool addOrdinaryDimension(IMDDimension_const_sptr dimensionToAdd) const
Add a dimension that is neither considered x, y, z or t.
bool hasYDimension() const
Determine whether a valid y dimension has been provided.
std::vector< IMDDimension_sptr > VecIMDDimension_sptr
Vector of shared pointers to IMDDimensions.
Definition: IMDDimension.h:105
std::shared_ptr< const IMDDimension > IMDDimension_const_sptr
Shared Pointer to const IMDDimension.
Definition: IMDDimension.h:101