Mantid
Loading...
Searching...
No Matches
CatalogSearch.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#if GCC_VERSION >= 40800 // 4.8.0
8GNU_DIAG_OFF("literal-suffix")
9#endif
11#if GCC_VERSION >= 40800 // 4.8.0
12GNU_DIAG_ON("literal-suffix")
13#endif
14
22
23#include <boost/algorithm/string/regex.hpp>
24#include <limits>
25
26namespace Mantid::ICat {
27DECLARE_ALGORITHM(CatalogSearch)
28
29
30void CatalogSearch::init() {
31 auto isDate = std::make_shared<Kernel::DateValidator>();
32
33 // Properties related to the search fields the user will fill in to improve
34 // search.
35 declareProperty("InvestigationName", "", "The name of the investigation to search for.");
36 declareProperty("Instrument", "", "The name of the instrument used in the investigation.");
37 declareProperty("RunRange", "",
38 "The range of runs to search for related "
39 "investigations. Must be in the format "
40 "0000-0000 or 0000:0000.");
41 declareProperty("StartDate", "", isDate,
42 "The start date for the range of "
43 "investigations to be searched. The "
44 "format must be DD/MM/YYYY.");
45 declareProperty("EndDate", "", isDate,
46 "The end date for the range of "
47 "investigations to be searched. The "
48 "format must be DD/MM/YYYY.");
49 declareProperty("Keywords", "", "A comma separated list of words to search for in the investigation.");
50 declareProperty("InvestigationId", "", "The ID of the investigation.");
51 declareProperty("InvestigatorSurname", "", "The surname of the investigator associated to the investigation.");
52 declareProperty("SampleName", "", "The name of the sample used in the investigation.");
53 declareProperty("DataFileName", "", "The name of the data file in the investigation.");
54 declareProperty("InvestigationType", "", "The type of the investigation.");
55 declareProperty("MyData", false,
56 "If set to true, only search in "
57 "investigations of which you are an "
58 "investigator, e.g. 'My Data'.");
59
60 // These are needed for paging on the interface, and to minimise the amount of
61 // results returned by the query.
62 declareProperty("CountOnly", false, "Boolean option to perform COUNT search only. This is used for paging.");
63 declareProperty<int>("Limit", 100,
64 "The maximum amount of search results to "
65 "return. Adds a LIMIT clause to the "
66 "query. This is used for paging.");
67 declareProperty<int>("Offset", 0, "The location to begin returning results from. This is used for paging.");
68
69 declareProperty("Session", "", "The session information of the catalog search in.");
70
71 declareProperty(
72 std::make_unique<API::WorkspaceProperty<API::ITableWorkspace>>("OutputWorkspace", "", Kernel::Direction::Output),
73 "The name of the workspace that will be created to store the "
74 "search results.");
75 declareProperty<int64_t>("NumberOfSearchResults", 0,
76 "The number of search results returned for the "
77 "INPUT. Performs a COUNT query to determine this. "
78 "This is used for paging.",
80}
81
84 // Obtains the inputs from the search interface.
85 CatalogSearchParam params;
86 // Get the user input search terms to search for.
87 getInputProperties(params);
88 // Create output workspace.
89 auto workspace = API::WorkspaceFactory::Instance().createTable("TableWorkspace");
90 // Obtain all the active catalogs.
91 auto catalogs = API::CatalogManager::Instance().getCatalog(getPropertyValue("Session"));
92 // Search for investigations with user specific search inputs.
93 setProperty("OutputWorkspace", workspace);
94 // Do not perform a full search if we only want a COUNT search.
95 if (getProperty("CountOnly")) {
96 // Set the related property needed for paging.
97 setProperty("NumberOfSearchResults", catalogs->getNumberOfSearchResults(params));
98 return;
99 }
100 // Search for investigations in the archives.
101 catalogs->search(params, workspace, getProperty("Offset"), getProperty("Limit"));
102}
103
109 params.setInvestigationName(getPropertyValue("InvestigationName"));
110 params.setInstrument(getPropertyValue("Instrument"));
111 std::string runRange = getProperty("RunRange");
112 setRunRanges(runRange, params);
113 params.setStartDate(params.getTimevalue(getPropertyValue("StartDate")));
114 params.setEndDate(params.getTimevalue(getPropertyValue("EndDate")));
115 params.setKeywords(getPropertyValue("Keywords"));
116 params.setInvestigationId(getPropertyValue("InvestigationId"));
117 params.setInvestigationName(getPropertyValue("InvestigationName"));
118 params.setInvestigatorSurName(getPropertyValue("InvestigatorSurname"));
119 params.setSampleName(getPropertyValue("SampleName"));
120 params.setDatafileName(getPropertyValue("DataFileName"));
121 params.setInvestigationType(getPropertyValue("InvestigationType"));
122 params.setMyData(boost::lexical_cast<bool>(getPropertyValue("MyData")));
123}
124
131void CatalogSearch::setRunRanges(std::string &runRange, CatalogSearchParam &params) {
132 // A container to hold the range of run numbers.
133 std::vector<std::string> runNumbers;
134 // Split the input text by "-",":" or "," and add contents to runNumbers.
135 boost::algorithm::split_regex(runNumbers, runRange, boost::regex("-|:"));
136
137 double startRange = 0;
138 double endRange = 0;
139
140 // If the user has only input a start range ("4444" or "4444-").
141 if (!runNumbers.at(0).empty()) {
142 startRange = boost::lexical_cast<double>(runNumbers.at(0));
143 // We set the end range to be equal now, so we do not have to do a check if
144 // it exists later.
145 endRange = boost::lexical_cast<double>(runNumbers.at(0));
146 }
147
148 // If the user has input a start and end range, or just an end range
149 // ("4444-4449" or "-4449").
150 if (runNumbers.size() == 2) {
151 // Has the user input an end range...
152 if (!runNumbers.at(1).empty()) {
153 endRange = boost::lexical_cast<double>(runNumbers.at(1));
154
155 // If they have not chosen a start range ("-4449");
156 if (startRange == 0) {
157 startRange = boost::lexical_cast<double>(runNumbers.at(1));
158 }
159 }
160 }
161
162 if (startRange > endRange) {
163 throw std::runtime_error("Run end number cannot be lower than run start number.");
164 }
165
166 params.setRunStart(startRange);
167 params.setRunEnd(endRange);
168}
169} // namespace Mantid::ICat
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
#define GNU_DIAG_ON(x)
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
A property class for workspaces.
This class is used in Catalog Search service to set/get all the inputs to search for.
void setKeywords(const std::string &keywords)
Set the keywords to search for.
void setSampleName(const std::string &sampleName)
Set the sample name to search for.
void setInvestigationId(const std::string &)
Set the investigation id to search for.
time_t getTimevalue(const std::string &inputDate)
Saves the start/end date times to time_t value.
void setRunEnd(const double &endRun)
Set the end run to search for.
void setDatafileName(const std::string &datafileName)
Set the datafile name to search for.
void setInvestigatorSurName(const std::string &investigatorName)
Set the investigators name to search for.
void setInvestigationName(const std::string &instName)
Set the investigation name to search for.
void setInstrument(const std::string &instrName)
Set the instrument to search for.
void setRunStart(const double &startRun)
Set the start run to search for.
void setStartDate(const time_t &startDate)
Set the start date to search for.
void setEndDate(const time_t &endDate)
Set the end date to search for.
void setInvestigationType(const std::string &invstType)
Set the investigation type to search for.
void setMyData(bool flag)
Set the "my data only" flag to search only user's data if true.
This class is responsible for searching the catalog using the properties specified.
Definition: CatalogSearch.h:42
void getInputProperties(CatalogSearchParam &params)
Get all inputs for the algorithm.
void exec() override
Overwrites Algorithm exec method.
void setRunRanges(std::string &runRange, CatalogSearchParam &params)
Parse the run-range input field, split it into start and end run, and set related parameters.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
@ Output
An output workspace.
Definition: Property.h:54