00001 #include "stdafx.h"
00002
00003 #include "BON.h"
00004 #include "BONImpl.h"
00005 #include "Sheet.h"
00006 #include "logger.h"
00007
00008 #include "globals.h"
00009 extern Globals global_vars;
00010
00011 Sheet * Sheet::m_theOnlyInstance = 0;
00012
00013 Sheet::Sheet()
00014 : m_projName(""),
00015 m_validProjName(""),
00016 m_projNamespace(""),
00017 m_validProjNamespace(""),
00018 m_rootFolder(),
00019 m_fcoRepList(),
00020 m_atomList(),
00021 m_modelList(),
00022 m_connList(),
00023 m_refList(),
00024 m_setList(),
00025 m_folderList(),
00026 m_attributeList()
00027 {
00028 for( unsigned int i = 0; i < FCO::NUMBER_OF_INHERITANCES; ++i)
00029 m_numberOfCliques[ i ] = 0;
00030 }
00031
00032
00033 Sheet::~Sheet()
00034 {
00035 for( unsigned int i = 0; i < FCO::NUMBER_OF_INHERITANCES; ++i)
00036 m_numberOfCliques[ i ] = 0;
00037 m_theOnlyInstance = 0;
00038
00039 {
00040 FcoRep_Iterator it( m_fcoRepList.begin() );
00041 for( ; it != m_fcoRepList.end(); ++it)
00042 {
00043 FcoRep * act_ptr = * it;
00044 delete act_ptr;
00045 }
00046 }
00047 {
00048 AtomRep_Iterator it = m_atomList.begin();
00049 for( ; it != m_atomList.end(); ++it)
00050 {
00051 AtomRep * act_ptr = * it;
00052 delete act_ptr;
00053 }
00054 }
00055 {
00056 ModelRep_Iterator it = m_modelList.begin();
00057 for( ; it != m_modelList.end(); ++it)
00058 {
00059 ModelRep * act_ptr = * it;
00060 delete act_ptr;
00061 }
00062 }
00063 {
00064 ConnectionRep_Iterator it = m_connList.begin();
00065 for( ; it != m_connList.end(); ++it)
00066 {
00067 ConnectionRep * act_ptr = * it;
00068 delete act_ptr;
00069 }
00070 }
00071 {
00072 SetRep_Iterator it = m_setList.begin();
00073 for( ; it != m_setList.end(); ++it)
00074 {
00075 SetRep * act_ptr = * it;
00076 delete act_ptr;
00077 }
00078 }
00079 {
00080 ReferenceRep_Iterator it = m_refList.begin();
00081 for( ; it != m_refList.end(); ++it)
00082 {
00083 ReferenceRep * act_ptr = * it;
00084 delete act_ptr;
00085 }
00086 }
00087 {
00088 FolderRep_Iterator ai = m_folderList.begin();
00089 for( ; ai != m_folderList.end(); ++ai)
00090 {
00091 FolderRep * act_ptr = * ai;
00092 delete act_ptr;
00093 }
00094 }
00095 {
00096 AttributeRep_Iterator ai = m_attributeList.begin();
00097 for( ; ai != m_attributeList.end(); ++ai)
00098 {
00099 AttributeRep * act_ptr = * ai;
00100 delete act_ptr;
00101 }
00102 }
00103 m_fcoRepList.clear();
00104 m_atomList.clear();
00105 m_modelList.clear();
00106 m_connList.clear();
00107 m_refList.clear();
00108 m_setList.clear();
00109
00110 m_folderList.clear();
00111
00112 m_attributeList.clear();
00113
00114 m_BON_Project_Root_Folder2 = BON::Folder();
00115
00116 m_setOfParadigmSheets2.erase( m_setOfParadigmSheets2.begin(), m_setOfParadigmSheets2.end());
00117 m_setOfParadigmSheets2.clear();
00118 }
00119
00120
00121 void Sheet::setProject( BON::Project& project)
00122 {
00123 m_validProjName = m_projName = project->getName();
00124 Any::convertToValidName( m_validProjName);
00125
00126 BON::RegistryNode rn = project->getRootFolder()->getRegistry()->getChild( "Namespace");
00127 if( rn) m_validProjNamespace = m_projNamespace = rn->getValue();
00128 else m_validProjNamespace = m_projNamespace = "";
00129
00130
00131 Any::convertToValidName( m_validProjNamespace);
00132 }
00133
00134 bool Sheet::finalize()
00135 {
00136 bool ret_val = false;
00137 if ( doInheritance( FCO::INTERFACE)
00138 && doInheritance( FCO::IMPLEMENTATION))
00139 {
00140 init();
00141 initRoleNames();
00142
00143 multipleInheritance();
00144 ret_val = true;
00145 }
00146 else
00147 global_vars.err << "Error: Inheritance problems\n";
00148
00149 return ret_val;
00150 }
00151
00152 void Sheet::init()
00153 {
00154 {FcoRep_Iterator it0( m_fcoRepList.begin());
00155 for( ; it0 != m_fcoRepList.end(); ++it0 )
00156 {
00157 (*it0)->initNamespace();
00158 (*it0)->initAttributes();
00159 if ( isInRootFolder( *it0)) m_rootFolder.addRootElement( *it0);
00160 }}
00161
00162 {AtomRep_Iterator it1 = m_atomList.begin();
00163 for( ; it1 != m_atomList.end(); ++it1 )
00164 {
00165 (*it1)->initNamespace();
00166 (*it1)->initAttributes();
00167 if ( isInRootFolder( *it1)) m_rootFolder.addRootElement( *it1);
00168 }}
00169
00170 {ModelRep_Iterator it2 = m_modelList.begin();
00171 for( ; it2 != m_modelList.end(); ++it2 )
00172 {
00173 (*it2)->initNamespace();
00174 (*it2)->initAttributes();
00175 if ( isInRootFolder( *it2)) m_rootFolder.addRootElement( *it2);
00176 }}
00177
00178 {ConnectionRep_Iterator it3 = m_connList.begin();
00179 for( ; it3 != m_connList.end(); ++it3 )
00180 {
00181 (*it3)->initNamespace();
00182 (*it3)->initAttributes();
00183 if ( isInRootFolder( *it3)) m_rootFolder.addRootElement( *it3);
00184 }}
00185
00186 {SetRep_Iterator it4 = m_setList.begin();
00187 for( ; it4 != m_setList.end(); ++it4 )
00188 {
00189 (*it4)->initNamespace();
00190 (*it4)->initAttributes();
00191 if ( isInRootFolder( *it4)) m_rootFolder.addRootElement( *it4);
00192 }}
00193
00194 {ReferenceRep_Iterator it5 = m_refList.begin();
00195 for( ; it5 != m_refList.end(); ++it5 )
00196 {
00197 (*it5)->initNamespace();
00198 (*it5)->initAttributes();
00199 if ( isInRootFolder( *it5)) m_rootFolder.addRootElement( *it5);
00200 }}
00201
00202 {FolderRep_Iterator it6 = m_folderList.begin();
00203 for( ; it6 != m_folderList.end(); ++it6 )
00204 {
00205 (*it6)->initNamespace();
00206 (*it6)->initAttributes();
00207 if ( isInRootFolder( *it6)) m_rootFolder.addSubFolder( *it6);
00208 }}
00209
00210 {AttributeRep_Iterator it7 = m_attributeList.begin();
00211 for( ; it7 != m_attributeList.end(); ++it7 )
00212 {
00213 (*it7)->initNamespace();
00214 }}
00215 }
00216
00217
00218 void Sheet::initRoleNames()
00219 {
00220 ModelRep_Iterator jt1( m_modelList.begin());
00221 for( ; jt1 != m_modelList.end(); ++jt1)
00222 {
00223 ModelRep * mod_ptr = *jt1;
00224 mod_ptr->initRoles();
00225 }
00226 }
00227
00228
00229 FcoRep* Sheet::createFcoRep( BON::FCO& ptr, BON::FCO& resp_ptr)
00230 {
00231 FcoRep * ll = new FcoRep( ptr, resp_ptr);
00232 addFcoRep( ll);
00233 return ll;
00234 }
00235
00236
00237 AtomRep* Sheet::createAtomRep( BON::FCO& ptr, BON::FCO& resp_ptr)
00238 {
00239 AtomRep * ll = new AtomRep( ptr, resp_ptr);
00240 addAtomRep( ll);
00241 return ll;
00242 }
00243
00244
00245 ModelRep* Sheet::createModelRep( BON::FCO& ptr, BON::FCO& resp_ptr)
00246 {
00247 ModelRep * ll = new ModelRep( ptr, resp_ptr);
00248 addModelRep( ll);
00249 return ll;
00250 }
00251
00252
00253 ConnectionRep* Sheet::createConnectionRep( BON::FCO& ptr, BON::FCO& resp_ptr)
00254 {
00255 ConnectionRep * ll = new ConnectionRep( ptr, resp_ptr);
00256 addConnectionRep( ll);
00257 return ll;
00258 }
00259
00260
00261 SetRep* Sheet::createSetRep( BON::FCO& ptr, BON::FCO& resp_ptr)
00262 {
00263 SetRep * ll = new SetRep( ptr, resp_ptr);
00264 addSetRep( ll);
00265 return ll;
00266 }
00267
00268
00269 ReferenceRep* Sheet::createReferenceRep( BON::FCO& ptr, BON::FCO& resp_ptr)
00270 {
00271 ReferenceRep * ll = new ReferenceRep( ptr, resp_ptr);
00272 addReferenceRep( ll);
00273 return ll;
00274 }
00275
00276
00277 FolderRep* Sheet::createFolderRep( BON::FCO& ptr, BON::FCO& resp_ptr)
00278 {
00279 FolderRep * ll = new FolderRep( ptr, resp_ptr);
00280 addFolderRep( ll);
00281 return ll;
00282 }
00283
00284
00285 AttributeRep* Sheet::createBoolAttributeRep( BON::FCO& ptr)
00286 {
00287 AttributeRep * ll = new BoolAttributeRep( ptr);
00288 addAttributeRep( ll);
00289 return ll;
00290 }
00291
00292
00293 AttributeRep* Sheet::createEnumAttributeRep( BON::FCO& ptr)
00294 {
00295 AttributeRep * ll = new EnumAttributeRep( ptr);
00296 addAttributeRep( ll);
00297 return ll;
00298 }
00299
00300
00301 AttributeRep* Sheet::createFieldAttributeRep( BON::FCO& ptr)
00302 {
00303 AttributeRep * ll = new FieldAttributeRep( ptr);
00304 addAttributeRep( ll);
00305 return ll;
00306 }
00307
00308
00309 void Sheet::addFcoRep( FcoRep * ptr)
00310 {
00311 m_fcoRepList.push_back( ptr);
00312 }
00313
00314
00315 void Sheet::addAtomRep( AtomRep * ptr)
00316 {
00317 m_atomList.push_back( ptr);
00318 }
00319
00320
00321 void Sheet::addModelRep( ModelRep * ptr)
00322 {
00323 m_modelList.push_back( ptr);
00324 }
00325
00326
00327 void Sheet::addConnectionRep( ConnectionRep * ptr)
00328 {
00329 m_connList.push_back( ptr);
00330 }
00331
00332
00333 void Sheet::addReferenceRep( ReferenceRep * ptr)
00334 {
00335 m_refList.push_back( ptr);
00336 }
00337
00338
00339 void Sheet::addSetRep( SetRep * ptr)
00340 {
00341 m_setList.push_back( ptr);
00342 }
00343
00344
00345 void Sheet::addFolderRep( FolderRep * ptr)
00346 {
00347 m_folderList.push_back(ptr);
00348 }
00349
00350
00351 void Sheet::addAttributeRep( AttributeRep * ptr)
00352 {
00353 m_attributeList.push_back(ptr);
00354 }
00355
00356
00357
00366 FCO* Sheet::findFCO( const BON::FCO& ptr)
00367 {
00368 FcoRep_Iterator it0( m_fcoRepList.begin());
00369 for( ; it0 != m_fcoRepList.end(); ++it0 )
00370 {
00371 if ((*it0)->getPtr() == ptr) return *it0;
00372 }
00373
00374 AtomRep_Iterator it1 = m_atomList.begin();
00375 for( ; it1 != m_atomList.end(); ++it1 )
00376 {
00377 if ((*it1)->getPtr() == ptr) return *it1;
00378 }
00379
00380 ModelRep_Iterator it2 = m_modelList.begin();
00381 for( ; it2 != m_modelList.end(); ++it2 )
00382 {
00383 if ((*it2)->getPtr() == ptr) return *it2;
00384 }
00385
00386 ConnectionRep_Iterator it3 = m_connList.begin();
00387 for( ; it3 != m_connList.end(); ++it3 )
00388 {
00389 if ((*it3)->getPtr() == ptr) return *it3;
00390 }
00391
00392 SetRep_Iterator it4 = m_setList.begin();
00393 for( ; it4 != m_setList.end(); ++it4 )
00394 {
00395 if ((*it4)->getPtr() == ptr) return *it4;
00396 }
00397
00398 ReferenceRep_Iterator it5 = m_refList.begin();
00399 for( ; it5 != m_refList.end(); ++it5 )
00400 {
00401 if ((*it5)->getPtr() == ptr) return *it5;
00402 }
00403 return 0;
00404 }
00405
00406
00407 ReferenceRep* Sheet::findRef( const BON::FCO& ptr)
00408 {
00409 ReferenceRep_Iterator it( m_refList.begin());
00410 for( ; it != m_refList.end(); ++it )
00411 {
00412 if ((*it)->getPtr() == ptr) return *it;
00413 }
00414 return 0;
00415
00416 }
00417
00418
00419 Any* Sheet::findAny( const BON::FCO& ptr)
00420 {
00421 FolderRep_Iterator it5 = m_folderList.begin();
00422 for( ; it5 != m_folderList.end(); ++it5 )
00423 {
00424 if ((*it5)->getPtr() == ptr) return *it5;
00425 }
00426
00427 AttributeRep_Iterator it4 = m_attributeList.begin();
00428 for( ; it4 != m_attributeList.end(); ++it4 )
00429 {
00430 if ((*it4)->getPtr() == ptr) return *it4;
00431 }
00432
00433 return findFCO( ptr);
00434 }
00435
00436
00437 bool Sheet::isInRootFolder( Any * elem)
00438 {
00439 return elem->isInRootFolder();
00440 }
00441
00442
00443 void Sheet::gatherNodes( NODE_VECTOR & nodes)
00444 {
00445 nodes.insert( nodes.end(), m_fcoRepList.begin(), m_fcoRepList.end());
00446 nodes.insert( nodes.end(), m_atomList.begin(), m_atomList.end());
00447 nodes.insert( nodes.end(), m_modelList.begin(), m_modelList.end());
00448 nodes.insert( nodes.end(), m_connList.begin(), m_connList.end());
00449 nodes.insert( nodes.end(), m_setList.begin(), m_setList.end());
00450 nodes.insert( nodes.end(), m_refList.begin(), m_refList.end());
00451
00452 }
00453
00454
00455 void Sheet::assignCliqueId( CLIQUE_VECTOR & clique_id, const NODE_VECTOR & nodes)
00456 {
00457 unsigned int cl_id = 0;
00458 unsigned int len = nodes.size();
00459
00460 for(unsigned int i = 0 ; i < len; ++i)
00461 clique_id.push_back(i+1);
00462 }
00463
00464
00465 void Sheet::replaceCliqueId( CLIQUE_VECTOR & clique_id, unsigned int id1, unsigned int id2)
00466 {
00467 unsigned int search_id = id1, replace_id = id2;
00468 if ( replace_id > search_id)
00469 {
00470 unsigned int storage = replace_id;
00471 replace_id = search_id;
00472 search_id = storage;
00473 }
00474
00475 for (unsigned int k = 0; k < clique_id.size(); ++k)
00476 if ( clique_id[k] == search_id)
00477 clique_id[k] = replace_id;
00478 }
00479
00480
00481 unsigned int Sheet::howManyCliques( CLIQUE_VECTOR & clique_id)
00482 {
00483 std::map<unsigned int,unsigned int> t_m;
00484 for (unsigned int k = 0; k < clique_id.size(); ++k)
00485 {
00486 unsigned int curr_id = clique_id[k];
00487 ++t_m[curr_id];
00488 }
00489 unsigned int current_clique_id_max = 1;
00490 unsigned int val = 0;
00491 std::map<unsigned int,unsigned int>::iterator i = t_m.begin();
00492 for( ; i != t_m.end(); ++i)
00493 {
00494
00495 ++val;
00496
00497
00498
00499
00500
00501 unsigned int old_id = i->first;
00502 if ( old_id != current_clique_id_max)
00503 for (unsigned int j = 0; j < clique_id.size(); ++j)
00504 if ( clique_id[j] == old_id)
00505 clique_id[j] = current_clique_id_max;
00506
00507 ++current_clique_id_max;
00508 }
00509
00510 return val;
00511 }
00512
00513
00514 void Sheet::getChildren( unsigned int base, const NODE_VECTOR & nodes, UI_LIST & children, FCO::INHERITANCE_TYPE inh_type)
00515 {
00516 for (unsigned int i = 0; i < nodes.size(); ++i)
00517 if ( edge(i, base, nodes, inh_type))
00518 {
00519 unsigned int j = 0;
00520 while( j < children.size() && children[j] != i)
00521 ++j;
00522 if ( j == children.size())
00523 children.push_back( i);
00524 }
00525 }
00526
00527 bool Sheet::multipleInheritance()
00528 {
00529 FcoRep_Iterator it0( m_fcoRepList.begin());
00530 for( ; it0 != m_fcoRepList.end(); ++it0 )
00531 {
00532 (*it0)->multipleInheritanceStep1();
00533 (*it0)->multipleInheritanceStep2();
00534 }
00535
00536 AtomRep_Iterator it1 = m_atomList.begin();
00537 for( ; it1 != m_atomList.end(); ++it1 )
00538 {
00539 (*it1)->multipleInheritanceStep1();
00540 (*it1)->multipleInheritanceStep2();
00541 }
00542
00543 ModelRep_Iterator it2 = m_modelList.begin();
00544 for( ; it2 != m_modelList.end(); ++it2 )
00545 {
00546 (*it2)->multipleInheritanceStep1();
00547 (*it2)->multipleInheritanceStep2();
00548 }
00549
00550 ConnectionRep_Iterator it3 = m_connList.begin();
00551 for( ; it3 != m_connList.end(); ++it3 )
00552 {
00553 (*it3)->multipleInheritanceStep1();
00554 (*it3)->multipleInheritanceStep2();
00555 }
00556
00557 SetRep_Iterator it4 = m_setList.begin();
00558 for( ; it4 != m_setList.end(); ++it4 )
00559 {
00560 (*it4)->multipleInheritanceStep1();
00561 (*it4)->multipleInheritanceStep2();
00562 }
00563
00564 ReferenceRep_Iterator it5 = m_refList.begin();
00565 for( ; it5 != m_refList.end(); ++it5 )
00566 {
00567 (*it5)->multipleInheritanceStep1();
00568 (*it5)->multipleInheritanceStep2();
00569 }
00570 return true;
00571 }
00572
00573
00574 void Sheet::vectorCopy( UI_LIST & target, const UI_LIST & source, UI_LIST & duplicates)
00575 {
00576 for(unsigned int i = 0; i < source.size(); ++i)
00577 {
00578 unsigned int j = 0;
00579 while( j != target.size() && target[j] != source[i])
00580 ++j;
00581 if ( j == target.size())
00582 target.push_back( source[i]);
00583 else
00584 duplicates.push_back( source[i]);
00585 }
00586 }
00587
00588
00589 bool Sheet::edge( unsigned int i, unsigned int j, const NODE_VECTOR & nodes, FCO::INHERITANCE_TYPE inh_type)
00590 {
00591 return nodes[i]->hasParent( nodes[j], inh_type);
00592 }
00593
00594
00595 bool Sheet::doInheritance( FCO::INHERITANCE_TYPE inh_type)
00596 {
00597 unsigned int i, j;
00598 std::map<unsigned int, unsigned int> depends_upon;
00599 CLIQUE_VECTOR clique_vec;
00600 NODE_VECTOR node_vec;
00601
00602 gatherNodes( node_vec);
00603
00604 assignCliqueId( clique_vec, node_vec);
00605
00606 for( i = 0; i < node_vec.size(); ++i)
00607 for ( j = 0; j < node_vec.size(); ++j)
00608 {
00609 if ( edge(i,j, node_vec, inh_type))
00610 {
00611 ++depends_upon[i];
00612
00613 if ( clique_vec[j] != clique_vec[i])
00614 replaceCliqueId( clique_vec, clique_vec[j], clique_vec[i]);
00615 }
00616 }
00617
00618 std::list<unsigned int> elements, elements_backup;
00619 std::vector<int> level( node_vec.size());
00620 for( int reset_i = 0; reset_i < node_vec.size(); ++reset_i)
00621 level[reset_i] = -1;
00622
00623
00624 std::vector<unsigned int> dupl_temp;
00625
00626 m_numberOfCliques[ inh_type] = howManyCliques( clique_vec);
00627
00628 for( i = 0; i < node_vec.size(); ++i)
00629 if (depends_upon[i]==0)
00630 {
00631 elements.push_back(i);
00632 level[i] = 0;
00633 }
00634
00635 if (elements.size() < m_numberOfCliques[ inh_type])
00636 {
00637 TO("Circle found in model");
00638 global_vars.err << "Circle found in model\n";
00639
00640
00641
00642 return false;
00643 }
00644
00645 std::map< unsigned int, std::vector<unsigned int> > ancestors;
00646 std::map< unsigned int, std::vector<unsigned int> > descendants;
00647
00648 bool loop = false;
00649
00650 elements_backup = elements;
00651
00652 while ( !elements.empty() && !loop)
00653 {
00654
00655 unsigned int base = *elements.begin();
00656 elements.pop_front();
00657
00658
00659 std::vector<unsigned int> children;
00660 getChildren( base, node_vec, children, inh_type);
00661 for( j = 0; j < children.size(); ++j)
00662 {
00663 unsigned int child = children[j];
00664 elements.push_back( child);
00665
00666 if (ancestors[child].end() ==
00667 std::find(ancestors[child].begin(), ancestors[child].end(), base))
00668 ancestors[child].push_back(base);
00669
00670 vectorCopy( ancestors[child], ancestors[base], dupl_temp);
00671 for ( unsigned int k = 0; k < ancestors[child].size(); ++k)
00672 if ( edge( ancestors[child][k], child, node_vec, inh_type))
00673 {
00674 FCO * fco1 = node_vec[ancestors[child][k]];
00675 FCO * fco2 = node_vec[child];
00676 TO("Loop between " + fco1->getName() + " and " + fco2->getName());
00677 global_vars.err << "Loop between " << fco1->getName() << " and " << fco2->getName() << "\n";
00678
00679 loop = true;
00680
00681 return false;
00682 }
00683 }
00684 }
00685
00686
00687
00688 loop = false;
00689 while ( !elements_backup.empty() && !loop)
00690 {
00691
00692 unsigned int base = *elements_backup.begin();
00693 elements_backup.pop_front();
00694
00695
00696 std::vector<unsigned int> children;
00697 getChildren( base, node_vec, children, inh_type);
00698 for( j = 0; j < children.size(); ++j)
00699 {
00700 unsigned int child = children[j];
00701 elements_backup.push_back( child);
00702
00703
00704 if ( level[child] < level[base] + 1) level[child] = level[base] + 1;
00705 }
00706 }
00707
00708
00709
00710 std::map< unsigned int, std::vector<unsigned int> >::iterator anc_it = ancestors.begin();
00711 for( ; anc_it != ancestors.end(); ++anc_it)
00712 {
00713 unsigned int id = anc_it->first;
00714 std::vector<unsigned int> & ancestors_of_id = anc_it->second;
00715
00716
00717 for( unsigned int k = 0; k < ancestors_of_id.size(); ++k)
00718 {
00719
00720
00721
00722
00723
00724
00725
00726
00727 if ( std::find( descendants[ancestors_of_id[k]].begin(), descendants[ancestors_of_id[k]].end(), id) ==
00728 descendants[ancestors_of_id[k]].end())
00729 {
00730 descendants[ancestors_of_id[k]].push_back(id);
00731
00732
00733 }
00734 }
00735 }
00736
00737
00738 for( i = 0; i < node_vec.size(); ++i)
00739 {
00740
00741 std::vector< FCO*> anc_list, desc_list;
00742 for ( unsigned int k = 0; k < ancestors[i].size(); ++k)
00743 {
00744 anc_list.push_back( node_vec[ancestors[i][k]]);
00745
00746 }
00747
00748 for ( unsigned int l = 0; l < descendants[i].size(); ++l)
00749 {
00750 desc_list.push_back( node_vec[descendants[i][l]]);
00751
00752 }
00753
00754
00755 node_vec[i]->setAncestors( inh_type, anc_list);
00756 node_vec[i]->setDescendants( inh_type, desc_list);
00757 node_vec[i]->setLevel( inh_type, level[i]);
00758 node_vec[i]->setCliqueId( inh_type, clique_vec[i]);
00759 }
00760 return true;
00761 }
00762
00763 std::vector< FCO *> Sheet::sortBasedOnLevels()
00764 {
00765
00766
00767
00768
00769 std::vector< FCO *> res;
00770 order( FCO::INTERFACE, res);
00771 bool good = check( FCO::IMPLEMENTATION, res);
00772 if (!good)
00773 {
00774 res.clear();
00775 order( FCO::IMPLEMENTATION, res);
00776 good = check( FCO::INTERFACE, res);
00777 if (!good) global_vars.err << "Cannot establish an order which would be good for both IMPLEMENTATION and INTERFACE hierarchy\n";
00778 }
00779 return res;
00780 }
00781
00782
00783 void Sheet::order( FCO::INHERITANCE_TYPE type, std::vector<FCO *>& res)
00784 {
00785
00786
00787
00788 std::map< unsigned int, unsigned int> how_many_models_in_clique;
00789
00790
00791
00792 for( int clique_index0 = 1; clique_index0 <= m_numberOfCliques[ type]; ++clique_index0)
00793 how_many_models_in_clique[ clique_index0] = 0;
00794
00795 ModelRep_Iterator itm = m_modelList.begin();
00796 for( ; itm != m_modelList.end(); ++itm )
00797 ++how_many_models_in_clique[ (*itm)->getCliqueId( type)];
00798
00799
00800
00801 std::map<unsigned int, std::list<unsigned int> > how_many_inverse;
00802 std::map<unsigned int,unsigned int>::iterator hmmic_ind = how_many_models_in_clique.begin();
00803 for( ; hmmic_ind != how_many_models_in_clique.end(); ++hmmic_ind)
00804 how_many_inverse[ hmmic_ind->second].push_back( hmmic_ind->first);
00805
00806
00807 std::map<unsigned int, std::list<unsigned int> >::reverse_iterator hminv_ind = how_many_inverse.rbegin();
00808
00809 for( ; hminv_ind != how_many_inverse.rend(); ++hminv_ind)
00810 {
00811 std::list<unsigned int>::iterator cliquelist_ind = hminv_ind->second.begin();
00812 for( ; cliquelist_ind != hminv_ind->second.end(); ++cliquelist_ind)
00813 {
00814 unsigned int clique_index = *cliquelist_ind;
00815 int curr_level = 0;
00816 bool more = true;
00817 while ( more)
00818 {
00819 int len_before = res.size();
00820 {
00821 ModelRep_Iterator it2 = m_modelList.begin();
00822 for( ; it2 != m_modelList.end(); ++it2 )
00823 if ( (*it2)->getCliqueId( type) == clique_index && (*it2)->getLevel( type) == curr_level)
00824 res.push_back( *it2);
00825 }
00826 {
00827 FcoRep_Iterator it0( m_fcoRepList.begin());
00828 for( ; it0 != m_fcoRepList.end(); ++it0 )
00829 if ( (*it0)->getCliqueId( type) == clique_index && (*it0)->getLevel( type) == curr_level)
00830 res.push_back( *it0);
00831 }
00832 {
00833 AtomRep_Iterator it1 = m_atomList.begin();
00834 for( ; it1 != m_atomList.end(); ++it1 )
00835 if ( (*it1)->getCliqueId( type) == clique_index && (*it1)->getLevel( type) == curr_level)
00836 res.push_back( *it1);
00837 }
00838 {
00839 ReferenceRep_Iterator it5 = m_refList.begin();
00840 for( ; it5 != m_refList.end(); ++it5 )
00841 if ( (*it5)->getCliqueId( type) == clique_index && (*it5)->getLevel( type) == curr_level)
00842 res.push_back( *it5);
00843 }
00844 {
00845 ConnectionRep_Iterator it3 = m_connList.begin();
00846 for( ; it3 != m_connList.end(); ++it3 )
00847 if ( (*it3)->getCliqueId( type) == clique_index && (*it3)->getLevel( type) == curr_level)
00848 res.push_back( *it3);
00849 }
00850 {
00851 SetRep_Iterator it4 = m_setList.begin();
00852 for( ; it4 != m_setList.end(); ++it4 )
00853 if ( (*it4)->getCliqueId( type) == clique_index && (*it4)->getLevel( type) == curr_level)
00854 res.push_back( *it4);
00855 }
00856
00857 ++curr_level;
00858 more = ( len_before != res.size());
00859 }
00860 }
00861 }
00862 }
00863
00864
00865 bool Sheet::check( FCO::INHERITANCE_TYPE type, std::vector<FCO*>& res)
00866 {
00867
00868
00869 std::map< int, int> m_maxLevelPerClique;
00870
00871 std::vector<FCO*>::iterator it = res.begin();
00872 for( ; it != res.end(); ++it)
00873 {
00874 int curr_level = (*it)->getLevel( type);
00875 int curr_clique = (*it)->getCliqueId( type);
00876
00877 if( m_maxLevelPerClique.find( curr_clique) == m_maxLevelPerClique.end())
00878 m_maxLevelPerClique[ curr_clique ] = curr_level;
00879 else
00880 {
00881 if( curr_level < m_maxLevelPerClique[ curr_clique])
00882 return false;
00883 }
00884 }
00885 return true;
00886 }