00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef __RESOURCE_BUILDER_H
00037 #define __RESOURCE_BUILDER_H
00038
00039 #include "Sim/Foundation/Resource/Builder/ResourceFactory.h"
00040
00041 namespace Onikiri
00042 {
00043 class ResourceBuilder
00044 {
00045 public:
00046
00047 struct ResNode
00048 {
00049 typedef
00050 boost::shared_ptr<ResNode>
00051 ResNodePtr;
00052
00053 ResNodePtr parent;
00054 std::vector< PhysicalResourceNode* > instances;
00055
00056 struct Child
00057 {
00058 String to;
00059 String name;
00060 ResNodePtr node;
00061 };
00062 std::vector< Child > children;
00063
00064 String type;
00065 String name;
00066 ParamXMLPath paramPath;
00067 ParamXMLPath resultPath;
00068 int count;
00069 int cluster;
00070 int copyCount;
00071 bool external;
00072
00073 ResNode();
00074 };
00075 typedef ResNode::ResNodePtr ResNodePtr;
00076
00077 typedef ParamXMLTree::Node XMLNode;
00078 typedef ParamXMLTree::NodePtr XMLNodePtr;
00079 typedef ParamXMLTree::NodeArray XMLNodeArray;
00080 typedef ParamXMLTree::ChildMap XMLChildMap;
00081 typedef ParamXMLTree::AttributeMap XMLAttributeMap;
00082
00083 protected:
00084 ResNodePtr m_rootNode;
00085
00086 typedef std::map<String, ResNodePtr> ResNodeMap;
00087 ResNodeMap m_resNodeMap;
00088 std::vector<PhysicalResourceNode*> m_instanceList;
00089
00090 ResourceFactory m_factory;
00091
00092
00093 typedef std::map<String, int> ConstantMap;
00094 ConstantMap m_constMap;
00095
00096
00097 ParamXMLPath GetConfigurationPath();
00098
00099
00100 void LoadConstantSection(const ParamXMLPath& path);
00101
00102
00103 void LoadParameterSection(const ParamXMLPath& path);
00104
00105
00106 void LoadStructureSection(const ParamXMLPath& path);
00107
00108
00109 int GetStructureNodeValue(const XMLNodePtr node, const String& attrName);
00110 String GetStructureNodeString(const XMLNodePtr node, const String& attrName);
00111
00112
00113 void TraverseStructureNode(ResNodePtr resParent, XMLNodePtr xmlParent, int copyCount);
00114
00115
00116 void ConstructResources();
00117
00118
00119 void ConnectResources();
00120
00121
00122 void InitializeNodes( PhysicalResourceNode::InitPhase phase );
00123
00124
00125 void ValidateConnection();
00126
00127
00128 void Dump();
00129
00130 public:
00131 ResourceBuilder();
00132 virtual ~ResourceBuilder();
00133
00134
00135 void SetExternalResource( ResNode* node );
00136
00137
00138 void Construct();
00139
00140
00141 void Release();
00142
00143
00144 std::vector<PhysicalResourceNode*> GetAllResources()
00145 {
00146 return m_instanceList;
00147 }
00148
00149
00150
00151 template <class T>
00152 bool GetResource(const String& name, PhysicalResourceArray<T>& resArray)
00153 {
00154 ResNodeMap::iterator ni = m_resNodeMap.find( name );
00155 if( ni == m_resNodeMap.end() )
00156 return false;
00157
00158 ResNodePtr node = ni->second;
00159 size_t size = node->instances.size();
00160 resArray.Resize( size );
00161 for( size_t i = 0; i < size; i++ ){
00162 resArray[i] = dynamic_cast<T*>( node->instances[i] );
00163 }
00164
00165 return true;
00166 }
00167
00168 template <class T>
00169 bool GetResource(const String& name, T*& resPtr)
00170 {
00171 PhysicalResourceArray<T> resArray;
00172 if( !GetResource( name, resArray ) ){
00173 return false;
00174 }
00175
00176 if( resArray.GetSize() != 1 ){
00177 return false;
00178 }
00179
00180 resPtr = resArray[0];
00181 return true;
00182 }
00183
00184
00185
00186 template <class T>
00187 bool GetResourceByType(const String& typeName, PhysicalResourceArray<T>& resArray)
00188 {
00189 resArray.Clear();
00190 for( size_t i = 0; i < m_instanceList.size(); ++i ){
00191 if( m_instanceList[i]->GetTypeName() == typeName ){
00192 resArray.Add( dynamic_cast<T*>( m_instanceList[i] ) );
00193 }
00194 }
00195
00196 return resArray.GetSize() != 0;
00197 }
00198
00199 template <class T>
00200 bool GetResourceByType(const String& typeName, T*& resPtr)
00201 {
00202 PhysicalResourceArray<T> resArray;
00203 if( !GetResourceByType( typeName, resArray ) ){
00204 return false;
00205 }
00206
00207 if( resArray.GetSize() != 1 ){
00208 return false;
00209 }
00210
00211 resPtr = resArray[0];
00212 return true;
00213 }
00214 };
00215
00216 }
00217
00218
00219 #endif