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