GME
13
|
00001 //############################################################################################################################################### 00002 // 00003 // Object Constraint Language Generic Manager 00004 // OCLGMEECFacade.cpp 00005 // 00006 //############################################################################################################################################### 00007 #include "Solve4786.h" 00008 #include "OCLGMEECFacade.h" 00009 #include "OCLTypeExGMEEC.h" 00010 #include "OCLCommonEx.h" 00011 00012 namespace OclGmeEC 00013 { 00014 void GetDerivedContext( CComPtr<IMgaFCO> spFCO, FCOVector& vecFCOs, FCOVector& vecExamined ); 00015 00016 void GetAllObjectsConsideredAsContext( CComPtr<IMgaFCO> spFCO, FCOVector& vecFCOs, FCOVector& vecExamined ) 00017 { 00018 if ( OclCommonEx::ContainsObject( spFCO, vecExamined ) ) 00019 return; 00020 vecExamined.push_back( spFCO.p ); 00021 CComPtr<IMgaFCOs> spRefs; 00022 if ( OclCommonEx::GetAllObjects( spFCO, spRefs ) ) { 00023 if ( ! OclCommonEx::IsAbstract( spFCO ) && OclCommonEx::GetObjectType( spFCO.p ) != OBJTYPE_REFERENCE ) 00024 OclCommonEx::AddObject( spFCO, vecFCOs ); 00025 GetDerivedContext( spFCO, vecFCOs, vecExamined ); 00026 if ( spRefs.p ) 00027 MGACOLL_ITERATE( IMgaFCO, spRefs ) { 00028 GetDerivedContext( MGACOLL_ITER, vecFCOs, vecExamined ); 00029 } MGACOLL_ITERATE_END; 00030 } 00031 } 00032 00033 void GetDerivedContext( CComPtr<IMgaFCO> spFCO, FCOVector& vecFCOs, FCOVector& vecExamined ) 00034 { 00035 FCOVector vecDeriveds; 00036 OclCommonEx::GetInheritances( spFCO, "Normal", false, vecDeriveds ); 00037 OclCommonEx::GetInheritances( spFCO, "Implementation", false, vecDeriveds ); 00038 OclCommonEx::GetInheritances( spFCO, "Interface", false, vecDeriveds ); 00039 for ( unsigned int i = 0 ; i < vecDeriveds.size() ; i++ ) 00040 GetAllObjectsConsideredAsContext( vecDeriveds[ i ].p, vecFCOs, vecExamined ); 00041 } 00042 00043 //############################################################################################################################################## 00044 // 00045 // C L A S S : OclGmeEC::ConstraintDefinitionFactory <<< + OclImplementation::ConstraintDefinitionFactory 00046 // 00047 //============================================================================================================================================== 00048 // 00049 // D E S C R I P T I O N : 00050 // 00051 //############################################################################################################################################## 00052 00053 class ConstraintDefinitionFactory 00054 : public OclImplementation::ConstraintDefinitionFactory 00055 { 00056 public : 00057 CComPtr<IMgaProject> m_spProject; 00058 OclTree::TreeManager* m_pTreeManager; 00059 00060 virtual void GetFeatures( const OclSignature::Attribute& signature, OclMeta::AttributeVector& vecFeatures ) 00061 { 00062 OclGme::ConstraintFunctionVector vecFunctions; 00063 GetConstraintFunctions( vecFunctions ); 00064 for ( unsigned int i = 0 ; i < vecFunctions.size() ; i++ ) { 00065 if ( m_pTreeManager->GetTypeManager()->IsTypeA( signature.GetTypeName(), vecFunctions[ i ]->GetContextType() ) >= 0 ) 00066 if ( vecFunctions[ i ]->GetName() == signature.GetName() && vecFunctions[ i ]->GetStereotype() == Ocl::Constraint::CS_ATTRIBUTEDEF ) 00067 vecFeatures.push_back( new OclMeta::Attribute( signature.GetName(), CreateReturnType( vecFunctions[ i ]->GetReturnType() ), NULL, true ) ); 00068 } 00069 } 00070 00071 virtual void GetFeatures( const OclSignature::Method& signature, OclMeta::MethodVector& vecFeatures ) 00072 { 00073 OclGme::ConstraintFunctionVector vecFunctions; 00074 GetConstraintFunctions( vecFunctions ); 00075 for ( unsigned int i = 0 ; i < vecFunctions.size() ; i++ ) { 00076 if ( m_pTreeManager->GetTypeManager()->IsTypeA( signature.GetTypeName(), vecFunctions[ i ]->GetContextType() ) >= 0 ) { 00077 OclCommon::FormalParameterVector vecParamsIn = vecFunctions[ i ]->GetFormalParameters(); 00078 if ( vecFunctions[ i ]->GetName() == signature.GetName() && vecFunctions[ i ]->GetStereotype() == Ocl::Constraint::CS_METHODDEF && (int) vecParamsIn.size() == signature.GetParameterCount() ) { 00079 OclCommon::FormalParameterVector vecParams; 00080 for ( unsigned int j = 0 ; j < vecParamsIn.size() ; j++ ) { 00081 TypeSeq vecType; 00082 OclCommon::Convert( vecParamsIn[ j ].GetTypeName(), vecType ); 00083 vecParams.push_back( OclCommon::FormalParameter( vecParamsIn[ j ].GetName(), vecType[ 0 ], true ) ); 00084 } 00085 vecFeatures.push_back( new OclMeta::Method( signature.GetName(), vecParams, CreateReturnType( vecFunctions[ i ]->GetReturnType() ), NULL, true ) ); 00086 } 00087 } 00088 } 00089 } 00090 00091 private : 00092 void GetConstraintFunctions( OclGme::ConstraintFunctionVector& vecFunctions ) 00093 { 00094 CComPtr<IMgaFilter> spFilter; 00095 COMTHROW( m_spProject->CreateFilter( &spFilter ) ); 00096 COMTHROW( spFilter->put_Kind( CComBSTR( "ConstraintFunc" ) ) ); 00097 00098 CComPtr<IMgaFCOs> spFCOs; 00099 COMTHROW( m_spProject->AllFCOs( spFilter, &spFCOs ) ); 00100 00101 MGACOLL_ITERATE( IMgaFCO, spFCOs ) { 00102 OclGme::SpConstraintFunction spFunction( new OclGme::ConstraintFunction( MGACOLL_ITER ) ); 00103 spFunction->Register( m_pTreeManager ); 00104 Ocl::Constraint::State eState = spFunction->ParseContext(); 00105 if ( eState == Ocl::Constraint::CS_CTX_PARSE_SUCCEEDED ) { 00106 eState = spFunction->CheckContext(); 00107 if ( eState == Ocl::Constraint::CS_CTX_CHECK_SUCCEEDED ) 00108 vecFunctions.push_back( spFunction ); 00109 } 00110 } MGACOLL_ITERATE_END; 00111 } 00112 00113 TypeSeq CreateReturnType( const std::string& strType ) 00114 { 00115 TypeSeq vecType; 00116 OclCommon::Convert( strType, vecType ); 00117 return vecType; 00118 } 00119 }; 00120 00121 //############################################################################################################################################## 00122 // 00123 // C L A S S : OclGmeEC::Facade 00124 // 00125 //############################################################################################################################################## 00126 00127 Facade::Facade() 00128 : m_pTreeManager( NULL ), m_spProject( NULL ) 00129 { 00130 } 00131 00132 Facade::~Facade() 00133 { 00134 Finalize(); 00135 if ( m_pTreeManager ) 00136 delete m_pTreeManager; 00137 } 00138 00139 CComPtr<IMgaProject> Facade::GetProject() const 00140 { 00141 return m_spProject; 00142 } 00143 00144 OclTree::TreeManager* Facade::GetTreeManager() const 00145 { 00146 return m_pTreeManager; 00147 } 00148 00149 void Facade::Initialize( CComPtr<IMgaProject> spProject ) 00150 { 00151 if ( m_pTreeManager ) 00152 delete m_pTreeManager; 00153 00154 m_spProject = spProject; 00155 00156 m_bEnabled = true; 00157 m_bEnabledEvents = false; 00158 m_bEnabledInteractions = true; 00159 00160 ConstraintDefinitionFactory* pCDFactory = new ConstraintDefinitionFactory(); 00161 pCDFactory->m_spProject = spProject; 00162 00163 OclMeta::TypeManager* pTypeManager = new OclMeta::TypeManager( new TypeFactory( spProject ), new OclBasic::OperatorFactory(), new OclImplementation::FunctionFactory(), pCDFactory ); 00164 00165 m_pTreeManager = new OclTree::TreeManager( pTypeManager, new OclTree::ObjectNodeAdaptor(), new OclTree::CollectionNodeAdaptor() ); 00166 00167 pCDFactory->m_pTreeManager = m_pTreeManager; 00168 } 00169 00170 void Facade::Finalize() 00171 { 00172 if ( m_pTreeManager ) 00173 m_pTreeManager->GetTypeManager()->ClearDynamicTypes(); 00174 } 00175 00176 bool Facade::IsConstraintDefined( CComPtr<IMgaFCO> spConstraint ) 00177 { 00178 CString strField; 00179 00180 COMTHROW( spConstraint->get_StrAttrByName( CComBSTR( "ConstraintEqn" ), PutOut( strField ) ) ); 00181 if ( strField.IsEmpty() ) 00182 return false; 00183 00184 return true; 00185 } 00186 00187 void Facade::CheckConstraint( CComPtr<IMgaFCO> spConstraint, OclGme::ConstraintVector& vecFaileds, bool bFieldsMandatory ) 00188 { 00189 if ( ! IsConstraintDefined( spConstraint ) ) 00190 return; 00191 00192 FCOVector vecAssociated, vecFCOs, vecExamined; 00193 OclCommonEx::GetAssociationEnds( spConstraint, "src", "HasConstraint", vecAssociated ); 00194 for ( unsigned int i = 0 ; i < vecAssociated.size() ; i++ ) 00195 GetAllObjectsConsideredAsContext( vecAssociated[ i ].p, vecFCOs, vecExamined ); 00196 00197 for ( unsigned int i = 0 ; i < vecFCOs.size() ; i++ ) { 00198 std::string strType = OclCommonEx::GetObjectName( vecFCOs[ i ].p ); 00199 if ( strType.empty() ) { 00200 char chNum[ 100 ]; 00201 sprintf_s( chNum, "%lu", i ); 00202 strType = "UntitledClass_" + std::string( chNum ); 00203 } 00204 OclGme::SpConstraint pConstraint( new OclGme::Constraint( strType, spConstraint, bFieldsMandatory ) ); 00205 pConstraint->Register( m_pTreeManager ); 00206 if ( pConstraint->GetState() == Ocl::Constraint::CS_DEFINED ) { 00207 Ocl::Constraint::State eState = pConstraint->Parse(); 00208 if ( eState != Ocl::Constraint::CS_PARSE_SUCCEEDED ) 00209 vecFaileds.push_back( pConstraint ); 00210 else { 00211 OclTree::TypeContextStack context; 00212 context.AddVariable( "project", TypeSeq( 1, "gme::Project" ) ); 00213 eState = pConstraint->Check( context ); 00214 if ( eState != Ocl::Constraint::CS_CHECK_SUCCEEDED ) 00215 vecFaileds.push_back( pConstraint ); 00216 } 00217 } 00218 } 00219 00220 if ( vecFCOs.empty() ) { 00221 OclGme::SpConstraint pConstraint( new OclGme::Constraint( "meta::RootFolder", spConstraint, bFieldsMandatory ) ); 00222 pConstraint->Register( m_pTreeManager ); 00223 if ( pConstraint->GetState() == Ocl::Constraint::CS_DEFINED ) { 00224 Ocl::Constraint::State eState = pConstraint->Parse(); 00225 if ( eState != Ocl::Constraint::CS_PARSE_SUCCEEDED ) 00226 vecFaileds.push_back( pConstraint ); 00227 else { 00228 OclTree::TypeContextStack context; 00229 context.AddVariable( "project", TypeSeq( 1, "gme::Project" ) ); 00230 eState = pConstraint->Check( context ); 00231 if ( eState != Ocl::Constraint::CS_CHECK_SUCCEEDED ) 00232 vecFaileds.push_back( pConstraint ); 00233 } 00234 } 00235 } 00236 } 00237 00238 void Facade::CheckAllConstraints( OclGme::ConstraintVector& vecFaileds ) 00239 { 00240 CComPtr<IMgaFilter> spFilter; 00241 COMTHROW( m_spProject->CreateFilter( &spFilter ) ); 00242 COMTHROW( spFilter->put_Kind( CComBSTR( "Constraint" ) ) ); 00243 00244 CComPtr<IMgaFCOs> spFCOs; 00245 COMTHROW( m_spProject->AllFCOs( spFilter, &spFCOs ) ); 00246 00247 MGACOLL_ITERATE( IMgaFCO, spFCOs ) { 00248 CheckConstraint( MGACOLL_ITER, vecFaileds, true ); 00249 } MGACOLL_ITERATE_END; 00250 } 00251 00252 bool Facade::IsConstraintFunctionDefined( CComPtr<IMgaFCO> spConstraintFunction ) 00253 { 00254 CString strField; 00255 00256 COMTHROW( spConstraintFunction->get_StrAttrByName( CComBSTR( "CFuncDefinition" ), PutOut( strField ) ) ); 00257 if ( strField.IsEmpty() ) 00258 return false; 00259 strField.Empty(); 00260 00261 COMTHROW( spConstraintFunction->get_StrAttrByName( CComBSTR( "CFuncContext" ), PutOut( strField ) ) ); 00262 if ( strField.IsEmpty() ) 00263 return false; 00264 strField.Empty(); 00265 00266 COMTHROW( spConstraintFunction->get_StrAttrByName( CComBSTR( "CFuncReturnType" ), PutOut( strField ) ) ); 00267 if ( strField.IsEmpty() ) 00268 return false; 00269 00270 return true; 00271 } 00272 00273 void Facade::CheckConstraintFunction( CComPtr<IMgaFCO> spConstraintFunction, OclGme::ConstraintFunctionVector& vecFaileds, bool bFieldsMandatory ) 00274 { 00275 if ( ! IsConstraintFunctionDefined( spConstraintFunction ) ) 00276 return; 00277 00278 OclGme::SpConstraintFunction pConstraintFunction( new OclGme::ConstraintFunction( spConstraintFunction, bFieldsMandatory ) ); 00279 pConstraintFunction->Register( m_pTreeManager ); 00280 if ( pConstraintFunction->GetState() == Ocl::Constraint::CS_DEFINED ) { 00281 Ocl::Constraint::State eState = pConstraintFunction->Parse(); 00282 if ( eState != Ocl::Constraint::CS_PARSE_SUCCEEDED ) 00283 vecFaileds.push_back( pConstraintFunction ); 00284 else { 00285 OclTree::TypeContextStack context; 00286 context.AddVariable( "project", TypeSeq( 1, "gme::Project" ) ); 00287 eState = pConstraintFunction->Check( context ); 00288 if ( eState != Ocl::Constraint::CS_CHECK_SUCCEEDED ) 00289 vecFaileds.push_back( pConstraintFunction ); 00290 } 00291 } 00292 } 00293 00294 void Facade::CheckAllConstraintFunctions( OclGme::ConstraintFunctionVector& vecFaileds ) 00295 { 00296 CComPtr<IMgaFilter> spFilter; 00297 COMTHROW( m_spProject->CreateFilter( &spFilter ) ); 00298 COMTHROW( spFilter->put_Kind( CComBSTR( "ConstraintFunc" ) ) ); 00299 00300 CComPtr<IMgaFCOs> spFCOs; 00301 COMTHROW( m_spProject->AllFCOs( spFilter, &spFCOs ) ); 00302 00303 MGACOLL_ITERATE( IMgaFCO, spFCOs ) { 00304 CheckConstraintFunction( MGACOLL_ITER, vecFaileds, true ); 00305 } MGACOLL_ITERATE_END; 00306 } 00307 00308 }; // namespace OclGmeEC