| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * 
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * FormatParser_common_common.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Mar 1, 2010
 | 
|---|
| 27 |  *      Author: metzler
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <iostream>
 | 
|---|
| 38 | #include <limits>
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 41 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
| 42 | #include "World.hpp"
 | 
|---|
| 43 | #include "ChangeTracker.hpp"
 | 
|---|
| 44 | #include "FormatParser_common.hpp"
 | 
|---|
| 45 | 
 | 
|---|
| 46 | using namespace std;
 | 
|---|
| 47 | 
 | 
|---|
| 48 | /**
 | 
|---|
| 49 |  * Constructor.
 | 
|---|
| 50 |  */
 | 
|---|
| 51 | FormatParser_common::FormatParser_common(FormatParser_Parameters *_parameters) :
 | 
|---|
| 52 |   Observer("FormatParser_common"),
 | 
|---|
| 53 |   saveStream(NULL)
 | 
|---|
| 54 | {
 | 
|---|
| 55 |   parameters = _parameters;
 | 
|---|
| 56 |   ChangeTracker::getInstance().signOn(this);
 | 
|---|
| 57 |   World::getInstance().signOn(this, World::AtomInserted);
 | 
|---|
| 58 |   World::getInstance().signOn(this, World::AtomRemoved);
 | 
|---|
| 59 | }
 | 
|---|
| 60 | 
 | 
|---|
| 61 | /**
 | 
|---|
| 62 |  * Destructor.
 | 
|---|
| 63 |  */
 | 
|---|
| 64 | FormatParser_common::~FormatParser_common() 
 | 
|---|
| 65 | {
 | 
|---|
| 66 |   ChangeTracker::getInstance().signOff(this);
 | 
|---|
| 67 |   World::getInstance().signOff(this, World::AtomInserted);
 | 
|---|
| 68 |   World::getInstance().signOff(this, World::AtomRemoved);
 | 
|---|
| 69 |   if (parameters != NULL)
 | 
|---|
| 70 |     delete parameters;
 | 
|---|
| 71 |   // clear id translation maps
 | 
|---|
| 72 |   LocaltoGobalIdMap.clear();
 | 
|---|
| 73 |   GlobaltoLocalIdMap.clear();
 | 
|---|
| 74 | 
 | 
|---|
| 75 |   // flush stream properly (is maintained in FormatParserStorage)
 | 
|---|
| 76 |   if (saveStream != NULL) {
 | 
|---|
| 77 |     saveStream->flush();
 | 
|---|
| 78 |     saveStream = NULL;
 | 
|---|
| 79 |   }
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | /**
 | 
|---|
| 83 |  * Update operation which can be invoked by the observable (which should be the
 | 
|---|
| 84 |  * change tracker here).
 | 
|---|
| 85 |  */
 | 
|---|
| 86 | void FormatParser_common::update(Observable *publisher) {
 | 
|---|
| 87 |   if (saveStream != NULL) { // only store when a saveStream is given
 | 
|---|
| 88 |     std::vector<const atom *> atoms = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 89 |         getAllAtoms();
 | 
|---|
| 90 |     save(saveStream, atoms);
 | 
|---|
| 91 |   }
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | /**
 | 
|---|
| 95 |  * With this, each format parser is informed about specific changes in the World.
 | 
|---|
| 96 |  */
 | 
|---|
| 97 | void FormatParser_common::recieveNotification(Observable *publisher, Notification_ptr notification) {
 | 
|---|
| 98 |   switch (notification->getChannelNo()) {
 | 
|---|
| 99 |     case World::AtomInserted:
 | 
|---|
| 100 |       AtomInserted(World::getInstance().lastChangedAtomId());
 | 
|---|
| 101 |       break;
 | 
|---|
| 102 |     case World::AtomRemoved:
 | 
|---|
| 103 |       AtomRemoved(World::getInstance().lastChangedAtomId());
 | 
|---|
| 104 |       break;
 | 
|---|
| 105 |     default:
 | 
|---|
| 106 |       ASSERT(0,
 | 
|---|
| 107 |           "FormatParser_common::recieveNotification() - unknown notification "
 | 
|---|
| 108 |           +toString(notification->getChannelNo())+" received.");
 | 
|---|
| 109 |       break;
 | 
|---|
| 110 |   }
 | 
|---|
| 111 | }
 | 
|---|
| 112 | 
 | 
|---|
| 113 | /**
 | 
|---|
| 114 |  * The observable can tell when it dies.
 | 
|---|
| 115 |  */
 | 
|---|
| 116 | void FormatParser_common::subjectKilled(Observable *publisher) {}
 | 
|---|
| 117 | 
 | 
|---|
| 118 | /**
 | 
|---|
| 119 |  * Sets the output stream for save, so the save() method can be invoked on update
 | 
|---|
| 120 |  * automatically.
 | 
|---|
| 121 |  *
 | 
|---|
| 122 |  * \param ostream where to save the World's state
 | 
|---|
| 123 |  */
 | 
|---|
| 124 | void FormatParser_common::setOstream(ostream* output)
 | 
|---|
| 125 | {
 | 
|---|
| 126 |   saveStream = output;
 | 
|---|
| 127 | }
 | 
|---|
| 128 | 
 | 
|---|
| 129 | /** Function to be called when beginning to parse a new file.
 | 
|---|
| 130 |  *
 | 
|---|
| 131 |  * Resets internal translation maps.
 | 
|---|
| 132 |  *
 | 
|---|
| 133 |  */
 | 
|---|
| 134 | void FormatParser_common::resetIdAssociations()
 | 
|---|
| 135 | {
 | 
|---|
| 136 |   LocaltoGobalIdMap.clear();
 | 
|---|
| 137 |   GlobaltoLocalIdMap.clear();
 | 
|---|
| 138 | }
 | 
|---|
| 139 | 
 | 
|---|
| 140 | /** Installs an association between a local id from a parsed file and the
 | 
|---|
| 141 |  * global, unique one.
 | 
|---|
| 142 |  *
 | 
|---|
| 143 |  * @param local local atom id
 | 
|---|
| 144 |  * @param global global atom id
 | 
|---|
| 145 |  */
 | 
|---|
| 146 | void FormatParser_common::associateLocaltoGlobalId(const int local, const int global)
 | 
|---|
| 147 | {
 | 
|---|
| 148 |   ASSERT(LocaltoGobalIdMap.count(local) == 0,
 | 
|---|
| 149 |       "FormatParser_common::associateLocaltoGlobalId() - local id "
 | 
|---|
| 150 |       +toString(local)+" is already contained.");
 | 
|---|
| 151 |   ASSERT(GlobaltoLocalIdMap.count(global) == 0,
 | 
|---|
| 152 |       "FormatParser_common::associateLocaltoGlobalId() - global id "
 | 
|---|
| 153 |       +toString(global)+" is already contained.");
 | 
|---|
| 154 |   LocaltoGobalIdMap[local] = global;
 | 
|---|
| 155 |   GlobaltoLocalIdMap[global] = local;
 | 
|---|
| 156 | }
 | 
|---|
| 157 | 
 | 
|---|
| 158 | /** Getter for the global id to a given \a local one.
 | 
|---|
| 159 |  *
 | 
|---|
| 160 |  * @param local local atom id
 | 
|---|
| 161 |  * @return global atom id, -1 if unknown
 | 
|---|
| 162 |  */
 | 
|---|
| 163 | int FormatParser_common::getGlobalId(const int local) const
 | 
|---|
| 164 | {
 | 
|---|
| 165 |   IdtoIdMap::const_iterator iter = LocaltoGobalIdMap.find(local);
 | 
|---|
| 166 |   if(iter == LocaltoGobalIdMap.end())
 | 
|---|
| 167 |     return -1;
 | 
|---|
| 168 |   return iter->second;
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | /** Getter for the local id to a given \a global one.
 | 
|---|
| 172 |  *
 | 
|---|
| 173 |  * @param global global atom id
 | 
|---|
| 174 |  * @return local atom id, -1 if unknown
 | 
|---|
| 175 |  */
 | 
|---|
| 176 | int FormatParser_common::getLocalId(const int global) const
 | 
|---|
| 177 | {
 | 
|---|
| 178 |   IdtoIdMap::const_iterator iter = GlobaltoLocalIdMap.find(global);
 | 
|---|
| 179 |   if(iter == GlobaltoLocalIdMap.end())
 | 
|---|
| 180 |     return -1;
 | 
|---|
| 181 |   return iter->second;
 | 
|---|
| 182 | }
 | 
|---|
| 183 | 
 | 
|---|
| 184 | std::pair<size_t, size_t> FormatParser_common::getMinMaxTrajectories(
 | 
|---|
| 185 |     const std::vector<const atom *> &_atoms) const
 | 
|---|
| 186 | {
 | 
|---|
| 187 |   // get max and min trajectories
 | 
|---|
| 188 |   size_t min_trajectories = std::numeric_limits<size_t>::max();
 | 
|---|
| 189 |   size_t max_trajectories = std::numeric_limits<size_t>::min();
 | 
|---|
| 190 |   for (std::vector<const atom *>::const_iterator iter = _atoms.begin();
 | 
|---|
| 191 |       iter != _atoms.end();
 | 
|---|
| 192 |       ++iter) {
 | 
|---|
| 193 |     if (max_trajectories < (*iter)->getTrajectorySize())
 | 
|---|
| 194 |       max_trajectories = (*iter)->getTrajectorySize();
 | 
|---|
| 195 |     if (min_trajectories > (*iter)->getTrajectorySize())
 | 
|---|
| 196 |       min_trajectories = (*iter)->getTrajectorySize();
 | 
|---|
| 197 |   }
 | 
|---|
| 198 |   // no atoms? Then, they all have same amount
 | 
|---|
| 199 |   if (_atoms.size() == 0)
 | 
|---|
| 200 |     min_trajectories = max_trajectories = 1;
 | 
|---|
| 201 |   ASSERT(min_trajectories == max_trajectories,
 | 
|---|
| 202 |       "FormatParser_common::getMinMaxTrajectories() - not all atoms have same number of trajectories: "
 | 
|---|
| 203 |       +toString(min_trajectories)+" != "+toString(max_trajectories)+".");
 | 
|---|
| 204 | 
 | 
|---|
| 205 |   return std::make_pair(min_trajectories, max_trajectories);
 | 
|---|
| 206 | }
 | 
|---|