?? cppruleexporter.cpp
字號:
//-------------------------------------------------------------------
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Revisions.....:
//===================================================================
#include <stdafx.h> // Precompiled headers.
#include <copyright.h>
#include <kernel/algorithms/cppruleexporter.h>
#include <kernel/structures/rules.h>
#include <kernel/structures/rule.h>
#include <kernel/utilities/systemkit.h>
#include <kernel/system/fstream.h>
#include <kernel/basic/message.h>
//-------------------------------------------------------------------
// Static helpers.
//===================================================================
//-------------------------------------------------------------------
// Method........: StaticGetName
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
static String
StaticGetName(const DecisionTable &table, int attribute_no, bool masked) {
String name = table.GetAttributeName(attribute_no, masked);
name.Replace(' ', '_');
name.Capitalize();
return name;
}
//-------------------------------------------------------------------
// Methods for class CPPRuleExporter.
//===================================================================
//-------------------------------------------------------------------
// Constructors/destructor.
//===================================================================
CPPRuleExporter::CPPRuleExporter() {
}
CPPRuleExporter::~CPPRuleExporter() {
}
//-------------------------------------------------------------------
// Methods inherited from Identifier.
//===================================================================
IMPLEMENTIDMETHODS(CPPRuleExporter, CPPRULEEXPORTER, RuleExporter)
//-------------------------------------------------------------------
// Methods inherited from Exporter.
//===================================================================
//-------------------------------------------------------------------
// Method........: ExportPrologue
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Exports the header (.h) file.
// Comments......:
// Revisions.....:
//===================================================================
bool
CPPRuleExporter::ExportPrologue(ofstream &stream, const Structure &structure) const {
int i;
// Cast to already verified type (in Apply method).
Handle<Rules> rules = dynamic_cast(Rules *, const_cast(Structure *, &structure));
// Get originating decision table.
Handle<DecisionTable> table = dynamic_cast(DecisionTable *, rules->FindParent(DECISIONTABLE));
if (table == NULL) {
Message::Error("Could not trace back to the rules' originating decision table for a dictionary.");
return false;
}
bool masked = true;
int no_attributes = table->GetNoAttributes(masked);
// Write marker.
stream << "//------------------------------------------------------------------" << endl;
stream << "// The header (.h) file starts here." << endl;
stream << "//==================================================================" << endl;
stream << endl;
// Write some general information.
stream << "// Classifier generated by ROSETTA." << endl;
stream << "// Exported " << SystemKit::GetTimestamp() << " by " << SystemKit::GetUser() << "." << endl;
stream << "//" << endl;
stream << "// " << rules->GetName() << endl;
stream << "// " << rules->GetNoRules() << " rules." << endl;
stream << endl;
// Add #ifndef section.
stream << "#ifndef __CLASSIFIER_EXPORTED_FROM_ROSETTA_H__" << endl;
stream << "#define __CLASSIFIER_EXPORTED_FROM_ROSETTA_H__" << endl;
stream << endl;
// Define the ROSETTAObject class.
stream << "//------------------------------------------------------------------" << endl;
stream << "// Class.........: ROSETTAObject" << endl;
stream << "// Author........: Aleksander 豩rn" << endl;
stream << "// Date..........: " << SystemKit::GetTimestamp() << endl;
stream << "// Description...: An object input to a ROSETTAClassifier." << endl;
stream << "//" << endl;
stream << "// For a legend to the encoding of slots for the" << endl;
stream << "// symbolic attributes, see the Lookup methods." << endl;
stream << "//==================================================================" << endl;
stream << endl;
stream << "class ROSETTAObject {" << endl;
stream << "public:" << endl;
stream << endl;
stream << " // Variable slots................................................." << endl;
for (i = 0; i < no_attributes; i++) {
if (!table->IsCondition(i, masked))
continue;
// Variable type.
if (table->IsFloat(i, masked))
stream << " float ";
else
stream << " int ";
String name = StaticGetName(*table, i, masked);
// Variable name.
stream << name << ';';
stream << endl;
}
stream << endl;
stream << " // Dictionary lookup methods......................................" << endl;
// Method names.
for (i = 0; i < no_attributes; i++) {
if (table->IsCondition(i, masked) && table->IsSymbolic(i, masked))
stream << " static int Lookup" << StaticGetName(*table, i, masked) << "(const char *text);" << endl;
}
stream << endl;
stream << "};" << endl;
stream << endl;
// Determine index of decision attribute.
int decision_attribute = table->GetDecisionAttribute(masked);
if (decision_attribute == Undefined::Integer()) {
Message::Error("No decision attribute detected in parent table.");
return false;
}
Vector(int) decisionvalues;
Vector(int) cardinalities;
// Determine set of possible decision values.
if (!table->GetValueSet(decisionvalues, cardinalities, decision_attribute, masked)) {
Message::Error("Failed to get set of possible decision values from parent table.");
return false;
}
// Define the ROSETTAClassifier class.
stream << "//------------------------------------------------------------------" << endl;
stream << "// Class.........: ROSETTAClassifier" << endl;
stream << "// Author........: Aleksander 豩rn" << endl;
stream << "// Date..........: " << SystemKit::GetTimestamp() << endl;
stream << "// Description...: Classifies a ROSETTAObject using a collection of" << endl;
stream << "// if-then rules." << endl;
stream << "//" << endl;
stream << "// The vote count associated with each decision value" << endl;
stream << "// is returned in-place in an array. To map from array" << endl;
stream << "// indices to decision values, see the Classify method." << endl;
stream << "//" << endl;
stream << "// The descriptor matching methods can be altered to" << endl;
stream << "// allow for tolerant matching." << endl;
stream << "//==================================================================" << endl;
stream << endl;
stream << "class ROSETTAClassifier {" << endl;
stream << "protected:" << endl;
stream << endl;
stream << " // Descriptor matching methods...................................." << endl;
for (i = 0; i < no_attributes; i++) {
if (!table->IsCondition(i, masked))
continue;
String name = StaticGetName(*table, i, masked);
// Method name.
stream << " bool Match" << name << "(";
int indent = 15;
// Method arguments and inlined body.
if (table->IsFloat(i, masked)) {
stream << "float value1, float value2";
indent -= 4;
}
else
stream << "int value1, int value2";
stream << ") const " << String(' ', indent - name.GetLength()) << "{return (value1 == value2);}" << endl;
}
stream << endl;
stream << "public:" << endl;
stream << endl;
stream << " // Classification method.........................................." << endl;
stream << " int Classify(const ROSETTAObject &object, int votes[" << decisionvalues.size() << "]) const;" << endl;
stream << endl;
stream << "};" << endl;
// Match the #ifndef section.
stream << endl;
stream << "#endif" << endl;
stream << endl;
// Write marker.
stream << "//------------------------------------------------------------------" << endl;
stream << "// The header (.h) file ends here." << endl;
stream << "//==================================================================" << endl;
stream << endl;
return true;
}
//-------------------------------------------------------------------
// Method........: ExportData
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Exports the implementation (.cpp) file.
// Comments......:
// Revisions.....:
//===================================================================
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -