Mantid
Loading...
Searching...
No Matches
SampleEnvironmentFactory.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 +
11
12#include <filesystem>
13#include <fstream>
14#include <utility>
15
16namespace Mantid::DataHandling {
17
18//------------------------------------------------------------------------------
19// Anonyomous
20//------------------------------------------------------------------------------
21namespace {
22// static logger object
23Mantid::Kernel::Logger g_log("SampleEnvironment");
24
25// Typedef for cache
26using SampleEnvironmentSpecCache = std::unordered_map<std::string, SampleEnvironmentSpec_uptr>;
27
33SampleEnvironmentSpecCache &retrieveSpecCache() {
34 static SampleEnvironmentSpecCache cache;
35 return cache;
36}
37
45std::string createCacheKey(const std::string &facility, const std::string &instrument, const std::string &specName) {
46 return facility + "/" + instrument + "/" + specName;
47}
48} // namespace
49
50//------------------------------------------------------------------------------
51// SampleEnvironmentFactory
52//------------------------------------------------------------------------------
60
71 const std::string &instrument,
72 const std::string &specName,
73 const std::string &canName) {
74 assert(m_finder);
75 auto &specCache = retrieveSpecCache();
76 auto cacheKey = createCacheKey(facility, instrument, specName);
77
78 SampleEnvironmentSpec *spec(nullptr);
79 auto iter = specCache.find(cacheKey);
80 if (iter != specCache.end()) {
81 spec = iter->second.get();
82 } else {
83 auto specUPtr = m_finder->find(facility, instrument, specName);
84 spec = specUPtr.get();
85 specCache.emplace(cacheKey, std::move(specUPtr));
86 }
87 return spec->buildEnvironment(canName);
88}
89
93size_t SampleEnvironmentFactory::cacheSize() const { return retrieveSpecCache().size(); }
94
98void SampleEnvironmentFactory::clearCache() { retrieveSpecCache().clear(); }
99
107 const std::string &filepath) const {
108 assert(m_finder);
109 std::filesystem::path fullpath = std::filesystem::path(filepath) / (filename + ".xml");
110 return m_finder->parseSpec(filename, fullpath.string());
111}
112
113//------------------------------------------------------------------------------
114// SampleEnvironmentSpecFileFinder
115//------------------------------------------------------------------------------
116
126 : m_rootDirs(std::move(directories)) {
127 if (m_rootDirs.empty()) {
128 throw std::invalid_argument("SampleEnvironmentSpecFileFinder() - Empty directory search list.");
129 }
130}
131
140 const std::string &instrument,
141 const std::string &name) const {
142 using std::filesystem::path;
143
144 path relpath_instr = path(facility) / instrument / (name + m_fileext);
145
146 path relpath_facil = path(facility) / (name + m_fileext);
147
148 // check for the instrument environment, then facility environment
149 for (const auto &rel_path : {relpath_instr, relpath_facil}) {
150 for (const auto &prefixStr : m_rootDirs) {
151 path prefix(prefixStr);
152 path fullpath = prefix / rel_path;
153 if (std::filesystem::exists(fullpath)) {
154 g_log.debug() << "Found environment at \"" << fullpath << "\"\n";
155 return parseSpec(name, fullpath.string());
156 } else {
157 g_log.debug() << "Failed to find environment at \"" << fullpath << "\"\n";
158 }
159 }
160 }
161
162 // no match if we get here
163 std::ostringstream msg;
164 msg << "Unable to find sample environment file '" << name << "' for facility '" << facility << "' and instrument '"
165 << instrument << "'";
166 throw std::runtime_error(msg.str());
167}
168
176 const std::string &filename) const {
177 std::ifstream reader(filename, std::ios_base::in);
178 if (!reader) {
179 throw std::runtime_error("SampleEnvironmentSpecFileFinder() - Error accessing file '" + filename + "'");
180 }
182 return parser.parse(name, filename, reader);
183}
184
185} // namespace Mantid::DataHandling
std::string name
Definition Run.cpp:60
SampleEnvironmentSpec_uptr parseSpec(const std::string &filename, const std::string &filepath) const
Calls SampleEnvironmentSpecFileFinder::parseSpec.
void clearCache()
Clear the cache of SampleEnvironmentSpec objects.
Geometry::SampleEnvironment_uptr create(const std::string &facility, const std::string &instrument, const std::string &specName, const std::string &canName)
Create a new SampleEnvironment instance from the given specification and can.
SampleEnvironmentSpecFileFinder(std::vector< std::string > directories)
Constructor accepting a list of directories to search.
SampleEnvironmentSpec_uptr parseSpec(const std::string &name, const std::string &filename) const override
Parses the specification from the given file.
SampleEnvironmentSpec_uptr find(const std::string &facility, const std::string &instrument, const std::string &name) const override
Find a named specification in a file.
Read an XML definition of a SampleEnvironmentSpec and produce a new SampleEnvironmentSpec object.
SampleEnvironmentSpec_uptr parse(const std::string &name, const std::string &filename, std::istream &istr)
Takes a stream that is assumed to contain a single complete SampleEnvironmentSpec definition,...
Defines the properties of a named SampleEnvironment setup.
Geometry::SampleEnvironment_uptr buildEnvironment(const std::string &canID) const
Build a new SampleEnvironment instance from a given can ID.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition Logger.h:51
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::unique_ptr< SampleEnvironmentSpec > SampleEnvironmentSpec_uptr
unique_ptr to a SampleEnvironmentSpec
std::unique_ptr< ISampleEnvironmentSpecFinder > ISampleEnvironmentSpecFinder_uptr
std::unique_ptr< SampleEnvironment > SampleEnvironment_uptr
STL namespace.