~ubuntu-branches/ubuntu/maverick/openturns/maverick

« back to all changes in this revision

Viewing changes to lib/src/Base/Common/Study.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2010-05-10 17:27:55 UTC
  • mfrom: (1.1.4 upstream) (5.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100510172755-cb5ynskknqqi5rhp
Tags: 0.13.2-2ubuntu1
* Merge with Debian testing. No changes left.
* ubuntu_fix-python-2.6.patch: fix detection of python 2.6 libs, to not use
  LOCALMODLIBS. This pulls a dependency on SSL and makes the package FTBFS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 *  @file  Study.cxx
4
4
 *  @brief Study keeps all PersistentObjects in a file
5
5
 *
6
 
 *  (C) Copyright 2005-2007 EDF-EADS-Phimeca
 
6
 *  (C) Copyright 2005-2010 EDF-EADS-Phimeca
7
7
 *
8
8
 *  This library is free software; you can redistribute it and/or
9
9
 *  modify it under the terms of the GNU Lesser General Public
20
20
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21
21
 *
22
22
 *  @author: $LastChangedBy: dutka $
23
 
 *  @date:   $LastChangedDate: 2009-05-28 14:47:53 +0200 (jeu. 28 mai 2009) $
24
 
 *  Id:      $Id: Study.cxx 1262 2009-05-28 12:47:53Z dutka $
 
23
 *  @date:   $LastChangedDate: 2010-02-04 16:44:49 +0100 (jeu. 04 févr. 2010) $
 
24
 *  Id:      $Id: Study.cxx 1473 2010-02-04 15:44:49Z dutka $
25
25
 */
26
26
#include <algorithm>
27
27
#include "InterfaceObject.hxx"
41
41
 
42
42
      CLASSNAMEINIT(Study);
43
43
 
44
 
      /* Assignment */
45
 
      // Study & Study::operator =(const Study & other);
46
 
 
47
 
 
48
44
      /*
49
45
       * Default constructor
50
46
       */
51
47
      Study::Study()
52
 
        : p_storageManager_(new StorageManager)
 
48
        : map_(),
 
49
          labelMap_(),
 
50
          p_storageManager_(new StorageManager)
53
51
      {
54
52
        p_storageManager_->setStudy(this);
55
53
      }
56
54
 
57
 
 
58
 
 
59
55
      /* String converter */
60
56
      String Study::__repr__() const
61
57
      {
62
58
        OSS oss;
63
59
        oss << "class=" << getClassName();
64
60
 
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();
 
63
            it != map_.end();
 
64
            it++, separator="\n  ") {
72
65
#if 0
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();
76
69
#endif
77
70
#if 0
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__();
81
74
#endif
82
75
#if 1
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__();
85
78
#endif
86
 
          }
87
 
          oss << "\n}";
88
 
        }
89
 
 
90
 
        return oss;
91
 
      }
92
 
 
 
79
        }
 
80
 
 
81
        oss << "\n";
 
82
        separator = "  ";
 
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;
 
87
        }
 
88
        return oss;
 
89
      }
 
90
 
 
91
      /* String converter */
 
92
      String Study::__str__() const
 
93
      {
 
94
        OSS oss;
 
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__();
 
98
        }
 
99
        oss << "\n";
 
100
        separator = "  ";
 
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;
 
105
        }
 
106
        return oss;
 
107
      }
93
108
 
94
109
      /* This method saves the study through the storage manager */
95
110
      void Study::save()
96
111
      {
97
 
        p_storageManager_->initialize(StorageManager::SAVE);
98
 
 
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);
105
 
          }
 
112
        p_storageManager_->initialize( SaveAction() );
 
113
 
 
114
        for(LabelMap::const_iterator it = labelMap_.begin(); it != labelMap_.end(); ++it) {
 
115
          p_storageManager_->save(*map_[ it->second ], it->first, true);
 
116
        }
 
117
 
 
118
        for(Map::const_iterator it = map_.begin(); it != map_.end(); it++) {
 
119
          p_storageManager_->save(*(it->second), "", true);
106
120
        }
107
121
 
108
122
        p_storageManager_->write();
109
 
        p_storageManager_->finalize(StorageManager::SAVE);
 
123
        p_storageManager_->finalize( SaveAction() );
110
124
      }
111
125
 
112
126
      /* This method reloads the study from the storage manager */
113
127
      void Study::load()
114
128
      {
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() );
119
133
 
120
134
        cleanUnvisibleObject();
121
135
        translateId();
124
138
      /* This method purges the study from the reloaded objects that are tagged unvisible */
125
139
      void Study::cleanUnvisibleObject()
