11#include "MantidJson/Json.h"
27Logger
g_log(
"PropertyManager");
34const std::string createKey(
const std::string &
name) {
35 std::string key =
name;
36 std::transform(key.begin(), key.end(), key.begin(), toupper);
47 std::string retVal =
"";
57 if (logName.length() >= ending.length()) {
58 return (0 == logName.compare(logName.length() - ending.length(), ending.length(), ending));
71 : m_properties(), m_orderedProperties(other.m_orderedProperties.size()) {
74 auto p = std::unique_ptr<Property>(other.m_orderedProperties[i]->clone());
90 auto p = std::unique_ptr<Property>(other.m_orderedProperties[i]->clone());
110 PropertyMap::const_iterator it;
111 for (it =
rhs.m_properties.begin(); it !=
rhs.m_properties.end(); ++it) {
113 string rhs_name = it->first;
117 (*lhs_prop) += it->second.get();
121 auto copy = std::unique_ptr<Property>(it->second->clone());
139 const std::vector<std::string> &excludedFromFiltering) {
140 auto filter = logFilter->
filter();
142 auto const propName = orderedProperty->
name();
143 if (std::find(excludedFromFiltering.cbegin(), excludedFromFiltering.cend(), propName) !=
144 excludedFromFiltering.cend()) {
153 std::unique_ptr<Property> filtered(
nullptr);
163 filtered = std::make_unique<FilteredTimeSeriesProperty<double>>(doubleSeries, *logFilter->
filter());
164 }
else if (filter->size() > 0) {
166 filtered = std::make_unique<FilteredTimeSeriesProperty<double>>(doubleSeries, *filter);
170 orderedProperty = filtered.get();
171 this->
m_properties[createKey(propName)] = std::move(filtered);
190 std::unique_ptr<Property> newProp;
192 newProp = std::unique_ptr<Property>(tsp->cloneInTimeROI(timeROI));
194 newProp = std::unique_ptr<Property>(prop->clone());
196 newMgr->
m_properties[createKey(newProp->name())] = std::move(newProp);
222 p->setDocumentation(doc);
224 const std::string key = createKey(p->name());
231 if (existing->second != p) {
243 p->setDocumentation(doc);
245 const std::string key = createKey(p->name());
249 auto oldPropPtr = existing->second.get();
254 *ordereredPropPos = p.get();
265 if (!prop->isDefault())
266 prop->setValue(prop->getDefault());
283 const std::unordered_set<std::string> &ignoreProperties,
bool createMissing) {
284 setProperties(propertiesJson,
this, ignoreProperties, createMissing);
301 const std::unordered_set<std::string> &ignoreProperties,
bool createMissing) {
302 ::Json::Value jsonValue;
304 if (Mantid::JsonHelpers::parse(propertiesJson, &jsonValue)) {
305 setProperties(jsonValue, targetPropertyManager, ignoreProperties, createMissing);
307 throw std::invalid_argument(
"propertiesArray was not valid json");
319 const std::unordered_set<std::string> &ignoreProperties,
bool createMissing) {
320 setProperties(jsonValue,
this, ignoreProperties, createMissing);
334 const std::unordered_set<std::string> &ignoreProperties,
bool createMissing) {
335 if (jsonValue.type() != ::Json::ValueType::objectValue)
339 static const std::string propFilename =
"Filename";
340 const ::Json::Value &filenameValue = jsonValue[propFilename];
341 if (!filenameValue.isNull()) {
342 const std::string
value = filenameValue.asString();
346 const auto memberNames = jsonValue.getMemberNames();
347 for (::Json::ArrayIndex i = 0; i < jsonValue.size(); i++) {
348 const auto &propName = memberNames[i];
349 if ((propFilename == propName) || (ignoreProperties.find(propName) != ignoreProperties.end())) {
352 const ::Json::Value &propValue = jsonValue[propName];
368 const std::unordered_set<std::string> &ignoreProperties) {
369 if (propertiesString.empty()) {
372 auto firstSymbol = propertiesString.find_first_not_of(
" \n\t");
373 if (firstSymbol == std::string::npos) {
376 if (propertiesString[firstSymbol] ==
'{') {
389 const std::unordered_set<std::string> &ignoreProperties) {
390 ::Json::Value propertyJson;
392 if (Mantid::JsonHelpers::parse(propertiesString, &propertyJson)) {
395 throw std::invalid_argument(
"Could not parse JSON string when trying to set a property from: " + propertiesString);
406 const std::unordered_set<std::string> &ignoreProperties) {
407 ::Json::Value propertyJson;
411 boost::char_separator<char> sep(
";");
414 for (
const auto &pair : propPairs) {
415 size_t n = pair.find(
'=');
416 if (
n != std::string::npos) {
418 std::string propName;
422 if (
n < pair.size() - 1) {
423 propName = pair.substr(0,
n);
424 value = pair.substr(
n + 1, pair.size() -
n - 1);
427 propName = pair.substr(0,
n);
431 propertyJson[propName] =
value;
448 auto helpMsg = prop->setValue(
value);
450 if (!helpMsg.empty()) {
451 helpMsg =
"Invalid value for property " + prop->name() +
" (" + prop->type() +
") from string \"" +
value +
453 throw std::invalid_argument(helpMsg);
466 auto helpMsg = prop->setValueFromJson(
value);
468 if (!helpMsg.empty()) {
469 helpMsg =
"Invalid value for property " + prop->name() +
" (" + prop->type() +
") from Json \"" +
470 value.toStyledString() +
"\": " + helpMsg;
471 throw std::invalid_argument(helpMsg);
488 if (!errorMsg.empty()) {
489 errorMsg =
"Invalid value for property " + p->
name() +
" (" + p->
type() +
") \"" +
value +
"\" : " + errorMsg;
490 throw std::invalid_argument(errorMsg);
501 const std::string key = createKey(
name);
511 bool allValid =
true;
514 std::string
error =
property.second->isValid();
516 if (!
error.empty()) {
517 g_log.
error() <<
"Property \"" <<
property.first <<
"\" is not set to a valid value: \"" <<
error <<
"\".\n";
551 Json::StreamWriterBuilder builder;
552 builder.settings_[
"indentation"] =
"";
553 const string output = Json::writeString(builder,
asJson(withDefaultValues));
565 ::Json::Value jsonMap;
567 for (
int i = 0; i <
count; ++i) {
569 bool is_enabled =
true;
585 if (other.m_properties.count(key) != 1)
587 if (*other.m_properties.at(key) != *
value)
602 const std::string key = createKey(
name);
605 return it->second.get();
616 const std::string key = createKey(
name);
619 return it->second.get();
635 throw std::runtime_error(
"Property index too high");
651 std::vector<std::string> names;
653 names.reserve(props.size());
654 std::transform(props.cbegin(), props.cend(), std::back_inserter(names),
655 [](
auto &propPtr) { return propPtr->name(); });
689 const std::string key = createKey(
name);
691 std::vector<Property *>::iterator itr;
706 const std::string key = createKey(property->name());
711 }
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.
A non-templated interface to a TimeSeriesProperty.
This class is for filtering TimeSeriesProperty data.
const TimeSeriesProperty< bool > * filter() const
Returns a reference to the filter.
void addFilter(const TimeSeriesProperty< bool > &filter)
Adds a filter using boolean AND.
void error(const std::string &msg)
Logs at error level.
Property manager helper class.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
void clear() override final
Clears the whole property map.
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)
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.
void removeDataOutsideTimeROI(const Kernel::TimeROI &timeROI)
For time series properties, remove time values outside of TimeROI regions, each defined as [roi_begin...
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)
Determine if the log's name has a substring indicating it should not be filtered.
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
static std::string getLogNameFromInvalidValuesFilter(const std::string &logName)
void filterByProperty(Mantid::Kernel::LogFilter *logFilter, const std::vector< std::string > &excludedFromFiltering=std::vector< std::string >()) override
Filter the managed properties by the given boolean property mask.
Property * getPointerToPropertyOrdinal(const int &index) const override
Get a property by an index.
::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.
PropertyManager * cloneInTimeROI(const Kernel::TimeROI &timeROI)
Create a partial copy of this object such that every time series property is cloned according to the ...
virtual ~PropertyManager() override
Virtual destructor.
std::vector< std::string > getDeclaredPropertyNames() const noexcept override
Return the list of declared property names.
Base class for properties.
const IPropertySettings * getSettings() const
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 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
TimeROI : Object that holds information about when the time measurement was active.
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.