| 1 | /*
 | 
|---|
| 2 |  * World.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Feb 3, 2010
 | 
|---|
| 5 |  *      Author: crueger
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "World.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "atom.hpp"
 | 
|---|
| 11 | #include "molecule.hpp"
 | 
|---|
| 12 | #include "periodentafel.hpp"
 | 
|---|
| 13 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
| 14 | #include "Descriptors/AtomDescriptor_impl.hpp"
 | 
|---|
| 15 | #include "Descriptors/MoleculeDescriptor.hpp"
 | 
|---|
| 16 | #include "Descriptors/MoleculeDescriptor_impl.hpp"
 | 
|---|
| 17 | #include "Actions/ManipulateAtomsProcess.hpp"
 | 
|---|
| 18 | 
 | 
|---|
| 19 | using namespace std;
 | 
|---|
| 20 | 
 | 
|---|
| 21 | /******************************* getter and setter ************************/
 | 
|---|
| 22 | periodentafel *&World::getPeriode(){
 | 
|---|
| 23 |   return periode;
 | 
|---|
| 24 | }
 | 
|---|
| 25 | 
 | 
|---|
| 26 | // Atoms
 | 
|---|
| 27 | 
 | 
|---|
| 28 | atom* World::getAtom(AtomDescriptor descriptor){
 | 
|---|
| 29 |   return descriptor.find();
 | 
|---|
| 30 | }
 | 
|---|
| 31 | 
 | 
|---|
| 32 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
 | 
|---|
| 33 |   return descriptor.findAll();
 | 
|---|
| 34 | }
 | 
|---|
| 35 | 
 | 
|---|
| 36 | vector<atom*> World::getAllAtoms(){
 | 
|---|
| 37 |   return getAllAtoms(AllAtoms());
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | int World::numAtoms(){
 | 
|---|
| 41 |   return atoms.size();
 | 
|---|
| 42 | }
 | 
|---|
| 43 | 
 | 
|---|
| 44 | // Molecules
 | 
|---|
| 45 | 
 | 
|---|
| 46 | molecule *World::getMolecule(MoleculeDescriptor descriptor){
 | 
|---|
| 47 |   return descriptor.find();
 | 
|---|
| 48 | }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
 | 
|---|
| 51 |   return descriptor.findAll();
 | 
|---|
| 52 | }
 | 
|---|
| 53 | 
 | 
|---|
| 54 | int World::numMolecules(){
 | 
|---|
| 55 |   return molecules_deprecated->ListOfMolecules.size();
 | 
|---|
| 56 | }
 | 
|---|
| 57 | 
 | 
|---|
| 58 | /******************** Methods to change World state *********************/
 | 
|---|
| 59 | 
 | 
