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