2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS 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 GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements gmx::AnalysisDataModuleManager.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_analysisdata
42 #include "gromacs/analysisdata/datamodulemanager.h"
46 #include "gromacs/analysisdata/abstractdata.h"
47 #include "gromacs/analysisdata/dataframe.h"
48 #include "gromacs/analysisdata/datamodule.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/gmxassert.h"
55 /********************************************************************
56 * AnalysisDataModuleManager::Impl
60 * Private implementation class for AnalysisDataModuleManager.
62 * \ingroup module_analysisdata
64 class AnalysisDataModuleManager::Impl
67 //! Shorthand for list of modules added to the data.
68 typedef std::vector<AnalysisDataModulePointer> ModuleList;
70 //! Describes the current state of the notification methods.
73 eNotStarted, //!< Initial state (nothing called).
74 eInData, //!< notifyDataStart() called, no frame in progress.
75 eInFrame, //!< notifyFrameStart() called, but notifyFrameFinish() not.
76 eFinished //!< notifyDataFinish() called.
82 * Checks whether a module is compatible with a given data property.
84 * \param[in] module Module to check.
85 * \param[in] property Property to check.
86 * \param[in] bSet Value of the property to check against.
87 * \throws APIError if \p module is not compatible with the data.
89 void checkModuleProperty(const AnalysisDataModuleInterface &module,
90 DataProperty property, bool bSet) const;
92 * Checks whether a module is compatible with the data properties.
94 * \param[in] module Module to check.
95 * \throws APIError if \p module is not compatible with the data.
97 * Does not currently check the actual data (e.g., missing values), but
98 * only the dimensionality and other preset properties of the data.
100 void checkModuleProperties(const AnalysisDataModuleInterface &module) const;
103 * Present data already added to the data object to a module.
105 * \param[in] data Data object to read data from.
106 * \param[in] module Module to present the data to.
107 * \throws APIError if \p module is not compatible with the data.
108 * \throws APIError if all data is not available through
110 * \throws unspecified Any exception thrown by \p module in its data
111 * notification methods.
113 * Uses getDataFrame() in \p data to access all data in the object, and
114 * calls the notification functions in \p module as if the module had
115 * been registered to the data object when the data was added.
117 void presentData(AbstractAnalysisData *data,
118 AnalysisDataModuleInterface *module);
120 //! List of modules added to the data.
122 //! Properties of the owning data for module checking.
123 bool bDataProperty_[eDataPropertyNR];
124 //! true if all modules support missing data.
128 * Current state of the notification methods.
130 * This is used together with \a currIndex_ for sanity checks on the
131 * input data; invalid call sequences trigger asserts.
132 * The state of these variables does not otherwise affect the behavior
133 * of this class; this is the reason they can be changed in const
136 //! Whether notifyDataStart() has been called.
137 mutable State state_;
138 //! Index of currently active frame or the next frame if not in frame.
139 mutable int currIndex_;
142 AnalysisDataModuleManager::Impl::Impl()
143 : bAllowMissing_(true), state_(eNotStarted), currIndex_(0)
145 // This must be in sync with how AbstractAnalysisData is actually
147 for (int i = 0; i < eDataPropertyNR; ++i)
149 bDataProperty_[i] = false;
154 AnalysisDataModuleManager::Impl::checkModuleProperty(
155 const AnalysisDataModuleInterface &module,
156 DataProperty property, bool bSet) const
159 const int flags = module.flags();
162 case eMultipleDataSets:
163 if (bSet && !(flags & AnalysisDataModuleInterface::efAllowMultipleDataSets))
168 case eMultipleColumns:
169 if (bSet && !(flags & AnalysisDataModuleInterface::efAllowMulticolumn))
175 if ((bSet && !(flags & AnalysisDataModuleInterface::efAllowMultipoint))
176 || (!bSet && (flags & AnalysisDataModuleInterface::efOnlyMultipoint)))
182 GMX_RELEASE_ASSERT(false, "Invalid data property enumeration");
186 GMX_THROW(APIError("Data module not compatible with data object properties"));
191 AnalysisDataModuleManager::Impl::checkModuleProperties(
192 const AnalysisDataModuleInterface &module) const
194 for (int i = 0; i < eDataPropertyNR; ++i)
196 checkModuleProperty(module, static_cast<DataProperty>(i), bDataProperty_[i]);
201 AnalysisDataModuleManager::Impl::presentData(AbstractAnalysisData *data,
202 AnalysisDataModuleInterface *module)
204 if (state_ == eNotStarted)
208 GMX_RELEASE_ASSERT(state_ != eInFrame,
209 "Cannot apply a modules in mid-frame");
210 module->dataStarted(data);
211 const bool bCheckMissing = bAllowMissing_
212 && !(module->flags() & AnalysisDataModuleInterface::efAllowMissing);
213 for (int i = 0; i < data->frameCount(); ++i)
215 AnalysisDataFrameRef frame = data->getDataFrame(i);
216 GMX_RELEASE_ASSERT(frame.isValid(), "Invalid data frame returned");
217 // TODO: Check all frames before doing anything for slightly better
218 // exception behavior.
219 if (bCheckMissing && !frame.allPresent())
221 GMX_THROW(APIError("Missing data not supported by a module"));
223 module->frameStarted(frame.header());
224 for (int j = 0; j < frame.pointSetCount(); ++j)
226 module->pointsAdded(frame.pointSet(j));
228 module->frameFinished(frame.header());
230 if (state_ == eFinished)
232 module->dataFinished();
236 /********************************************************************
237 * AnalysisDataModuleManager
240 AnalysisDataModuleManager::AnalysisDataModuleManager()
245 AnalysisDataModuleManager::~AnalysisDataModuleManager()
250 AnalysisDataModuleManager::dataPropertyAboutToChange(DataProperty property, bool bSet)
252 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eNotStarted,
253 "Cannot change data properties after data has been started");
254 if (impl_->bDataProperty_[property] != bSet)
256 Impl::ModuleList::const_iterator i;
257 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
259 impl_->checkModuleProperty(**i, property, bSet);
261 impl_->bDataProperty_[property] = bSet;
266 AnalysisDataModuleManager::addModule(AbstractAnalysisData *data,
267 AnalysisDataModulePointer module)
269 impl_->checkModuleProperties(*module);
270 GMX_RELEASE_ASSERT(impl_->state_ != Impl::eInFrame,
271 "Cannot add a data module in mid-frame");
272 impl_->presentData(data, module.get());
274 if (!(module->flags() & AnalysisDataModuleInterface::efAllowMissing))
276 impl_->bAllowMissing_ = false;
278 impl_->modules_.push_back(module);
282 AnalysisDataModuleManager::applyModule(AbstractAnalysisData *data,
283 AnalysisDataModuleInterface *module)
285 impl_->checkModuleProperties(*module);
286 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eFinished,
287 "Data module can only be applied to ready data");
288 impl_->presentData(data, module);
293 AnalysisDataModuleManager::notifyDataStart(AbstractAnalysisData *data) const
295 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eNotStarted,
296 "notifyDataStart() called more than once");
297 for (int d = 0; d < data->dataSetCount(); ++d)
299 GMX_RELEASE_ASSERT(data->columnCount(d) > 0,
300 "Data column count is not set");
302 impl_->state_ = Impl::eInData;
304 Impl::ModuleList::const_iterator i;
305 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
307 // This should not fail, since addModule() and
308 // dataPropertyAboutToChange() already do the checks, but kept here to
309 // catch potential bugs (perhaps it would be best to assert on failure).
310 impl_->checkModuleProperties(**i);
311 (*i)->dataStarted(data);
317 AnalysisDataModuleManager::notifyFrameStart(const AnalysisDataFrameHeader &header) const
319 GMX_ASSERT(impl_->state_ == Impl::eInData, "Invalid call sequence");
320 GMX_ASSERT(header.index() == impl_->currIndex_, "Out of order frames");
321 impl_->state_ = Impl::eInFrame;
323 Impl::ModuleList::const_iterator i;
324 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
326 (*i)->frameStarted(header);
332 AnalysisDataModuleManager::notifyPointsAdd(const AnalysisDataPointSetRef &points) const
334 GMX_ASSERT(impl_->state_ == Impl::eInFrame, "notifyFrameStart() not called");
335 // TODO: Add checks for column spans (requires passing the information
336 // about the column counts from somewhere).
337 //GMX_ASSERT(points.lastColumn() < columnCount(points.dataSetIndex()),
338 // "Invalid columns");
339 GMX_ASSERT(points.frameIndex() == impl_->currIndex_,
340 "Points do not correspond to current frame");
341 if (!impl_->bAllowMissing_ && !points.allPresent())
343 GMX_THROW(APIError("Missing data not supported by a module"));
346 Impl::ModuleList::const_iterator i;
347 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
349 (*i)->pointsAdded(points);
355 AnalysisDataModuleManager::notifyFrameFinish(const AnalysisDataFrameHeader &header) const
357 GMX_ASSERT(impl_->state_ == Impl::eInFrame, "notifyFrameStart() not called");
358 GMX_ASSERT(header.index() == impl_->currIndex_,
359 "Header does not correspond to current frame");
360 // TODO: Add a check for the frame count in the source data including this
362 impl_->state_ = Impl::eInData;
365 Impl::ModuleList::const_iterator i;
366 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
368 (*i)->frameFinished(header);
374 AnalysisDataModuleManager::notifyDataFinish() const
376 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eInData, "Invalid call sequence");
377 impl_->state_ = Impl::eFinished;
379 Impl::ModuleList::const_iterator i;
380 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
382 (*i)->dataFinished();