|---|
| 60 | molecule* World::createMolecule(){
 | 
|---|
| 61 |   OBSERVE;
 | 
|---|
| 62 |   molecule *mol = NULL;
 | 
|---|
| 63 |   mol = NewMolecule();
 | 
|---|
| 64 |   assert(!molecules.count(currMoleculeId));
 | 
|---|
| 65 |   mol->setId(currMoleculeId++);
 | 
|---|
| 66 |   // store the molecule by ID
 | 
|---|
| 67 |   molecules[mol->getId()] = mol;
 | 
|---|
| 68 |   mol->signOn(this);
 | 
|---|
| 69 |   return mol;
 | 
|---|
| 70 | }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | void World::destroyMolecule(molecule* mol){
 | 
|---|
| 73 |   OBSERVE;
 | 
|---|
| 74 |   destroyMolecule(mol->getId());
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | void World::destroyMolecule(moleculeId_t id){
 | 
|---|
| 78 |   OBSERVE;
 | 
|---|
| 79 |   molecule *mol = molecules[id];
 | 
|---|
| 80 |   assert(mol);
 | 
|---|
| 81 |   DeleteMolecule(mol);
 | 
|---|
| 82 |   molecules.erase(id);
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | 
 | 
|---|
| 86 | atom *World::createAtom(){
 | 
|---|
| 87 |   OBSERVE;
 | 
|---|
| 88 |   atomId_t id = getNextAtomId();
 | 
|---|
| 89 |   atom *res = NewAtom(id);
 | 
|---|
| 90 |   res->setWorld(this);
 | 
|---|
| 91 |   // store the atom by ID
 | 
|---|
| 92 |   atoms[res->getId()] = res;
 | 
|---|
| 93 |   return res;
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | int World::registerAtom(atom *atom){
 | 
|---|
| 97 |   OBSERVE;
 | 
|---|
| 98 |   atomId_t id = getNextAtomId();
 | 
|---|
| 99 |   atom->setId(id);
 | 
|---|
| 100 |   atom->setWorld(this);
 | 
|---|
| 101 |   atoms[atom->getId()] = atom;
 | 
|---|
| 102 |   return atom->getId();
 | 
|---|
| 103 | }
 | 
|---|
| 104 | 
 | 
|---|
| 105 | void World::destroyAtom(atom* atom){
 | 
|---|
| 106 |   OBSERVE;
 | 
|---|
| 107 |   int id = atom->getId();
 | 
|---|
| 108 |   destroyAtom(id);
 | 
|---|
| 109 | }
 | 
|---|
| 110 | 
 | 
|---|
| 111 | void World::destroyAtom(atomId_t id) {
 | 
|---|
| 112 |   OBSERVE;
 | 
|---|
| 113 |   atom *atom = atoms[id];
 | 
|---|
| 114 |   assert(atom);
 | 
|---|
| 115 |   DeleteAtom(atom);
 | 
|---|
| 116 |   atoms.erase(id);
 | 
|---|
| 117 |   releaseAtomId(id);
 | 
|---|
| 118 | }
 | 
|---|
| 119 | 
 | 
|---|
| 120 | bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
 | 
|---|
| 121 |   OBSERVE;
 | 
|---|
| 122 |   // in case this call did not originate from inside the atom, we redirect it,
 | 
|---|
| 123 |   // to also let it know that it has changed
 | 
|---|
| 124 |   if(!target){
 | 
|---|
| 125 |     target = atoms[oldId];
 | 
|---|
| 126 |     assert(target && "Atom with that ID not found");
 | 
|---|
| 127 |     return target->changeId(newId);
 | 
|---|
| 128 |   }
 | 
|---|
| 129 |   else{
 | 
|---|
| 130 |     if(reserveAtomId(newId)){
 | 
|---|
| 131 |       atoms.erase(oldId);
 | 
|---|
| 132 |       atoms.insert(pair<atomId_t,atom*>(newId,target));
 | 
|---|
| 133 |       return true;
 | 
|---|
| 134 |     }
 | 
|---|
| 135 |     else{
 | 
|---|
| 136 |       return false;
 | 
|---|
| 137 |     }
 | 
|---|
| 138 |   }
 | 
|---|
| 139 | }
 | 
|---|
| 140 | 
 | 
|---|
| 141 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
 | 
|---|
| 142 |   return new ManipulateAtomsProcess(op, descr,name,true);
 | 
|---|
| 143 | }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
 | 
|---|
| 146 |   return manipulateAtoms(op,name,AllAtoms());
 | 
|---|
| 147 | }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | /********************* Internal Change methods for double Callback and Observer mechanism ********/
 | 
|---|
| 150 | 
 | 
|---|
| 151 | void World::doManipulate(ManipulateAtomsProcess *proc){
 | 
|---|
| 152 |   proc->signOn(this);
 | 
|---|
| 153 |   {
 | 
|---|
| 154 |     OBSERVE;
 | 
|---|
| 155 |     proc->doManipulate(this);
 | 
|---|
| 156 |   }
 | 
|---|
| 157 |   proc->signOff(this);
 | 
|---|
| 158 | }
 | 
|---|
| 159 | /******************************* IDManagement *****************************/
 | 
|---|
| 160 | 
 | 
|---|
| 161 | // Atoms
 | 
|---|
| 162 | 
 | 
