comparison src/Support/ModuleTree.cc @ 195:0db637cac96b

- Added as-yet-unfinished support for a proper configuraiton file format - Added a couple of pythin scripts to generate HMM configuration files - Variable name changes and other cosmetic things - Added the option for the noise generation to do pink noise (untested)
author tomwalters
date Thu, 12 Aug 2010 11:28:11 +0000
parents 3cdaa81c3aca
children 503b6f40d0aa
comparison
equal deleted inserted replaced
194:578ec2d32170 195:0db637cac96b
22 /*! \author: Thomas Walters <tom@acousticscale.org> 22 /*! \author: Thomas Walters <tom@acousticscale.org>
23 * \date 2010/08/08 23 * \date 2010/08/08
24 * \version \$Id: $ 24 * \version \$Id: $
25 */ 25 */
26 26
27 #include "Support/ModuleFactory.h"
28 #include "Support/Module.h"
29 #include "Support/ModuleTree.h"
30
31 namespace aimc { 27 namespace aimc {
32 using std::endl; 28 ParseConfigFile(string )
33 ModuleTree::ModuleTree() : root_module_(NULL),
34 initialized_(false) {
35 29
36 }
37
38 bool ModuleTree::LoadConfigFile(const string &filename) {
39 config_.Load(filename.c_str());
40 return ConstructTree();
41 }
42
43 bool ModuleTree::LoadConfigText(const string &config) {
44 config_.Parse(config.c_str());
45 return ConstructTree();
46 }
47
48 bool ModuleTree::ConstructTree() {
49 // Make two passes over the configuration file.
50 // The first pass creates all the named modules with their parameters.
51 bool done = false;
52 bool error = false;
53 int module_number = 1;
54 LOG_INFO("Parsing tree...");
55 while (!done) {
56 char module_name_var[Parameters::MaxParamNameLength];
57 char module_id_var[Parameters::MaxParamNameLength];
58 char module_parameters_var[Parameters::MaxParamNameLength];
59 sprintf(module_name_var, "module%d.name", module_number);
60 sprintf(module_id_var, "module%d.id", module_number);
61 sprintf(module_parameters_var, "module%d.parameters", module_number);
62 if (config_.IsSet(module_name_var)) {
63 string module_name(config_.GetString(module_name_var));
64 LOG_INFO("Module number %d, name %s", module_number, module_name.c_str());
65 if (config_.IsSet(module_id_var)) {
66 const char* id = config_.GetString(module_id_var);
67 const char* parameters = config_.DefaultString(module_parameters_var, "");
68 linked_ptr<Parameters> params(new Parameters);
69 parameters_[module_name] = params;
70 parameters_[module_name]->Parse(parameters);
71 linked_ptr<Module> module(ModuleFactory::Create(id, parameters_[module_name].get()));
72 if (module.get() != NULL) {
73 module->set_instance_name(module_name);
74 modules_[module_name] = module;
75 } else {
76 LOG_ERROR("Module name: %s of type %s not created", module_name.c_str(), id);
77 done = true;
78 error = true;
79 }
80 if (module_number == 1) {
81 root_module_ = module.get();
82 }
83 } else {
84 LOG_ERROR("id field missing for module named %s", module_name.c_str());
85 error = true;
86 done = true;
87 }
88 } else {
89 done = true;
90 }
91 ++module_number;
92 }
93 // The second pass connects up all the modules into a tree.
94 char module_child_var[Parameters::MaxParamNameLength];
95 char module_name_var[Parameters::MaxParamNameLength];
96 LOG_INFO("A total of %d modules", modules_.size());
97 for (unsigned int i = 1; i < modules_.size() + 1; ++i) {
98 int child_number = 1;
99 done = false;
100 sprintf(module_name_var, "module%d.name", i);
101 if (config_.IsSet(module_name_var)) {
102 string module_name(config_.GetString(module_name_var));
103 while (!done) {
104 sprintf(module_child_var, "module%d.child%d", i, child_number);
105 LOG_INFO("Trying %s", module_child_var);
106 if (config_.IsSet(module_child_var)) {
107 string child(config_.GetString(module_child_var));
108 if ((modules_.find(module_name) != modules_.end())
109 && (modules_.find(child) != modules_.end())) {
110 modules_[module_name]->AddTarget(modules_[child].get());
111 } else {
112 LOG_ERROR("Module name not found");
113 }
114 } else {
115 done = true;
116 }
117 ++child_number;
118 }
119 } else {
120 LOG_ERROR("field missing for entry %s", module_name_var);
121 error = true;
122 break;
123 }
124 }
125 return !error;
126 }
127
128 bool ModuleTree::Initialize(Parameters *global_parameters) {
129 if (root_module_ == NULL) {
130 return false;
131 }
132 // Dummy signal bank for the root module.
133 s_.Initialize(1, 1, 1);
134 initialized_ = root_module_->Initialize(s_, global_parameters);
135 return initialized_;
136 }
137
138 void ModuleTree::Reset() {
139 if (root_module_ == NULL) {
140 return;
141 }
142 root_module_->Reset();
143 }
144
145 void ModuleTree::PrintConfiguration(ostream &out) {
146 if (root_module_ == NULL) {
147 return;
148 }
149 root_module_->PrintTargets(out);
150 root_module_->PrintConfiguration(out);
151 }
152
153 void ModuleTree::Process() {
154 if (root_module_ == NULL) {
155 return;
156 }
157 if (!initialized_) {
158 LOG_ERROR(_T("Module tree not initialized."));
159 return;
160 }
161 while (!root_module_->done()) {
162 root_module_->Process(s_);
163 }
164 }
165
166 void ModuleTree::MakeDotGraph(ostream &out) {
167 if (root_module_ == NULL) {
168 return;
169 }
170 out << "digraph G {" << endl;
171 root_module_->PrintTargetsForDot(out);
172 out << "}" << endl;
173 }
174
175 } // namespace aimc 30 } // namespace aimc