7 #ifndef stateRuleEngine_indiCompRules_hpp
8 #define stateRuleEngine_indiCompRules_hpp
11 #include <mx/mxException.hpp>
13 #include "../../libMagAOX/libMagAOX.hpp"
42 else if(cstr ==
"Neq")
54 else if(cstr ==
"LtEq")
58 else if(cstr ==
"GtEq")
62 else if(cstr ==
"And")
66 else if(cstr ==
"Nand")
74 else if(cstr ==
"Nor")
78 else if(cstr ==
"Xor")
82 else if(cstr ==
"Xnor")
88 mxThrowException(mx::err::invalidarg,
"string2comp", cstr +
" is not a valid comparison");
111 else if(pstr ==
"info")
115 else if(pstr ==
"caution")
119 else if(pstr ==
"warning")
123 else if(pstr ==
"alert")
129 mxThrowException(mx::err::invalidarg,
"string2priority", pstr +
" is not a valid priority");
147 return (rv.index() > 0);
233 const std::string & str2
241 if(str1 == str2) rv =
true;
244 if(str1 != str2) rv =
true;
247 rv =
"operator not valid for string comparison";
261 const pcf::IndiElement::SwitchStateType & sw2
269 if(sw1 == sw2) rv =
true;
272 if(sw1 != sw2) rv =
true;
275 rv =
"operator not valid for switch comparison";
299 if( fabs(num1 - num2) <= tol ) rv =
true;
302 if( fabs(num1 - num2) > tol ) rv =
true;
305 if( num1 < num2) rv =
true;
308 if( num1 > num2) rv =
true;
311 if( fabs(num1 - num2) <= tol ) rv =
true;
312 else if (num1 < num2) rv =
true;
315 if( fabs(num1 - num2) <= tol ) rv =
true;
316 else if (num1 > num2) rv =
true;
319 rv =
"operator not valid for compNum";
340 if(b1 == b2) rv =
true;
343 if(b1 != b2) rv =
true;
346 if(b1 && b2) rv =
true;
349 if(!(b1 && b2)) rv =
true;
352 if(b1 || b2) rv =
true;
355 if(!b1 && !b2) rv =
true;
358 rv =
"operator not valid for ruleCompRule";
395 mxThrowException(mx::err::invalidarg,
"onePropRule::property",
"property is nullptr");
400 mxThrowException(mx::err::invalidconfig,
"onePropRule::property",
"property is not correct type");
442 rv =
"property is null";
446 rv =
"element is not found";
489 if(property ==
nullptr)
491 mxThrowException(mx::err::invalidarg,
"twoPropRule::property1",
"property is nullptr");
494 if(property->getType() !=
m_type)
496 mxThrowException(mx::err::invalidconfig,
"twoPropRule::property1",
"property is not correct type");
533 if(property ==
nullptr)
535 mxThrowException(mx::err::invalidarg,
"twoPropRule::property2",
"property is nullptr");
538 if(property->getType() !=
m_type)
540 mxThrowException(mx::err::invalidconfig,
"twoPropRule::property2",
"property is not correct type");
583 rv =
"property1 is null";
589 rv =
"element1 is not found";
595 rv =
"property2 is null";
601 rv =
"element2 is not found";
620 static constexpr
char name[] =
"numVal";
654 void tol(
const double & t )
658 mxThrowException(mx::err::invalidarg,
"numValRule::tol",
"tolerance can't be negative");
687 mxThrowException(mx::err::invalidconfig,
"numValRule::value", std::get<std::string>(rv));
690 double val = (*m_property)[
m_element].get<
double>();
695 mxThrowException(mx::err::invalidconfig,
"numValRule::value", std::get<std::string>(rv));
698 return std::get<bool>(rv);
711 static constexpr
char name[] =
"txtVal";
751 mxThrowException(mx::err::invalidconfig,
"txtValRule::value", std::get<std::string>(rv));
757 mxThrowException(mx::err::invalidconfig,
"txtValRule::value()", std::get<std::string>(rv));
760 return std::get<bool>(rv);
773 static constexpr
char name[] =
"swVal";
776 pcf::IndiElement::SwitchStateType
m_target {pcf::IndiElement::UnknownSwitchState};
785 void target(
const pcf::IndiElement::SwitchStateType & ss )
795 void target(
const std::string & switchState )
797 if(switchState ==
"On")
801 else if(switchState ==
"Off")
807 mxThrowException(mx::err::invalidarg,
"swValRule::target",
"invalid switch state");
815 const pcf::IndiElement::SwitchStateType &
target()
834 mxThrowException(mx::err::invalidconfig,
"swValRule::value", std::get<std::string>(rv));
840 mxThrowException(mx::err::invalidconfig,
"elCompSwRule::value()", std::get<std::string>(rv));
843 return std::get<bool>(rv);
854 static constexpr
char name[] =
"elCompNum";
871 void tol(
const double & t )
875 mxThrowException(mx::err::invalidarg,
"numValRule::tol",
"tolerance can't be negative");
904 mxThrowException(mx::err::invalidconfig,
"elCompNumRule::value", std::get<std::string>(rv));
910 mxThrowException(mx::err::invalidconfig,
"elCompNumRule::value()", std::get<std::string>(rv));
913 return std::get<bool>(rv);
923 static constexpr
char name[] =
"elCompTxt";
943 mxThrowException(mx::err::invalidconfig,
"elCompTxtRule::value", std::get<std::string>(rv));
949 mxThrowException(mx::err::invalidconfig,
"elCompTxtRule::value()", std::get<std::string>(rv));
952 return std::get<bool>(rv);
963 static constexpr
char name[] =
"elCompSw";
983 mxThrowException(mx::err::invalidconfig,
"elCompSwRule::value", std::get<std::string>(rv));
989 mxThrowException(mx::err::invalidconfig,
"elCompSwRule::value()", std::get<std::string>(rv));
992 return std::get<bool>(rv);
1006 static constexpr
char name[] =
"ruleComp";
1063 rv =
"rule1 is nullptr";
1067 rv =
"rule2 is nullptr";
1103 mxThrowException(mx::err::invalidconfig,
"ruleCompRule::value", std::get<std::string>(rv));
1109 mxThrowException(mx::err::invalidconfig,
"ruleCompRule::value", std::get<std::string>(rv));
1112 return std::get<bool>(rv);
ruleComparison
Logical comparisons for the INDI rules.
@ Xnor
boolean xnor, equivalent to equal
@ LtEq
Less than or equal to.
@ Xor
boolean xor, equivalent to not equal
@ GtEq
Greater than or equal to.
ruleComparison string2comp(const std::string &cstr)
Get the ruleComparison member from a string representation.
rulePriority string2priority(const std::string &pstr)
Get the rulePriority member from a string representation.
rulePriority
Reporting priorities for rules.
@ caution
Caution – make sure you know what you're doing.
@ warning
Warning – something is probably wrong, you should check.
@ alert
Alert – something is definitely wrong, you should take action.
@ info
For information only.
Compare two elements based on their numeric values.
virtual bool value()
Get the value of this rule.
double m_tol
The tolerance for the comparison.
elCompNumRule()
Default c'tor.
void tol(const double &t)
Set the tolerance.
const double & tol()
Get the tolerance.
static constexpr char name[]
Name of this rule, used by config system.
Compare two elements based on their switch values.
static constexpr char name[]
Name of this rule, used by config system.
elCompSwRule()
Default c'tor.
virtual bool value()
Get the value of this rule.
Compare two elements based on their text values.
virtual bool value()
Get the value of this rule.
elCompTxtRule()
Default c'tor.
static constexpr char name[]
Name of this rule, used by config system.
Virtual base-class for all rules.
void priority(const rulePriority &p)
Set priority of this rule.
virtual ~indiCompRule()
Virtual destructor.
rulePriority m_priority
The reporting priority for this rule.
const rulePriority & priority()
Get the rule priority.
virtual boolorerr_t valid()=0
Report whether the rule is valid as configured.
boolorerr_t compSw(const pcf::IndiElement::SwitchStateType &sw1, const pcf::IndiElement::SwitchStateType &sw2)
Compare two switches.
boolorerr_t compNum(const double &num1, const double &num2, const double &tol)
Compare two numbers.
virtual bool value()=0
Get the value of this rule.
std::variant< bool, std::string > boolorerr_t
In-band error reporting type.
boolorerr_t compTxt(const std::string &str1, const std::string &str2)
Compare two strings.
void comparison(const ruleComparison &c)
Set the comparison for this rule.
ruleComparison m_comparison
The comparison for this rule.
bool isError(boolorerr_t rv)
Check if returned value indicates an error.
std::string m_message
The message used for notifications.
void message(const std::string &m)
Set the message.
const std::string & message()
Get the message.
boolorerr_t compBool(const bool &b1, const bool &b2)
Compare two booleans.
const ruleComparison & comparison()
Get the rule comparison.
Compare the value of a number element to a target.
numValRule()
Default c'tor.
const double & tol()
Get the tolerance.
double m_tol
The tolerance for the comparison.
double m_target
The target value for comparison.
void target(const double &tgt)
Set the target for the comparison.
void tol(const double &t)
Set the tolerance.
virtual bool value()
Get the value of this rule.
static constexpr char name[]
Name of this rule, used by config system.
const double & target()
Get the target.
A rule base class for testing an element in one property.
std::string m_element
The element name within the property.
int m_type
The property type, from pcf::IndiProperty::Type.
const std::string & element()
Get the element name.
const pcf::IndiProperty * property()
Get the property pointer.
onePropRule(int type)
Constructor. You must provide the property type to construct a onePropRule.
void property(pcf::IndiProperty *property)
Set the property pointer.
void element(const std::string &el)
Set the element name.
pcf::IndiProperty * m_property
Pointer to the property.
virtual boolorerr_t valid()
Check if this rule is valid.
A rule to compare two rules.
ruleCompRule()
Default c'tor.
void rule2(indiCompRule *r)
Set the pointer to the second rule.
void rule1(indiCompRule *r)
Set the pointer to the first rule.
static constexpr char name[]
Name of this rule, used by config system.
indiCompRule * m_rule1
rule one
const indiCompRule * rule1()
Get the pointer to the first rule.
const indiCompRule * rule2()
Get the pointer to the first rule.
virtual boolorerr_t valid()
Check if this rule is valid.
virtual bool value()
Get the value of this rule.
indiCompRule * m_rule2
rule two
Compare the value of a switch to a target value.
virtual bool value()
Get the value of this rule.
void target(const pcf::IndiElement::SwitchStateType &ss)
Set the target for the comparison.
swValRule()
Default c'tor.
void target(const std::string &switchState)
Set the target for the comparison.
pcf::IndiElement::SwitchStateType m_target
The target value for comparison.
const pcf::IndiElement::SwitchStateType & target()
Get the target.
static constexpr char name[]
Name of this rule, used by config system.
A rule base class for testing elements in two properties.
const std::string & element1()
Get the first element name.
int m_type
The property type, from pcf::IndiProperty::Type.
void property1(pcf::IndiProperty *property)
Set the first property pointer.
virtual boolorerr_t valid()
Check if this rule is valid.
void element2(const std::string &el)
Set the second element name.
std::string m_element2
The element name within the second property.
twoPropRule(int type)
Constructor. You must provide the property type to construct a twoPropRule.
void element1(const std::string &el)
Set the first element name.
const pcf::IndiProperty * property1()
Get the first property pointer.
const std::string & element2()
Get the second element name.
void property2(pcf::IndiProperty *property)
Set the second property pointer.
std::string m_element1
The element name within the first property.
const pcf::IndiProperty * property2()
Get the second property pointer.
pcf::IndiProperty * m_property2
Pointer to the second property.
pcf::IndiProperty * m_property1
Pointer to the first property.
Compare the value of a text element to a target value.
void target(const std::string &target)
Set the target for the comparison.
const std::string & target()
Get the target.
static constexpr char name[]
Name of this rule, used by config system.
std::string m_target
The target value for comparison.
txtValRule()
Default c'tor.
virtual bool value()
Get the value of this rule.