📄 rsesrule.cpp
字号:
//-------------------------------------------------------------------
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Revisions.....:
//===================================================================
#include <stdafx.h> // Precompiled headers.
#include <copyright.h>
#include <kernel/rses/structures/rsesrule.h>
#include <kernel/rses/library/trule.h>
#include <kernel/rses/library/treduct.h>
#include <kernel/rses/library/err.h>
#include <kernel/structures/decisiontable.h>
#include <kernel/utilities/mathkit.h>
//-------------------------------------------------------------------
// Methods for class RSESRule.
//===================================================================
//-------------------------------------------------------------------
// Constructors/destructor.
//===================================================================
//-------------------------------------------------------------------
// Method........: Copy constructor
// Author........:
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
RSESRule::RSESRule(const RSESRule &in) : Rule(in) {
// Set owner flags. Not copied from input since a new embedded object is created.
is_rule_owner_ = true;
is_reduct_owner_ = true;
rule_ = NULL;
reduct_ = NULL;
// Create a "virgin" rule object.
try {
if (in.rule_ != NULL) {
rule_ = new TRule();
*rule_ = *in.rule_;
}
if (in.reduct_ != NULL) {
reduct_ = new TReduct();
*reduct_ = *in.reduct_;
}
/*
if (reduct_ != NULL && rule_ != NULL)
reduct_->AddRule(rule_); // Causes crash, rule_ gets deleted...
*/
}
catch (Error &error) {
Message::RSESError("Failed to create embedded RSES objects.", error.GetMessage());
delete rule_;
delete reduct_;
rule_ = NULL;
reduct_ = NULL;
return;
}
}
//-------------------------------------------------------------------
// Method........: Constructor
// Author........:
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
RSESRule::RSESRule() {
rule_ = NULL;
reduct_ = NULL;
// Create a "virgin" rule.
try {
rule_ = new TRule();
reduct_ = new TReduct();
/*
reduct_->AddRule(rule_);
*/
}
catch (Error &error) {
Message::RSESError("Failed to create embedded RSES objects.", error.GetMessage());
delete rule_;
delete reduct_;
rule_ = NULL;
reduct_ = NULL;
}
// Set owner flags.
is_rule_owner_ = true;
is_reduct_owner_ = true;
}
//-------------------------------------------------------------------
// Method........: Destructor
// Author........:
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
RSESRule::~RSESRule() {
if (is_rule_owner_)
delete rule_;
if (is_reduct_owner_)
delete reduct_;
}
//-------------------------------------------------------------------
// Methods inherited from Identifier.
//===================================================================
IMPLEMENTIDMETHODS(RSESRule, RSESRULE, Rule)
//-------------------------------------------------------------------
// Methods inherited from Structure.
//===================================================================
//------------------------------------------------------------------
// Method........: Duplicate
// Author........:
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
Structure *
RSESRule::Duplicate() const {
return new RSESRule(*this);
}
//------------------------------------------------------------------
// Method........: Clear
// Author........:
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
void
RSESRule::Clear() {
Message::Error("The RSES library does not support general clearing of rules.");
}
//-------------------------------------------------------------------
// Methods inherited from Rule.
//===================================================================
//------------------------------------------------------------------
// Method........: GetNoConditionDescriptors
// Author........:
// Date..........:
// Description...: Returns the number of attributes in the parent
// reduct.
// Comments......:
// Revisions.....:
//===================================================================
int
RSESRule::GetNoConditionDescriptors() const {
#ifdef _DEBUG
// Is the embedded reduct instantiated?
if (reduct_ == NULL) {
Message::Error("The embedded RSES reduct is not instantiated.");
return 0;
}
#endif
try {
return reduct_->NoAttr();
}
catch (Error &error) {
Message::RSESError("Error accessing embedded RSES reduct.", error.GetMessage());
return 0;
}
}
//------------------------------------------------------------------
// Method........: GetConditionAttribute
// Author........:
// Date..........:
// Description...: Returns the specified attribute in the parent
// reduct.
// Comments......:
// Revisions.....:
//===================================================================
int
RSESRule::GetConditionAttribute(int position_no) const {
#ifdef _DEBUG
// Is the embedded reduct instantiated?
if (reduct_ == NULL) {
Message::Error("The embedded RSES reduct is not instantiated.");
return Undefined::Integer();
}
#endif
#ifdef _DEBUG
// Is the index argument legal?
if ((position_no < 0) || (position_no >= GetNoConditionDescriptors())) {
Message::Error("Condition attribute position index out of range.");
return Undefined::Integer();
}
#endif
try {
return reduct_->GetAttr(position_no);
}
catch (Error &error) {
Message::RSESError("Error accessing embedded RSES reduct.", error.GetMessage());
return Undefined::Integer();
}
}
//------------------------------------------------------------------
// Method........: AppendConditionDescriptor
// Author........:
// Date..........:
// Description...: Appends an (attribute, value) pair to the
// condition part of the rule.
//
// Comments......: Assumes that TReduct::AddAttr does an append...
// Revisions.....:
//===================================================================
bool
RSESRule::AppendConditionDescriptor(int attribute_no, int value) {
#ifdef _DEBUG
// Is the RSES object instantiated?
if (reduct_ == NULL) {
Message::Error("RSES reduct is not properly instantiated.");
return false;
}
#endif
// Is the attribute already a member? If so, do not violate uniqueness.
try {
if (reduct_->AttrInReduct(attribute_no) == 1)
return false;
}
catch (Error &error) {
Message::RSESError("Error accessing embedded RSES reduct.", error.GetMessage());
return false;
}
// Add the attribute.
try {
reduct_->AddAttr(attribute_no);
}
catch (Error &error) {
Message::RSESError("Error adding attribute to embedded RSES reduct.", error.GetMessage());
return false;
}
// Add the value.
return AppendConditionValue(value);
}
//------------------------------------------------------------------
// Method........: SetDecisionAttribute
// Author........:
// Date..........:
// Description...: Sets the given attribute number as the decision
// attribute. The attribute number is relative to
// the origin table.
// Comments......:
// Revisions.....:
//===================================================================
bool
RSESRule::SetDecisionAttribute(int attribute_no) {
#ifdef _DEBUG
// Is the embedded rule instantiated?
if (rule_ == NULL) {
Message::Error("The embedded RSES rule is not instantiated.");
return false;
}
#endif
#ifdef _DEBUG
// Does the input index make sense?
Message::Debug("Attribute index not verified.");
#endif
try {
rule_->SetDecAttr(attribute_no);
}
catch (Error &error) {
Message::RSESError("Error accessing the embedded RSES rule.", error.GetMessage());
return false;
}
return true;
}
//------------------------------------------------------------------
// Method........: GetDecisionAttribute
// Author........:
// Date..........:
// Description...: Returns the rule's decision attribute (if any).
// Comments......:
// Revisions.....: A
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -