| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /** | 
|---|
| 9 | * \file observers_observables.dox | 
|---|
| 10 | * | 
|---|
| 11 | * Created on: Oct 12, 2011 | 
|---|
| 12 | *    Author: heber | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | /** \page observers Observers and Observables | 
|---|
| 16 | * | 
|---|
| 17 | * The Observer/Observerable mechanism is crucial to let different and | 
|---|
| 18 | * independent components know about changes among one another. E.g. when an | 
|---|
| 19 | * the element of an atom changes, the GUI (\ref graphical) needs to know | 
|---|
| 20 | * about this change to plot the atom with a different color or shape. | 
|---|
| 21 | * | 
|---|
| 22 | * The Observer/Observerable mechanism is basically just a call-back: A | 
|---|
| 23 | * class announces itself as Observable with having a specific interface | 
|---|
| 24 | * of functions. Observer may signOn() to this class and have a specific | 
|---|
| 25 | * function (update()) be called whenever a change occurs in the Observable | 
|---|
| 26 | * class. | 
|---|
| 27 | * | 
|---|
| 28 | * Note that additionally an Observable may have various \a channels and | 
|---|
| 29 | * Observers may signOn() to just such a channel to get a very specific | 
|---|
| 30 | * update. E.g. a LinkedCell class needs to know when the position of an | 
|---|
| 31 | * atom changes but it does not need to know about changes of element or | 
|---|
| 32 | * velocity, ... These updates are called \a Notifications. | 
|---|
| 33 | * | 
|---|
| 34 | * As an example: | 
|---|
| 35 | * \code | 
|---|
| 36 | * World::getInstance().signOn(this, World::getInstance().getChannel(World::AtomInserted)); | 
|---|
| 37 | * \endcode | 
|---|
| 38 | * Here, in the constructor of GLWorldView the class signs on to atoms | 
|---|
| 39 | * being inserted into the World such that it can add these onto the display | 
|---|
| 40 | * as well. Notifications are received via recieveNotifications(), e.g. | 
|---|
| 41 | * which you have to implement for an Observer class | 
|---|
| 42 | * \code | 
|---|
| 43 | *   switch (notification->getChannelNo()) { | 
|---|
| 44 | *   case World::AtomInserted: | 
|---|
| 45 | *   { | 
|---|
| 46 | *     const atom *_atom = World::getInstance().lastChanged<atom>(); | 
|---|
| 47 | *     emit atomInserted(_atom); | 
|---|
| 48 | *     break; | 
|---|
| 49 | *   } | 
|---|
| 50 | * \endcode | 
|---|
| 51 | * Here, we just switch over all events that we process and care about (note | 
|---|
| 52 | * that we only get called for those for which we have subscribed) and proceed. | 
|---|
| 53 | * | 
|---|
| 54 | * \note There is a complete logging mechanism available for this. However, | 
|---|
| 55 | * it has to be compiled via a specific switch (\ref install). So, when | 
|---|
| 56 | * you need to know why your function isn't notified, that's the way to | 
|---|
| 57 | * find out. | 
|---|
| 58 | * | 
|---|
| 59 | * Be wary of where the update occurs: while the World tells you about new | 
|---|
| 60 | * or destroyed atoms, only the atom itself tells you when its position has | 
|---|
| 61 | * changed! | 
|---|
| 62 | * | 
|---|
| 63 | * \section observers-where Who observes what? | 
|---|
| 64 | * | 
|---|
| 65 | * Where to look for a specific update is very important. Hence, we list here | 
|---|
| 66 | * all (major) Observables and what exactly they report in their channels: | 
|---|
| 67 | *  -# \ref World: Gives updates on insertion or removal of atoms and molecules. | 
|---|
| 68 | *     It does not tell whether the inner state of either has changed. | 
|---|
| 69 | *  -# \ref atom: Gives updates on changes to inner state of an atom, i.e. | 
|---|
| 70 | *    -# bonds | 
|---|
| 71 | *    -# element | 
|---|
| 72 | *    -# position | 
|---|
| 73 | *    It does not tell when e.g. it is associated to another molecule and | 
|---|
| 74 | *    like-wise not if its AtomInfo::Nr changes, as this is given by the | 
|---|
| 75 | *    molecule also. | 
|---|
| 76 | *  -# \ref AtomObserver: Gives updates on changes occuring in any atom. It | 
|---|
| 77 | *     acts as a relay that observes all atoms at once and gives notice when | 
|---|
| 78 | *     a single one has changed. It does not tell when a new has arrived or | 
|---|
| 79 | *     one has been removed. | 
|---|
| 80 | *  -# \ref molecule: Gives updates on changes occuring in its internal state, | 
|---|
| 81 | *     e.g. atoms being inserted or removed. It does not tell when atoms themself | 
|---|
| 82 | *     change. | 
|---|
| 83 | *  -# \ref Box: Gives updates on changes of its box matrix or boundary | 
|---|
| 84 | *     conditions. | 
|---|
| 85 | * | 
|---|
| 86 | * It should be adhered to these Does and Don'ts strictly. Each of these entities | 
|---|
| 87 | * can only report about stuff it knows about. The World controls insertion and | 
|---|
| 88 | * removal, hence its updates are concerned with these only! | 
|---|
| 89 | * | 
|---|
| 90 | * | 
|---|
| 91 | * \section observers-world Observers and the World | 
|---|
| 92 | * | 
|---|
| 93 | * The World, containing the arrays of all atoms and molecules, is a bit | 
|---|
| 94 | * special with these observers. E.g. when you request a non-const array | 
|---|
| 95 | * of atoms you receive an ObservedIterator. That is one where automatically | 
|---|
| 96 | * it is assumed that you changed something and hence update() is called | 
|---|
| 97 | * after you stepped over one of its elements. | 
|---|
| 98 | * | 
|---|
| 99 | * Thus, use const-iterators and arrays whenever possible, first to avoid | 
|---|
| 100 | * this overhead, but second to avoid making pain-in-the-ass, hard-to-find | 
|---|
| 101 | * mistakes. | 
|---|
| 102 | * | 
|---|
| 103 | * Also, the world stores specific information about what changed in the | 
|---|
| 104 | * template function World::lastChanged() where it might contain reference | 
|---|
| 105 | * to an atom that is about to be destroyed or just added. | 
|---|
| 106 | * | 
|---|
| 107 | * | 
|---|
| 108 | * \date 2011-10-31 | 
|---|
| 109 | * | 
|---|
| 110 | */ | 
|---|