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 |
|
---|
39 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
40 | #include "World.hpp"
|
---|
41 | #include "ChangeTracker.hpp"
|
---|
42 | #include "FormatParser_common.hpp"
|
---|
43 |
|
---|
44 | using namespace std;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Constructor.
|
---|
48 | */
|
---|
49 | FormatParser_common::FormatParser_common(FormatParser_Parameters *_parameters) :
|
---|
50 | Observer("FormatParser_common"),
|
---|
51 | saveStream(NULL)
|
---|
52 | {
|
---|
53 | parameters = _parameters;
|
---|
54 | ChangeTracker::getInstance().signOn(this);
|
---|
55 | World::getInstance().signOn(this, World::AtomInserted);
|
---|
56 | World::getInstance().signOn(this, World::AtomRemoved);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Destructor.
|
---|
61 | */
|
---|
62 | FormatParser_common::~FormatParser_common()
|
---|
63 | {
|
---|
64 | ChangeTracker::getInstance().signOff(this);
|
---|
65 | World::getInstance().signOff(this, World::AtomInserted);
|
---|
66 | World::getInstance().signOff(this, World::AtomRemoved);
|
---|
67 | if (parameters != NULL)
|
---|
68 | delete parameters;
|
---|
69 | // clear id translation maps
|
---|
70 | LocaltoGobalIdMap.clear();
|
---|
71 | GlobaltoLocalIdMap.clear();
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Update operation which can be invoked by the observable (which should be the
|
---|
76 | * change tracker here).
|
---|
77 | */
|
---|
78 | void FormatParser_common::update(Observable *publisher) {
|
---|
79 | if (saveStream != NULL) { // only store when a saveStream is given
|
---|
80 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
81 | save(saveStream, atoms);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * With this, each format parser is informed about specific changes in the World.
|
---|
87 | */
|
---|
88 | void FormatParser_common::recieveNotification(Observable *publisher, Notification_ptr notification) {
|
---|
89 | switch (notification->getChannelNo()) {
|
---|
90 | case World::AtomInserted:
|
---|
91 | AtomInserted(World::getInstance().lastChanged<atom>()->getId());
|
---|
92 | break;
|
---|
93 | case World::AtomRemoved:
|
---|
94 | AtomRemoved(World::getInstance().lastChanged<atom>()->getId());
|
---|
95 | break;
|
---|
96 | default:
|
---|
97 | ASSERT(0,
|
---|
98 | "FormatParser_common::recieveNotification() - unknown notification "
|
---|
99 | +toString(notification->getChannelNo())+" received.");
|
---|
100 | break;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * The observable can tell when it dies.
|
---|
106 | */
|
---|
107 | void FormatParser_common::subjectKilled(Observable *publisher) {}
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Sets the output stream for save, so the save() method can be invoked on update
|
---|
111 | * automatically.
|
---|
112 | *
|
---|
113 | * \param ostream where to save the World's state
|
---|
114 | */
|
---|
115 | void FormatParser_common::setOstream(ostream* output)
|
---|
116 | {
|
---|
117 | saveStream = output;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Function to be called when beginning to parse a new file.
|
---|
121 | *
|
---|
122 | * Resets internal translation maps.
|
---|
123 | *
|
---|
124 | */
|
---|
125 | void FormatParser_common::resetIdAssociations()
|
---|
126 | {
|
---|
127 | LocaltoGobalIdMap.clear();
|
---|
128 | GlobaltoLocalIdMap.clear();
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** Installs an association between a local id from a parsed file and the
|
---|
132 | * global, unique one.
|
---|
133 | *
|
---|
134 | * @param local local atom id
|
---|
135 | * @param global global atom id
|
---|
136 | */
|
---|
137 | void FormatParser_common::associateLocaltoGlobalId(const int local, const int global)
|
---|
138 | {
|
---|
139 | ASSERT(LocaltoGobalIdMap.count(local) == 0,
|
---|
140 | "FormatParser_common::associateLocaltoGlobalId() - local id "
|
---|
141 | +toString(local)+" is already contained.");
|
---|
142 | ASSERT(GlobaltoLocalIdMap.count(global) == 0,
|
---|
143 | "FormatParser_common::associateLocaltoGlobalId() - global id "
|
---|
144 | +toString(global)+" is already contained.");
|
---|
145 | LocaltoGobalIdMap[local] = global;
|
---|
146 | GlobaltoLocalIdMap[global] = local;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /** Getter for the global id to a given \a local one.
|
---|
150 | *
|
---|
151 | * @param local local atom id
|
---|
152 | * @return global atom id, -1 if unknown
|
---|
153 | */
|
---|
154 | int FormatParser_common::getGlobalId(const int local) const
|
---|
155 | {
|
---|
156 | IdtoIdMap::const_iterator iter = LocaltoGobalIdMap.find(local);
|
---|
157 | if(iter == LocaltoGobalIdMap.end())
|
---|
158 | return -1;
|
---|
159 | return iter->second;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** Getter for the local id to a given \a global one.
|
---|
163 | *
|
---|
164 | * @param global global atom id
|
---|
165 | * @return local atom id, -1 if unknown
|
---|
166 | */
|
---|
167 | int FormatParser_common::getLocalId(const int global) const
|
---|
168 | {
|
---|
169 | IdtoIdMap::const_iterator iter = GlobaltoLocalIdMap.find(global);
|
---|
170 | if(iter == GlobaltoLocalIdMap.end())
|
---|
171 | return -1;
|
---|
172 | return iter->second;
|
---|
173 | }
|
---|