|---|
| 163 | atomId_t World::getNextAtomId(){
 | 
|---|
| 164 |   // see if we can reuse some Id
 | 
|---|
| 165 |   if(atomIdPool.empty()){
 | 
|---|
| 166 |     return currAtomId++;
 | 
|---|
| 167 |   }
 | 
|---|
| 168 |   else{
 | 
|---|
| 169 |     // we give out the first ID from the pool
 | 
|---|
| 170 |     atomId_t id = *(atomIdPool.begin());
 | 
|---|
| 171 |     atomIdPool.erase(id);
 | 
|---|
| 172 |     return id;
 | 
|---|
| 173 |   }
 | 
|---|
| 174 | }
 | 
|---|
| 175 | 
 | 
|---|
| 176 | void World::releaseAtomId(atomId_t id){
 | 
|---|
| 177 |   atomIdPool.insert(id);
 | 
|---|
| 178 |   // defragmentation of the pool
 | 
|---|
| 179 |   set<atomId_t>::reverse_iterator iter;
 | 
|---|
| 180 |   // go through all Ids in the pool that lie immediately below the border
 | 
|---|
| 181 |   while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){
 | 
|---|
| 182 |     atomIdPool.erase(--currAtomId);
 | 
|---|
| 183 |   }
 | 
|---|
| 184 | }
 | 
|---|
| 185 | 
 | 
|---|
| 186 | bool World::reserveAtomId(atomId_t id){
 | 
|---|
| 187 |   if(id>=currAtomId ){
 | 
|---|
| 188 |     // add all ids between the new one and current border as available
 | 
|---|
| 189 |     for(atomId_t pos=currAtomId; pos<id; ++pos){
 | 
|---|
| 190 |       atomIdPool.insert(pos);
 | 
|---|
| 191 |     }
 | 
|---|
| 192 |     currAtomId=id+1;
 | 
|---|
| 193 |     return true;
 | 
|---|
| 194 |   }
 | 
|---|
| 195 |   else if(atomIdPool.count(id)){
 | 
|---|
| 196 |     atomIdPool.erase(id);
 | 
|---|
| 197 |     return true;
 | 
|---|
| 198 |   }
 | 
|---|
| 199 |   else{
 | 
|---|
| 200 |     // this ID could not be reserved
 | 
|---|
| 201 |     return false;
 | 
|---|
| 202 |   }
 | 
|---|
| 203 | }
 | 
|---|
| 204 | 
 | 
|---|
| 205 | // Molecules
 | 
|---|
| 206 | 
 | 
|---|
| 207 | /******************************* Iterators ********************************/
 | 
|---|
| 208 | 
 | 
|---|
| 209 | /*
 | 
|---|
| 210 |  * Actual Implementation of the iterators can be found in WorldIterators.cpp
 | 
|---|
| 211 |  */
 | 
|---|
| 212 | 
 | 
|---|
| 213 | World::AtomIterator World::getAtomIter(AtomDescriptor descr){
 | 
|---|
| 214 |   return AtomIterator(descr,this);
 | 
|---|
| 215 | }
 | 
|---|
| 216 | 
 | 
|---|
| 217 | World::AtomSet::iterator World::atomEnd(){
 | 
|---|
| 218 |   return atoms.end();
 | 
|---|
| 219 | }
 | 
|---|
| 220 | 
 | 
|---|
| 221 | World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
 | 
|---|
| 222 |   return MoleculeIterator(descr,this);
 | 
|---|
| 223 | }
 | 
|---|
| 224 | 
 | 
|---|
| 225 | World::MoleculeSet::iterator World::moleculeEnd(){
 | 
|---|
| 226 |   return molecules.end();
 | 
|---|
| 227 | }
 | 
|---|
| 228 | 
 | 
|---|
| 229 | /******************************* Singleton Stuff **************************/
 | 
|---|
| 230 | 
 | 
|---|
| 231 | // TODO: Hide boost-thread using Autotools stuff when no threads are used
 | 
|---|
| 232 | World* World::theWorld = 0;
 | 
|---|
| 233 | boost::mutex World::worldLock;
 | 
|---|
| 234 | 
 | 
|---|
| 235 | World::World() :
 | 
|---|
| 236 |     periode(new periodentafel),
 | 
