?? rsesreducts.cpp
字號(hào):
// Save the children recursively.
for (i = 0; i < GetNoChildren(); i++) {
Handle<Structure> child = GetChild(i);
String type = IdHolder::GetClassname(child->GetId());
// Save the type description of the child (for later reconstruction).
if (!IOKit::Save(stream, type))
return false;
if (!IOKit::Save(stream, '\n'))
return false;
// Save the child.
if (!child->Save(stream))
return false;
}
return true;
}
//-------------------------------------------------------------------
// Methods inherited from Structure.
//===================================================================
//-------------------------------------------------------------------
// Method........: Duplicate
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
Structure *
RSESReducts::Duplicate() const {
return new RSESReducts(*this);
}
//-------------------------------------------------------------------
// Method........: Clear
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
void
RSESReducts::Clear() {
#ifdef _DEBUG
// Is the RSES object instantiated?
if (reducts_ == NULL) {
Message::Error("Embedded RSES object not instantiated.");
return;
}
#endif
// Clear stuff higher up in the hierarchy.
Structures::Clear();
// Clear RSES object.
try {
reducts_->Clear();
}
catch (Error &error) {
Message::RSESError("Failed to clear embedded RSES object.", error.GetMessage());
}
}
//-------------------------------------------------------------------
// Methods inherited from ParentStructure.
//===================================================================
//-------------------------------------------------------------------
// Method........: InsertChild
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Overloaded to ensure consistency. A set of RSES
// reducts can only have one set of RSES rules as a
// child. This because the RSES library couples the
// two so tightly via the TRedRulMem structure.
// Comments......:
// Revisions.....:
//===================================================================
bool
RSESReducts::InsertChild(Structure *child, int i) {
if (child == NULL) {
Message::Error("Cannot insert a NULL structure as a child.");
return false;
}
// If the structure's already a member of the child set, then do nothing.
if (IsParent(child, true))
return true;
if (child->IsA(RSESRULES)) {
if (HasChild(RSESRULES, false)) {
Message::Error("The RSES library only allows one RSES rule set child.");
return false;
}
}
return ParentStructure::InsertChild(child, i);
}
//-------------------------------------------------------------------
// Method........: AppendChild
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Cf. description of the InsertChild method.
// Comments......:
// Revisions.....:
//===================================================================
bool
RSESReducts::AppendChild(Structure *child) {
return InsertChild(child, GetNoChildren());
}
//-------------------------------------------------------------------
// Methods inherited from Structures.
//===================================================================
//-------------------------------------------------------------------
// Method........: InsertStructure
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
RSESReducts::InsertStructure(Structure *structure, int i) {
// Is the insertion really an append operation?
if (i == GetNoStructures())
return AppendStructure(structure);
// Give an error message.
Message::Error("The RSES library does currently not support general reduct insertion.");
return false;
}
//-------------------------------------------------------------------
// Method........: AppendStructure
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
RSESReducts::AppendStructure(Structure *structure) {
#ifdef _DEBUG
// Is the RSES object instantiated?
if (reducts_ == NULL) {
Message::Error("Embedded RSES object not instantiated.");
return false;
}
#endif
// Is the object valid?
if (structure == NULL) {
Message::Error("Cannot append a NULL reduct.");
return false;
}
// Is the object of correct type?
if (!structure->IsA(RSESREDUCT)) {
Message::Error("Only RSES reducts can be appended to an RSES reduct set.");
return false;
}
// Cast to correct type.
RSESReduct *reduct = dynamic_cast(RSESReduct *, structure);
// Check if embedded reduct already exists...?
// ...
// Add the embedded RSES reduct to the embedded reduct and rule memory.
try {
reducts_->AddRed(reduct->reduct_);
}
catch (Error &error) {
Message::RSESError("Failed to add the embedded RSES reduct to the embedded reduct and rule memory.", error.GetMessage());
return false;
}
// The embedded reduct and rule memory takes ownership of the embedded reduct.
reduct->is_owner_ = false;
// Add the wrapper structure to the (wrapper) structure container, avoiding recursion.
return Structures::InsertStructure(structure, Structures::GetNoStructures());
}
//-------------------------------------------------------------------
// Method........: RemoveStructure
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Any rules attached to the RSES library
// are thus implicitly removed from the rule set
// child, it such exists. However, the rule set
// wrapper update is currently quite inefficiently
// implemented... Therefore, call this method
// judiciously.
// Revisions.....:
//===================================================================
bool
RSESReducts::RemoveStructure(int i) {
int no_reducts = GetNoReducts();
#ifdef _DEBUG
// Is the RSES object instantiated?
if (reducts_ == NULL) {
Message::Error("Embedded RSES object not instantiated.");
return false;
}
// Is the index in range?
if ((i < 0) || (i >= no_reducts)) {
Message::Error("Reduct index out of range.");
return false;
}
#endif
// Get the wrapper of the reduct to remove.
Handle<RSESReduct> wrapper = dynamic_cast(RSESReduct *, GetStructure(i));
if (wrapper == NULL || wrapper->reduct_ == NULL) {
Message::Error("Failed to access embedded RSES reduct.");
return false;
}
int j, index = Undefined::Integer();
// Locate index into the TRedRulMem object, if present.
try {
if (reducts_->GetRed(i) == wrapper->reduct_) { // Is there a one-to-one correspondence?
index = i;
}
else { // Search through all embedded reducts.
for (j = no_reducts - 1; j >= 0; j--) {
if (reducts_->GetRed(j) == wrapper->reduct_) {
index = j;
break;
}
}
}
if (index == Undefined::Integer()) {
Message::Error("Index into embedded RSES reduct and rule memory not found.");
return false;
}
}
// Catch any exceptions thrown by the RSES library.
catch (Error &error) {
Message::RSESError("Failed to remove reduct from the embedded RSES reduct and rule memory.", error.GetMessage());
return false;
}
int no_children = GetNoChildren();
// Remove wrappers of rules in RSES rule set children.
for (j = 0; j < no_children; j++) {
// Get current child.
Handle<Structure> child = GetChild(j);
// If the child is not a RSES rule set, continue to the next child.
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 that originate from the specified TReduct object.
if (!rules->RemoveAllRules(*(wrapper->reduct_), index)) {
Message::Error("Failed to remove derived rules with specified RSES reduct as parent.");
return false;
}
}
// Remove the reduct from the embedded RSES reduct and rule memory.
try {
reducts_->RemoveReduct(index);
}
// Catch any exceptions thrown by the RSES library.
catch (Error &error) {
Message::RSESError("Failed to remove reduct from the embedded RSES reduct and rule memory.", error.GetMessage());
return false;
}
// Remove wrapper structure.
if (!Structures::RemoveStructure(i)) {
Message::Error("Failed to remove wrapper structure.");
return false;
}
return true;
}
//-------------------------------------------------------------------
// Method........: RemoveAllStructures
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Implicitly also removes all derived RSES rule
// objects.
// Revisions.....:
//===================================================================
bool
RSESReducts::RemoveAllStructures() {
#ifdef _DEBUG
// Is the RSES object instantiated?
if (reducts_ == NULL) {
Message::Error("Embedded RSES object not instantiated.");
return false;
}
#endif
// Remove all wrappers.
if (!Structures::RemoveAllStructures()) {
Message::Error("Failed to remove all wrapper structures.");
return false;
}
// Clear embedded RSES object for all reducts and rules.
try {
reducts_->Clear();
}
catch (Error &error) {
Message::RSESError("Failed to clear embedded RSES object.", error.GetMessage());
return false;
}
int j, no_children = GetNoChildren();
// Deal with RSES rule set children.
for (j = 0; j < no_children; j++) {
// Get current child.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -