annotate src/Support/ModuleTree.cc @ 611:0fbaf443ec82

Carfac C++ revision 3, indluding more style improvements. The output structs are now classes again, and have separate storage methods for each output structure along with flags in the Run and RunSegment methods to allow for only storing NAPs if desired.
author alexbrandmeyer
date Fri, 17 May 2013 19:52:45 +0000
parents af531fc3f280
children
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@232 27 #include "Support/ModuleFactory.h"
tomwalters@232 28 #include "Support/Module.h"
tomwalters@225 29 #include "Support/ModuleTree.h"
tomwalters@225 30
tomwalters@84 31 namespace aimc {
tomwalters@232 32 using std::endl;
tomwalters@232 33 ModuleTree::ModuleTree() : root_module_(NULL),
tomwalters@232 34 initialized_(false) {
tomwalters@232 35
tomwalters@232 36 }
tomwalters@232 37
tomwalters@231 38 bool ModuleTree::LoadConfigFile(const string &filename) {
tomwalters@231 39 config_.Load(filename.c_str());
tomwalters@225 40 return ConstructTree();
tomwalters@225 41 }
tomwalters@225 42
tomwalters@231 43 bool ModuleTree::LoadConfigText(const string &config) {
tomwalters@231 44 config_.Parse(config.c_str());
tomwalters@225 45 return ConstructTree();
tomwalters@225 46 }
tomwalters@225 47
tomwalters@231 48 bool ModuleTree::ConstructTree() {
tomwalters@225 49 // Make two passes over the configuration file.
tomwalters@225 50 // The first pass creates all the named modules with their parameters.
tomwalters@225 51 bool done = false;
tomwalters@225 52 bool error = false;
tomwalters@225 53 int module_number = 1;
tomwalters@232 54 LOG_INFO("Parsing tree...");
tomwalters@232 55 while (!done) {
tomwalters@232 56 char module_name_var[Parameters::MaxParamNameLength];
tomwalters@232 57 char module_id_var[Parameters::MaxParamNameLength];
tomwalters@232 58 char module_parameters_var[Parameters::MaxParamNameLength];
tomwalters@231 59 sprintf(module_name_var, "module%d.name", module_number);
tomwalters@231 60 sprintf(module_id_var, "module%d.id", module_number);
tomwalters@231 61 sprintf(module_parameters_var, "module%d.parameters", module_number);
tomwalters@232 62 if (config_.IsSet(module_name_var)) {
tomwalters@232 63 string module_name(config_.GetString(module_name_var));
tomwalters@232 64 LOG_INFO("Module number %d, name %s", module_number, module_name.c_str());
tomwalters@231 65 if (config_.IsSet(module_id_var)) {
tomwalters@232 66 const char* id = config_.GetString(module_id_var);
tomwalters@232 67 const char* parameters = config_.DefaultString(module_parameters_var, "");
tomwalters@232 68 linked_ptr<Parameters> params(new Parameters);
tomwalters@232 69 parameters_[module_name] = params;
tomwalters@232 70 parameters_[module_name]->Parse(parameters);
tomwalters@232 71 linked_ptr<Module> module(ModuleFactory::Create(id, parameters_[module_name].get()));
tomwalters@232 72 if (module.get() != NULL) {
tomwalters@232 73 module->set_instance_name(module_name);
tomwalters@232 74 modules_[module_name] = module;
tomwalters@232 75 } else {
tomwalters@232 76 LOG_ERROR("Module name: %s of type %s not created", module_name.c_str(), id);
tomwalters@232 77 done = true;
tomwalters@232 78 error = true;
tomwalters@232 79 }
tomwalters@232 80 if (module_number == 1) {
tomwalters@232 81 root_module_ = module.get();
tomwalters@232 82 }
tomwalters@231 83 } else {
tomwalters@232 84 LOG_ERROR("id field missing for module named %s", module_name.c_str());
tomwalters@231 85 error = true;
tomwalters@231 86 done = true;
tomwalters@231 87 }
tomwalters@226 88 } else {
tomwalters@226 89 done = true;
tomwalters@226 90 }
tomwalters@226 91 ++module_number;
tomwalters@226 92 }
tomwalters@226 93 // The second pass connects up all the modules into a tree.
tomwalters@232 94 char module_child_var[Parameters::MaxParamNameLength];
tomwalters@232 95 char module_name_var[Parameters::MaxParamNameLength];
tomwalters@232 96 LOG_INFO("A total of %d modules", modules_.size());
tomwalters@232 97 for (unsigned int i = 1; i < modules_.size() + 1; ++i) {
tomwalters@231 98 int child_number = 1;
tomwalters@231 99 done = false;
tomwalters@232 100 sprintf(module_name_var, "module%d.name", i);
tomwalters@232 101 if (config_.IsSet(module_name_var)) {
tomwalters@232 102 string module_name(config_.GetString(module_name_var));
tomwalters@231 103 while (!done) {
tomwalters@231 104 sprintf(module_child_var, "module%d.child%d", i, child_number);
tomwalters@232 105 LOG_INFO("Trying %s", module_child_var);
tomwalters@232 106 if (config_.IsSet(module_child_var)) {
tomwalters@232 107 string child(config_.GetString(module_child_var));
tomwalters@232 108 if ((modules_.find(module_name) != modules_.end())
tomwalters@232 109 && (modules_.find(child) != modules_.end())) {
tomwalters@232 110 modules_[module_name]->AddTarget(modules_[child].get());
tomwalters@232 111 } else {
tomwalters@232 112 LOG_ERROR("Module name not found");
tomwalters@232 113 }
tomwalters@231 114 } else {
tomwalters@231 115 done = true;
tomwalters@231 116 }
tomwalters@232 117 ++child_number;
tomwalters@231 118 }
tomwalters@225 119 } else {
tomwalters@232 120 LOG_ERROR("field missing for entry %s", module_name_var);
tomwalters@231 121 error = true;
tomwalters@231 122 break;
tomwalters@225 123 }
tomwalters@225 124 }
tomwalters@232 125 return !error;
tomwalters@225 126 }
tomwalters@231 127
tomwalters@231 128 bool ModuleTree::Initialize(Parameters *global_parameters) {
tomwalters@232 129 if (root_module_ == NULL) {
tomwalters@232 130 return false;
tomwalters@232 131 }
tomwalters@232 132 // Dummy signal bank for the root module.
tomwalters@232 133 s_.Initialize(1, 1, 1);
tomwalters@232 134 initialized_ = root_module_->Initialize(s_, global_parameters);
tomwalters@232 135 return initialized_;
tomwalters@232 136 }
tomwalters@232 137
tomwalters@232 138 void ModuleTree::Reset() {
tomwalters@232 139 if (root_module_ == NULL) {
tomwalters@232 140 return;
tomwalters@232 141 }
tomwalters@232 142 root_module_->Reset();
tomwalters@232 143 }
tomwalters@232 144
tomwalters@232 145 void ModuleTree::PrintConfiguration(ostream &out) {
tomwalters@232 146 if (root_module_ == NULL) {
tomwalters@232 147 return;
tomwalters@232 148 }
tomwalters@232 149 root_module_->PrintTargets(out);
tomwalters@232 150 root_module_->PrintConfiguration(out);
tomwalters@232 151 }
tomwalters@232 152
tomwalters@232 153 void ModuleTree::Process() {
tomwalters@232 154 if (root_module_ == NULL) {
tomwalters@232 155 return;
tomwalters@232 156 }
tomwalters@232 157 if (!initialized_) {
tomwalters@232 158 LOG_ERROR(_T("Module tree not initialized."));
tomwalters@232 159 return;
tomwalters@232 160 }
tomwalters@232 161 while (!root_module_->done()) {
tomwalters@232 162 root_module_->Process(s_);
tomwalters@232 163 }
tomwalters@232 164 }
tomwalters@232 165
tomwalters@232 166 void ModuleTree::MakeDotGraph(ostream &out) {
tomwalters@232 167 if (root_module_ == NULL) {
tomwalters@232 168 return;
tomwalters@232 169 }
tomwalters@232 170 out << "digraph G {" << endl;
tomwalters@232 171 root_module_->PrintTargetsForDot(out);
tomwalters@232 172 out << "}" << endl;
tomwalters@231 173 }
tomwalters@231 174
tomwalters@226 175 } // namespace aimc