7#ifndef stateRuleEngine_indiCompRules_hpp
8#define stateRuleEngine_indiCompRules_hpp
12#include "../../libMagAOX/libMagAOX.hpp"
14#include <mx/mxException.hpp>
44 else if(cstr ==
"Neq")
56 else if(cstr ==
"LtEq")
60 else if(cstr ==
"GtEq")
64 else if(cstr ==
"And")
68 else if(cstr ==
"Nand")
76 else if(cstr ==
"Nor")
80 else if(cstr ==
"Xor")
84 else if(cstr ==
"Xnor")
88 else if(cstr ==
"Imply")
92 else if(cstr ==
"Nimply")
98 mxThrowException(mx::err::invalidarg,
"string2comp", cstr +
" is not a valid comparison");
121 else if(pstr ==
"info")
125 else if(pstr ==
"caution")
129 else if(pstr ==
"warning")
133 else if(pstr ==
"alert")
139 mxThrowException(mx::err::invalidarg,
"string2priority", pstr +
" is not a valid priority");
157 return (rv.index() > 0);
243 const std::string & str2
251 if(str1 == str2) rv =
true;
254 if(str1 != str2) rv =
true;
257 rv =
"operator not valid for string comparison";
271 const pcf::IndiElement::SwitchStateType & sw2
279 if(sw1 == sw2) rv =
true;
282 if(sw1 != sw2) rv =
true;
285 rv =
"operator not valid for switch comparison";
309 if( fabs(num1 - num2) <= tol ) rv =
true;
312 if( fabs(num1 - num2) > tol ) rv =
true;
315 if( num1 < num2) rv =
true;
318 if( num1 > num2) rv =
true;
321 if( fabs(num1 - num2) <= tol ) rv =
true;
322 else if (num1 < num2) rv =
true;
325 if( fabs(num1 - num2) <= tol ) rv =
true;
326 else if (num1 > num2) rv =
true;
329 rv =
"operator not valid for compNum";
350 if(b1 == b2) rv =
true;
353 if(b1 != b2) rv =
true;
356 if(b1 && b2) rv =
true;
359 if(!(b1 && b2)) rv =
true;
362 if(b1 || b2) rv =
true;
365 if(!b1 && !b2) rv =
true;
369 if(!b1 || b2) rv=
true;
373 if(b1 && !b2) rv=
true;
376 rv =
"operator not valid for ruleCompRule";
413 mxThrowException(mx::err::invalidarg,
"onePropRule::property",
"property is nullptr");
418 mxThrowException(mx::err::invalidconfig,
"onePropRule::property",
"property is not correct type");
460 rv =
"property is null";
464 rv =
"element is not found";
507 if(property ==
nullptr)
509 mxThrowException(mx::err::invalidarg,
"twoPropRule::property1",
"property is nullptr");
512 if(property->getType() !=
m_type)
514 mxThrowException(mx::err::invalidconfig,
"twoPropRule::property1",
"property is not correct type");
551 if(property ==
nullptr)
553 mxThrowException(mx::err::invalidarg,
"twoPropRule::property2",
"property is nullptr");
556 if(property->getType() !=
m_type)
558 mxThrowException(mx::err::invalidconfig,
"twoPropRule::property2",
"property is not correct type");
601 rv =
"property1 is null";
607 rv =
"element1 is not found";
613 rv =
"property2 is null";
619 rv =
"element2 is not found";
638 static constexpr char name[] =
"numVal";
672 void tol(
const double & t )
676 mxThrowException(mx::err::invalidarg,
"numValRule::tol",
"tolerance can't be negative");
705 mxThrowException(mx::err::invalidconfig,
"numValRule::value", std::get<std::string>(rv));
708 double val = (*m_property)[
m_element].get<
double>();
713 mxThrowException(mx::err::invalidconfig,
"numValRule::value", std::get<std::string>(rv));
716 return std::get<bool>(rv);
729 static constexpr char name[] =
"txtVal";
769 mxThrowException(mx::err::invalidconfig,
"txtValRule::value", std::get<std::string>(rv));
775 mxThrowException(mx::err::invalidconfig,
"txtValRule::value()", std::get<std::string>(rv));
778 return std::get<bool>(rv);
791 static constexpr char name[] =
"swVal";
794 pcf::IndiElement::SwitchStateType
m_target {pcf::IndiElement::UnknownSwitchState};
803 void target(
const pcf::IndiElement::SwitchStateType & ss )
813 void target(
const std::string & switchState )
815 if(switchState ==
"On")
819 else if(switchState ==
"Off")
825 mxThrowException(mx::err::invalidarg,
"swValRule::target",
"invalid switch state");
833 const pcf::IndiElement::SwitchStateType &
target()
852 mxThrowException(mx::err::invalidconfig,
"swValRule::value", std::get<std::string>(rv));
858 mxThrowException(mx::err::invalidconfig,
"elCompSwRule::value()", std::get<std::string>(rv));
861 return std::get<bool>(rv);
872 static constexpr char name[] =
"elCompNum";
889 void tol(
const double & t )
893 mxThrowException(mx::err::invalidarg,
"numValRule::tol",
"tolerance can't be negative");
922 mxThrowException(mx::err::invalidconfig,
"elCompNumRule::value", std::get<std::string>(rv));
928 mxThrowException(mx::err::invalidconfig,
"elCompNumRule::value()", std::get<std::string>(rv));
931 return std::get<bool>(rv);
941 static constexpr char name[] =
"elCompTxt";
961 mxThrowException(mx::err::invalidconfig,
"elCompTxtRule::value", std::get<std::string>(rv));
967 mxThrowException(mx::err::invalidconfig,
"elCompTxtRule::value()", std::get<std::string>(rv));
970 return std::get<bool>(rv);
981 static constexpr char name[] =
"elCompSw";
1001 mxThrowException(mx::err::invalidconfig,
"elCompSwRule::value", std::get<std::string>(rv));
1007 mxThrowException(mx::err::invalidconfig,
"elCompSwRule::value()", std::get<std::string>(rv));
1010 return std::get<bool>(rv);
1024 static constexpr char name[] =
"ruleComp";
1081 rv =
"rule1 is nullptr";
1085 rv =
"rule2 is nullptr";
1121 mxThrowException(mx::err::invalidconfig,
"ruleCompRule::value", std::get<std::string>(rv));
1127 mxThrowException(mx::err::invalidconfig,
"ruleCompRule::value", std::get<std::string>(rv));
1130 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.
const double & tol()
Get the tolerance.
double m_tol
The tolerance for the comparison.
elCompNumRule()
Default c'tor.
void tol(const double &t)
Set 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.
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.
const ruleComparison & comparison()
Get the rule comparison.
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.
const std::string & message()
Get the message.
void message(const std::string &m)
Set the message.
const rulePriority & priority()
Get the rule priority.
boolorerr_t compBool(const bool &b1, const bool &b2)
Compare two booleans.
Compare the value of a number element to a target.
const double & target()
Get the target.
numValRule()
Default c'tor.
double m_tol
The tolerance for the comparison.
double m_target
The target value for comparison.
const double & tol()
Get the tolerance.
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.
A rule base class for testing an element in one property.
const pcf::IndiProperty * property()
Get the property pointer.
std::string m_element
The element name within the property.
int m_type
The property type, from pcf::IndiProperty::Type.
onePropRule(int type)
Constructor. You must provide the property type to construct a onePropRule.
const std::string & element()
Get the element name.
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.
const indiCompRule * rule2()
Get the pointer to the first rule.
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.
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.
const pcf::IndiElement::SwitchStateType & target()
Get the target.
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.
static constexpr char name[]
Name of this rule, used by config system.
A rule base class for testing elements in two properties.
int m_type
The property type, from pcf::IndiProperty::Type.
const std::string & element1()
Get the first element name.
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 * property2()
Get the second 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.
pcf::IndiProperty * m_property2
Pointer to the second property.
const pcf::IndiProperty * property1()
Get the first property pointer.
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.
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.
const std::string & target()
Get the target.