📄 rsesreduct.cpp
字号:
//-------------------------------------------------------------------
// Author........: Aleksander 豩rn/Merete Hvalshagen
// Date..........:
// Description...:
// Revisions.....:
//===================================================================
#include <stdafx.h> // Precompiled headers.
#include <copyright.h>
#include <kernel/rses/structures/rsesreduct.h>
#include <kernel/rses/library/treduct.h>
#include <kernel/rses/library/err.h>
//-------------------------------------------------------------------
// Methods for class RSESReduct.
//===================================================================
//-------------------------------------------------------------------
// Constructors/destructor.
//===================================================================
//-------------------------------------------------------------------
// Method........: Copy constructor.
// Author........:
// Date..........:
// Description...:
// Comments......: Clear associated RSES rule objects?
// Revisions.....:
//===================================================================
RSESReduct::RSESReduct(const RSESReduct &in) : Reduct(in) {
// Set owner flag. Not copied from input since a new embedded object is created.
is_owner_ = true;
// Is the embedded input RSES reduct NULL?
if (in.reduct_ == NULL) {
reduct_ = NULL;
return;
}
// Create a virgin RSES object.
try {
reduct_ = new TReduct();
}
catch (Error &error) {
Message::RSESError("Error instantiating RSES reduct.", error.GetMessage());
reduct_ = NULL;
return;
}
// Copy the embedded input RSES object.
try {
*reduct_ = *(in.reduct_);
}
catch (Error &error) {
Message::RSESError("Error copying RSES reduct.", error.GetMessage());
delete reduct_;
reduct_ = NULL;
}
}
//-------------------------------------------------------------------
// Method........: Constructor.
// Author........:
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
RSESReduct::RSESReduct() {
// Create a virgin RSES object.
try {
reduct_ = new TReduct();
}
catch (Error &error) {
Message::RSESError("Error instantiating RSES reduct.", error.GetMessage());
reduct_ = NULL;
}
// Set owner flag.
is_owner_ = true;
}
//-------------------------------------------------------------------
// Method........: Destructor.
// Author........:
// Date..........:
// Description...:
// Comments......: The embedded TReduct object might be a member of a
// TRedRulMem object from the RSES library. If so,
// the TRedRulMem object takes ownership of the TReduct
// object and deletes it.
//
// The owner flag thus avoids multiple deletes as well
// as delas with potential memory leaks.
// Revisions.....:
//===================================================================
RSESReduct::~RSESReduct() {
// Does this wrapper own the embedded reduct?
if (is_owner_)
delete reduct_;
}
//-------------------------------------------------------------------
// Methods inherited from Identifier.
//===================================================================
IMPLEMENTIDMETHODS(RSESReduct, RSESREDUCT, Reduct)
//-------------------------------------------------------------------
// Methods inherited from Structure.
//===================================================================
Structure *
RSESReduct::Duplicate() const {
return new RSESReduct(*this);
}
//-------------------------------------------------------------------
// Methods inherited from Reduct.
//===================================================================
//-------------------------------------------------------------------
// Method........: GetNoAttributes
// Author........:
// Date..........:
// Description...: Returns the number of attributes (indices) defining
// the reduct.
//
// Example: R = {3, 6, 7, 9}
// R has four attributes.
//
// Comments......:
// Revisions.....:
//===================================================================
int
RSESReduct::GetNoAttributes() const {
#ifdef _DEBUG
// Is the RSES object instantiated?
if (reduct_ == NULL) {
Message::Error("RSES reduct is not properly instantiated.");
return 0;
}
#endif
try {
return reduct_->NoAttr();
}
catch (Error &error) {
Message::RSESError("Error accessing embedded RSES reduct.", error.GetMessage());
return 0;
}
}
//-------------------------------------------------------------------
// Method........: GetAttribute
// Author........:
// Date..........:
// Description...: Returns the attribute number in the given position.
//
// Example: R = {3, 6, 7, 9}
// R.GetAttribute(0) will return 3.
// R.GetAttribute(1) will return 6.
// R.GetAttribute(2) will return 7.
// R.GetAttribute(3) will return 9.
// R.GetAttribute(4) will return Undefined::Integer()
//
// Comments......:
// Revisions.....:
//===================================================================
int
RSESReduct::GetAttribute(int position_no) const {
#ifdef _DEBUG
// Is the RSES object instantiated?
if (reduct_ == NULL) {
Message::Error("RSES reduct is not properly instantiated.");
return Undefined::Integer();
}
#endif
#ifdef _DEBUG
// Is the index argument legal?
if ((position_no < 0) || (position_no >= GetNoAttributes())) {
Message::Error("Reduct 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........: InsertAttribute
// Author........:
// Date..........:
// Description...: A reduct is conceptually defined through a list of
// attribute numbers (indices). Returns true if the
// specified attribute was actually appended to the
// reduct, false otherwise. The list of attribute
// indices is hence kept unique.
//
// Example: R = {3, 6, 7, 9}
// R.AppendAttribute(5) returns true.
// R = {3, 6, 7, 9, 5}
// R.AppendAttribute(7) returns false.
// R = {3, 5, 6, 7, 9}
//
// Comments......: The RSES library reduct does not have an interface
// to explicitly keep the reduct sorted.
// Revisions.....:
//===================================================================
bool
RSESReduct::InsertAttribute(int attribute_no) {
// Is the attribute already a member? If so, do not violate uniqueness.
if (IsMember(attribute_no))
return false;
#ifdef _DEBUG
// Is the RSES object instantiated?
if (reduct_ == NULL) {
Message::Error("RSES reduct is not properly instantiated.");
return false;
}
#endif
try {
reduct_->AddAttr(attribute_no);
}
catch (Error &error) {
Message::RSESError("Error adding attribute to embedded RSES reduct.", error.GetMessage());
return false;
}
return true;
}
//-------------------------------------------------------------------
// Method........: RemoveAttribute
// Author........:
// Date..........:
// Description...: Given a position index into the list of attribute
// indices defining the reduct, returns true if the
// specified attribute was removed from the reduct,
// false otherwise.
//
// Example: R = {3, 6, 7, 9}
// R.RemoveAttribute(4) returns false.
// R = {3, 6, 7, 9}
// R.RemoveAttribute(1) returns true.
// R = {3, 7, 9}
//
// Comments......: For the associated rule objects from the RSES library,
// the corresponding attributes are removed, too.
// Revisions.....:
//===================================================================
bool
RSESReduct::RemoveAttribute(int position_no) {
#ifdef _DEBUG
// Legal index?
if ((position_no < 0) || (position_no >= GetNoAttributes())) {
Message::Error("Position index in reduct out of range.");
return false;
}
#endif
try {
reduct_->RemoveAttr(position_no);
}
catch (Error &error) {
Message::RSESError("Error removing attribute from embedded RSES reduct.", error.GetMessage());
return false;
}
return true;
}
//-------------------------------------------------------------------
// Method........: IsMember
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Returns true if the given attribute is a member of
// the reduct.
// Comments......:
// Revisions.....:
//===================================================================
bool
RSESReduct::IsMember(int attribute_no) const {
#ifdef _DEBUG
// Is the RSES object instantiated?
if (reduct_ == NULL) {
Message::Error("RSES reduct is not properly instantiated.");
return false;
}
#endif
try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -