42
42
CLASSNAMEINIT(Study);
45
// Study & Study::operator =(const Study & other);
49
45
* Default constructor
52
: p_storageManager_(new StorageManager)
50
p_storageManager_(new StorageManager)
54
52
p_storageManager_->setStudy(this);
59
55
/* String converter */
60
56
String Study::__repr__() const
63
59
oss << "class=" << getClassName();
65
MapCollection::const_iterator map_it;
66
for(map_it = maps.begin(); map_it != maps.end(); map_it++) {
67
oss << " map #" << (*map_it).first << "={\n";
68
ObjectMap map = (*map_it).second;
69
ObjectMap::const_iterator it;
70
const char * separator = " ";
71
for(it = map.begin(); it != map.end(); it++, separator="\n ") {
61
const char * separator = " ";
62
for(Map::const_iterator it = map_.begin();
64
it++, separator="\n ") {
73
oss << separator << (*it).first << " => " << ((*it).second)->getClassName()
74
<< " name='" << ((*it).second)->getName()
75
<< "' id=" << ((*it).second)->getId();
66
oss << separator << (*it).first << " => " << ((*it).second)->getClassName()
67
<< " name='" << ((*it).second)->getName()
68
<< "' id=" << ((*it).second)->getId();
78
if (((*it).second)->getVisibility())
79
oss << separator << (*it).first << " => id=" << ((*it).second)->getId()
80
<< " " << ((*it).second)->__repr__();
71
if (((*it).second)->getVisibility())
72
oss << separator << (*it).first << " => id=" << ((*it).second)->getId()
73
<< " " << ((*it).second)->__repr__();
83
oss << separator << (*it).first << " => " << (((*it).second)->getVisibility()?"*":" ")<< " id=" << ((*it).second)->getId()
84
<< " " << ((*it).second)->__repr__();
76
oss << separator << (*it).first << " => " << (((*it).second)->getVisibility()?"*":" ")<< " id=" << ((*it).second)->getId()
77
<< " " << ((*it).second)->__repr__();
83
for(LabelMap::const_iterator it = labelMap_.begin();
84
it != labelMap_.end();
85
++it, separator="\n ") {
86
oss << separator << "'" << (*it).first << "' is aliased to " << (*it).second;
91
/* String converter */
92
String Study::__str__() const
95
const char * separator = " ";
96
for(Map::const_iterator it = map_.begin(); it != map_.end(); it++, separator="\n ") {
97
oss << separator << (*it).first << " => " << ((*it).second)->__str__();
101
for(LabelMap::const_iterator it = labelMap_.begin();
102
it != labelMap_.end();
103
++it, separator="\n ") {
104
oss << separator << "'" << (*it).first << "' is aliased to " << (*it).second;
94
109
/* This method saves the study through the storage manager */
95
110
void Study::save()
97
p_storageManager_->initialize(StorageManager::SAVE);
99
MapCollection::const_iterator map_it;
100
for(map_it = maps.begin(); map_it != maps.end(); ++map_it) {
101
const ObjectMap & map = map_it->second;
102
ObjectMap::const_iterator it;
103
for(it = map.begin(); it != map.end(); it++) {
104
p_storageManager_->save(*(it->second), true);
112
p_storageManager_->initialize( SaveAction() );
114
for(LabelMap::const_iterator it = labelMap_.begin(); it != labelMap_.end(); ++it) {
115
p_storageManager_->save(*map_[ it->second ], it->first, true);
118
for(Map::const_iterator it = map_.begin(); it != map_.end(); it++) {
119
p_storageManager_->save(*(it->second), "", true);
108
122
p_storageManager_->write();
109
p_storageManager_->finalize(StorageManager::SAVE);
123
p_storageManager_->finalize( SaveAction() );
112
126
/* This method reloads the study from the storage manager */
113
127
void Study::load()
115
p_storageManager_->initialize(StorageManager::LOAD);
129
p_storageManager_->initialize( LoadAction() );
116
130
p_storageManager_->read();
117
131
p_storageManager_->load(*this);
118
p_storageManager_->finalize(StorageManager::LOAD);
132
p_storageManager_->finalize( LoadAction() );
120
134
cleanUnvisibleObject();
124
138
/* This method purges the study from the reloaded objects that are tagged unvisible */
125
139
void Study::cleanUnvisibleObject()
127
MapCollection::iterator map_it;
128
for(map_it = maps.begin(); map_it != maps.end(); ++map_it) {
129
ObjectMap & map = map_it->second;
131
ObjectMap::iterator it;
132
for(it = map.begin(); it != map.end(); ++it) {
133
if (it->second->getVisibility())
134
newMap[it->second->getShadowedId()] = it->second;
142
for(Map::const_iterator it = map_.begin(); it != map_.end(); ++it) {
143
if (it->second->getVisibility())
144
newMap[ it->second->getShadowedId() ] = it->second;
140
149
/* This method performs the translation of ids after a study load */
141
150
void Study::translateId()
143
MapCollection::iterator map_it;
144
for(map_it = maps.begin(); map_it != maps.end(); ++map_it) {
145
ObjectMap & map = map_it->second;
147
ObjectMap::iterator it;
148
for(it = map.begin(); it != map.end(); ++it) {
149
newMap[it->second->getId()] = it->second;
152
std::map<Id, Id> translationTable;
155
for(Map::const_iterator it = map_.begin(); it != map_.end(); ++it) {
156
translationTable[ it->first ] = it->second->getId();
157
newMap[ it->second->getId() ] = it->second;
160
LabelMap newLabelMap;
161
for(LabelMap::const_iterator it = labelMap_.begin(); it != labelMap_.end(); ++it)
162
newLabelMap[ it->first ] = translationTable[ it->second ];
165
labelMap_ = newLabelMap;
159
173
/* Query if object is stored in study */
160
174
Bool Study::hasObject(Id id) const
164
MapCollection::const_iterator map_it;
165
for(map_it = maps.begin(); !result && (map_it != maps.end()); map_it++) {
166
const ObjectMap & map = map_it->second;
167
result |= (map.find(id) != map.end());
176
Map::const_iterator it = map_.find( id );
177
return ( it != map_.end() );
173
180
/* Get object whose id is given */
174
181
Study::MapElement Study::getObject(Id id) const
176
if (!hasObject(id)) throw ObjectNotInStudyException(HERE) << "Object of id " << id << " is NOT in study";
177
183
MapElement element;
178
MapCollection::const_iterator map_it;
179
for(map_it = maps.begin(); map_it != maps.end(); map_it++) {
180
const ObjectMap & map = map_it->second;
181
ObjectMap::const_iterator it;
182
if ((it=map.find(id)) != map.end()) {
183
element = (*it).second;
184
Map::const_iterator it = map_.find( id );
185
if (it != map_.end()) {
186
element = (*it).second;
194
/* Query if object is stored in study */
195
Bool Study::hasObject(const String & label) const
197
if (label.empty()) return false;
198
LabelMap::const_iterator it = labelMap_.find( label );
199
return (it == labelMap_.end()) ? false : hasObject( it->second );
202
/* Get object whose id is given */
203
Study::MapElement Study::getObject(const String & label) const
205
LabelMap::const_iterator it_label = labelMap_.find( label );
206
if (it_label == labelMap_.end()) throw InvalidArgumentException(HERE) << "No object with label '" << label << "' in study";
207
return getObject( it_label->second );
192
214
/* Local class for the following method. Should have been declared inside the method but find_if crashes */
193
struct class_and_name_are
215
struct element_whose_class_and_name_are
197
class_and_name_are(const String & className, const String & name) : className_(className), name_(name) {}
198
Bool operator()(const Study::ObjectMap::value_type & element) const
217
const String & className_;
218
const String & name_;
219
element_whose_class_and_name_are(const String & className, const String & name) : className_(className), name_(name) {}
220
Bool operator()(const Study::Map::value_type & element) const
200
222
return (element.second->getClassName() == className_) && (element.second->getName() == name_);
205
227
Study::MapElement Study::getObjectByName(const String & className, const String & name) const
207
229
MapElement element;
208
MapCollection::const_iterator map_it;
209
for(map_it = maps.begin(); map_it != maps.end(); map_it++) {
210
const ObjectMap & map = map_it->second;
211
ObjectMap::const_iterator it;
212
if ((it=std::find_if(map.begin(),map.end(),class_and_name_are(className,name))) != map.end()) {
213
element = (*it).second;
230
Map::const_iterator it = std::find_if(map_.begin(), map_.end(), element_whose_class_and_name_are(className,name));
231
if (it != map_.end()) {
232
element = (*it).second;
221
238
/* Fill an object with one got from study */
222
void Study::fillObject(PersistentObject & po, const String & name) const
239
void Study::fillObjectByName(PersistentObject & po, const String & name) const
224
241
MapElement element = getObjectByName(po.getClassName(), name);
225
if (!element.isNull()) Catalog::GetInstance().get(po.getClassName()).assign(po, *element);
226
else throw InvalidArgumentException(HERE) << "No object of name " << name << " in study";
242
if (! element) throw InvalidArgumentException(HERE) << "No object of name " << name << " in study";
243
Catalog::GetInstance().get(po.getClassName()).assign(po, *element);
229
void Study::fillObject(InterfaceObject & io, const String & name) const
246
void Study::fillObjectByName(InterfaceObject & io, const String & name) const
231
248
MapElement element = getObjectByName(io.getImplementationAsPersistentObject()->getClassName(), name);
232
if (!element.isNull()) {
233
io.setImplementationAsPersistentObject(element);
234
} else throw InvalidArgumentException(HERE) << "No object of name " << name << " in study";
237
void Study::fillObject(PersistentObject & po, const Id id) const
239
MapElement element = getObject(id);
240
if (!element.isNull()) Catalog::GetInstance().get(po.getClassName()).assign(po, *element);
241
else throw InvalidArgumentException(HERE) << "No object of id " << id << " in study";
244
void Study::fillObject(InterfaceObject & io, const Id id) const
246
MapElement element = getObject(id);
247
if (!element.isNull()) {
248
io.setImplementationAsPersistentObject(element);
249
} else throw InvalidArgumentException(HERE) << "No object of id " << id << " in study";
249
if (! element) throw InvalidArgumentException(HERE) << "No object of name " << name << " in study";
250
io.setImplementationAsPersistentObject(element);
253
void Study::fillObject(Id id, PersistentObject & po) const
255
MapElement element = getObject(id);
256
if (! element) throw InvalidArgumentException(HERE) << "No object of id " << id << " in study";
257
Catalog::GetInstance().get(po.getClassName()).assign(po, *element);
260
void Study::fillObject(Id id, InterfaceObject & io) const
262
MapElement element = getObject(id);
263
if (! element) throw InvalidArgumentException(HERE) << "No object of id " << id << " in study";
264
io.setImplementationAsPersistentObject(element);
267
void Study::fillObject(const String & label, PersistentObject & po) const
269
MapElement element = getObject(label);
270
if (! element) throw InvalidArgumentException(HERE) << "No object labelled '" << label << "' in study";
271
Catalog::GetInstance().get(po.getClassName()).assign(po, *element);
274
void Study::fillObject(const String & label, InterfaceObject & io) const
276
MapElement element = getObject(label);
277
if (! element) throw InvalidArgumentException(HERE) << "No object labelled '" << label << "' in study";
278
io.setImplementationAsPersistentObject(element);
253
282
/* Storage manager accessor */
254
283
void Study::setStorageManager(const StorageManager & smgr)
267
/* Add a PersistentObject to the study (main map) */
268
void Study::add(const InterfaceObject & io, Map mapTag)
270
add(io, maps[mapTag] );
273
/* Remove a PersistentObject from the study (main map) */
274
void Study::remove(const InterfaceObject & io, Map mapTag)
276
remove(io, maps[mapTag] );
279
/* Add a PersistentObject to the study (any map) */
280
void Study::add(const InterfaceObject & io, ObjectMap & map)
282
map[io.getId()] = io.getImplementationAsPersistentObject();
285
/* Remove a PersistentObject from the study (any map) */
286
void Study::remove(const InterfaceObject & io, ObjectMap & map)
288
map.erase(io.getId());
293
/* Add a PersistentObject to the study (main map) */
294
void Study::add(const PersistentObject * po, Map mapTag)
296
add(po, maps[mapTag] );
299
/* Add a PersistentObject to the study (main map) */
300
void Study::add(const PersistentObject & po, Map mapTag)
302
add(po.clone(), maps[mapTag] );
305
/* Add a PersistentObject to the study (any map) */
306
void Study::add(const PersistentObject * po, ObjectMap & map)
308
map[po->getShadowedId()] = const_cast<PersistentObject *>(po);
296
/* Add a PersistentObject to the study */
297
void Study::add(const InterfaceObject & io)
299
map_[ io.getId() ] = io.getImplementationAsPersistentObject();
302
/* Add a PersistentObject to the study */
303
void Study::add(const String & label, const InterfaceObject & io, Bool force)
305
if ( hasObject( label ) ) {
306
if (force) remove( label );
307
else throw InvalidArgumentException(HERE) << "Label '" << label << "' already defined in study. Use 'force = true' to remove previously saved element before saving this one";
309
map_[ io.getId() ] = io.getImplementationAsPersistentObject();
310
if (! label.empty()) labelMap_[ label ] = io.getId();
313
/* Remove a PersistentObject from the study */
314
void Study::remove(const InterfaceObject & io)
316
Map::iterator it = map_.find( io.getId() );
320
/* Remove a PersistentObject from the study */
321
void Study::remove(const String & label)
323
LabelMap::iterator it_label = labelMap_.find( label );
324
if (it_label == labelMap_.end()) throw InvalidArgumentException(HERE) << "No object with label '" << label << "' in study";
325
Map::iterator it_obj = map_.find( it_label->second );
332
/* Add a PersistentObject to the study */
333
void Study::add(const PersistentObject & po)
335
std::cout<< " add "<<po.GetClassName()<<std::endl;
339
/* Add a PersistentObject to the study */
340
void Study::add(const String & label, const PersistentObject & po, Bool force)
342
add(label, po.clone(), force);
345
/* Add a PersistentObject to the study (any map) */
346
void Study::add(const PersistentObject * po)
348
if (! po) throw InvalidArgumentException(HERE) << "Null pointer passed to method";
349
map_[ po->getShadowedId() ] = const_cast<PersistentObject *>(po);
352
/* Add a PersistentObject to the study (any map) */
353
void Study::add(const String & label, const PersistentObject * po, Bool force)
355
if (! po) throw InvalidArgumentException(HERE) << "Null pointer passed to method";
356
if ( hasObject( label ) ) {
357
if (force) remove( label );
358
else throw InvalidArgumentException(HERE) << "Label '" << label << "' already defined in study. Use 'force = true' to remove previously saved element before saving this one";
360
map_[ po->getShadowedId() ] = const_cast<PersistentObject *>(po);
361
if (! label.empty()) labelMap_[ label ] = po->getShadowedId();