00001 #include "stdafx.h"
00002
00003 #include "FolderRep.h"
00004 #include "Dumper.h"
00005 #include "Broker.h"
00006
00007 #include "algorithm"
00008 #include "fstream"
00009
00010 #include "globals.h"
00011 extern Globals global_vars;
00012
00013
00014 FolderRep::FolderRep( BON::FCO& ptr, BON::FCO& resp_ptr)
00015 : Any( ptr)
00016 , m_fcoList()
00017 , m_fcoCardList()
00018 , m_subFolderList()
00019 , m_subCardList()
00020 , m_respPointer( resp_ptr)
00021 {
00022 }
00023
00024
00025 FolderRep::~FolderRep()
00026 {
00027 m_fcoList.clear();
00028 m_fcoCardList.clear();
00029 m_subFolderList.clear();
00030 m_subCardList.clear();
00031 m_respPointer = BON::FCO();
00032 }
00033
00034
00035 std::string FolderRep::getName() const
00036 {
00037 #if(0)
00038 if (m_respPointer == BON::FCO())
00039 {
00040 global_vars.err << MSG_ERROR << "Null pointer error in getFolderName\n";
00041 return std::string("Null pointer error in getFolderName");
00042 }
00043 return m_respPointer->getName();
00044 #else // new way
00045 if( m_respPointer)
00046 {
00047 return m_namespace + (m_namespace.empty()?"":Any::NamespaceDelimiter_str) + m_respPointer->getName();
00048 }
00049 else
00050 {
00051 return m_namespace + (m_namespace.empty()?"":Any::NamespaceDelimiter_str) + m_ptr->getName();
00052 }
00053 #endif
00054 }
00055
00056 std::string FolderRep::getDispName() const
00057 {
00058 #if(0)
00059
00060
00061 if (m_respPointer == BON::FCO())
00062 {
00063 global_vars.err << MSG_ERROR << "Null pointer error in getDispName for aspect \"" + getName() + "\"\n";
00064 return std::string("Null pointer error in getDispName for folder \"") + getName() + "\"";
00065 }
00066 else
00067 {
00068 return m_ptr->getAttribute( Any::DisplayedName_str)->getStringValue();
00069 }
00070 #else // new way
00071 if( m_respPointer)
00072 {
00073
00074 return m_userSelectedDisplayName;
00075 }
00076 else
00077 {
00078 return m_ptr->getAttribute( Any::DisplayedName_str)->getStringValue();
00079 }
00080 #endif
00081 }
00082
00083
00084 void FolderRep::addFCO( FCO * ptr, const std::string& card)
00085 {
00086 if ( std::find( m_fcoList.begin(), m_fcoList.end(), ptr) == m_fcoList.end())
00087 {
00088 m_fcoList.push_back( ptr);
00089 m_fcoCardList.push_back( card);
00090 }
00091 else
00092 global_vars.err << MSG_ERROR << "CHECK: "<< ptr->getPtr() << " contained by folder " << m_ptr << " twice. Disregarded.\n";
00093 }
00094
00095
00096 bool FolderRep::isFCOContained( FCO * ptr)
00097 {
00098 return m_fcoList.end() != std::find( m_fcoList.begin(), m_fcoList.end(), ptr);
00099 }
00100
00101
00102 bool FolderRep::isEmpty() const
00103 {
00104
00105 bool fco_list_empty = true;
00106 FCO_ConstIterator it = m_fcoList.begin();
00107 while ( it != m_fcoList.end())
00108 {
00109 if (!(*it)->isAbstract())
00110 fco_list_empty = false;
00111 ++it;
00112 }
00113 return m_subFolderList.empty() && fco_list_empty;
00114 }
00115
00116
00117 void FolderRep::addSubFolderRep( FolderRep *ptr, const std::string& card)
00118 {
00119 if ( std::find( m_subFolderList.begin(), m_subFolderList.end(), ptr)
00120 == m_subFolderList.end())
00121 {
00122 m_subFolderList.push_back( ptr);
00123 m_subCardList.push_back( card);
00124 }
00125 else
00126 global_vars.err << MSG_ERROR << "CHECK: Folder " << ptr->getPtr() << " contained by folder " << m_ptr << " twice. Disregarded.\n";
00127 }
00128
00129
00130 void FolderRep::extendMembership()
00131 {
00132 std::vector<FCO *> temp_list;
00133 FCO_Iterator fco_it = m_fcoList.begin();
00134 for( ; fco_it != m_fcoList.end(); ++fco_it)
00135 {
00136 FCO * fco_ptr = *fco_it;
00137 std::vector<FCO*> descendants;
00138 fco_ptr->getIntDescendants( descendants);
00139 std::vector<FCO*>::iterator desc_it = descendants.begin();
00140 for( ; desc_it != descendants.end(); ++desc_it)
00141 {
00142 FCO * new_member = *desc_it;
00143 if ( temp_list.end() ==
00144 std::find( temp_list.begin(), temp_list.end(), new_member))
00145 temp_list.push_back( new_member);
00146 }
00147 }
00148
00149 m_fcoList.insert( m_fcoList.end(), temp_list.begin(), temp_list.end());
00150 }
00151
00152
00153 std::string FolderRep::doDump()
00154 {
00155 std::string m_ref = askMetaRef();
00156
00157
00158 std::string mmm = indStr() + "<folder name = \"" + getName() + "\" metaref = \"" + m_ref + "\" ";
00159
00160 AnyLexicographicSort lex;
00161 std::sort( m_subFolderList.begin(), m_subFolderList.end(), lex);
00162
00163 SubFolder_ConstIterator it_f = m_subFolderList.begin();
00164 if ( it_f != m_subFolderList.end())
00165 {
00166 mmm += " subfolders = \"" + (*it_f)->getName();
00167 for( ++it_f; it_f != m_subFolderList.end(); ++it_f)
00168 {
00169 mmm += " " + (*it_f)->getName();
00170 }
00171 mmm += "\"";
00172 }
00173
00174 std::sort( m_fcoList.begin(), m_fcoList.end(), lex);
00175
00176 if ( !m_fcoList.empty())
00177 {
00178 std::string nnn = "";
00179 bool first = true;
00180 FCO_Iterator it = m_fcoList.begin();
00181 while ( it != m_fcoList.end())
00182 {
00183 if (!(*it)->isAbstract())
00184 {
00185 if ( !first) nnn += " ";
00186 nnn += (*it)->getName();
00187 first = false;
00188 }
00189 ++it;
00190 }
00191 if ( !nnn.empty())
00192 mmm += " rootobjects = \"" + nnn + "\"";
00193 }
00194 mmm += " >\n";
00195
00196 ++ind;
00197
00198
00199 std::string f_disp = getDispName();
00200 if (!f_disp.empty() && f_disp != getName())
00201 mmm += dumpDispName();
00202
00203 mmm += dumpGeneralPref();
00204 mmm += dumpConstraints();
00205 --ind;
00206
00207 mmm += indStr() + "</folder>\n";
00208
00209 return mmm;
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219 void FolderRep::createConstraints( Sheet * s)
00220 {
00221 std::vector<FCO *>::iterator fco_it = m_fcoList.begin();
00222 SubFolder_Iterator sub_it = m_subFolderList.begin();
00223
00224 for ( unsigned int i = 0; i < m_fcoList.size(); ++i)
00225 {
00226 FCO * ptr = m_fcoList[i];
00227 std::string card = "";
00228 if (i < m_fcoCardList.size())
00229 card = m_fcoCardList[i];
00230 else
00231 global_vars.err << MSG_ERROR << "Error in folder \"" << m_ptr << "\" element constraint generation: not enough cardList members\n";
00232
00233 std::string str_expr_end;
00234 std::string str_card_context;
00235 str_card_context = "[containment] In folder: " + getName() + ", FCO " + ptr->getName();
00236 bool valid_constr = ! Dumper::doParseCardinality( card, "rootObjCount", str_card_context, str_expr_end);
00237 if ( ! valid_constr )
00238 {
00239 global_vars.err << MSG_ERROR << "Ignoring invalid cardinality string in folder part: " << ptr->getPtr() + ". String: " << card << "\n";
00240 }
00241
00242 std::string str_expr_begin =
00243 "let rootObjCount = rootChildren()->select( child | child.oclIsTypeOf( " + ptr->getName() + " )";
00244
00245 std::vector<FCO*> descendants;
00246 ptr->getIntDescendants( descendants);
00247 std::vector<FCO*>::iterator desc_it = descendants.begin();
00248 for( ; desc_it != descendants.end(); ++desc_it)
00249 str_expr_begin += " or child.oclIsTypeOf( " + (*desc_it)->getName() + ")";
00250
00251 str_expr_begin += " )->size in\n ";
00252
00253
00254 if ( valid_constr && ! str_expr_end.empty() ) {
00255
00256
00257
00258 int id = Broker::getNextConstraintId();
00259 char str_id[64];
00260 sprintf( str_id, "%d", id);
00261
00262 std::string str_cons_name;
00263 str_cons_name = "Valid" + getName() + "PartCardinality" + std::string(str_id);
00264 std::string::size_type pos = str_cons_name.find( "::");
00265 if( pos != std::string::npos)
00266 str_cons_name.replace( pos, 2, 2, '_');
00267
00268 int iEventMask = 0;
00269 char chMask[64];
00270 sprintf( chMask, "%x", iEventMask );
00271
00272 std::string str_desc;
00273 str_desc = "Multiplicity of parts, which are contained by folder "
00274 + getName() + ", has to match "+ card + ".";
00275
00276 ConstraintRep * cr = s->createConstraintRep( BON::FCO());
00277 std::string s_b = str_expr_begin + str_expr_end;
00278 cr->init( str_cons_name, global_vars.genConstr.fol_cont_mask, "1", global_vars.genConstr.priority, s_b, str_desc);
00279
00280 this->addInitialConstraintRep( cr);
00281 cr->attachedTo();
00282 }
00283 }
00284 }