| 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 | #include "Patterns/Singleton_impl.hpp" | 
|---|
| 20 |  | 
|---|
| 21 | using namespace std; | 
|---|
| 22 |  | 
|---|
| 23 | /******************************* getter and setter ************************/ | 
|---|
| 24 | periodentafel *&World::getPeriode(){ | 
|---|
| 25 | return periode; | 
|---|
| 26 | } | 
|---|
| 27 |  | 
|---|
| 28 | // Atoms | 
|---|
| 29 |  | 
|---|
| 30 | atom* World::getAtom(AtomDescriptor descriptor){ | 
|---|
| 31 | return descriptor.find(); | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){ | 
|---|
| 35 | return descriptor.findAll(); | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | vector<atom*> World::getAllAtoms(){ | 
|---|
| 39 | return getAllAtoms(AllAtoms()); | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | int World::numAtoms(){ | 
|---|
| 43 | return atoms.size(); | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | // Molecules | 
|---|
| 47 |  | 
|---|
| 48 | molecule *World::getMolecule(MoleculeDescriptor descriptor){ | 
|---|
| 49 | return descriptor.find(); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){ | 
|---|
| 53 | return descriptor.findAll(); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | int World::numMolecules(){ | 
|---|
| 57 | return molecules_deprecated->ListOfMolecules.size(); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | /******************** Methods to change World state *********************/ | 
|---|
| 61 |  | 
|---|
| 62 | molecule* World::createMolecule(){ | 
|---|
| 63 | OBSERVE; | 
|---|
| 64 | molecule *mol = NULL; | 
|---|
| 65 | mol = NewMolecule(); | 
|---|
| 66 | assert(!molecules.count(currMoleculeId)); | 
|---|
| 67 | mol->setId(currMoleculeId++); | 
|---|
| 68 | // store the molecule by ID | 
|---|
| 69 | molecules[mol->getId()] = mol; | 
|---|
| 70 | mol->signOn(this); | 
|---|
| 71 | return mol; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | void World::destroyMolecule(molecule* mol){ | 
|---|
| 75 | OBSERVE; | 
|---|
| 76 | destroyMolecule(mol->getId()); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | void World::destroyMolecule(moleculeId_t id){ | 
|---|
| 80 | OBSERVE; | 
|---|
| 81 | molecule *mol = molecules[id]; | 
|---|
| 82 | assert(mol); | 
|---|
| 83 | DeleteMolecule(mol); | 
|---|
| 84 | molecules.erase(id); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 |  | 
|---|
| 88 | atom *World::createAtom(){ | 
|---|
| 89 | OBSERVE; | 
|---|
| 90 | atomId_t id = getNextAtomId(); | 
|---|
| 91 | atom *res = NewAtom(id); | 
|---|
| 92 | res->setWorld(this); | 
|---|
| 93 | // store the atom by ID | 
|---|
| 94 | atoms[res->getId()] = res; | 
|---|
| 95 | return res; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | int World::registerAtom(atom *atom){ | 
|---|
| 99 | OBSERVE; | 
|---|
| 100 | atomId_t id = getNextAtomId(); | 
|---|
| 101 | atom->setId(id); | 
|---|
| 102 | atom->setWorld(this); | 
|---|
| 103 | atoms[atom->getId()] = atom; | 
|---|
| 104 | return atom->getId(); | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | void World::destroyAtom(atom* atom){ | 
|---|
| 108 | OBSERVE; | 
|---|
| 109 | int id = atom->getId(); | 
|---|
| 110 | destroyAtom(id); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | void World::destroyAtom(atomId_t id) { | 
|---|
| 114 | OBSERVE; | 
|---|
| 115 | atom *atom = atoms[id]; | 
|---|
| 116 | assert(atom); | 
|---|
| 117 | DeleteAtom(atom); | 
|---|
| 118 | atoms.erase(id); | 
|---|
| 119 | releaseAtomId(id); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){ | 
|---|
| 123 | OBSERVE; | 
|---|
| 124 | // in case this call did not originate from inside the atom, we redirect it, | 
|---|
| 125 | // to also let it know that it has changed | 
|---|
| 126 | if(!target){ | 
|---|
| 127 | target = atoms[oldId]; | 
|---|
| 128 | assert(target && "Atom with that ID not found"); | 
|---|
| 129 | return target->changeId(newId); | 
|---|
| 130 | } | 
|---|
| 131 | else{ | 
|---|
| 132 | if(reserveAtomId(newId)){ | 
|---|
| 133 | atoms.erase(oldId); | 
|---|
| 134 | atoms.insert(pair<atomId_t,atom*>(newId,target)); | 
|---|
| 135 | return true; | 
|---|
| 136 | } | 
|---|
| 137 | else{ | 
|---|
| 138 | return false; | 
|---|
| 139 | } | 
|---|
| 140 | } | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){ | 
|---|
| 144 | return new ManipulateAtomsProcess(op, descr,name,true); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){ | 
|---|
| 148 | return manipulateAtoms(op,name,AllAtoms()); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | /********************* Internal Change methods for double Callback and Observer mechanism ********/ | 
|---|
| 152 |  | 
|---|
| 153 | void World::doManipulate(ManipulateAtomsProcess *proc){ | 
|---|
| 154 | proc->signOn(this); | 
|---|
| 155 | { | 
|---|
| 156 | OBSERVE; | 
|---|
| 157 | proc->doManipulate(this); | 
|---|
| 158 | } | 
|---|
| 159 | proc->signOff(this); | 
|---|
| 160 | } | 
|---|
| 161 | /******************************* IDManagement *****************************/ | 
|---|
| 162 |  | 
|---|
| 163 | // Atoms | 
|---|
| 164 |  | 
|---|
| 165 | atomId_t World::getNextAtomId(){ | 
|---|
| 166 | // see if we can reuse some Id | 
|---|
| 167 | if(atomIdPool.empty()){ | 
|---|
| 168 | return currAtomId++; | 
|---|
| 169 | } | 
|---|
| 170 | else{ | 
|---|
| 171 | // we give out the first ID from the pool | 
|---|
| 172 | atomId_t id = *(atomIdPool.begin()); | 
|---|
| 173 | atomIdPool.erase(id); | 
|---|
| 174 | return id; | 
|---|
| 175 | } | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | void World::releaseAtomId(atomId_t id){ | 
|---|
| 179 | atomIdPool.insert(id); | 
|---|
| 180 | // defragmentation of the pool | 
|---|
| 181 | set<atomId_t>::reverse_iterator iter; | 
|---|
| 182 | // go through all Ids in the pool that lie immediately below the border | 
|---|
| 183 | while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){ | 
|---|
| 184 | atomIdPool.erase(--currAtomId); | 
|---|
| 185 | } | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | bool World::reserveAtomId(atomId_t id){ | 
|---|
| 189 | if(id>=currAtomId ){ | 
|---|
| 190 | // add all ids between the new one and current border as available | 
|---|
| 191 | for(atomId_t pos=currAtomId; pos<id; ++pos){ | 
|---|
| 192 | atomIdPool.insert(pos); | 
|---|
| 193 | } | 
|---|
| 194 | currAtomId=id+1; | 
|---|
| 195 | return true; | 
|---|
| 196 | } | 
|---|
| 197 | else if(atomIdPool.count(id)){ | 
|---|
| 198 | atomIdPool.erase(id); | 
|---|
| 199 | return true; | 
|---|
| 200 | } | 
|---|
| 201 | else{ | 
|---|
| 202 | // this ID could not be reserved | 
|---|
| 203 | return false; | 
|---|
| 204 | } | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | // Molecules | 
|---|
| 208 |  | 
|---|
| 209 | /******************************* Iterators ********************************/ | 
|---|
| 210 |  | 
|---|
| 211 | /* | 
|---|
| 212 | * Actual Implementation of the iterators can be found in WorldIterators.cpp | 
|---|
| 213 | */ | 
|---|
| 214 |  | 
|---|
| 215 | World::AtomIterator World::getAtomIter(AtomDescriptor descr){ | 
|---|
| 216 | return AtomIterator(descr,this); | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | World::AtomSet::iterator World::atomEnd(){ | 
|---|
| 220 | return atoms.end(); | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){ | 
|---|
| 224 | return MoleculeIterator(descr,this); | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | World::MoleculeSet::iterator World::moleculeEnd(){ | 
|---|
| 228 | return molecules.end(); | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 | /******************************* Singleton Stuff **************************/ | 
|---|
| 232 |  | 
|---|
| 233 | World::World() : | 
|---|
| 234 | periode(new periodentafel), | 
|---|
| 235 | atoms(), | 
|---|
| 236 | currAtomId(0), | 
|---|
| 237 | molecules(), | 
|---|
| 238 | currMoleculeId(0), | 
|---|
| 239 | molecules_deprecated(new MoleculeListClass(this)) | 
|---|
| 240 | { | 
|---|
| 241 | molecules_deprecated->signOn(this); | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | World::~World() | 
|---|
| 245 | { | 
|---|
| 246 | molecules_deprecated->signOff(this); | 
|---|
| 247 | delete molecules_deprecated; | 
|---|
| 248 | delete periode; | 
|---|
| 249 | MoleculeSet::iterator molIter; | 
|---|
| 250 | for(molIter=molecules.begin();molIter!=molecules.end();++molIter){ | 
|---|
| 251 | DeleteMolecule((*molIter).second); | 
|---|
| 252 | } | 
|---|
| 253 | molecules.clear(); | 
|---|
| 254 | AtomSet::iterator atIter; | 
|---|
| 255 | for(atIter=atoms.begin();atIter!=atoms.end();++atIter){ | 
|---|
| 256 | DeleteAtom((*atIter).second); | 
|---|
| 257 | } | 
|---|
| 258 | atoms.clear(); | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | // Explicit instantiation of the singleton mechanism at this point | 
|---|
| 262 |  | 
|---|
| 263 | CONSTRUCT_SINGLETON(World) | 
|---|
| 264 |  | 
|---|
| 265 | /******************************* deprecated Legacy Stuff ***********************/ | 
|---|
| 266 |  | 
|---|
| 267 | MoleculeListClass *&World::getMolecules() { | 
|---|
| 268 | return molecules_deprecated; | 
|---|
| 269 | } | 
|---|