126
140
      {
127
 
        MapCollection::iterator map_it;
128
 
        for(map_it = maps.begin(); map_it != maps.end(); ++map_it) {
129
 
          ObjectMap & map = map_it->second;
130
 
          ObjectMap newMap;
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;
135
 
          }
136
 
          map = newMap;
 
141
        Map newMap;
 
142
        for(Map::const_iterator it = map_.begin(); it != map_.end(); ++it) {
 
143
          if (it->second->getVisibility())
 
144
            newMap[ it->second->getShadowedId() ] = it->second;
137
145
        }
 
146
        map_ = newMap;
138
147
      }
139
148
 
140
149
      /* This method performs the translation of ids after a study load */
141
150
      void Study::translateId()
142
151
      {
143
 
        MapCollection::iterator map_it;
144
 
        for(map_it = maps.begin(); map_it != maps.end(); ++map_it) {
145
 
          ObjectMap & map = map_it->second;
146
 
          ObjectMap newMap;
147
 
          ObjectMap::iterator it;
148
 
          for(it = map.begin(); it != map.end(); ++it) {
149
 
            newMap[it->second->getId()] = it->second;
150
 
          }
151
 
          map = newMap;
 
152
        std::map<Id, Id> translationTable;
 
153
 
 
154
        Map newMap;
 
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;
152
158
        }
 
159
 
 
160
        LabelMap newLabelMap;
 
161
        for(LabelMap::const_iterator it = labelMap_.begin(); it != labelMap_.end(); ++it)
 
162
          newLabelMap[ it->first ] = translationTable[ it->second ];
 
163
 
 
164
        map_      = newMap;
 
165
        labelMap_ = newLabelMap;
153
166
      }
154
167
 
155
168
 
156
169
 
157
170
 
158
171
 
 
172
 
159
173
      /* Query if object is stored in study */
160
174
      Bool Study::hasObject(Id id) const
161
175
      {
162
 
        Bool result = false;
163
 
 
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());
168
 
        }
169
 
        
170
 
        return result;
 
176
        Map::const_iterator it = map_.find( id );
 
177
        return ( it != map_.end() );
171
178
      }
172
 
 
 
179
      
173
180
      /* Get object whose id is given */
174
181
      Study::MapElement Study::getObject(Id id) const
175
182
      {
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
 
            break;
185
 
          }
 
184
        Map::const_iterator it = map_.find( id );
 
185
        if (it != map_.end()) {
 
186
          element = (*it).second;
186
187
        }
187
188
        return element;
188
189
      }
189
190
 
190
191
 
191
192
 
 
193
 
 
194
      /* Query if object is stored in study */
 
195
      Bool Study::hasObject(const String & label) const
 
196
      {
 
197
        if (label.empty()) return false;
 
198
        LabelMap::const_iterator it = labelMap_.find( label );
 
199
        return (it == labelMap_.end()) ? false : hasObject( it->second );
 
200
      }
 
201
      
 
202
      /* Get object whose id is given */
 
203
      Study::MapElement Study::getObject(const String & label) const
 
204
      {
 
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 );
 
208
      }
 
209
 
 
210
 
 
211
 
 
212
 
 
213
 
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
194
216
      {
195
 
        String className_;
196
 
        String name_;
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
199
221
        {
200
222
          return (element.second->getClassName() == className_) && (element.second->getName() == name_);
201
223
        }
205
227
      Study::MapElement Study::getObjectByName(const String & className, const String & name) const
206
228
      {
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;
214
 
            break;
215
 
          }
 
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;
216
233
        }
217
234
        return element;
218
235
      }
219
236
 
220
237
 
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
223
240
      {
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);
227
244
      }
228
245
 
229
 
      void Study::fillObject(InterfaceObject  & io, const String & name) const
 
246
      void Study::fillObjectByName(InterfaceObject  & io, const String & name) const
230
247
      {
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";     
235
 
      }
236
 
 
237
 
      void Study::fillObject(PersistentObject & po, const Id id) const
238
 
      {
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";
242
 
      }
243
 
 
244
 
      void Study::fillObject(InterfaceObject & io, const Id id) const
245
 
      {
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"; 
250
 
      }
251
 
 
252
 
 
 
249
        if (! element) throw InvalidArgumentException(HERE) << "No object of name " << name << " in study";
 
250
        io.setImplementationAsPersistentObject(element);
 
251
      }
 
252
 
 
253
      void Study::fillObject(Id id, PersistentObject & po) const
 
254
      {
 
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);
 
258
      }
 
259
 
 
260
      void Study::fillObject(Id id, InterfaceObject & io) const
 
261
      {
 
262
        MapElement element = getObject(id);
 
263
        if (! element) throw InvalidArgumentException(HERE) << "No object of id " << id << " in study";
 
264
        io.setImplementationAsPersistentObject(element);
 
265
      }
 
266
 
 
267
      void Study::fillObject(const String & label, PersistentObject & po) const
 
