| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 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/>.
 | 
|---|
| [bcf653] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
| [ab4b55] | 23 | /*
 | 
|---|
| [765f16] | 24 |  * FormatParser_common_common.cpp
 | 
|---|
| [ab4b55] | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Mar 1, 2010
 | 
|---|
 | 27 |  *      Author: metzler
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
| [bf3817] | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
| [ad011c] | 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 36 | 
 | 
|---|
| [9131f3] | 37 | #include <iostream>
 | 
|---|
| [ab4b55] | 38 | 
 | 
|---|
| [02ce36] | 39 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
| [765f16] | 40 | #include "World.hpp"
 | 
|---|
 | 41 | #include "ChangeTracker.hpp"
 | 
|---|
 | 42 | #include "FormatParser_common.hpp"
 | 
|---|
 | 43 | 
 | 
|---|
| [ab4b55] | 44 | using namespace std;
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 | /**
 | 
|---|
 | 47 |  * Constructor.
 | 
|---|
 | 48 |  */
 | 
|---|
| [765f16] | 49 | FormatParser_common::FormatParser_common(FormatParser_Parameters *_parameters) :
 | 
|---|
| [091838] | 50 |   Observer("FormatParser_common"),
 | 
|---|
 | 51 |   saveStream(NULL)
 | 
|---|
| [cd5047] | 52 | {
 | 
|---|
| [765f16] | 53 |   parameters = _parameters;
 | 
|---|
| [2f40c0e] | 54 |   ChangeTracker::getInstance().signOn(this);
 | 
|---|
| [02ce36] | 55 |   World::getInstance().signOn(this, World::AtomInserted);
 | 
|---|
 | 56 |   World::getInstance().signOn(this, World::AtomRemoved);
 | 
|---|
| [ab4b55] | 57 | }
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | /**
 | 
|---|
 | 60 |  * Destructor.
 | 
|---|
 | 61 |  */
 | 
|---|
| [765f16] | 62 | FormatParser_common::~FormatParser_common() 
 | 
|---|
 | 63 | {
 | 
|---|
| [2f40c0e] | 64 |   ChangeTracker::getInstance().signOff(this);
 | 
|---|
| [02ce36] | 65 |   World::getInstance().signOff(this, World::AtomInserted);
 | 
|---|
 | 66 |   World::getInstance().signOff(this, World::AtomRemoved);
 | 
|---|
| [765f16] | 67 |   if (parameters != NULL)
 | 
|---|
 | 68 |     delete parameters;
 | 
|---|
| [c0e28c] | 69 |   // clear id translation maps
 | 
|---|
 | 70 |   LocaltoGobalIdMap.clear();
 | 
|---|
 | 71 |   GlobaltoLocalIdMap.clear();
 | 
|---|
| [3b75c2] | 72 | 
 | 
|---|
 | 73 |   // flush stream properly (is maintained in FormatParserStorage)
 | 
|---|
 | 74 |   if (saveStream != NULL) {
 | 
|---|
 | 75 |     saveStream->flush();
 | 
|---|
 | 76 |     saveStream = NULL;
 | 
|---|
 | 77 |   }
 | 
|---|
| [ab4b55] | 78 | }
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 | /**
 | 
|---|
 | 81 |  * Update operation which can be invoked by the observable (which should be the
 | 
|---|
 | 82 |  * change tracker here).
 | 
|---|
 | 83 |  */
 | 
|---|
| [765f16] | 84 | void FormatParser_common::update(Observable *publisher) {
 | 
|---|
| [091838] | 85 |   if (saveStream != NULL) { // only store when a saveStream is given
 | 
|---|
 | 86 |     std::vector<atom *> atoms = World::getInstance().getAllAtoms();
 | 
|---|
 | 87 |     save(saveStream, atoms);
 | 
|---|
| [ab4b55] | 88 |   }
 | 
|---|
 | 89 | }
 | 
|---|
 | 90 | 
 | 
|---|
| [38f991] | 91 | /**
 | 
|---|
 | 92 |  * With this, each format parser is informed about specific changes in the World.
 | 
|---|
 | 93 |  */
 | 
|---|
| [765f16] | 94 | void FormatParser_common::recieveNotification(Observable *publisher, Notification_ptr notification) {
 | 
|---|
| [38f991] | 95 |   switch (notification->getChannelNo()) {
 | 
|---|
 | 96 |     case World::AtomInserted:
 | 
|---|
 | 97 |       AtomInserted(World::getInstance().lastChanged<atom>()->getId());
 | 
|---|
 | 98 |       break;
 | 
|---|
 | 99 |     case World::AtomRemoved:
 | 
|---|
 | 100 |       AtomRemoved(World::getInstance().lastChanged<atom>()->getId());
 | 
|---|
 | 101 |       break;
 | 
|---|
 | 102 |     default:
 | 
|---|
 | 103 |       ASSERT(0,
 | 
|---|
| [765f16] | 104 |           "FormatParser_common::recieveNotification() - unknown notification "
 | 
|---|
| [38f991] | 105 |           +toString(notification->getChannelNo())+" received.");
 | 
|---|
 | 106 |       break;
 | 
|---|
 | 107 |   }
 | 
|---|
 | 108 | }
 | 
