Skip to content

Commit

Permalink
Refs #9136. Code for copying static attributes and test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbekasov committed Mar 20, 2014
1 parent c352861 commit c18b9f2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
Expand Up @@ -10,7 +10,9 @@
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/ProgressBase.h"
#include "MantidKernel/UnitFactory.h"

#include <fstream>

#include <Poco/DOM/Document.h>
#include <Poco/DOM/DOMParser.h>
#include <Poco/DOM/DOMWriter.h>
Expand All @@ -19,7 +21,9 @@
#include <Poco/DOM/NodeIterator.h>
#include <Poco/DOM/NodeList.h>
#include <Poco/SAX/AttributesImpl.h>

#include <boost/make_shared.hpp>
#include <boost/assign/list_of.hpp>

using namespace Mantid;
using namespace Mantid::Kernel;
Expand Down Expand Up @@ -2348,6 +2352,32 @@ namespace Geometry
nameCountStart = boost::lexical_cast<int>(pElem->getAttribute("name-count-start"));
}

// A list of numeric attributes which are allowed to have corresponding -end
std::set<std::string> rangeAttrs = boost::assign::list_of("x")("y")("z")("r")("t")("p")("rot");

// Numeric attributes related to rotation. Doesn't make sense to have -end for those
std::set<std::string> rotAttrs = boost::assign::list_of("x-axis")("y-axis")("z-axis");

// A set of all numeric attributes for convenience
std::set<std::string> allAttrs;
allAttrs.insert(rangeAttrs.begin(), rangeAttrs.end());
allAttrs.insert(rotAttrs.begin(), rotAttrs.end());

// Attribute values as read from <locations>. If the attribute doesn't have a value here, it
// means that it wasn't set
std::map<std::string, std::string> attrValues;

// Read all the set attribute values
for (auto it = allAttrs.begin(); it != allAttrs.end(); ++it)
{
if ( pElem->hasAttribute(*it) )
{
attrValues[*it] = pElem->getAttribute(*it);
}
}

// TODO: read -end values and calculate steps. If no value for -end - error

std::ostringstream xml;

Poco::XML::XMLWriter writer(xml, Poco::XML::XMLWriter::CANONICAL);
Expand All @@ -2365,6 +2395,14 @@ namespace Geometry
name + boost::lexical_cast<std::string>(nameCountStart + i));
}

// Copy values of all the attributes set
for (auto it = attrValues.begin(); it != attrValues.end(); ++it)
{
attr.addAttribute("", "", it->first, "", it->second);

// TODO: if is a range attribute, increase the value by the step
}

writer.emptyElement("", "", "location", attr);
}

Expand Down
Expand Up @@ -810,8 +810,7 @@ class InstrumentDefinitionParserTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS( errorMsg.substr(0,25), "Detector location element");
}

void initializeLocationsParser(InstrumentDefinitionParser& parser, const std::string& locations,
size_t numDetectors)
Instrument_sptr loadInstrLocations(const std::string& locations, detid_t numDetectors)
{
// TODO: the following could be done only once, not for every initialization
std::string filename = ConfigService::Instance().getInstrumentDirectory()
Expand All @@ -821,27 +820,43 @@ class InstrumentDefinitionParserTest : public CxxTest::TestSuite
boost::replace_first(contents, "%LOCATIONS%", locations);
boost::replace_first(contents, "%NUM_DETECTORS%", boost::lexical_cast<std::string>(numDetectors));

InstrumentDefinitionParser parser;
parser.initialize(filename, "LocationsTestInstrument", contents);

Instrument_sptr instr;

TS_ASSERT_THROWS_NOTHING(instr = parser.parseXML(NULL));
TS_ASSERT_EQUALS(instr->getNumberDetectors(), numDetectors);

return instr;
}

void testLocationsNaming()
{
std::string locations = "<locations n-elements=\"5\" name-count-start=\"10\" name=\"det\" />";
size_t numDetectors = 5;
detid_t numDetectors = 5;

InstrumentDefinitionParser parser;
initializeLocationsParser(parser, locations, numDetectors);

Instrument_sptr instr;
Instrument_sptr instr = loadInstrLocations(locations, numDetectors);

TS_ASSERT_THROWS_NOTHING(instr = parser.parseXML(NULL));

TS_ASSERT_EQUALS(instr->getNumberDetectors(), numDetectors);
TS_ASSERT_EQUALS(instr->getDetector(1)->getName(), "det10");
TS_ASSERT_EQUALS(instr->getDetector(3)->getName(), "det12");
TS_ASSERT_EQUALS(instr->getDetector(5)->getName(), "det14");
}

void testLocationsStaticValues()
{
std::string locations = "<locations n-elements=\"5\" x=\"1.0\" y=\"2.0\" z=\"3.0\" />";
detid_t numDetectors = 5;

Instrument_sptr instr = loadInstrLocations(locations, numDetectors);

for (detid_t i = 1; i <= numDetectors; ++i)
{
TS_ASSERT_DELTA(instr->getDetector(i)->getPos().X(), 1.0, 1.0E-8);
TS_ASSERT_DELTA(instr->getDetector(i)->getPos().Y(), 2.0, 1.0E-8);
TS_ASSERT_DELTA(instr->getDetector(i)->getPos().Z(), 3.0, 1.0E-8);
}
}

/*
<locations n-elements="7" r="0.5" t="0.0" t-end="180.0" rot="0.0" rot-end="180.0" axis-x="0.0" axis-y="1.0" axis-z="0.0"/>
Expand Down

0 comments on commit c18b9f2

Please sign in to comment.