00001 #include "stdafx.h"
00002
00003 #include "BON.h"
00004 #include "BONImpl.h"
00005
00006 #include "AspectRep.h"
00007 #include "RoleRep.h"
00008 #include "ModelRep.h"
00009 #include "Dumper.h"
00010
00011 #include "algorithm"
00012
00013 #include "globals.h"
00014 extern Globals global_vars;
00015
00016 const std::string AspectRep::m_aspectRegistryRoot = "AspectRegistry";
00017 const std::string AspectRep::m_aspectMetaRefsRoot = "AspectMetaRefs";
00018 const std::string AspectRep::m_aspectNamesRoot = "AspectSelNames";
00019
00020 bool PartCompare::operator()( const PartRep& peer1, const PartRep& peer2) const
00021 {
00022 std::string name1 = peer1.getRoleRepPtr()->getSmartRoleName();
00023 std::string name2 = peer2.getRoleRepPtr()->getSmartRoleName();
00024
00025 int k = name1.compare( name2);
00026 return ( k < 0);
00027 }
00028
00029
00030 bool AspectCompare::operator()( const AspectRep* peer1, const AspectRep* peer2) const
00031 {
00032 return peer1->lessThan( *peer2);
00033 }
00034
00035
00036 bool AspNamePriorityComp::operator()( const Type& p1, const Type& p2) const
00037 {
00038 return p1.second != p2.second ? p1.second < p2.second : (p1.first.compare( p2.first) < 0);
00039 }
00040
00041
00042 AspectRep::AspectRep( BON::FCO& ptr, BON::FCO& resp_ptr)
00043 : Any( ptr)
00044 , m_priority( 0)
00045 , m_respPointer( resp_ptr)
00046 , m_fcoInitialList()
00047 , m_roleInitialList()
00048 , m_fcoFinalList()
00049 , m_roleFinalList()
00050 , m_partsMap()
00051
00052 {
00053 #if(0)
00054 if (m_respPointer == BON::FCO())
00055 {
00056
00057 p global_vars.err << MSG_WARNING << "Dummy aspect created. You should define aspects for the models.\n";
00058 }
00059 #endif
00060 }
00061
00062
00063 AspectRep::~AspectRep()
00064 {
00065 m_fcoInitialList.clear();
00066 m_roleInitialList.clear();
00067 m_fcoFinalList.clear();
00068 m_roleFinalList.clear();
00069 m_partsMap.clear();
00070 }
00071
00072
00073 std::string AspectRep::doDump() { return ""; }
00074
00075
00076 Any::KIND_TYPE AspectRep::getMyKind() const { return Any::ASPECT; }
00077
00078
00079 std::string AspectRep::getName() const
00080 {
00081 #if(0)
00082
00083 if (m_respPointer == BON::FCO())
00084 {
00085 global_vars.err << MSG_ERROR << "Null pointer error in getAspectName\n";
00086 return std::string("Null pointer error in getAspectName");
00087 }
00088 else
00089 {
00090 return m_respPointer->getName();
00091 }
00092 #else
00093 #if(0)
00094 if( m_respPointer != BON::FCO())
00095 {
00096 return m_respPointer->getName();
00097 }
00098 else
00099 {
00100 return "Aspect";
00101 }
00102 #else // new way
00103 if( m_respPointer)
00104 {
00105 return m_respPointer->getName();
00106 }
00107 else if( m_ptr)
00108 {
00109 return m_ptr->getName();
00110 }
00111 else
00112 {
00113 return "Aspect";
00114 }
00115 #endif
00116 #endif
00117 }
00118
00119 std::string AspectRep::getDispName() const
00120 {
00121 #if(0)
00122 if (m_respPointer == BON::FCO())
00123 {
00124 global_vars.err << MSG_ERROR << "Null pointer error in getDispName for aspect \"" + getName() + "\"\n";
00125 return std::string("Null pointer error in getDispName for aspect \"") + getName() + "\"";
00126 }
00127 else
00128 {
00129 return m_ptr->getAttribute( Any::DisplayedName_str)->getStringValue();
00130 }
00131 #else
00132
00133
00134
00135
00136
00137
00138 #if(0)
00139 if( m_ptr != BON::FCO())
00140 {
00141 return m_ptr->getAttribute( Any::DisplayedName_str)->getStringValue();
00142 }
00143 else
00144 {
00145 return "Aspect";
00146 }
00147 #else // new way
00148 if( m_respPointer)
00149 {
00150
00151 return m_userSelectedDisplayName;
00152 }
00153 else if( m_ptr)
00154 {
00155 return m_ptr->getAttribute( Any::DisplayedName_str)->getStringValue();
00156 }
00157 else
00158 {
00159 return "";
00160 }
00161 #endif
00162 #endif
00163 }
00164
00165 bool AspectRep::getReadOnly() const
00166 {
00167 if( m_ptr)
00168 {
00169 BON::Attribute readOnlyAttribute = m_ptr->getAttribute("ReadOnly");
00170 if (readOnlyAttribute)
00171 {
00172 return m_ptr->getAttribute("ReadOnly")->getBooleanValue();
00173 }
00174 return false;
00175 }
00176 else
00177 {
00178 return false;
00179 }
00180 }
00181
00182 std::string AspectRep::getMyPrefix() const
00183 {
00184
00185
00186 return AspectRep::m_aspectRegistryRoot + "/" + AspectRep::m_aspectMetaRefsRoot + "/" + getName();
00187 }
00188
00189
00190 BON::RegistryNode AspectRep::getMyRegistry() const
00191 {
00192
00193
00194
00195
00196 return Sheet::m_BON_Project_Root_Folder->getRegistry()->getChild( getMyPrefix());
00197 }
00198
00199
00200
00201
00202
00203 std::string AspectRep::getMyPrefix( BON::FCO& fco, BON::Folder& f)
00204 {
00205 ASSERT( fco);
00206 return AspectRep::m_aspectRegistryRoot + "/" + AspectRep::m_aspectNamesRoot + "/" + fco->getName();
00207 }
00208
00209
00210 BON::RegistryNode AspectRep::getMyRegistry( BON::FCO& fco, BON::Folder& f)
00211 {
00212 ASSERT( fco);
00213 ASSERT( f);
00214 return Sheet::m_BON_Project_Root_Folder->getRegistry()->getChild( AspectRep::getMyPrefix( fco, f));
00215 }
00216
00217
00218 void AspectRep::addFCO( FCO* fco)
00219 {
00220 m_fcoInitialList.push_back( fco);
00221 }
00222
00223
00224 void AspectRep::addRRole( RoleRep & role)
00225 {
00226 m_roleInitialList.push_back( role);
00227 }
00228
00229
00230 bool AspectRep::findFinalFCO( const FCO * fco_ptr) const
00231 {
00232 return ( m_fcoFinalList.end() !=
00233 std::find(m_fcoFinalList.begin(), m_fcoFinalList.end(), fco_ptr));
00234 }
00235
00236
00237 bool AspectRep::findFinalRole( const RoleRep& a_role) const
00238 {
00239 std::vector<RoleRep>::const_iterator it = m_roleFinalList.begin();
00240 bool found = false;
00241 while ( it != m_roleFinalList.end() && !found)
00242 {
00243 if ( a_role.getFCOPtr() == it->getFCOPtr() &&
00244 a_role.getModelRepPtr() == it->getModelRepPtr() &&
00245 a_role.getOnlyRoleName() == it->getOnlyRoleName())
00246 {
00247 found = true;
00248 }
00249 ++it;
00250 }
00251 return found;
00252 }
00253
00254
00255 void AspectRep::extendAspectWithDescendants()
00256 {
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 std::vector<FCO*>::iterator init_it = m_fcoInitialList.begin();
00282 for( ; init_it != m_fcoInitialList.end(); ++init_it)
00283 {
00284 FCO* fco_ptr = *init_it;
00285 std::string fco_nm = fco_ptr->getName();
00286 std::vector<FCO*> children;
00287 fco_ptr->getIntDescendants( children);
00288 children.push_back( fco_ptr);
00289
00290 for( unsigned int i = 0; i < children.size(); ++i)
00291 if ( m_fcoFinalList.end() ==
00292 std::find( m_fcoFinalList.begin(), m_fcoFinalList.end(), children[i]))
00293 m_fcoFinalList.push_back( children[i]);
00294 }
00295
00296
00297 std::vector< RoleRep>::iterator r_init_it = m_roleInitialList.begin();
00298 for( ; r_init_it != m_roleInitialList.end(); ++r_init_it)
00299 {
00300 FCO* fco_ptr = r_init_it->getFCOPtr();
00301 std::vector<FCO*> children;
00302 fco_ptr->getIntDescendants( children);
00303 children.push_back( fco_ptr);
00304
00305 ModelRep* model_ptr = r_init_it->getModelRepPtr();
00306 std::vector<FCO*> model_descendants;
00307 model_ptr->getImpDescendants( model_descendants);
00308 model_descendants.push_back( model_ptr);
00309
00310 for( unsigned int i = 0; i < children.size(); ++i)
00311 {
00312 for( unsigned int j = 0; j < model_descendants.size(); ++j)
00313 {
00314 ModelRep * model_desc = dynamic_cast<ModelRep *>(model_descendants[j]);
00315 if (!model_desc) global_vars.err << MSG_ERROR << "Internal error: model descendant badly casted to model\n";
00316
00317 RoleRep r(
00318 r_init_it->getOnlyRoleName(),
00319 children[i],
00320 model_desc,
00321 r_init_it->isPort(),
00322 r_init_it->getCardinality(),
00323 j + 1 == model_descendants.size(),
00324 r_init_it->isLongForm()
00325 );
00326
00327 if ( m_roleFinalList.end() ==
00328 std::find( m_roleFinalList.begin(), m_roleFinalList.end(), r))
00329 m_roleFinalList.push_back( r);
00330 }
00331 }
00332 }
00333 }
00334
00335
00336 void AspectRep::sortPartMap()
00337 {
00338 PartCompare partCompare;
00339 ModelPartsMap_Iterator it = m_partsMap.begin();
00340 for( ; it != m_partsMap.end(); ++it)
00341 {
00342 PartRepSeries &p = it->second;
00343 std::sort( p.begin(), p.end(), partCompare);
00344 }
00345 }
00346
00347
00348 void AspectRep::addPart2Map( ModelRep * mod_ptr, const PartRep & part)
00349 {
00350 PartRepSeries & series = m_partsMap[mod_ptr];
00351 if ( series.end() ==
00352 std::find( series.begin(), series.end(), part))
00353 series.push_back( part);
00354 else
00355 global_vars.err << MSG_ERROR << "Part " << part.getRoleRepPtr()->getSmartRoleName()
00356 << " attempted to insert twice into aspect " << m_ptr << "\n";
00357 }
00358
00359
00360 const AspectRep::PartRepSeries& AspectRep::getPartSeries( ModelRep* mod_ptr)
00361 {
00362 return m_partsMap[ mod_ptr];
00363 }
00364
00365
00366 bool AspectRep::lessThan( const AspectRep& rep) const
00367 {
00368 if ( m_priority)
00369 return m_priority < rep.m_priority;
00370
00371 unsigned int x, y;
00372 unsigned int rep_x, rep_y;
00373 getXY( &x, &y);
00374 rep.getXY( &rep_x, &rep_y);
00375 return ( y < rep_y || (y == rep_y && x < rep_x ));
00376 }
00377
00378
00379 void AspectRep::getXY( unsigned int * x, unsigned int * y) const
00380 {
00381 if( m_ptr != BON::FCO())
00382 {
00383 BON::Point p = BON::Set( m_ptr)->getRegistry()->getLocation("Visualization");
00384 *x = p.first;
00385 *y = p.second;
00386 }
00387 else
00388 {
00389 *x = *y = 0;
00390 }
00391 }
00392
00393
00394 void AspectRep::setPriority( int prior)
00395 {
00396 ASSERT( prior > 0);
00397 m_priority = prior;
00398 }
00399
00400 int AspectRep::getPriority() const
00401 {
00402 return m_priority;
00403 }
00404
00405
00406 bool AspectRep::isDummy() const
00407 {
00408 return m_ptr == BON::FCO() && m_respPointer == BON::FCO();
00409 }