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