?? rsesreducts.cpp
字號:
Handle<Structure> child = GetChild(j);
// Make sure it's a RSES rule set.
if (!child->IsA(RSESRULES))
continue;
Handle<RSESRules> rules = dynamic_cast(RSESRules *, child.GetPointer());
// Check that the embedded TRedRulMem objects are the same.
if (rules->rules_ != reducts_)
continue;
// Remove all rules.
if (!rules->RemoveAllStructures()) {
return false;
}
}
return true;
}
//-------------------------------------------------------------------
// Method........: DuplicateStructure
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Duplicates only a specified reduct in the reduct
// set.
// Comments......: Duplicates any associated rules as well.
// Stuff higher up (e.g. annotations) is not
// duplicated.
// Revisions.....:
//===================================================================
Structures *
RSESReducts::DuplicateStructure(int i) const {
// Check argument.
if (i < 0 || i >= GetNoReducts())
return NULL;
Handle<RSESReducts> rsesreducts = Creator::RSESReducts();
// Duplicate specified reduct wrapper (duplicates associated rules as well).
try {
// Get the reduct wrapper.
Handle<RSESReduct> wrapper = dynamic_cast(RSESReduct *, GetStructure(i));
// Duplicate the embedded RSES reduct.
TReduct *duplicate = new TReduct();
TReduct *rsesreduct = wrapper->reduct_;
*duplicate = *rsesreduct;
// Add it to the embedded set to return.
rsesreducts->reducts_->AddRed(duplicate);
}
// Catch any RSES exceptions.
catch (Error &error) {
Message::RSESError("Failed to duplicate embedded RSES reduct.", error.GetMessage());
return NULL;
}
// Build wrappers.
if (!rsesreducts->BuildWrappers())
return NULL;
return rsesreducts.Release();
}
//-------------------------------------------------------------------
// Methods inherited from Reducts.
//===================================================================
//-------------------------------------------------------------------
// Method........: Sort
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Sorts the wrappers only.
// Revisions.....:
//===================================================================
bool
RSESReducts::Sort(int property, bool ascending) {
return Reducts::Sort(property, ascending);
}
//-------------------------------------------------------------------
// Method........: PostCreation
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Typically invoked after all the calls to the
// AppendReduct method have been made.
// Revisions.....:
//===================================================================
bool
RSESReducts::PostCreation(const DecisionTable &table, const Discerner &discerner, bool modulo) {
MessageProxy message;
#ifdef _DEBUG
// Is the RSES object instantiated?
if (reducts_ == NULL) {
Message::Error("Embedded RSES object not instantiated.");
return false;
}
#endif
// Check compatibility.
if (!table.IsA(RSESDECISIONTABLE))
return false;
// Cast to correct type.
RSESDecisionTable *rsestable = dynamic_cast(RSESDecisionTable *, const_cast(DecisionTable *, &table));
message.Notify("Computing statistics...");
// Issue warnings?
if (discerner.HasIDGs()) {
Message::Warning("Support counts with IDGs not implemented by RSES.", false);
if (modulo)
Message::Warning("Generalized decisions with IDGs not implemented by RSES.", false);
}
// Let RSES create the rules/patterns.
try {
reducts_->UpDateRules(rsestable->decisiontable_, modulo, 0);
}
catch (Error &error) {
Message::RSESError("Failed to update the embedded RSES reduct and rule memory.", error.GetMessage());
return false;
}
return BuildWrappers(NULL);
}
//-------------------------------------------------------------------
// Method........: AppendReduct
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: After all the calls to this method have been made,
// a call MUST be made to BuildWrappers if consistency
// between reduct wrappers and RSES reducts is to be
// ensured. (The system may crash otherwise, if you're
// unlucky.) The reason for this is that the AddRed
// method may *delete* the reduct we want to add,
// if an indentical reduct already exists. Thus, we
// can end up with a wrapper with a dangling pointer.
//
// RSES requires verifies uniqueness anyway (and
// does a deletion!), regardless of the state of
// the verify flag.
//
// Revisions.....:
//===================================================================
bool
RSESReducts::AppendReduct(Reduct *reduct, const DecisionTable &table, int object_no, bool /*masked*/, bool modulo, bool /*verify*/) {
#ifdef _DEBUG
// Is the RSES object instantiated?
if (reducts_ == NULL) {
Message::Error("Embedded RSES object not instantiated.");
return false;
}
#endif
// Check compatibility.
if (reduct == NULL || !reduct->IsA(RSESREDUCT) || !table.IsA(RSESDECISIONTABLE))
return false;
// Cast to correct type.
RSESReduct *rsesreduct = dynamic_cast(RSESReduct *, reduct);
RSESDecisionTable *rsestable = dynamic_cast(RSESDecisionTable *, const_cast(DecisionTable *, &table));
bool did_delete = false;
// Add the embedded RSES reduct to the embedded reduct and rule memory.
TDTable *tmp1 = reducts_->CurrentTable;
int tmp2 = reducts_->DecTable;
reducts_->SetCurrentTable(rsestable->decisiontable_);
reducts_->SetDecTable(modulo);
did_delete = (reducts_->AddRed(rsesreduct->reduct_, object_no) != rsesreduct->reduct_);
reducts_->SetCurrentTable(tmp1);
reducts_->SetDecTable(tmp2);
}
catch (Error &error) {
Message::RSESError("Failed to add the embedded RSES reduct to the embedded reduct and rule memory.", error.GetMessage());
return false;
}
// Do something if the reduct was deleted. Return false, perhaps?
if (did_delete) {
// ...
}
// The embedded reduct and rule memory takes ownership of the embedded reduct.
rsesreduct->is_owner_ = false;
// Add the wrapper structure to the (wrapper) structure container, avoiding recursion.
return Structures::InsertStructure(reduct, Structures::GetNoStructures());
}
//-------------------------------------------------------------------
// Local methods.
//===================================================================
//-------------------------------------------------------------------
// Method........: BuildWrappers
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Creates wrappers for individual embedded reduct
// and rule objects from the RSES library.
//
// Wrappers are created so there is a one-to-one
// correspondence between the indices of the wrappers
// and the indices into the embedded TRedRulMem object.
//
// Comments......: Structures::InsertStructures used to avoid
// adding an embedded reduct to the embedded reduct
// memory when it is already a member.
//
// For similar reasons, the Structures:: qualifier is
// used when appropriate.
// Revisions.....:
//===================================================================
bool
RSESReducts::BuildWrappers(RSESRules *rules) {
// Does an embedded RSES library object exist?
if (reducts_ == NULL)
return false;
int i, no_reducts = GetNoStructures();
// Remove any wrappers already present (keeping the embedded objects).
if (no_reducts > 0) {
for (i = no_reducts - 1; i >= 0; i--) {
if (!Structures::RemoveStructure(i)) {
Message::Error("Failed to remove existing wrapper before building new ones.");
return false;
}
}
}
// Create wrappers for individual reducts.
try {
// Determine how many wrappers to build.
no_reducts = reducts_->NoReducts();
// Build wrappers.
for (i = 0; i < no_reducts; i++) {
// Get the RSES reduct to embed.
TReduct *embedded = reducts_->GetRed(i);
// Instantiate a wrapper.
Handle<RSESReduct> wrapper = Creator::RSESReduct();
// Delete the default embedded reduct of the wrapper, and substitute it with the new one.
delete wrapper->reduct_;
wrapper->reduct_ = embedded;
wrapper->is_owner_ = false;
// Insert the wrapper into the wrapper list.
if (!Structures::InsertStructure(const_cast(RSESReduct *, wrapper.GetPointer()), Structures::GetNoStructures())) {
Message::Error("Error inserting individual reduct wrapper.");
return false;
}
}
}
catch (Error &error) {
Message::RSESError("Error accessing embedded RSES object, wrappers not properly built.", error.GetMessage());
return false;
}
int no_rules;
// Are there any rules associated with the embedded RSES reduct and rule memory?
try {
no_rules = reducts_->NoRules();
}
catch (Error &error) {
Message::RSESError("Error accessing embedded RSES reduct and rule memory, only partial wrapping performed.", error.GetMessage());
return false;
}
// No rules, we're done.
if (no_rules == 0)
return true;
Handle<RSESRules> child;
// Create a rule set wrapper, or use the one supplied.
if (rules == NULL)
child = Creator::RSESRules();
else
child = rules;
// Append the rule set as a child.
if (!AppendChild(child.GetPointer())) {
Message::Error("Failed to append the set of RSES rules to the list of children.");
return false;
}
// Set common embedded pointer to RSES reduct and rule memory.
child->rules_ = reducts_;
child->is_owner_ = !is_owner_;
// Build wrappers for the rules.
if (!child->BuildWrappers()) {
Message::Error("Failed to create wrappers for individual RSES rules.");
return false;
}
return true;
}
//-------------------------------------------------------------------
// Method........: TakeOwnership
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
void
RSESReducts::TakeOwnership(bool ownership) {
// Set ownership flag for the TRedRulMem structure.
is_owner_ = ownership;
int i, no_reducts = GetNoReducts();
// None of the wrappers own the embedded TReduct objects, the TRedRulMem object
// takes ownership once a TReduct is added to the pool.
for (i = 0; i < no_reducts; i++) {
// Get wrapper around RSES reduct.
Handle<RSESReduct> wrapper = dynamic_cast(RSESReduct *, GetReduct(i));
// Set ownership flag for the embedded TReduct structure.
wrapper->is_owner_ = false;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -