00001 #include "stdafx.h"
00002
00003 #include "BON.h"
00004 #include "BONImpl.h"
00005
00006 #include "ReferenceRep.h"
00007 #include "ModelRep.h"
00008 #include "Dumper.h"
00009 #include "CodeGen.h"
00010
00011 #include "logger.h"
00012 #include "algorithm"
00013
00014 #include "globals.h"
00015 extern Globals global_vars;
00016 extern int h_ind;
00017
00018 ModelRep::ModelRep( BON::FCO& ptr, BON::FCO& resp_ptr)
00019 : FCO( ptr, resp_ptr)
00020 , m_initialRoleMap()
00021 , m_finalRoleMap()
00022 , m_methods()
00023 {
00024 }
00025
00026
00027 ModelRep::~ModelRep()
00028 {
00029 m_initialRoleMap.clear();
00030 m_finalRoleMap.clear();
00031 m_methods.clear();
00032 }
00033
00034
00035 void ModelRep::addRole( FCO* whose, RoleSeriesValue& role)
00036 {
00037 RoleMapValue &series = m_initialRoleMap[ whose ];
00038
00039
00040
00041 RoleSeries_Iterator it = std::find( series.begin(), series.end(), role);
00042
00043 if ( it == series.end() )
00044 series.push_back( role);
00045 else
00046 global_vars.err << "Error: Found duplicate role \"" << role.getSmartRoleName() << "\" in model \"" << getName() << "\"\n";
00047 }
00048
00049
00050 void ModelRep::initRoles()
00051 {
00052 RoleMap_Iterator it = m_initialRoleMap.begin();
00053 for( ; it != m_initialRoleMap.end(); ++it)
00054 {
00055 RoleMapKey fco = it->first;
00056 std::string nn = fco->getName();
00057
00058 std::vector<FCO*> children;
00059 fco->getIntDescendants( children);
00060
00061
00062
00063 int how_many_non_abstract = 0;
00064 if ( !fco->isAbstract()) ++how_many_non_abstract;
00065 for( unsigned int i = 0; i < children.size(); ++i)
00066 {
00067 std::string cn = children[i]->getName();
00068 if (! children[i]->isAbstract())
00069 ++how_many_non_abstract;
00070 }
00071
00072
00073
00074
00075
00076
00077
00078 if ( how_many_non_abstract > 1 )
00079 {
00080 RoleMapValue &series = it->second;
00081 RoleSeries_Iterator role_it = series.begin();
00082 for(; role_it != series.end(); ++role_it)
00083 {
00084 role_it->setLongForm( true);
00085 }
00086 }
00087 }
00088 }
00089
00090
00091 bool ModelRep::getRoles( FCO * ptr, RoleMapValue& map_val) const
00092 {
00093 RoleMap_ConstIterator it = m_initialRoleMap.find( ptr);
00094 if ( it == m_initialRoleMap.end())
00095 return false;
00096
00097 map_val = it->second;
00098 return true;
00099 }
00100
00101
00102 std::vector< ModelRep* > ModelRep::getInnerModels() const
00103 {
00104 std::vector< ModelRep* > models;
00105
00106 RoleMap_ConstIterator it = m_initialRoleMap.begin();
00107 while ( it != m_initialRoleMap.end())
00108 {
00109 FCO * const ptr = it->first;
00110 if ( ptr->getMyKind() == Any::MODEL)
00111 {
00112 ModelRep * mod_ptr = dynamic_cast< ModelRep *> ( ptr);
00113 models.push_back( mod_ptr);
00114 }
00115 ++it;
00116 }
00117 return models;
00118 }
00119
00120
00121
00122
00123
00124 void ModelRep::addFinalRole( FCO* whose, RoleSeriesValue& role)
00125 {
00126 RoleMapValue &series = m_finalRoleMap[ whose ];
00127
00128
00129
00130 RoleSeries_Iterator it = std::find( series.begin(), series.end(), role);
00131
00132 if ( it == series.end() )
00133 series.push_back( role);
00134 else
00135 global_vars.err << "Error: Found duplicate final role \"" << role.getSmartRoleName() << "\" in model \"" << getName() << "\"\n";
00136 }
00137
00138 bool ModelRep::getFinalRoles( const FCO * ptr, RoleMapValue& map_val) const
00139 {
00140 RoleMap_ConstIterator it = m_finalRoleMap.begin();
00141 while ( it != m_finalRoleMap.end() && it->first != ptr)
00142 ++it;
00143 if ( it == m_finalRoleMap.end())
00144 return false;
00145 map_val = it->second;
00146 return true;
00147
00148
00149
00150
00151
00152
00153 }
00154
00155
00156 std::vector< ModelRep* > ModelRep::getInnerModelsFinal() const
00157 {
00158 std::vector< ModelRep* > models;
00159
00160 RoleMap_ConstIterator it = m_finalRoleMap.begin();
00161 while ( it != m_finalRoleMap.end())
00162 {
00163 FCO * const ptr = it->first;
00164 if ( ptr->getMyKind() == Any::MODEL)
00165 {
00166 ModelRep * mod_ptr = dynamic_cast< ModelRep *> ( ptr);
00167 models.push_back( mod_ptr);
00168 }
00169 ++it;
00170 }
00171 return models;
00172 }
00173
00174
00175 std::vector< ReferenceRep *> ModelRep::getInnerModelReferencesFinal() const
00176 {
00177 std::vector< ReferenceRep *> refs;
00178 RoleMap_ConstIterator it = m_finalRoleMap.begin();
00179 while ( it != m_finalRoleMap.end())
00180 {
00181 FCO * const ptr = it->first;
00182 if ( ptr->getMyKind() == Any::REF)
00183 {
00184 ReferenceRep * ref_ptr = dynamic_cast< ReferenceRep *> ( ptr);
00185 if ( ref_ptr->pointsToModels())
00186 refs.push_back( ref_ptr);
00187 }
00188 ++it;
00189 }
00190 return refs;
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 void ModelRep::inherit()
00210 {
00211
00212 std::string m_name;
00213 std::string f_name;
00214
00215
00216 RoleMap temp_map;
00217 std::vector<FCO*> model_descendants;
00218 this->getImpDescendants( model_descendants);
00219 model_descendants.push_back( this);
00220
00221
00222 std::vector<FCO*>::reverse_iterator model_it = model_descendants.rbegin();
00223 for( ; model_it != model_descendants.rend(); ++model_it)
00224 {
00225 if ( (*model_it)->getMyKind() != Any::MODEL)
00226 global_vars.err << std::string("ERROR: model inherit\n");
00227 ModelRep * mod_desc_ptr = dynamic_cast<ModelRep*>(*model_it);
00228 if (!mod_desc_ptr) global_vars.err << std::string("NULL PTR ERROR: model inherit\n");
00229 m_name = mod_desc_ptr->getName();
00230
00231 RoleMap_Iterator fco_it( m_initialRoleMap.begin());
00232
00233 for( ; fco_it != m_initialRoleMap.end(); ++fco_it)
00234 {
00235 RoleMapKey fco_ptr = fco_it->first;
00236 std::vector<FCO*> children;
00237
00238 fco_ptr->getIntDescendants( children);
00239 children.push_back( fco_ptr);
00240
00241
00242 RoleSeries &roles = fco_it->second;
00243
00244 RoleSeries_Iterator role_it = roles.begin();
00245
00246 for( ; role_it != roles.end(); ++role_it)
00247 {
00248 std::vector<FCO*>::iterator desc_it = children.begin();
00249
00250 for( ; desc_it != children.end(); ++desc_it)
00251 {
00252 f_name = (*desc_it)->getName();
00253
00254
00255
00256
00257 RoleRep r(
00258 role_it->getOnlyRoleName(),
00259 *desc_it,
00260 mod_desc_ptr,
00261 role_it->isPort(),
00262 role_it->getCardinality(),
00263 mod_desc_ptr != this,
00264 role_it->isLongForm());
00265
00266 mod_desc_ptr->addFinalRole( *desc_it, r);
00267 (*desc_it)->iAmPartOfFinal( mod_desc_ptr);
00268
00269 }
00270 }
00271 }
00272 }
00273 }
00274
00275
00276 bool ModelRep::check()
00277 {
00278 return true;
00279 }
00280
00281
00282
00283
00284
00285 std::string ModelRep::roleGetterMethodName2(FCO * fco, RoleRep* role, bool use_fco_name, const std::string& diff_nmsp)
00286 {
00287 #if(LONG_NAMES)
00288 return "get_Role_" + (role->getOnlyRoleName().empty()?fco->getValidName():role->getOnlyRoleName());
00289 #else
00290 return "get" + diff_nmsp + (role->getOnlyRoleName().empty()?fco->getValidName():role->getOnlyRoleName());
00291 #endif
00292 }
00293
00294
00295
00296
00297
00298 std::string ModelRep::roleGetterMethodName3(FCO * fco, RoleRep* role, bool use_fco_name, const std::string& diff_nmsp)
00299 {
00300 #if(LONG_NAMES)
00301 return "get_Role_" + (role->getOnlyRoleName().empty()?fco->getValidName():fco->getValidName() + role->getOnlyRoleName());
00302 #else
00303 return "get" + diff_nmsp + (role->getOnlyRoleName().empty()?fco->getValidName():fco->getValidName() + role->getOnlyRoleName());
00304 #endif
00305 }
00306
00307
00308 void ModelRep::createMethods()
00309 {
00310 RoleMap_Iterator r_it = m_initialRoleMap.begin();
00311 while ( r_it != m_initialRoleMap.end())
00312 {
00313 RoleMapKey ptr = r_it->first;
00314 RoleMapValue &roles = r_it->second;
00315 RoleSeries_Iterator jt = roles.begin();
00316 for( ; jt != roles.end() ; ++jt)
00317 {
00318 CodeGen::dumpRoleGetter( ptr, &*jt, this);
00319 }
00320 ++r_it;
00321 }
00322 }
00323
00324
00325 std::string ModelRep::doDump()
00326 {
00327 std::string h_file, c_file;
00328
00329 dumpPre( h_file, c_file);
00330 dumpFCO( h_file, c_file);
00331
00332 if ( !m_methods.empty())
00333 h_file += CodeGen::indent(1) + "//\n" + CodeGen::indent(1) + "// kind and role getters\n";
00334
00335 MethodLexicographicSort lex;
00336 std::sort( m_methods.begin(), m_methods.end(), lex);
00337
00338 std::vector<Method>::iterator i = m_methods.begin();
00339 for( ; i != m_methods.end(); ++i)
00340 {
00341 if ( !i->m_template)
00342 {
00343 h_file += i->getHeader() + "\n";
00344 c_file += i->getSource() + "";
00345 }
00346 else
00347 h_file += i->getHeader() + "\n";
00348 }
00349
00350 h_file += hideAndExpose();
00351
00352 dumpPost( h_file, c_file);
00353
00354 sendOutH( h_file);
00355 sendOutS( c_file);
00356
00357 return "";
00358 }
00359
00360 std::string ModelRep::expose( const std::string& repl_container)
00361 {
00362 std::string h_file;
00363 h_file += FCO::expose( repl_container);
00364
00365 if (!m_methods.empty())
00366 h_file += CodeGen::indent(h_ind) + "//\n" + CodeGen::indent(h_ind) + "// exposed kind and role getters\n";
00367 std::vector<Method>::iterator i = m_methods.begin();
00368 for( ; i != m_methods.end(); ++i)
00369 h_file += i->getExposed( repl_container) + "\n";
00370
00371 return h_file;
00372 }
00373
00374
00375 std::string ModelRep::hide()
00376 {
00377 std::string h_file;
00378 h_file += FCO::hide();
00379
00380 if (!m_methods.empty())
00381 h_file += CodeGen::indent(h_ind) + "//\n" + CodeGen::indent(h_ind) + "// hidden kind and role getters\n";
00382 std::vector<Method>::iterator i = m_methods.begin();
00383 for( ; i != m_methods.end(); ++i)
00384 h_file += i->getHidden() + "\n";
00385
00386 return h_file;
00387 }
00388