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 <stdexcept>
18
19using namespace Mantid::Kernel;
20
21namespace Mantid::Kernel {
28EnabledWhenProperty::EnabledWhenProperty(const std::string &otherPropName, const ePropertyCriterion when,
29 const std::string &value)
31 m_propertyDetails(std::make_shared<PropertyDetails>(PropertyDetails{otherPropName, when, value})) {}
32
43 const EnabledWhenProperty &conditionTwo, eLogicOperator logicOperator)
44 : // This method allows the Python interface to easily construct these
45 // objects
46 // Copy the object then forward onto our move constructor
47 EnabledWhenProperty(std::make_shared<EnabledWhenProperty>(conditionOne),
48 std::make_shared<EnabledWhenProperty>(conditionTwo), logicOperator) {}
49
60EnabledWhenProperty::EnabledWhenProperty(std::shared_ptr<EnabledWhenProperty> &&conditionOne,
61 std::shared_ptr<EnabledWhenProperty> &&conditionTwo,
62 eLogicOperator logicOperator)
64 m_comparisonDetails(std::make_shared<ComparisonDetails<EnabledWhenProperty>>(
65 ComparisonDetails<EnabledWhenProperty>{std::move(conditionOne), std::move(conditionTwo), logicOperator})) {}
66
68 : IPropertySettings(), m_propertyDetails{other.m_propertyDetails}, m_comparisonDetails{other.m_comparisonDetails} {}
69
79 const auto &comparison = m_comparisonDetails;
80 const auto &objectOne = comparison->conditionOne;
81 const auto &objectTwo = comparison->conditionTwo;
82
83 switch (comparison->logicOperator) {
84 case AND:
85 return objectOne->isEnabled(algo) && objectTwo->isEnabled(algo);
86 break;
87 case OR:
88 return objectOne->isEnabled(algo) || objectTwo->isEnabled(algo);
89 break;
90 case XOR:
91 return objectOne->isEnabled(algo) ^ objectTwo->isEnabled(algo);
92 break;
93 default:
94 throw std::runtime_error("Unknown logic operator in EnabledWhenProperty");
95 }
96}
97
106
107 // Value of the other property
108 const std::string propValue = getPropertyValue(algo);
109 // This is safe as long as getPropertyValue (which checks) has been called
110 // already
111 auto prop = algo->getPointerToProperty(m_propertyDetails->otherPropName);
112
113 // OK, we have the property. Check the condition
114 switch (m_propertyDetails->criterion) {
115 case IS_DEFAULT:
116 return prop->isDefault();
117 case IS_NOT_DEFAULT:
118 return !prop->isDefault();
119 case IS_EQUAL_TO:
120 return (propValue == m_propertyDetails->value);
121 case IS_NOT_EQUAL_TO:
122 return (propValue != m_propertyDetails->value);
123 case IS_MORE_OR_EQ: {
124 auto check = boost::lexical_cast<int>(m_propertyDetails->value);
125 auto iPropV = boost::lexical_cast<int>(propValue);
126 return (iPropV >= check);
127 }
128 default:
129 // Unknown criterion
130 std::string errString = "The EnabledWhenProperty criterion set"
131 " for the following property ";
132 errString += m_propertyDetails->otherPropName;
133 errString += " is unknown";
134 throw std::invalid_argument(errString);
135 }
136}
137
147 // Find the property
148 if (algo == nullptr)
149 throw std::runtime_error("Algorithm properties passed to EnabledWhenProperty was null");
150
151 Property *prop = nullptr;
152 try {
153 prop = algo->getPointerToProperty(m_propertyDetails->otherPropName);
154 } catch (Exception::NotFoundError &) {
155 prop = nullptr; // Ensure we still have null pointer
156 }
157 if (!prop)
158 throw std::runtime_error("Property " + m_propertyDetails->otherPropName + " was not found in EnabledWhenProperty");
159 return prop->value();
160}
161
171 if (m_propertyDetails) {
172 return checkCriterion(algo);
173 } else if (m_comparisonDetails) {
174 return checkComparison(algo);
175 } else {
176 throw std::runtime_error("Both PropertyDetails and ComparisonDetails were "
177 "null in EnabledWhenProperty");
178 }
179}
180
189 // VisisbleWhenProperty uses algo so we have to keep it to match interface
190 UNUSED_ARG(algo);
191 return true;
192}
193
196
205
206} // 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:64
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::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.
void modify_allowed_values(Property *const)
Stub function to satisfy the interface.
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 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.