Mantid
Loading...
Searching...
No Matches
EnabledWhenProperty.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 +
8
12
13#include <boost/lexical_cast.hpp>
14
15#include <exception>
16#include <memory>
17#include <set>
18#include <stdexcept>
19
20using namespace Mantid::Kernel;
21
22namespace Mantid::Kernel {
29EnabledWhenProperty::EnabledWhenProperty(const std::string &otherPropName, const ePropertyCriterion when,
30 const std::string &value)
32 m_propertyDetails(std::make_shared<PropertyDetails>(PropertyDetails{otherPropName, when, value})) {}
33
44 const EnabledWhenProperty &conditionTwo, eLogicOperator logicOperator)
45 : // This method allows the Python interface to easily construct these
46 // objects
47 // Copy the object then forward onto our move constructor
48 EnabledWhenProperty(std::make_shared<EnabledWhenProperty>(conditionOne),
49 std::make_shared<EnabledWhenProperty>(conditionTwo), logicOperator) {}
50
61EnabledWhenProperty::EnabledWhenProperty(std::shared_ptr<EnabledWhenProperty> &&conditionOne,
62 std::shared_ptr<EnabledWhenProperty> &&conditionTwo,
63 eLogicOperator logicOperator)
65 m_comparisonDetails(std::make_shared<ComparisonDetails<EnabledWhenProperty>>(
66 ComparisonDetails<EnabledWhenProperty>{std::move(conditionOne), std::move(conditionTwo), logicOperator})) {}
67
69 : IPropertySettings(), m_propertyDetails{other.m_propertyDetails}, m_comparisonDetails{other.m_comparisonDetails} {}
70
80 const auto &comparison = m_comparisonDetails;
81 const auto &objectOne = comparison->conditionOne;
82 const auto &objectTwo = comparison->conditionTwo;
83
84 switch (comparison->logicOperator) {
85 case AND:
86 return objectOne->isEnabled(algo) && objectTwo->isEnabled(algo);
87 break;
88 case OR:
89 return objectOne->isEnabled(algo) || objectTwo->isEnabled(algo);
90 break;
91 case XOR:
92 return objectOne->isEnabled(algo) ^ objectTwo->isEnabled(algo);
93 break;
94 default:
95 throw std::runtime_error("Unknown logic operator in EnabledWhenProperty");
96 }
97}
98
107
108 // Value of the other property
109 const std::string propValue = getPropertyValue(algo);
110 // This is safe as long as getPropertyValue (which checks) has been called
111 // already
112 const auto *prop = algo->getPointerToProperty(m_propertyDetails->otherPropName);
113
114 // OK, we have the property. Check the condition
115 switch (m_propertyDetails->criterion) {
116 case IS_DEFAULT:
117 return prop->isDefault();
118 case IS_NOT_DEFAULT:
119 return !prop->isDefault();
120 case IS_EQUAL_TO:
121 return (propValue == m_propertyDetails->value);
122 case IS_NOT_EQUAL_TO:
123 return (propValue != m_propertyDetails->value);
124 case IS_MORE_OR_EQ: {
125 auto check = boost::lexical_cast<int>(m_propertyDetails->value);
126 auto iPropV = boost::lexical_cast<int>(propValue);
127 return (iPropV >= check);
128 }
129 default:
130 // Unknown criterion
131 std::string errString = "The EnabledWhenProperty criterion set"
132 " for the following property ";
133 errString += m_propertyDetails->otherPropName;
134 errString += " is unknown";
135 throw std::invalid_argument(errString);
136 }
137}
138
148 // Find the property
149 if (algo == nullptr)
150 throw std::runtime_error("Algorithm properties passed to EnabledWhenProperty was null");
151
152 Property const *prop = nullptr;
153 try {
154 prop = algo->getPointerToProperty(m_propertyDetails->otherPropName);
155 } catch (Exception::NotFoundError &) {
156 prop = nullptr; // Ensure we still have null pointer
157 }
158 if (!prop)
159 throw std::runtime_error("Property " + m_propertyDetails->otherPropName + " was not found in EnabledWhenProperty");
160 return prop->value();
161}
162
172 if (m_propertyDetails) {
173 return checkCriterion(algo);
174 } else if (m_comparisonDetails) {
175 return checkComparison(algo);
176 } else {
177 throw std::runtime_error("Both PropertyDetails and ComparisonDetails were "
178 "null in EnabledWhenProperty");
179 }
180}
181
190 // VisibleWhenProperty uses algo so we have to keep it to match interface
191 UNUSED_ARG(algo);
192 return true;
193}
194
196std::vector<std::string> EnabledWhenProperty::dependsOn(const std::string &thisProp) const {
197 if (m_propertyDetails) {
198 const std::string &otherProp = m_propertyDetails->otherPropName;
199 if (otherProp == thisProp)
200 throw std::runtime_error("EnabledWhenProperty: circular dependency detected");
201 return std::vector<std::string>{otherProp};
202 } else if (m_comparisonDetails) {
203 std::set<std::string> ps;
204 const auto ps1 = m_comparisonDetails->conditionOne->dependsOn(thisProp);
205 const auto ps2 = m_comparisonDetails->conditionTwo->dependsOn(thisProp);
206 ps.insert(ps1.cbegin(), ps1.cend());
207 ps.insert(ps2.cbegin(), ps2.cend());
208 return std::vector<std::string>(ps.cbegin(), ps.cend());
209 } else
210 return std::vector<std::string>{};
211}
212
221
222} // namespace Mantid::Kernel
double value
The value of the point.
Definition FitMW.cpp:51
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:48
EnabledWhenProperty()=default
Protected Constructor for derived classes to skip setting up the comparator in the base class as they...
std::string getPropertyValue(const IPropertyManager *algo) const
Checks the algorithm and property are both valid and attempts to get the value associated with the pr...
std::shared_ptr< ComparisonDetails< EnabledWhenProperty > > m_comparisonDetails
Holds an object containing details of multiple comparisons.
bool isEnabled(const IPropertyManager *algo) const override
Return true/false based on whether the other property satisfies the criterion.
virtual bool checkCriterion(const IPropertyManager *algo) const
Checks that the specified property matches the criteria given.
std::vector< std::string > dependsOn(const std::string &thisProp) const override
Other properties that this property depends on.
std::shared_ptr< PropertyDetails > m_propertyDetails
Holds the various details used within the comparison.
virtual bool checkComparison(const IPropertyManager *algo) const
Checks two EnabledWhenProperty objects match the logic operator specified and returns the result of b...
IPropertySettings * clone() const override
Make a copy of the present type of validator.
bool isVisible(const IPropertyManager *algo) const override
Return true always.
Exception for when an item is not found in a collection.
Definition Exception.h:145
Interface to PropertyManager.
virtual Property * getPointerToProperty(const std::string &name) const =0
Get a pointer to property by name.
Interface for modifiers to Property's that specify if they should be enabled or visible in a GUI.
Base class for properties.
Definition Property.h:94
virtual bool isDefault() const =0
Overriden function that returns if property has the same value that it was initialised with,...
virtual std::string value() const =0
Returns the value of the property as a string.
ePropertyCriterion
Enum for use in EnabledWhenProperty.
eLogicOperator
Enum for use when combining two EnabledWhenPropertyItems.
STL namespace.
Struct which holds details for comparison between two EnabledWhenPropertyObjects.
Struct which holds associated property details for comparison.