annotate src/Support/ModuleTree.cc @ 169:78cbca1cf218

- AWS fixes (part 1)
author tomwalters
date Wed, 11 Aug 2010 08:55:29 +0000
parents 3cdaa81c3aca
children 0db637cac96b
rev   line source
tomwalters@84 1 // Copyright 2010, Thomas Walters
tomwalters@84 2 //
tomwalters@84 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@84 4 // http://www.acousticscale.org/AIMC
tomwalters@84 5 //
tomwalters@84 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@84 7 // you may not use this file except in compliance with the License.
tomwalters@84 8 // You may obtain a copy of the License at
tomwalters@84 9 //
tomwalters@84 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@84 11 //
tomwalters@84 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@84 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@84 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@84 15 // See the License for the specific language governing permissions and
tomwalters@84 16 // limitations under the License.
tomwalters@84 17
tomwalters@84 18 /*! \file
tomwalters@84 19 * \brief Parse a configuration file to generate a tree of modules.
tomwalters@84 20 */
tomwalters@84 21
tomwalters@84 22 /*! \author: Thomas Walters <tom@acousticscale.org>
tomwalters@84 23 * \date 2010/08/08
tomwalters@84 24 * \version \$Id: $
tomwalters@84 25 */
tomwalters@84 26
tomwalters@121 27 #include "Support/ModuleFactory.h"
tomwalters@121 28 #include "Support/Module.h"
tomwalters@114 29 #include "Support/ModuleTree.h"
tomwalters@114 30
tomwalters@84 31 namespace aimc {
tomwalters@121 32 using std::endl;
tomwalters@121 33 ModuleTree::ModuleTree() : root_module_(NULL),
tomwalters@121 34 initialized_(false) {
tomwalters@121 35
tomwalters@121 36 }
tomwalters@121 37
tomwalters@120 38 bool ModuleTree::LoadConfigFile(const string &filename) {
tomwalters@120 39 config_.Load(filename.c_str());
tomwalters@114 40 return ConstructTree();
tomwalters@114 41 }
tomwalters@114 42
tomwalters@120 43 bool ModuleTree::LoadConfigText(const string &config) {
tomwalters@120 44 config_.Parse(config.c_str());
tomwalters@114 45 return ConstructTree();
tomwalters@114 46 }
tomwalters@114 47
tomwalters@120 48 bool ModuleTree::ConstructTree() {
tomwalters@114 49 // Make two passes over the configuration file.
tomwalters@114 50 // The first pass creates all the named modules with their parameters.
tomwalters@114 51 bool done = false;
tomwalters@114 52 bool error = false;
tomwalters@114 53 int module_number = 1;
tomwalters@121 54 LOG_INFO("Parsing tree...");
tomwalters@121 55 while (!done) {
tomwalters@121 56 char module_name_var[Parameters::MaxParamNameLength];
tomwalters@121 57 char module_id_var[Parameters::MaxParamNameLength];
tomwalters@121 58 char module_parameters_var[Parameters::MaxParamNameLength];
tomwalters@120 59 sprintf(module_name_var, "module%d.name", module_number);
tomwalters@120 60 sprintf(module_id_var, "module%d.id", module_number);
tomwalters@120 61 sprintf(module_parameters_var, "module%d.parameters", module_number);
tomwalters@121 62 if (config_.IsSet(module_name_var)) {
tomwalters@121 63 string module_name(config_.GetString(module_name_var));
tomwalters@121 64 LOG_INFO("Module number %d, name %s", module_number, module_name.c_str());
tomwalters@120 65 if (config_.IsSet(module_id_var)) {
tomwalters@121 66 const char* id = config_.GetString(module_id_var);
tomwalters@121 67 const char* parameters = config_.DefaultString(module_parameters_var, "");
tomwalters@121 68 linked_ptr<Parameters> params(new Parameters);
tomwalters@121 69 parameters_[module_name] = params;
tomwalters@121 70 parameters_[module_name]->Parse(parameters);
tomwalters@121 71 linked_ptr<Module> module(ModuleFactory::Create(id, parameters_[module_name].get()));
tomwalters@121 72 if (module.get() != NULL) {
tomwalters@121 73 module->set_instance_name(module_name);
tomwalters@121 74 modules_[module_name] = module;
tomwalters@121 75 } else {
tomwalters@121 76 LOG_ERROR("Module name: %s of type %s not created", module_name.c_str(), id);
tomwalters@121 77 done = true;
tomwalters@121 78 error = true;
tomwalters@121 79 }
tomwalters@121 80 if (module_number == 1) {
tomwalters@121 81 root_module_ = module.get();
tomwalters@121 82 }
tomwalters@120 83 } else {
tomwalters@121 84 LOG_ERROR("id field missing for module named %s", module_name.c_str());
tomwalters@120 85 error = true;
tomwalters@120 86 done = true;
tomwalters@120 87 }
tomwalters@115 88 } else {
tomwalters@115 89 done = true;
tomwalters@115 90 }
tomwalters@115 91 ++module_number;
tomwalters@115 92 }
tomwalters@115 93 // The second pass connects up all the modules into a tree.
tomwalters@121 94 char module_child_var[Parameters::MaxParamNameLength];
tomwalters@121 95 char module_name_var[Parameters::MaxParamNameLength];
tomwalters@121 96 LOG_INFO("A total of %d modules", modules_.size());
tomwalters@121 97 for (unsigned int i = 1; i < modules_.size() + 1; ++i) {
tomwalters@120 98 int child_number = 1;
tomwalters@120 99 done = false;
tomwalters@121 100 sprintf(module_name_var, "module%d.name", i);
tomwalters@121 101 if (config_.IsSet(module_name_var)) {
tomwalters@121 102 string module_name(config_.GetString(module_name_var));
tomwalters@120 103 while (!done) {
tomwalters@120 104 sprintf(module_child_var, "module%d.child%d", i, child_number);
tomwalters@121 105 LOG_INFO("Trying %s", module_child_var);
tomwalters@121 106 if (config_.IsSet(module_child_var)) {
tomwalters@121 107 string child(config_.GetString(module_child_var));
tomwalters@121 108 if ((modules_.find(module_name) != modules_.end())
tomwalters@121 109 && (modules_.find(child) != modules_.end())) {
tomwalters@121 110 modules_[module_name]->AddTarget(modules_[child].get());
tomwalters@121 111 } else {
tomwalters@121 112 LOG_ERROR("Module name not found");
tomwalters@121 113 }
tomwalters@120 114 } else {
tomwalters@120 115 done = true;
tomwalters@120 116 }
tomwalters@121 117 ++child_number;
tomwalters@120 118 }
tomwalters@114 119 } else {
tomwalters@121 120 LOG_ERROR("field missing for entry %s", module_name_var);
tomwalters@120 121 error = true;
tomwalters@120 122 break;
tomwalters@114 123 }
tomwalters@114 124 }
tomwalters@121 125 return !error;
tomwalters@114 126 }
tomwalters@120 127
tomwalters@120 128 bool ModuleTree::Initialize(Parameters *global_parameters) {
tomwalters@121 129 if (root_module_ == NULL) {
tomwalters@121 130 return false;
tomwalters@121 131 }
tomwalters@121 132 // Dummy signal bank for the root module.
tomwalters@121 133 s_.Initialize(1, 1, 1);
tomwalters@121 134 initialized_ = root_module_->Initialize(s_, global_parameters);
tomwalters@121 135 return initialized_;
tomwalters@121 136 }
tomwalters@121 137
tomwalters@121 138 void ModuleTree::Reset() {
tomwalters@121 139 if (root_module_ == NULL) {
tomwalters@121 140 return;
tomwalters@121 141 }
tomwalters@121 142 root_module_->Reset();
tomwalters@121 143 }
tomwalters@121 144
tomwalters@121 145 void ModuleTree::PrintConfiguration(ostream &out) {
tomwalters@121 146 if (root_module_ == NULL) {
tomwalters@121 147 return;
tomwalters@121 148 }
tomwalters@121 149 root_module_->PrintTargets(out);
tomwalters@121 150 root_module_->PrintConfiguration(out);
tomwalters@121 151 }
tomwalters@121 152
tomwalters@121 153 void ModuleTree::Process() {
tomwalters@121 154 if (root_module_ == NULL) {
tomwalters@121 155 return;
tomwalters@121 156 }
tomwalters@121 157 if (!initialized_) {
tomwalters@121 158 LOG_ERROR(_T("Module tree not initialized."));
tomwalters@121 159 return;
tomwalters@121 160 }
tomwalters@121 161 while (!root_module_->done()) {
tomwalters@121 162 root_module_->Process(s_);
tomwalters@121 163 }
tomwalters@121 164 }
tomwalters@121 165
tomwalters@121 166 void ModuleTree::MakeDotGraph(ostream &out) {
tomwalters@121 167 if (root_module_ == NULL) {
tomwalters@121 168 return;
tomwalters@121 169 }
tomwalters@121 170 out << "digraph G {" << endl;
tomwalters@121 171 root_module_->PrintTargetsForDot(out);
tomwalters@121 172 out << "}" << endl;
tomwalters@120 173 }
tomwalters@120 174
tomwalters@115 175 } // namespace aimc