Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkStandaloneDataStorage.h"
00019
00020 #include "mitkDataNode.h"
00021 #include "mitkProperties.h"
00022 #include "mitkNodePredicateBase.h"
00023 #include "mitkNodePredicateProperty.h"
00024 #include "mitkGroupTagProperty.h"
00025 #include "itkSimpleFastMutexLock.h"
00026 #include "itkMutexLockHolder.h"
00027
00028
00029 mitk::StandaloneDataStorage::StandaloneDataStorage()
00030 : mitk::DataStorage()
00031 {
00032 }
00033
00034
00035 mitk::StandaloneDataStorage::~StandaloneDataStorage()
00036 {
00037 for(AdjacencyList::iterator it = m_SourceNodes.begin();
00038 it != m_SourceNodes.end(); it++)
00039 {
00040 this->RemoveListeners(it->first);
00041 }
00042 }
00043
00044
00045
00046
00047 bool mitk::StandaloneDataStorage::IsInitialized() const
00048 {
00049 return true;
00050 }
00051
00052
00053 void mitk::StandaloneDataStorage::Add(mitk::DataNode* node, const mitk::DataStorage::SetOfObjects* parents)
00054 {
00055 {
00056 itk::MutexLockHolder<itk::SimpleFastMutexLock> locked(m_Mutex);
00057 if (!IsInitialized())
00058 throw std::logic_error("DataStorage not initialized");
00059
00060 if ((parents != NULL) && (std::find(parents->begin(), parents->end(), node) != parents->end()))
00061 throw std::invalid_argument("Node is it's own parent");
00062
00063 if (m_SourceNodes.find(node) != m_SourceNodes.end())
00064 throw std::invalid_argument("Node is already in DataStorage");
00065
00066
00067 mitk::DataStorage::SetOfObjects::ConstPointer sp;
00068 if (parents != NULL)
00069 sp = parents;
00070 else
00071 sp = mitk::DataStorage::SetOfObjects::New();
00072
00073 m_SourceNodes.insert(std::make_pair(node, sp));
00074
00075
00076 mitk::DataStorage::SetOfObjects::Pointer children = mitk::DataStorage::SetOfObjects::New();
00077 m_DerivedNodes.insert(std::make_pair(node, children));
00078
00079
00080 for (SetOfObjects::ConstIterator it = sp->Begin(); it != sp->End(); it++)
00081 {
00082 mitk::DataNode::ConstPointer parent = it.Value().GetPointer();
00083 mitk::DataStorage::SetOfObjects::ConstPointer derivedObjects = m_DerivedNodes[parent];
00084 if (derivedObjects.IsNull())
00085 m_DerivedNodes[parent] = mitk::DataStorage::SetOfObjects::New();
00086 mitk::DataStorage::SetOfObjects* deob = const_cast<mitk::DataStorage::SetOfObjects*>(m_DerivedNodes[parent].GetPointer());
00087 deob->InsertElement(deob->Size(), node);
00088 }
00089
00090
00091 this->AddListeners(node);
00092 }
00093
00094
00095 EmitAddNodeEvent(node);
00096
00097 }
00098
00099
00100 void mitk::StandaloneDataStorage::Remove(const mitk::DataNode* node)
00101 {
00102 if (!IsInitialized())
00103 throw std::logic_error("DataStorage not initialized");
00104 if (node == NULL)
00105 return;
00106
00107
00108 this->RemoveListeners(node);
00109
00110
00111
00112
00113
00114
00115
00116 mitk::DataNode::ConstPointer nodeGuard(node);
00117
00118
00119 EmitRemoveNodeEvent(node);
00120 {
00121 itk::MutexLockHolder<itk::SimpleFastMutexLock> locked(m_Mutex);
00122
00123 this->RemoveFromRelation(node, m_SourceNodes);
00124 this->RemoveFromRelation(node, m_DerivedNodes);
00125 }
00126 }
00127
00128 bool mitk::StandaloneDataStorage::Exists(const mitk::DataNode* node) const
00129 {
00130 itk::MutexLockHolder<itk::SimpleFastMutexLock> locked(m_Mutex);
00131 return (m_SourceNodes.find(node) != m_SourceNodes.end());
00132 }
00133
00134 void mitk::StandaloneDataStorage::RemoveFromRelation(const mitk::DataNode* node, AdjacencyList& relation)
00135 {
00136 for (AdjacencyList::const_iterator mapIter = relation.begin(); mapIter != relation.end(); ++mapIter)
00137 if (mapIter->second.IsNotNull())
00138 {
00139 SetOfObjects::Pointer s = const_cast<SetOfObjects*>(mapIter->second.GetPointer());
00140 SetOfObjects::STLContainerType::iterator relationListIter = std::find(s->begin(), s->end(), node);
00141 if (relationListIter != s->end())
00142 s->erase(relationListIter);
00143 }
00144
00145 AdjacencyList::iterator adIt;
00146 adIt = relation.find(node);
00147 if (adIt != relation.end())
00148 relation.erase(adIt);
00149 }
00150
00151
00152 mitk::DataStorage::SetOfObjects::ConstPointer mitk::StandaloneDataStorage::GetAll() const
00153 {
00154 itk::MutexLockHolder<itk::SimpleFastMutexLock > locked(m_Mutex);
00155 if (!IsInitialized())
00156 throw std::logic_error("DataStorage not initialized");
00157
00158 mitk::DataStorage::SetOfObjects::Pointer resultset = mitk::DataStorage::SetOfObjects::New();
00159
00160 unsigned int index = 0;
00161 for (AdjacencyList::const_iterator it = m_SourceNodes.begin(); it != m_SourceNodes.end(); ++it)
00162 if (it->first.IsNull())
00163 continue;
00164 else
00165 resultset->InsertElement(index++, const_cast<mitk::DataNode*>(it->first.GetPointer()));
00166
00167 return SetOfObjects::ConstPointer(resultset);
00168 }
00169
00170
00171 mitk::DataStorage::SetOfObjects::ConstPointer mitk::StandaloneDataStorage::GetRelations(const mitk::DataNode* node, const AdjacencyList& relation, const NodePredicateBase* condition, bool onlyDirectlyRelated) const
00172 {
00173 if (node == NULL)
00174 throw std::invalid_argument("invalid node");
00175
00176
00177 if (onlyDirectlyRelated)
00178 {
00179 AdjacencyList::const_iterator it = relation.find(node);
00180 if ((it == relation.end()) || (it->second.IsNull()))
00181 return SetOfObjects::ConstPointer(mitk::DataStorage::SetOfObjects::New());
00182 else
00183 return this->FilterSetOfObjects(it->second, condition);
00184 }
00185
00186
00187 std::vector<mitk::DataNode::ConstPointer> resultset;
00188 std::vector<mitk::DataNode::ConstPointer> openlist;
00189
00190
00191
00192 openlist.push_back(node);
00193
00194 while (openlist.size() > 0)
00195 {
00196 mitk::DataNode::ConstPointer current = openlist.back();
00197 openlist.pop_back();
00198 resultset.push_back(current);
00199 AdjacencyList::const_iterator it = relation.find(current);
00200 if ( (it == relation.end())
00201 || (it->second.IsNull())
00202 || (it->second->Size() == 0))
00203 continue;
00204 else
00205 for (SetOfObjects::ConstIterator parentIt = it->second->Begin(); parentIt != it->second->End(); ++parentIt)
00206 {
00207 mitk::DataNode::ConstPointer p = parentIt.Value().GetPointer();
00208 if ( !(std::find(resultset.begin(), resultset.end(), p) != resultset.end())
00209 && !(std::find(openlist.begin(), openlist.end(), p) != openlist.end()))
00210 openlist.push_back(p);
00211 }
00212 }
00213
00214
00215 mitk::DataStorage::SetOfObjects::Pointer realResultset = mitk::DataStorage::SetOfObjects::New();
00216 if (condition != NULL)
00217 {
00218 for (std::vector<mitk::DataNode::ConstPointer>::iterator resultIt = resultset.begin(); resultIt != resultset.end(); resultIt++)
00219 if ((*resultIt != node) && (condition->CheckNode(*resultIt) == true))
00220 realResultset->InsertElement(realResultset->Size(), mitk::DataNode::Pointer(const_cast<mitk::DataNode*>((*resultIt).GetPointer())));
00221 }
00222 else
00223 {
00224 for (std::vector<mitk::DataNode::ConstPointer>::iterator resultIt = resultset.begin(); resultIt != resultset.end(); resultIt++)
00225 if (*resultIt != node)
00226 realResultset->InsertElement(realResultset->Size(), mitk::DataNode::Pointer(const_cast<mitk::DataNode*>((*resultIt).GetPointer())));
00227 }
00228 return SetOfObjects::ConstPointer(realResultset);
00229 }
00230
00231
00232 mitk::DataStorage::SetOfObjects::ConstPointer mitk::StandaloneDataStorage::GetSources(const mitk::DataNode* node, const NodePredicateBase* condition, bool onlyDirectSources) const
00233 {
00234 itk::MutexLockHolder<itk::SimpleFastMutexLock> locked(m_Mutex);
00235 return this->GetRelations(node, m_SourceNodes, condition, onlyDirectSources);
00236 }
00237
00238
00239 mitk::DataStorage::SetOfObjects::ConstPointer mitk::StandaloneDataStorage::GetDerivations(const mitk::DataNode* node, const NodePredicateBase* condition, bool onlyDirectDerivations) const
00240 {
00241 itk::MutexLockHolder<itk::SimpleFastMutexLock>locked(m_Mutex);
00242 return this->GetRelations(node, m_DerivedNodes, condition, onlyDirectDerivations);
00243 }
00244
00245
00246 void mitk::StandaloneDataStorage::PrintSelf(std::ostream& os, itk::Indent indent) const
00247 {
00248 os << indent << "StandaloneDataStorage:\n";
00249 Superclass::PrintSelf(os, indent);
00250 }