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 #include <boost/tokenizer.hpp>
00030 #include "node.h"
00031 #include <salt/path.h>
00032 #include <iostream>
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 using namespace boost;
00048 using namespace salt;
00049 using namespace std;
00050 using namespace zeitgeist;
00051
00052
00053 Node::Node(const std::string& name) : Leaf(name)
00054 {
00055 }
00056
00057 Node::~Node()
00058 {
00059 }
00060
00061 boost::shared_ptr<Leaf>
00062 Node::GetChild(const std::string& name, bool recursive)
00063 {
00064 shared_ptr<Leaf> leaf = Leaf::GetChild(name, recursive);
00065
00066 if (leaf.get() != NULL) return leaf;
00067
00068 for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00069 {
00070
00071 if ((*i)->GetName().compare(name) == 0)
00072 return (*i);
00073
00074 if (recursive)
00075 {
00076 shared_ptr<Leaf> ret = (*i)->GetChild(name, recursive);
00077 if (ret.get() != NULL) return ret;
00078 }
00079 }
00080
00081 return boost::shared_ptr<Leaf>();
00082 }
00083
00084 boost::shared_ptr<Leaf>
00085 Node::GetChildOfClass(const std::string& name, bool recursive)
00086 {
00087 for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00088 {
00089
00090 shared_ptr<Class> theClass = (*i)->GetClass();
00091 if (theClass.get() != NULL && theClass->GetName().compare(name) == 0)
00092 {
00093 return (*i);
00094 }
00095
00096 if (recursive)
00097 {
00098 shared_ptr<Leaf> ret = (*i)->GetChildOfClass(name, recursive);
00099 if (ret.get() != NULL) return ret;
00100 }
00101 }
00102
00103 return shared_ptr<Leaf>();
00104 }
00105
00106 boost::shared_ptr<Leaf>
00107 Node::GetChildSupportingClass(const std::string& name, bool recursive)
00108 {
00109 for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00110 {
00111
00112 shared_ptr<Class> theClass = (*i)->GetClass();
00113 if (theClass.get() != NULL && theClass->SupportsClass(name))
00114 {
00115 return (*i);
00116 }
00117
00118 if (recursive)
00119 {
00120 shared_ptr<Leaf> ret = (*i)->GetChildSupportingClass(name, recursive);
00121 if (ret.get() != NULL) return ret;
00122 }
00123 }
00124
00125 return shared_ptr<Leaf>();
00126 }
00127
00128 void
00129 Node::GetChildren(const std::string& name, TLeafList& baseList, bool recursive)
00130 {
00131 Leaf::GetChildren(name, baseList, recursive);
00132
00133 for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00134 {
00135
00136 if ((*i)->GetName().compare(name) == 0)
00137 {
00138 baseList.push_back(*i);
00139 }
00140
00141 if (recursive)
00142 (*i)->GetChildren(name, baseList, recursive);
00143 }
00144 }
00145
00146 void
00147 Node::GetChildrenOfClass(const std::string& name, TLeafList& baseList, bool recursive)
00148 {
00149 Leaf::GetChildrenOfClass(name, baseList, recursive);
00150
00151 for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00152 {
00153
00154 shared_ptr<Class> theClass = (*i)->GetClass();
00155 if (theClass.get() != NULL && theClass->GetName().compare(name) == 0)
00156 {
00157 baseList.push_back(*i);
00158 }
00159
00160 if (recursive)
00161 (*i)->GetChildrenOfClass(name, baseList, recursive);
00162 }
00163 }
00164
00165 void
00166 Node::GetChildrenSupportingClass(const std::string& name, TLeafList& baseList, bool recursive)
00167 {
00168 Leaf::GetChildrenSupportingClass(name, baseList, recursive);
00169
00170 for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00171 {
00172
00173 shared_ptr<Class> theClass = (*i)->GetClass();
00174 if (theClass.get() != NULL && theClass->SupportsClass(name))
00175 {
00176 baseList.push_back(*i);
00177 }
00178
00179 if (recursive)
00180 {
00181 (*i)->GetChildrenSupportingClass(name, baseList, recursive);
00182 }
00183 }
00184 }
00185
00186 bool
00187 Node::IsLeaf() const
00188 {
00189 return false;
00190 }
00191
00192 void
00193 Node::RemoveChildReference(const boost::shared_ptr<Leaf>& leaf)
00194 {
00195 mChildren.remove(leaf);
00196 }
00197
00198 void
00199 Node::UnlinkChildren()
00200 {
00201 string cname;
00202 if (GetClass().get() != 0)
00203 {
00204 cname = GetClass()->GetName();
00205 }
00206
00207 while (! mChildren.empty())
00208 {
00209 shared_ptr<Leaf> node = mChildren.front();
00210
00211 string className;
00212 if (node->GetClass().get()!= 0)
00213 {
00214 className=node->GetClass()->GetName();
00215 }
00216 node->UnlinkChildren();
00217 node->Unlink();
00218 }
00219 }
00220
00221 bool
00222 Node::AddChildReference(const boost::shared_ptr<Leaf>& leaf)
00223 {
00224 if (leaf.get() == 0)
00225 {
00226 return false;
00227 }
00228
00229 if (leaf->GetClass() == 0)
00230 {
00231 if (leaf->GetName() != "ClassClass")
00232 {
00233 cerr << "(Node::AddChildReference) ERROR: object "
00234 << leaf->GetName()
00235 << " has no assigned class object." << endl;
00236 }
00237 return false;
00238 }
00239
00240 mChildren.push_back(leaf);
00241 leaf->SetParent(shared_static_cast<Node>(make_shared(GetSelf())));
00242 return true;
00243 }
00244
00245 void
00246 Node::Dump() const
00247 {
00248 Leaf::Dump();
00249 cout << "Node: numChildren = " << mChildren.size() << endl;
00250
00251 for (TLeafList::const_iterator i = mChildren.begin(); i!=mChildren.end(); ++i)
00252 {
00253 (*i)->Dump();
00254 cout << endl;
00255 }
00256
00257 cout << "End Node" << endl;
00258 }
00259
00260 Leaf::TLeafList::iterator
00261 Node::begin()
00262 {
00263 return mChildren.begin();
00264 }
00265
00266 Leaf::TLeafList::const_iterator
00267 Node::begin() const
00268 {
00269 return mChildren.begin();
00270 }
00271
00272 Leaf::TLeafList::iterator
00273 Node::end()
00274 {
00275 return mChildren.end();
00276 }
00277
00278 Leaf::TLeafList::const_iterator
00279 Node::end() const
00280 {
00281 return mChildren.end();
00282 }
00283
00284 void
00285 Node::UpdateCached()
00286 {
00287
00288 for (TLeafList::iterator iter = begin();
00289 iter != end();
00290 ++iter
00291 )
00292 {
00293
00294 (*iter)->UpdateCachedInternal();
00295
00296
00297 (*iter)->UpdateCached();
00298 }
00299 }
00300