268
      {
 
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);
 
272
      }
 
273
 
 
274
      void Study::fillObject(const String & label, InterfaceObject & io) const
 
275
      {
 
276
        MapElement element = getObject(label);
 
277
        if (! element) throw InvalidArgumentException(HERE) << "No object labelled '" << label << "' in study";
 
278
        io.setImplementationAsPersistentObject(element);
 
279
      }
 
280
 
 
281
      
253
282
      /* Storage manager accessor */
254
283
      void Study::setStorageManager(const StorageManager & smgr)
255
284
      {
264
293
      }
265
294
 
266
295
 
267
 
      /* Add a PersistentObject to the study (main map) */
268
 
      void Study::add(const InterfaceObject & io, Map mapTag)
269
 
      {
270
 
        add(io, maps[mapTag] );
271
 
      }
272
 
      
273
 
      /* Remove a PersistentObject from the study (main map) */
274
 
      void Study::remove(const InterfaceObject & io, Map mapTag)
275
 
      {
276
 
        remove(io, maps[mapTag] );
277
 
      }
278
 
 
279
 
      /* Add a PersistentObject to the study (any map) */
280
 
      void Study::add(const InterfaceObject & io, ObjectMap & map)
281
 
      {
282
 
        map[io.getId()] = io.getImplementationAsPersistentObject();
283
 
      }
284
 
      
285
 
      /* Remove a PersistentObject from the study (any map) */
286
 
      void Study::remove(const InterfaceObject & io, ObjectMap & map)
287
 
      {
288
 
        map.erase(io.getId());
289
 
      }
290
 
 
291
 
 
292
 
 
293
 
      /* Add a PersistentObject to the study (main map) */
294
 
      void Study::add(const PersistentObject * po, Map mapTag)
295
 
      {
296
 
        add(po, maps[mapTag] );
297
 
      }
298
 
      
299
 
      /* Add a PersistentObject to the study (main map) */
300
 
      void Study::add(const PersistentObject & po, Map mapTag)
301
 
      {
302
 
        add(po.clone(), maps[mapTag] );
303
 
      }
304
 
      
305
 
      /* Add a PersistentObject to the study (any map) */
306
 
      void Study::add(const PersistentObject * po, ObjectMap & map)
307
 
      {
308
 
        map[po->getShadowedId()] = const_cast<PersistentObject *>(po);
 
296
      /* Add a PersistentObject to the study */
 
297
      void Study::add(const InterfaceObject & io)
 
298
      {
 
299
        map_[ io.getId() ] = io.getImplementationAsPersistentObject();
 
300
      }
 
301
      
 
302
      /* Add a PersistentObject to the study */
 
303
      void Study::add(const String & label, const InterfaceObject & io, Bool force)
 
304
      {
 
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";
 
308
        }
 
309
        map_[ io.getId() ] = io.getImplementationAsPersistentObject();
 
310
        if (! label.empty()) labelMap_[ label ] = io.getId();
 
311
      }
 
312
      
 
313
      /* Remove a PersistentObject from the study */
 
314
      void Study::remove(const InterfaceObject & io)
 
315
      {
 
316
        Map::iterator it = map_.find( io.getId() );
 
317
        map_.erase(it);
 
318
      }
 
319
 
 
320
      /* Remove a PersistentObject from the study */
 
321
      void Study::remove(const String & label)
 
322
      {
 
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 );
 
326
        map_.erase(it_obj);
 
327
      }
 
328
 
 
329
 
 
330
 
 
331
     
 
332
      /* Add a PersistentObject to the study */
 
333
      void Study::add(const PersistentObject & po)
 
334
      {
 
335
  std::cout<< " add "<<po.GetClassName()<<std::endl;
 
336
        add(po.clone());
 
337
      }
 
338
      
 
339
      /* Add a PersistentObject to the study */
 
340
      void Study::add(const String & label, const PersistentObject & po, Bool force)
 
341
      {
 
342
        add(label, po.clone(), force);
 
343
      }
 
344
      
 
345
      /* Add a PersistentObject to the study (any map) */
 
346
      void Study::add(const PersistentObject * po)
 
347
      {
 
348
        if (! po) throw InvalidArgumentException(HERE) << "Null pointer passed to method";      
 
349
        map_[ po->getShadowedId() ] = const_cast<PersistentObject *>(po);
 
350
      }
 
351
      
 
352
      /* Add a PersistentObject to the study (any map) */
 
353
      void Study::add(const String & label, const PersistentObject * po, Bool force)
 
354
      {
 
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";
 
359
        }
 
360
        map_[ po->getShadowedId() ] = const_cast<PersistentObject *>(po);
 
361
        if (! label.empty()) labelMap_[ label ] = po->getShadowedId();
309
362
      }
310
363
      
311
364