|---|
| 237 |     atoms(),
 | 
|---|
| 238 |     currAtomId(0),
 | 
|---|
| 239 |     molecules(),
 | 
|---|
| 240 |     currMoleculeId(0),
 | 
|---|
| 241 |     molecules_deprecated(new MoleculeListClass(this))
 | 
|---|
| 242 | {
 | 
|---|
| 243 |   molecules_deprecated->signOn(this);
 | 
|---|
| 244 | }
 | 
|---|
| 245 | 
 | 
|---|
| 246 | World::~World()
 | 
|---|
| 247 | {
 | 
|---|
| 248 |   molecules_deprecated->signOff(this);
 | 
|---|
| 249 |   delete molecules_deprecated;
 | 
|---|
| 250 |   delete periode;
 | 
|---|
| 251 |   MoleculeSet::iterator molIter;
 | 
|---|
| 252 |   for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
 | 
|---|
| 253 |     DeleteMolecule((*molIter).second);
 | 
|---|
| 254 |   }
 | 
|---|
| 255 |   molecules.clear();
 | 
|---|
| 256 |   AtomSet::iterator atIter;
 | 
|---|
| 257 |   for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
 | 
|---|
| 258 |     DeleteAtom((*atIter).second);
 | 
|---|
| 259 |   }
 | 
|---|
| 260 |   atoms.clear();
 | 
|---|
| 261 | }
 | 
|---|
| 262 | 
 | 
|---|
| 263 | World* World::get(){
 | 
|---|
| 264 |   // boost supports RAII-Style locking, so we don't need to unlock
 | 
|---|
| 265 |   boost::mutex::scoped_lock guard(worldLock);
 | 
|---|
| 266 |   if(!theWorld) {
 | 
|---|
| 267 |     theWorld = new World();
 | 
|---|
| 268 |   }
 | 
|---|
| 269 |   return theWorld;
 | 
|---|
| 270 | }
 | 
|---|
| 271 | 
 | 
|---|
| 272 | void World::destroy(){
 | 
|---|
| 273 |   // boost supports RAII-Style locking, so we don't need to unlock
 | 
|---|
| 274 |   boost::mutex::scoped_lock guard(worldLock);
 | 
|---|
| 275 |   delete theWorld;
 | 
|---|
| 276 |   theWorld = 0;
 | 
|---|
| 277 | }
 | 
|---|
| 278 | 
 | 
|---|
| 279 | World* World::reset(){
 | 
|---|
| 280 |   World* oldWorld = 0;
 | 
|---|
| 281 |   {
 | 
|---|
| 282 |     // boost supports RAII-Style locking, so we don't need to unlock
 | 
|---|
| 283 |     boost::mutex::scoped_lock guard(worldLock);
 | 
|---|
| 284 | 
 | 
|---|
| 285 |     oldWorld = theWorld;
 | 
|---|
| 286 |     theWorld = new World();
 | 
|---|
| 287 |     // oldworld does not need protection any more,
 | 
|---|
| 288 |     // since we should have the only reference
 | 
|---|
| 289 | 
 | 
|---|
| 290 |     // worldLock handles access to the pointer,
 | 
|---|
| 291 |     // not to the object
 | 
|---|
| 292 |   } // scope-end releases the lock
 | 
|---|
| 293 | 
 | 
|---|
| 294 |   // we have to let all the observers know that the
 | 
|---|
| 295 |   // oldWorld was destroyed. oldWorld calls subjectKilled
 | 
|---|
| 296 |   // upon destruction. Every Observer getting that signal
 | 
|---|
| 297 |   // should see that it gets the updated new world
 | 
|---|
| 298 |   delete oldWorld;
 | 
|---|
| 299 |   return theWorld;
 | 
|---|
| 300 | }
 | 
|---|
| 301 | 
 | 
|---|
| 302 | /******************************* deprecated Legacy Stuff ***********************/
 | 
|---|
| 303 | 
 | 
|---|
| 304 | MoleculeListClass *&World::getMolecules() {
 | 
|---|
| 305 |   return molecules_deprecated;
 | 
|---|
| 306 | }
 | 
|---|