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

📄 rsesdecisiontableimporter.cpp

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

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

#include <kernel/rses/algorithms/rsesdecisiontableimporter.h>

#include <kernel/rses/structures/rsesdecisiontable.h>

#include <kernel/rses/library/tdict.h>
#include <kernel/rses/library/tdtable.h>
#include <kernel/rses/library/err.h>

#include <kernel/structures/decisiontable.h>
#include <kernel/structures/attribute.h>
#include <kernel/structures/integerattribute.h>
#include <kernel/structures/floatattribute.h>
#include <kernel/structures/stringattribute.h>
#include <kernel/structures/dictionary.h>

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

#include <kernel/basic/message.h>

//-------------------------------------------------------------------
// Methods for class RSESDecisionTableImporter.
//===================================================================

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

RSESDecisionTableImporter::RSESDecisionTableImporter() {
}

RSESDecisionTableImporter::~RSESDecisionTableImporter() {
}

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

IMPLEMENTIDMETHODS(RSESDecisionTableImporter, RSESDECISIONTABLEIMPORTER, DecisionTableImporter)

//-------------------------------------------------------------------
// Methods inherited from Algorithm.
//===================================================================

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

bool
RSESDecisionTableImporter::IsApplicable(const Structure &structure, bool /*warn*/) const {
	return structure.IsA(RSESDECISIONTABLE);
}

//-------------------------------------------------------------------
// Method........: Apply
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Given a RSES decision table, empties it and fills it
//                 with data imported from a file on alien format.
//
// Comments......: Assumes that library clients use handles.
// Revisions.....:
//===================================================================

Structure *
RSESDecisionTableImporter::Apply(Structure &structure) const {

	// Check input.
	if (!IsApplicable(structure))
		return NULL;

	// Cast input to verified type.
	Handle<RSESDecisionTable> table = dynamic_cast(RSESDecisionTable *, &structure);

	// Convert filename parameter to a form used by the RSES library.
	char *filename = const_cast(char *, GetFilename().GetBuffer());

	// Remove all children of the input table.
	if (!table->RemoveAllChildren()) {
		Message::Error("Failed to remove children.");
		return NULL;
	}

	ifstream stream;

	if (!IOKit::Open(stream, filename)) {
		Message::Error("Unable to peek inside file " + GetFilename() + ".");
		return NULL;
	}

	String firstline;

	// Peek inside the file and see if the first line consists of two integers.  If not, do not attempt
	// to use the ordinary RSES loading method, as this will crash.
	if (!IOKit::Load(stream, firstline, false)) {
		Message::Error("Error peeking inside file to determine file type.");
		return NULL;
	}

	IOKit::Close(stream);

	// Define a flag to decide whether to try ordinary RSES library table loading or RSES library import routine.
	bool ordinary = true;

	// Do a crude analysis of the first line.
	if (firstline.GetNoTokens(" \t") != 2) {
		ordinary = false;
	}
	else {
		String token;
		while (firstline.GetToken(token, " \t")) {
			if (!token.IsInteger())
				ordinary = false;
		}
	}

	TDictionary rsesdictionary;

	// Import the table.
	try {
#if 0
		Message::Information("Importing tables using RSES I/O code is temporarily disabled.");
		return NULL;
#else
		if (ordinary) {                                                                 // Try ordinary RSES format.
			Message::Debug("Trying to read ordinary RSES format.");
			table->decisiontable_->LoadTable(filename);                                   // Load the table.
			//Message::Reset();                                                             // RSES may try to "hold" the reset window indefinetely.
		}
		else {                                                                          // Try RSES import format.
			Message::Debug("Trying to read RSES import format.");
			TDTable *imported = rsesdictionary.TDImport(filename, 2);                     // Import the table.
			//Message::Reset();                                                             // RSES may try to "hold" the reset window indefinetely.
			if (imported == NULL) {                                                       // Import routine failed.
				Message::Error("RSES library failed to import decision table.");
				return NULL;
			}
			delete table->decisiontable_;                                                 // Delete the old embedded RSES table.
			table->decisiontable_ = imported;                                             // Replace the embedded RSES table with the new one.
		}
#endif
	}
	catch (Error &error) {                                                            // RSES library threw an exception.
		Message::RSESError("RSES library failed to import decision table.", error.GetMessage());
		return NULL;
	}

	// Create an empty dictionary.
	Handle<Dictionary> dictionary = Creator::Dictionary();

	int i, j;

	// Construct dictionary.
	try {
		if (ordinary) {                                                                 // Ordinary RSES format.
			for (i = 0; i < table->GetNoAttributes(false); i++) {                         // Assume all integer attributes.
				Handle<IntegerAttribute> attribute = Creator::IntegerAttribute();
				attribute->SetName("Attribute " + String::Format(i + 1));                   // Assign name.
				dictionary->AppendAttribute(attribute.GetPointer());                        // Append attribute to dictionary.
			}
		}
		else {                                                                          // Try RSES import format
			for (i = 0; i < table->GetNoAttributes(false); i++) {                         // Assume all string attributes.
				Handle<StringAttribute> attribute = Creator::StringAttribute();
				attribute->SetName(String(rsesdictionary.GetAttrName(i)));                  // Assign name.
				try {                                                                       // Analyze dictionary.  Use exception mechanism to end iteration
					j = 0;                                                                    //    since the RSES library supplies no way to.
					while (true) {                                                            // query the domain size (sic!).
						int   code = rsesdictionary.GetValueCode(i, j);                         // Get integral code.
						char *text = rsesdictionary.GetValueText(i, j);                         // Get corresponding real-world value.
						attribute->SetEntry(code, text);
						j++;
					}
				}
				catch (...) {                                                               // Were done analyzing this attribute, an out-of-range
				}                                                                           // exception was thrown.
				dictionary->AppendAttribute(attribute.GetPointer());                        // Append attribute to dictionary.
			}
		}
	}
	catch (Error &error) {                                                            // RSES library threw an exception.
		Message::RSESError("Failed to access RSES library.", error.GetMessage());
		return NULL;
	}

	Message::Warning("All attributes are treated as string attributes.");

	// Assign the dictionary to the table.
	table->SetDictionary(dictionary.GetPointer());

	// Set status fields.
	for (i = 0; i < table->GetNoAttributes(false); i++) {
		if (i == table->GetNoAttributes(false) - 1)
			table->SetStatus(i, DecisionTable::STATUS_DECISION, false);
		else
			table->SetStatus(i, DecisionTable::STATUS_CONDITION, false);
	}

	return table.Release();

}


⌨️ 快捷键说明

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