⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsesreducts.cpp

📁 粗慥集成算法集合 ,并有详细的文档资料和测试数据处
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//-------------------------------------------------------------------
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Revisions.....:
//===================================================================

#include <stdafx.h> // Precompiled headers.
#include <copyright.h>

#include <kernel/rses/structures/rsesreducts.h>
#include <kernel/rses/structures/rsesreduct.h>
#include <kernel/rses/structures/rsesrules.h>
#include <kernel/rses/structures/rsesdecisiontable.h>

#include <kernel/rses/library/trr_mem.h>
#include <kernel/rses/library/treduct.h>
#include <kernel/rses/library/err.h>

#include <kernel/utilities/iokit.h>
#include <kernel/utilities/creator.h>
#include <kernel/utilities/discerner.h>

#include <kernel/basic/message.h>

#include <common/configuration.h>

//-------------------------------------------------------------------
// Methods for class RSESReducts.
//===================================================================

//-------------------------------------------------------------------
// Constructors/destructor.
//===================================================================

//-------------------------------------------------------------------
// Method........: Copy constructor.
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: The input structure should be passed on up the
//                 hierarchy with some steps skipped!  Ideally, the
//                 input argument should be passed on directly to
//                 the AnnotatedStructure level.  This is necessary to
//                 ensure wrapper consistency.  However, skipping of such
//                 steps are not allowed.
//
// Revisions.....: Duplicates embedded rule children as well.
//===================================================================

RSESReducts::RSESReducts(const RSESReducts &in) {

	// This wrapper "owns" the newly created (duplicated) RSES object.
	is_owner_ = true;

	// Is the input empty?
	if (in.reducts_ == NULL) {
		reducts_ = NULL;
		return;
	}

	// Create a virgin RSES reduct and rule memory.
	try {
		reducts_ = new TRedRulMem();
	}
	catch (Error &error) {
		Message::RSESError("Error creating RSES reduct and rule memory.", error.GetMessage());
		reducts_ = NULL;
		return;
	}

	int no_reducts = in.GetNoReducts();

	// Any reducts to duplicate?
	if (no_reducts == 0)
		return;

	// Duplicate embedded input objects.
	try {

		int i;

		// Duplicate them in the order the wrappers are arranged.
		for (i = 0; i < no_reducts; i++) {

			// Get the current reduct wrapper.
			Handle<RSESReduct> wrapper = dynamic_cast(RSESReduct *, in.GetReduct(i));

			// Duplicate the embedded RSES reduct.
			TReduct *duplicate  = new TReduct();
			TReduct *rsesreduct = wrapper->reduct_;

			*duplicate = *rsesreduct;

			// Add it to this set of embedded RSES reducts.
			reducts_->AddRed(duplicate);

		}

	}

	// Catch any exceptions thrown by RSES.
	catch (Error &error) {
		Message::RSESError("Failed to duplicate embedded RSES reducts.", error.GetMessage());
		delete reducts_;
		reducts_ = NULL;
		return;
	}

#if 0
	// Embedded children are not duplicated.
	try {
		if (reducts_->NoRules() != 0)
		  reducts_->ClearRules();
	}
	catch (Error &error) {
		Message::RSESError("Failed to clear rules associated with embedded RSES reducts.", error.GetMessage());
	}
#endif

	// Build wrappers.
	if (!BuildWrappers())
		Message::Error("Error building individual reduct wrappers.");

	// Stuff higher up in the hierarchy should be handled better (cf. comments above).
	if (in.GetAnnotation() != NULL)
		SetAnnotation(dynamic_cast(Annotation *, in.GetAnnotation()->Duplicate()));

}

//-------------------------------------------------------------------
// Method........: Constructor.
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

RSESReducts::RSESReducts() {

	// Create a virgin RSES reduct and rule memory.
	try {
		reducts_ = new TRedRulMem();
	}
	catch (Error &error) {
		Message::RSESError("Error creating RSES reduct and rule memory.", error.GetMessage());
		reducts_ = NULL;
	}

	// This wrapper "owns" the newly created RSES object.
	is_owner_ = true;

}

//-------------------------------------------------------------------
// Method........: Destructor.
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Since any derived RSESRules object contains a pointer
//                 to the same TRedRulMem presumabky "owned" by this set,
//                 take care of ownership.
// Revisions.....:
//===================================================================

