tomwalters@268
|
1 // Copyright 2010, Thomas Walters
|
tomwalters@268
|
2 //
|
tomwalters@268
|
3 // AIM-C: A C++ implementation of the Auditory Image Model
|
tomwalters@268
|
4 // http://www.acousticscale.org/AIMC
|
tomwalters@268
|
5 //
|
tomwalters@318
|
6 // Licensed under the Apache License, Version 2.0 (the "License");
|
tomwalters@318
|
7 // you may not use this file except in compliance with the License.
|
tomwalters@318
|
8 // You may obtain a copy of the License at
|
tomwalters@268
|
9 //
|
tomwalters@318
|
10 // http://www.apache.org/licenses/LICENSE-2.0
|
tomwalters@268
|
11 //
|
tomwalters@318
|
12 // Unless required by applicable law or agreed to in writing, software
|
tomwalters@318
|
13 // distributed under the License is distributed on an "AS IS" BASIS,
|
tomwalters@318
|
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
tomwalters@318
|
15 // See the License for the specific language governing permissions and
|
tomwalters@318
|
16 // limitations under the License.
|
tomwalters@268
|
17
|
tomwalters@268
|
18 /*! \file
|
tomwalters@268
|
19 * \brief Base class for all AIM-C modules.
|
tomwalters@268
|
20 */
|
tomwalters@268
|
21
|
tomwalters@268
|
22 /*! \author: Thomas Walters <tom@acousticscale.org>
|
tomwalters@268
|
23 * \date 2010/01/23
|
tomwalters@268
|
24 * \version \$Id$
|
tomwalters@268
|
25 */
|
tomwalters@268
|
26
|
tomwalters@268
|
27 #include "Support/Module.h"
|
tomwalters@268
|
28
|
tomwalters@268
|
29 #include <utility>
|
tomwalters@268
|
30
|
tomwalters@268
|
31 namespace aimc {
|
tomwalters@268
|
32 using std::pair;
|
tomwalters@323
|
33 using std::ostream;
|
tomwalters@402
|
34 using std::endl;
|
tomwalters@268
|
35 Module::Module(Parameters *parameters) {
|
tomwalters@268
|
36 initialized_ = false;
|
tomwalters@268
|
37 targets_.clear();
|
tomwalters@268
|
38 parameters_ = parameters;
|
tomwalters@268
|
39 module_identifier_ = "MODULE IDENTIFIER NOT SET";
|
tomwalters@268
|
40 module_type_ = "MODULE TYPE NOT SET";
|
tomwalters@268
|
41 module_description_ = "MODULE DESCRIPTION NOT SET";
|
tomwalters@268
|
42 module_version_ = "MODULE VERSION NOT SET";
|
tomwalters@402
|
43 instance_name_ = "";
|
tomwalters@402
|
44 done_ = false;
|
tomwalters@268
|
45 };
|
tomwalters@268
|
46
|
tomwalters@268
|
47 Module::~Module() {
|
tomwalters@268
|
48 };
|
tomwalters@268
|
49
|
tomwalters@401
|
50 bool Module::Initialize(const SignalBank &input,
|
tomwalters@401
|
51 Parameters *global_parameters) {
|
tomwalters@402
|
52 if (global_parameters == NULL) {
|
tomwalters@402
|
53 return false;
|
tomwalters@402
|
54 }
|
tomwalters@401
|
55 global_parameters_ = global_parameters;
|
tomwalters@268
|
56 // Validate the input
|
tomwalters@268
|
57 if (!input.Validate()) {
|
tomwalters@278
|
58 LOG_ERROR(_T("Input SignalBank not valid"));
|
tomwalters@268
|
59 return false;
|
tomwalters@268
|
60 }
|
tomwalters@273
|
61 if (!InitializeInternal(input)) {
|
tomwalters@278
|
62 LOG_ERROR(_T("Initialization failed in module %s"),
|
tomwalters@278
|
63 module_identifier_.c_str());
|
tomwalters@268
|
64 return false;
|
tomwalters@268
|
65 }
|
tomwalters@268
|
66 // If the module has an output bank, then we can set up the targets
|
tomwalters@401
|
67 // of this module.
|
tomwalters@268
|
68 if (output_.initialized()) {
|
tomwalters@268
|
69 // Check that the output SignalBank has been set up correctly
|
tomwalters@268
|
70 if (!output_.Validate()) {
|
tomwalters@278
|
71 LOG_ERROR(_T("Output SignalBank not valid in module %s"),
|
tomwalters@278
|
72 module_identifier_.c_str());
|
tomwalters@268
|
73 return false;
|
tomwalters@268
|
74 }
|
tomwalters@268
|
75 // Iterate through all the targets of this module, initializing
|
tomwalters@268
|
76 // them if they have not already been initialized. If they have
|
tomwalters@268
|
77 // already been initialized, then they are assumed to have been
|
tomwalters@268
|
78 // set up to accept an input SignalBank of the correct form, but
|
tomwalters@268
|
79 // this is not checked.
|
tomwalters@268
|
80 set<Module*>::const_iterator it;
|
tomwalters@268
|
81 for (it = targets_.begin(); it != targets_.end(); ++it) {
|
tomwalters@402
|
82 //if (!(*it)->initialized())
|
tomwalters@401
|
83 if (!(*it)->Initialize(output_, global_parameters_))
|
tomwalters@268
|
84 return false;
|
tomwalters@268
|
85 }
|
tomwalters@268
|
86 }
|
tomwalters@268
|
87 initialized_ = true;
|
tomwalters@268
|
88 return true;
|
tomwalters@268
|
89 }
|
tomwalters@268
|
90
|
tomwalters@275
|
91 void Module::Reset() {
|
tomwalters@275
|
92 if (!initialized_)
|
tomwalters@275
|
93 return;
|
tomwalters@275
|
94
|
tomwalters@275
|
95 ResetInternal();
|
tomwalters@275
|
96
|
tomwalters@275
|
97 // Iterate through all the targets of this module, resetting
|
tomwalters@275
|
98 // them.
|
tomwalters@275
|
99 set<Module*>::const_iterator it;
|
tomwalters@275
|
100 for (it = targets_.begin(); it != targets_.end(); ++it)
|
tomwalters@275
|
101 (*it)->Reset();
|
tomwalters@275
|
102 }
|
tomwalters@275
|
103
|
tomwalters@268
|
104 bool Module::initialized() const {
|
tomwalters@268
|
105 return initialized_;
|
tomwalters@268
|
106 }
|
tomwalters@268
|
107
|
tomwalters@268
|
108 bool Module::AddTarget(Module* target_module) {
|
tomwalters@401
|
109 bool result = false;
|
tomwalters@268
|
110 if (target_module) {
|
tomwalters@268
|
111 pair<set<Module*>::iterator, bool> ret;
|
tomwalters@268
|
112 ret = targets_.insert(target_module);
|
tomwalters@401
|
113 result = ret.second;
|
tomwalters@401
|
114 if (result) {
|
tomwalters@402
|
115 if (initialized_) {
|
tomwalters@402
|
116 if (output_.initialized()) {
|
tomwalters@401
|
117 if (!target_module->initialized()) {
|
tomwalters@401
|
118 target_module->Initialize(output_, global_parameters_);
|
tomwalters@401
|
119 }
|
tomwalters@401
|
120 }
|
tomwalters@401
|
121 }
|
tomwalters@401
|
122 }
|
tomwalters@268
|
123 }
|
tomwalters@401
|
124 return result;
|
tomwalters@268
|
125 }
|
tomwalters@268
|
126
|
tomwalters@281
|
127 bool Module::RemoveTarget(Module* target_module) {
|
tomwalters@268
|
128 if (targets_.erase(target_module) != 0)
|
tomwalters@268
|
129 return true;
|
tomwalters@268
|
130 return false;
|
tomwalters@268
|
131 }
|
tomwalters@268
|
132
|
tomwalters@281
|
133 void Module::RemoveAllTargets() {
|
tomwalters@268
|
134 targets_.clear();
|
tomwalters@268
|
135 }
|
tomwalters@268
|
136
|
tomwalters@268
|
137 const SignalBank* Module::GetOutputBank() const {
|
tomwalters@268
|
138 return &output_;
|
tomwalters@268
|
139 }
|
tomwalters@268
|
140
|
tomwalters@268
|
141 void Module::PushOutput() {
|
tomwalters@268
|
142 if (output_.initialized()) {
|
tomwalters@268
|
143 set<Module*>::const_iterator it;
|
tomwalters@268
|
144 for (it = targets_.begin(); it != targets_.end(); ++it) {
|
tomwalters@268
|
145 (*it)->Process(output_);
|
tomwalters@268
|
146 }
|
tomwalters@268
|
147 }
|
tomwalters@268
|
148 }
|
tomwalters@323
|
149
|
tomwalters@402
|
150 void Module::PrintTargetsForDot(ostream &out) {
|
tomwalters@402
|
151 //string parameters_string = parameters_->WriteString();
|
tomwalters@402
|
152 out << " " << instance_name() << " [shape = none, margin = 0, label = <" << endl;
|
tomwalters@402
|
153 out << " <TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\"> " << endl;
|
tomwalters@402
|
154 out << " <TR><TD>" << instance_name() << "</TD></TR><TR><TD>" << id();
|
tomwalters@402
|
155 out << "</TD></TR></TABLE>>]" << ";" << endl;
|
tomwalters@402
|
156 // <TD><TR>" << parameters_string << "</TD></TR>
|
tomwalters@402
|
157 set<Module*>::const_iterator it;
|
tomwalters@402
|
158 for (it = targets_.begin(); it != targets_.end(); ++it) {
|
tomwalters@402
|
159 out << " " << instance_name() << " -> " << (*it)->instance_name() << ";" << endl;
|
tomwalters@402
|
160 (*it)->PrintTargetsForDot(out);
|
tomwalters@323
|
161 }
|
tomwalters@323
|
162 }
|
tomwalters@323
|
163
|
tomwalters@402
|
164 void Module::PrintTargets(ostream &out) {
|
tomwalters@323
|
165 set<Module*>::const_iterator it;
|
tomwalters@323
|
166 for (it = targets_.begin(); it != targets_.end(); ++it) {
|
tomwalters@402
|
167 out << " " << instance_name() << " -> " << (*it)->instance_name() << ";" << endl;
|
tomwalters@402
|
168 (*it)->PrintTargets(out);
|
tomwalters@402
|
169 }
|
tomwalters@402
|
170 }
|
tomwalters@402
|
171
|
tomwalters@402
|
172 void Module::PrintConfiguration(ostream &out) {
|
tomwalters@402
|
173 out << "# " << id() << endl;
|
tomwalters@402
|
174 out << "# " << instance_name() << endl;
|
tomwalters@402
|
175 out << "# " << version() << endl;
|
tomwalters@402
|
176 string parameters_string = parameters_->WriteString();
|
tomwalters@402
|
177 out << parameters_string << endl;
|
tomwalters@402
|
178 set<Module*>::const_iterator it;
|
tomwalters@402
|
179 for (it = targets_.begin(); it != targets_.end(); ++it) {
|
tomwalters@402
|
180 (*it)->PrintConfiguration(out);
|
tomwalters@323
|
181 }
|
tomwalters@323
|
182 }
|
tomwalters@280
|
183 } // namespace aimc
|
tomwalters@268
|
184
|