|---|
 | 109 | 
 | 
|---|
| [ab4b55] | 110 | /**
 | 
|---|
 | 111 |  * The observable can tell when it dies.
 | 
|---|
 | 112 |  */
 | 
|---|
| [765f16] | 113 | void FormatParser_common::subjectKilled(Observable *publisher) {}
 | 
|---|
| [ab4b55] | 114 | 
 | 
|---|
 | 115 | /**
 | 
|---|
 | 116 |  * Sets the output stream for save, so the save() method can be invoked on update
 | 
|---|
 | 117 |  * automatically.
 | 
|---|
 | 118 |  *
 | 
|---|
 | 119 |  * \param ostream where to save the World's state
 | 
|---|
 | 120 |  */
 | 
|---|
| [c0e28c] | 121 | void FormatParser_common::setOstream(ostream* output)
 | 
|---|
 | 122 | {
 | 
|---|
| [ab4b55] | 123 |   saveStream = output;
 | 
|---|
 | 124 | }
 | 
|---|
| [c0e28c] | 125 | 
 | 
|---|
 | 126 | /** Function to be called when beginning to parse a new file.
 | 
|---|
 | 127 |  *
 | 
|---|
 | 128 |  * Resets internal translation maps.
 | 
|---|
 | 129 |  *
 | 
|---|
 | 130 |  */
 | 
|---|
 | 131 | void FormatParser_common::resetIdAssociations()
 | 
|---|
 | 132 | {
 | 
|---|
 | 133 |   LocaltoGobalIdMap.clear();
 | 
|---|
 | 134 |   GlobaltoLocalIdMap.clear();
 | 
|---|
 | 135 | }
 | 
|---|
 | 136 | 
 | 
|---|
 | 137 | /** Installs an association between a local id from a parsed file and the
 | 
|---|
 | 138 |  * global, unique one.
 | 
|---|
 | 139 |  *
 | 
|---|
 | 140 |  * @param local local atom id
 | 
|---|
 | 141 |  * @param global global atom id
 | 
|---|
 | 142 |  */
 | 
|---|
 | 143 | void FormatParser_common::associateLocaltoGlobalId(const int local, const int global)
 | 
|---|
 | 144 | {
 | 
|---|
 | 145 |   ASSERT(LocaltoGobalIdMap.count(local) == 0,
 | 
|---|
 | 146 |       "FormatParser_common::associateLocaltoGlobalId() - local id "
 | 
|---|
 | 147 |       +toString(local)+" is already contained.");
 | 
|---|
 | 148 |   ASSERT(GlobaltoLocalIdMap.count(global) == 0,
 | 
|---|
 | 149 |       "FormatParser_common::associateLocaltoGlobalId() - global id "
 | 
|---|
 | 150 |       +toString(global)+" is already contained.");
 | 
|---|
 | 151 |   LocaltoGobalIdMap[local] = global;
 | 
|---|
 | 152 |   GlobaltoLocalIdMap[global] = local;
 | 
|---|
 | 153 | }
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 | /** Getter for the global id to a given \a local one.
 | 
|---|
 | 156 |  *
 | 
|---|
 | 157 |  * @param local local atom id
 | 
|---|
 | 158 |  * @return global atom id, -1 if unknown
 | 
|---|
 | 159 |  */
 | 
|---|
 | 160 | int FormatParser_common::getGlobalId(const int local) const
 | 
|---|
 | 161 | {
 | 
|---|
 | 162 |   IdtoIdMap::const_iterator iter = LocaltoGobalIdMap.find(local);
 | 
|---|
 | 163 |   if(iter == LocaltoGobalIdMap.end())
 | 
|---|
 | 164 |     return -1;
 | 
|---|
 | 165 |   return iter->second;
 | 
|---|
 | 166 | }
 | 
|---|
 | 167 | 
 | 
|---|
 | 168 | /** Getter for the local id to a given \a global one.
 | 
|---|
 | 169 |  *
 | 
|---|
 | 170 |  * @param global global atom id
 | 
|---|
 | 171 |  * @return local atom id, -1 if unknown
 | 
|---|
 | 172 |  */
 | 
|---|
 | 173 | int FormatParser_common::getLocalId(const int global) const
 | 
|---|
 | 174 | {
 | 
|---|
 | 175 |   IdtoIdMap::const_iterator iter = GlobaltoLocalIdMap.find(global);
 | 
|---|
 | 176 |   if(iter == GlobaltoLocalIdMap.end())
 | 
|---|
 | 177 |     return -1;
 | 
|---|
 | 178 |   return iter->second;
 | 
|---|
 | 179 | }
 | 
|---|