11#include "MantidJson/Json.h"
26Logger
g_log(
"PropertyManager");
33const std::string createKey(
const std::string &name) {
34 std::string key = name;
35 std::transform(key.begin(), key.end(), key.begin(), toupper);
47 std::string retVal =
"";
55 if (logName.length() >= ending.length()) {
56 return (0 == logName.compare(logName.length() - ending.length(), ending.length(), ending));
69 : m_properties(), m_orderedProperties(other.m_orderedProperties.size()) {
72 auto p = std::unique_ptr<Property>(other.m_orderedProperties[i]->clone());
88 auto p = std::unique_ptr<Property>(other.m_orderedProperties[i]->clone());
108 PropertyMap::const_iterator it;
109 for (it =
rhs.m_properties.begin(); it !=
rhs.m_properties.end(); ++it) {
111 string rhs_name = it->first;
115 (*lhs_prop) += it->second.get();
119 auto copy = std::unique_ptr<Property>(it->second->clone());
141 PropertyMap::const_iterator it;
142 for (it = this->
m_properties.begin(); it != this->m_properties.end(); ++it) {
144 auto prop = it->second.get();
145 prop->filterByTime(start, stop);
159 std::vector<PropertyManager *> outputs)
const {
160 size_t n = outputs.size();
163 PropertyMap::const_iterator it;
164 for (it = this->
m_properties.begin(); it != this->m_properties.end(); ++it) {
171 std::vector<Property *> output_properties;
172 for (
size_t i = 0; i <
n; i++) {
176 output_properties.emplace_back(
nullptr);
180 bool isProtonCharge = prop->
name() ==
"proton_charge";
181 prop->
splitByTime(splitter, output_properties, isProtonCharge);
196 const std::vector<std::string> &excludedFromFiltering) {
198 if (std::find(excludedFromFiltering.cbegin(), excludedFromFiltering.cend(), orderedProperty->name()) !=
199 excludedFromFiltering.cend()) {
204 Property *currentProp = orderedProperty;
209 std::unique_ptr<Property> filtered(
nullptr);
212 auto logFilter = std::make_unique<LogFilter>(filter);
217 logFilter->addFilter(*tspFilterProp);
219 filtered = std::make_unique<FilteredTimeSeriesProperty<double>>(doubleSeries, *logFilter->filter());
221 filtered = std::make_unique<FilteredTimeSeriesProperty<double>>(doubleSeries, filter);
223 orderedProperty = filtered.get();
239 p->setDocumentation(doc);
241 const std::string key = createKey(p->name());
248 if (existing->second != p) {
260 p->setDocumentation(doc);
262 const std::string key = createKey(p->name());
266 auto oldPropPtr = existing->second.get();
271 *ordereredPropPos = p.get();
282 if (!prop->isDefault())
283 prop->setValue(prop->getDefault());
300 const std::unordered_set<std::string> &ignoreProperties,
bool createMissing) {
301 setProperties(propertiesJson,
this, ignoreProperties, createMissing);
318 const std::unordered_set<std::string> &ignoreProperties,
bool createMissing) {
319 ::Json::Value jsonValue;
321 if (Mantid::JsonHelpers::parse(propertiesJson, &jsonValue)) {
322 setProperties(jsonValue, targetPropertyManager, ignoreProperties, createMissing);
324 throw std::invalid_argument(
"propertiesArray was not valid json");
336 const std::unordered_set<std::string> &ignoreProperties,
bool createMissing) {
337 setProperties(jsonValue,
this, ignoreProperties, createMissing);
351 const std::unordered_set<std::string> &ignoreProperties,
bool createMissing) {
352 if (jsonValue.type() != ::Json::ValueType::objectValue)
356 static const std::string propFilename =
"Filename";
357 const ::Json::Value &filenameValue = jsonValue[propFilename];
358 if (!filenameValue.isNull()) {
359 const std::string
value = filenameValue.asString();
363 const auto memberNames = jsonValue.getMemberNames();
364 for (::Json::ArrayIndex i = 0; i < jsonValue.size(); i++) {
365 const auto &propName = memberNames[i];
366 if ((propFilename == propName) || (ignoreProperties.find(propName) != ignoreProperties.end())) {
369 const ::Json::Value &propValue = jsonValue[propName];
385 const std::unordered_set<std::string> &ignoreProperties) {
386 if (propertiesString.empty()) {
389 auto firstSymbol = propertiesString.find_first_not_of(
" \n\t");
390 if (firstSymbol == std::string::npos) {
393 if (propertiesString[firstSymbol] ==
'{') {
406 const std::unordered_set<std::string> &ignoreProperties) {
407 ::Json::Value propertyJson;
409 if (Mantid::JsonHelpers::parse(propertiesString, &propertyJson)) {
412 throw std::invalid_argument(
"Could not parse JSON string when trying to set a property from: " + propertiesString);
423 const std::unordered_set<std::string> &ignoreProperties) {
424 ::Json::Value propertyJson;
428 boost::char_separator<char> sep(
";");
431 for (
const auto &pair : propPairs) {
432 size_t n = pair.find(
'=');
433 if (
n != std::string::npos) {
435 std::string propName;
439 if (
n < pair.size() - 1) {
440 propName = pair.substr(0,
n);
441 value = pair.substr(
n + 1, pair.size() -
n - 1);
444 propName = pair.substr(0,
n);
448 propertyJson[propName] =
value;
465 auto helpMsg = prop->setValue(
value);
467 if (!helpMsg.empty()) {
468 helpMsg =
"Invalid value for property " + prop->name() +
" (" + prop->type() +
") from string \"" +
value +
470 throw std::invalid_argument(helpMsg);
483 auto helpMsg = prop->setValueFromJson(
value);
485 if (!helpMsg.empty()) {
486 helpMsg =
"Invalid value for property " + prop->name() +
" (" + prop->type() +
") from Json \"" +
487 value.toStyledString() +
"\": " + helpMsg;
488 throw std::invalid_argument(helpMsg);
505 if (!errorMsg.empty()) {
506 errorMsg =
"Invalid value for property " + p->
name() +
" (" + p->
type() +
") \"" +
value +
"\" : " + errorMsg;
507 throw std::invalid_argument(errorMsg);
518 const std::string key = createKey(name);
528 bool allValid =
true;
531 std::string
error =
property.second->isValid();
533 if (!
error.empty()) {
534 g_log.
error() <<
"Property \"" <<
property.first <<
"\" is not set to a valid value: \"" <<
error <<
"\".\n";
568 Json::StreamWriterBuilder builder;
569 builder.settings_[
"indentation"] =
"";
570 const string output = Json::writeString(builder,
asJson(withDefaultValues));
582 ::Json::Value jsonMap;
584 for (
int i = 0; i <
count; ++i) {
586 bool is_enabled =
true;
602 if (other.m_properties.count(key) != 1)
604 if (*other.m_properties.at(key) != *
value)
619 const std::string key = createKey(name);
622 return it->second.get();
633 const std::string key = createKey(name);
636 return it->second.get();
652 throw std::runtime_error(
"Property index too high");
668 std::vector<std::string> names;
670 names.reserve(props.size());
671 std::transform(props.cbegin(), props.cend(), std::back_inserter(names),
672 [](
auto &propPtr) { return propPtr->name(); });
706 const std::string key = createKey(name);
708 std::vector<Property *>::iterator itr;
723 const std::string key = createKey(property->name());
728 }
catch (
const std::out_of_range &) {
const std::vector< double > & rhs
double value
The value of the point.
std::map< DeltaEMode::Type, std::string > index
Exception for when an item is already in a collection.
Exception for when an item is not found in a collection.
Interface to PropertyManager.
virtual void setPropertyValue(const std::string &name, const std::string &value)=0
Sets property value from a string.
virtual void declareOrReplaceProperty(std::unique_ptr< Property > p, const std::string &doc="")=0
Function to declare properties (i.e. store them)
virtual void afterPropertySet(const std::string &)
Override this method to perform a custom action right after a property was set.
virtual void setPropertyValueFromJson(const std::string &name, const Json::Value &value)=0
Sets property value from a Json::Value.
virtual bool isEnabled(const IPropertyManager *algo) const
Is the property to be shown as "enabled" in the GUI.
void error(const std::string &msg)
Logs at error level.
Property manager helper class.
bool operator!=(const PropertyManager &other) const
void setPropertyValue(const std::string &name, const std::string &value) override
Set the value of a property by string N.B.
bool validateProperties() const override
Validates all the properties in the collection.
void removeProperty(const std::string &name, const bool delproperty=true) override
removes the property from properties map
void setPropertyOrdinal(const int &index, const std::string &value) override
Set the value of a property by an index N.B.
void declareOrReplaceProperty(std::unique_ptr< Property > p, const std::string &doc="") override
Add or replace a property in the list of managed properties.
size_t propertyCount() const override
Count the number of properties under management.
PropertyManager & operator+=(const PropertyManager &rhs)
Addition operator.
PropertyManager()
Default constructor.
void resetProperties() override
Reset property values back to initial values (blank or default values)
void filterByProperty(const TimeSeriesProperty< bool > &filter, const std::vector< std::string > &excludedFromFiltering=std::vector< std::string >()) override
Filter the managed properties by the given boolean property mask.
Property * getPointerToProperty(const std::string &name) const override
Get a property by name.
bool existsProperty(const std::string &name) const override
Checks whether the named property is already in the list of managed property.
void setProperties(const std::string &propertiesJson, const std::unordered_set< std::string > &ignoreProperties=std::unordered_set< std::string >(), bool createMissing=false) override
Set the ordered list of properties by one string of values, separated by semicolons.
void setPropertiesWithString(const std::string &propertiesString, const std::unordered_set< std::string > &ignoreProperties=std::unordered_set< std::string >()) override
Sets all the declared properties from a string.
PropertyMap m_properties
The properties under management.
std::string asString(bool withDefaultValues=false) const override
Return the property manager serialized as a string.
void setPropertiesWithJSONString(const std::string &propertiesString, const std::unordered_set< std::string > &ignoreProperties)
Sets all the declared properties from a string.
static bool isAnInvalidValuesFilterLog(const std::string &logName)
void setPropertiesWithSimpleString(const std::string &propertiesString, const std::unordered_set< std::string > &ignoreProperties)
Sets all the declared properties from a string.
static std::string getInvalidValuesFilterLogName(const std::string &logName)
Gets the correct log name for the matching invalid values log for a given log name.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
std::unique_ptr< Property > takeProperty(const size_t index) override
removes the property from the properties map and returns a pointer to it
void filterByTime(const Types::Core::DateAndTime &start, const Types::Core::DateAndTime &stop) override
Filter out a run by time.
static std::string getLogNameFromInvalidValuesFilter(const std::string &logName)
void clear() override
Clears the whole property map.
Property * getPointerToPropertyOrdinal(const int &index) const override
Get a property by an index.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
void splitByTime(std::vector< SplittingInterval > &splitter, std::vector< PropertyManager * > outputs) const override
Split a run by time (splits the TimeSeriesProperties contained).
::Json::Value asJson(bool withDefaultValues=false) const override
Return the property manager serialized as a json object.
Property * getPointerToPropertyOrNull(const std::string &name) const
Get a property by name.
std::vector< Property * > m_orderedProperties
Stores the order in which the properties were declared.
void declareProperty(std::unique_ptr< Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
PropertyManager & operator=(const PropertyManager &)
Assignment operator - performs a deep copy.
bool operator==(const PropertyManager &other) const
void setPropertyValueFromJson(const std::string &name, const Json::Value &value) override
Set the value of a property by Json::Value.
static const std::string INVALID_VALUES_SUFFIX
const std::vector< Property * > & getProperties() const override
Get the list of managed properties.
virtual ~PropertyManager() override
Virtual destructor.
std::vector< std::string > getDeclaredPropertyNames() const noexcept override
Return the list of declared property names.
Base class for properties.
IPropertySettings * getSettings()
virtual std::string setValue(const std::string &)=0
Set the value of the property via a string.
virtual bool isDefault() const =0
Overriden function that returns if property has the same value that it was initialised with,...
const std::string & name() const
Get the property's name.
const std::string type() const
Returns the type of the property as a string.
virtual void splitByTime(std::vector< SplittingInterval > &splitter, std::vector< Property * > outputs, bool isProtonCharge=true) const
Split a property by time.
virtual Json::Value valueAsJson() const =0
Returns the value of the property as a Json::Value.
virtual std::string value() const =0
Returns the value of the property as a string.
virtual bool isValueSerializable() const
Whether the string returned by value() can be used for serialization.
@ TOK_TRIM
remove leading and trailing whitespace from tokens
A specialised Property class for holding a series of time-value pairs.
Logger g_log("DateAndTime")
MANTID_KERNEL_DLL std::unique_ptr< Property > decodeAsProperty(const std::string &name, const Json::Value &value)
Attempt to create a Property of the most appropriate type from a string name and Json value object.
MANTID_KERNEL_DLL::Json::Value encodeAsJson(const OptionalBool &)
Encode an OptionalBool as a Json::Value.
Utility class that enables the getProperty() method to effectively be templated on the return type.