RSESReducts::~RSESReducts() {

	// Exit if this wrapper is not responsible for dealing with the
	// embedded object.
	if (!is_owner_)
		return;

	bool transfer = false;

	int j;

	// Transfer ownership to a RSES rule set child, if any.
	for (j = 0; j < GetNoChildren(); j++) {

		Handle<Structure> child = GetChild(j);

		if (!child->IsA(RSESRULES))
			continue;

		Handle<RSESRules> rules  = dynamic_cast(RSESRules *, child.GetPointer());

		// Transfer ownership, if not already done.
		if (rules->rules_ == reducts_) {
			rules->TakeOwnership(!transfer);
			transfer = true;
		}

	}

	// If ownership was not transferred, delete the embedded RSES object.
	if (!transfer)
		delete reducts_;

}

//-------------------------------------------------------------------
// Methods inherited from Identifier.
//===================================================================

IMPLEMENTIDMETHODS(RSESReducts, RSESREDUCTS, Reducts)

//-------------------------------------------------------------------
// Methods inherited from Persistent.
//===================================================================

//-------------------------------------------------------------------
// Method........: Load
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Since a RSESReducts object physically contains the
//                 data that logically belongs in a RSESRules child
//                 object, special considerations are made.
// Revisions.....:
//===================================================================

bool
RSESReducts::Load(ifstream &stream) {

	// Load stuff higher up (skip the Structures and ParentStructures levels).
	if (!AnnotatedStructure::Load(stream))
		return false;

	// Delete the current reduct and rule memory.
	delete reducts_;

	// Create a new "virgin" RSES library object.
	try {
		reducts_ = new TRedRulMem();
	}
	catch (Error &error) {
		Message::RSESError("Error creating embedded RSES reduct and rule memory.", error.GetMessage());
		reducts_ = NULL;
		return false;
	}

	/*
	// RSES Version 1.3.
	Message::Warning("RSESReducts I/O temporarily disabled due to a bug in the RSES library.");
	return true;
	*/

	// Read a newline character so that the RSES I/O does not get confused.
	char dummy[10];
  stream.getline(dummy, sizeof(dummy), '\n');

	// Let the embedded object load itself.
	try {
		reducts_->LoadMemory(stream);
	}
	catch (Error &error) {
		Message::RSESError("Error loading embedded RSES reduct and rule memory.", error.GetMessage());
		return false;
	}

	// In case the RSES code calls the stop function...
	//Message::Reset();

	int no_children;

	// Load the number of children.
	if (!IOKit::Load(stream, no_children))
		return false;

	Handle<RSESRules> rules;

	int i;

	// Load the children recursively.
  for (i = 0; i < no_children; i++) {

		String type;

		// Load the type description of the child.
		if (!IOKit::Load(stream, type))
			return false;

		// Reconstruct the type of the child.
		Id id = IdHolder::GetId(type);

		if (id == Undefined::Id()) {
			Message::Error("Unknown child type identifier (" + type + ").");
			return false;
		}

		// Create a new child.
		Handle<Structure> child = Creator::Create(id);

		// Let the child load itself.
		if (!child->Load(stream))
			return false;

		// Keep a handle to pass to BuildWrappers.
		if (child->IsA(RSESRULES))
			rules = dynamic_cast(RSESRules *, child.GetPointer());

		// Append the child to the list of children.
		if (!AppendChild(child.GetPointer()))
			return false;

	}

	// Build wrappers for reducts and rules.
	if (!BuildWrappers(rules.GetPointer())) {
		Message::Error("Error building wrappers.");
		return false;
	}

	return true;

}

//-------------------------------------------------------------------
// Method........: Save
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Cf. Load method comments.
// Revisions.....:
//===================================================================

bool
RSESReducts::Save(ofstream &stream) const {

	// Save stuff higher up (skip the Structures and ParentStructures levels).
	if (!AnnotatedStructure::Save(stream))
		return false;

#if 0 // RSES Version 1.3.
	Message::Warning("RSESReducts I/O temporarily disabled due to a bug in the RSES library.");

	return true;
#endif

	Message::Warning("Reduct set (and any derived rules) saved unsorted.", false);

	// To ensure that stop function displays correct text.
	Configuration::Kernel::RSES::IsSavingRRMemory(true);

	// Let the embedded object save itself.
	try {
		reducts_->SaveMemory(stream);
	}
	catch (Error &error) {
		Message::RSESError("Error saving embedded RSES reduct and rule memory.", error.GetMessage());
		Configuration::Kernel::RSES::IsSavingRRMemory(false);
		return false;
	}

	// To ensure that stop function displays correct text.
	Configuration::Kernel::RSES::IsSavingRRMemory(false);

	// In case the RSES code calls the stop function...
	//Message::Reset();

	// Save the number of children.
	if (!IOKit::Save(stream, GetNoChildren()))
		return false;

	if (!IOKit::Save(stream, '\n'))
		return false;

	int i;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -