📄 decisiontable.cpp
字号:
//-------------------------------------------------------------------
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Revisions.....:
//===================================================================
#include <stdafx.h> // Precompiled headers.
#include <copyright.h>
#include <kernel/structures/decisiontable.h>
#include <kernel/structures/structures.h>
#include <kernel/structures/dictionary.h>
#include <kernel/structures/attribute.h>
#include <kernel/structures/floatattribute.h>
#include <kernel/structures/integerattribute.h>
#include <kernel/structures/indiscernibilitygraph.h>
#include <kernel/structures/generalizeddecision.h>
#include <kernel/utilities/discerner.h>
#include <kernel/utilities/iokit.h>
#include <kernel/utilities/mathkit.h>
#include <kernel/utilities/creator.h>
#include <kernel/system/fstream.h>
#include <kernel/basic/types.h>
#include <kernel/basic/message.h>
//-------------------------------------------------------------------
// Static methods (file scope).
//===================================================================
//-------------------------------------------------------------------
// Method........: StaticRescaleEntries
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
static bool
StaticRescaleEntries(Vector(float) &entries, const DecisionTable &table, int attribute_no, bool masked) {
if (!table.HasDictionary() || !table.IsFloat(attribute_no, masked))
return false;
// Get unmasked attribute index.
int unmasked = masked ? table.GetUnmaskedAttribute(attribute_no) : attribute_no;
// Get dictionary attribute.
Handle<Attribute> attribute = table.GetDictionary()->GetAttribute(unmasked);
if (attribute == NULL)
return false;
// Get scaling exponent.
int exponent = attribute->GetScalingExponent();
int i;
// Do the conversion, if needed.
if (exponent != 0) {
float factor = MathKit::Power(10.0, -exponent);
for (i = 0; i < entries.size(); i++)
entries[i] *= factor;
}
return true;
}
//-------------------------------------------------------------------
// Methods for class DecisionTable.
//===================================================================
//-------------------------------------------------------------------
// Constructors/destructor.
//===================================================================
//-------------------------------------------------------------------
// Method........: Copy constructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
DecisionTable::DecisionTable(const DecisionTable &in) : ParentStructure(in) {
// Duplicate dictionary.
if (in.dictionary_ == NULL)
dictionary_ = NULL;
else
dictionary_ = dynamic_cast(Dictionary *, in.dictionary_->Duplicate());
}
//-------------------------------------------------------------------
// Method........: Constructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: No dictionary as default.
// Revisions.....:
//===================================================================
DecisionTable::DecisionTable() {
}
//-------------------------------------------------------------------
// Method........: Destructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
DecisionTable::~DecisionTable() {
}
//-------------------------------------------------------------------
// Methods inherited from Identifier.
//===================================================================
IMPLEMENTIDMETHODS(DecisionTable, DECISIONTABLE, ParentStructure)
//-------------------------------------------------------------------
// Methods inherited from Persistent.
//===================================================================
//-------------------------------------------------------------------
// Method........: Load
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Cf. Save method comments.
// Revisions.....:
//===================================================================
bool
DecisionTable::Load(ifstream &stream) {
Message message;
// Load header and type identifier only, load the rest of the stuff higher up later.
if (!Persistent::Load(stream))
return false;
bool masked = false;
int no_objects;
int no_attributes;
// Load table dimensions.
if (!IOKit::Load(stream, no_objects) || !IOKit::Load(stream, no_attributes))
return false;
message.Notify("Table has " + String::Format(no_objects) + " objects and " + String::Format(no_attributes) + " attributes.");
Handle<Dictionary> dictionary = GetDictionary();
// The resizing method may indirectly alter the dictionary, which may already have been read OK.
SetDictionary(NULL);
// Adjust current dimensions.
if (!Resize(no_objects, no_attributes, masked))
return false;
SetDictionary(dictionary.GetPointer());
int i, j;
// Load table entries.
for (i = 0; i < no_objects; i++) {
for (j = 0; j < no_attributes; j++) {
int value;
if (!IOKit::Load(stream, value) || !SetEntry(i, j, value, masked))
return false;
}
}
// Load all attribute status fields.
for (i = 0; i < no_attributes; i++) {
int status;
if (!IOKit::Load(stream, status))
return false;
if (!SetStatus(i, static_cast(DecisionTable::Status, status), masked))
return false;
}
// Load all attribute masks.
for (i = 0; i < no_attributes; i++) {
int mask;
if (!IOKit::Load(stream, mask))
return false;
if (!SetAttributeMask(i, static_cast(DecisionTable::Mask, mask)))
return false;
}
// Load all object masks.
for (i = 0; i < no_objects; i++) {
int mask;
if (!IOKit::Load(stream, mask))
return false;
if (!SetObjectMask(i, static_cast(DecisionTable::Mask, mask)))
return false;
}
int has_dictionary;
// Is there a dictionary associated with the dec. table?
if (!IOKit::Load(stream, has_dictionary))
return false;
// Create a dictionary if needed. Not having a dictionary is OK.
if (has_dictionary)
SetDictionary(Creator::Dictionary());
else
SetDictionary(NULL);
// Load dictionary.
if (HasDictionary() && !GetDictionary()->Load(stream))
return false;
// Load stuff higher up in the hierarchy.
if (!ParentStructure::Load(stream))
return false;
return true;
}
//-------------------------------------------------------------------
// Method........: Save
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Table data has to be loaded before the children,
// since loading table data alters the table, and table
// altering is only allowed if there are no children.
// Revisions.....:
//===================================================================
bool
DecisionTable::Save(ofstream &stream) const {
Message message;
// Save header and type identifier only, save the rest of the stuff higher up later.
if (!Persistent::Save(stream))
return false;
bool masked = false;
// Get table dimensions.
int no_objects = GetNoObjects(masked);
int no_attributes = GetNoAttributes(masked);
message.Notify("Table has " + String::Format(no_objects) + " objects and " + String::Format(no_attributes) + " attributes.");
// Save table dimensions.
if (!IOKit::Save(stream, no_objects))
return false;
if (!IOKit::Save(stream, no_attributes))
return false;
if (!IOKit::Save(stream, '\n'))
return false;
int i, j;
// Save table entries.
for (i = 0; i < no_objects; i++) {
for (j = 0; j < no_attributes; j++) {
if (!IOKit::Save(stream, GetEntry(i, j, masked)))
return false;
}
if (!IOKit::Save(stream, '\n'))
return false;
}
// Save all attribute status fields.
for (i = 0; i < no_attributes; i++) {
if (!IOKit::Save(stream, GetStatus(i, masked)))
return false;
}
if (!IOKit::Save(stream, '\n'))
return false;
// Save all attribute masks.
for (i = 0; i < no_attributes; i++) {
if (!IOKit::Save(stream, GetAttributeMask(i)))
return false;
}
if (!IOKit::Save(stream, '\n'))
return false;
// Save all object masks.
for (i = 0; i < no_objects; i++) {
if (!IOKit::Save(stream, GetObjectMask(i)))
return false;
}
if (!IOKit::Save(stream, '\n'))
return false;
// Does the decision table have an associated dictionary?
if (!IOKit::Save(stream, static_cast(int, HasDictionary())))
return false;
if (!IOKit::Save(stream, '\n'))
return false;
// Not having a dictionary is OK.
if (HasDictionary() && !GetDictionary()->Save(stream))
return false;
// Save stuff higher up in the hierarchy. Children can be safely loaded now
// that the decision table has been loaded.
if (!ParentStructure::Save(stream))
return false;
return true;
}
//-------------------------------------------------------------------
// Methods inherited from Structure.
//===================================================================
//-------------------------------------------------------------------
// Method........: Clear
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
void
DecisionTable::Clear() {
// Do stuff higher up.
ParentStructure::Clear();
// Resize table.
Resize(0, 0, false);
}
//-------------------------------------------------------------------
// Dimensionality methods.
//===================================================================
//-------------------------------------------------------------------
// Method........: Reserve
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Reserves space, if relevant for the physical
// implementation, for a decision table of the
// specified size.
//
// Comments......: Since this is an abstract base class, must be
// overloaded. Default implementation does nothing.
// Revisions.....:
//===================================================================
bool
DecisionTable::Reserve(int /*no_objects*/, int /*no_attributes*/) {
return true;
}
//-------------------------------------------------------------------
// Method........: Resize
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Resizes the current decision table.
// Comments......:
// Revisions.....:
//===================================================================
bool
DecisionTable::Resize(int no_objects, int no_attributes, bool masked) {
int i;
// Negative dimensions are illegal.
if ((no_objects < 0) || (no_attributes < 0))
return false;
if (!Reserve(no_objects, no_attributes))
return false;
// Get the current dimensions.
int current_no_objects = GetNoObjects(masked);
int current_no_attributes = GetNoAttributes(masked);
// Adjust the number of objects if needed.
if (current_no_objects > no_objects) {
for (i = 0; i < (current_no_objects - no_objects); i++) {
if (!RemoveObject(current_no_objects - 1 - i, masked))
return false;
}
}
else if (current_no_objects < no_objects) {
for (i = 0; i < (no_objects - current_no_objects); i++) {
if (!AppendObject(masked))
return false;
}
}
// Adjust the number of attributes if needed.
if (current_no_attributes > no_attributes) {
for (i = 0; i < (current_no_attributes - no_attributes); i++) {
if (!RemoveAttribute(current_no_attributes - 1 - i, masked))
return false;
}
}
else if (current_no_attributes < no_attributes) {
for (i = 0; i < (no_attributes - current_no_attributes); i++) {
if (!AppendAttribute(NULL, masked))
return false;
}
}
return true;
}
//-------------------------------------------------------------------
// Access methods, mapped though the dictionary.
//===================================================================
//-------------------------------------------------------------------
// Method........: GetEntry
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Returns a requested table entry, mapped (one way)
// through the associated dictionary if specified.
// Comments......:
// Revisions.....:
//===================================================================
const String &
DecisionTable::GetEntry(int object_no, int attribute_no, bool use_dictionary, bool masked) const {
// Static so we can return a reference.
static String dummy;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -