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 classes in selectionoption.h and selectionoptionstorage.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
42 #include "selectionoption.h"
43 #include "selectionfileoption.h"
44 #include "selectionoptionstorage.h"
45 #include "selectionfileoptionstorage.h"
49 #include "gromacs/selection/selection.h"
50 #include "gromacs/selection/selectionoptionmanager.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/gmxassert.h"
53 #include "gromacs/utility/messagestringcollector.h"
58 /********************************************************************
59 * SelectionOptionStorage
62 SelectionOptionStorage::SelectionOptionStorage(const SelectionOption &settings)
63 : MyBase(settings, OptionFlags() | efOption_NoDefaultValue
64 | efOption_DontCheckMinimumCount),
65 info_(this), manager_(NULL), selectionFlags_(settings.selectionFlags_)
67 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
68 "allowMultiple() is not supported for selection options");
72 void SelectionOptionStorage::setManager(SelectionOptionManager *manager)
74 GMX_RELEASE_ASSERT(manager_ == NULL || manager_ == manager,
75 "Manager cannot be changed once set");
78 manager->registerOption(this);
84 std::string SelectionOptionStorage::formatSingleValue(const Selection &value) const
86 return value.selectionText();
90 void SelectionOptionStorage::addSelections(
91 const SelectionList &selections,
94 if (bFullValue && selections.size() < static_cast<size_t>(minValueCount()))
96 GMX_THROW(InvalidInputError("Too few selections provided"));
102 SelectionList::const_iterator i;
103 for (i = selections.begin(); i != selections.end(); ++i)
105 // TODO: Having this check in the parser would make interactive input
107 if (selectionFlags_.test(efSelection_OnlyStatic) && i->isDynamic())
109 GMX_THROW(InvalidInputError("Dynamic selections not supported"));
121 void SelectionOptionStorage::convertValue(const std::string &value)
123 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
125 manager_->convertOptionValue(this, value);
128 void SelectionOptionStorage::processSetValues(ValueList *values)
130 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
132 if (values->size() == 0)
134 manager_->requestOptionDelayedParsing(this);
136 else if (values->size() < static_cast<size_t>(minValueCount()))
138 GMX_THROW(InvalidInputError("Too few (valid) values provided"));
140 ValueList::iterator i;
141 for (i = values->begin(); i != values->end(); ++i)
143 i->data().setFlags(selectionFlags_);
147 void SelectionOptionStorage::processAll()
149 if (isRequired() && !isSet())
151 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
153 manager_->requestOptionDelayedParsing(this);
158 void SelectionOptionStorage::setAllowedValueCount(int count)
160 // TODO: It should be possible to have strong exception safety here.
161 MessageStringCollector errors;
162 errors.startContext("In option '" + name() + "'");
165 // Should not throw because efOption_DontCheckMinimumCount is set.
166 setMinValueCount(count);
167 if (valueCount() > 0 && valueCount() < count)
169 errors.append("Too few (valid) values provided");
174 setMaxValueCount(count);
176 catch (const UserInputError &ex)
178 errors.append(ex.what());
180 errors.finishContext();
181 if (!errors.isEmpty())
183 GMX_THROW(InvalidInputError(errors.toString()));
187 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
189 ValueList::iterator i;
190 for (i = values().begin(); i != values().end(); ++i)
192 if (flag == efSelection_OnlyStatic && bSet && i->isDynamic())
194 MessageStringCollector errors;
195 errors.startContext("In option '" + name() + "'");
196 errors.append("Dynamic selections not supported");
197 errors.finishContext();
198 GMX_THROW(InvalidInputError(errors.toString()));
201 selectionFlags_.set(flag, bSet);
202 for (i = values().begin(); i != values().end(); ++i)
204 i->data().setFlags(selectionFlags_);
209 /********************************************************************
210 * SelectionOptionInfo
213 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage *option)
218 SelectionOptionStorage &SelectionOptionInfo::option()
220 return static_cast<SelectionOptionStorage &>(OptionInfo::option());
223 const SelectionOptionStorage &SelectionOptionInfo::option() const
225 return static_cast<const SelectionOptionStorage &>(OptionInfo::option());
228 void SelectionOptionInfo::setManager(SelectionOptionManager *manager)
230 option().setManager(manager);
233 void SelectionOptionInfo::setValueCount(int count)
235 option().setAllowedValueCount(count);
238 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
240 option().setSelectionFlag(efSelection_EvaluateVelocities, bEnabled);
243 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
245 option().setSelectionFlag(efSelection_EvaluateForces, bEnabled);
248 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
250 option().setSelectionFlag(efSelection_OnlyAtoms, bEnabled);
253 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
255 option().setSelectionFlag(efSelection_OnlyStatic, bEnabled);
258 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
260 option().setSelectionFlag(efSelection_DynamicMask, bEnabled);
264 /********************************************************************
268 AbstractOptionStoragePointer SelectionOption::createStorage() const
270 return AbstractOptionStoragePointer(new SelectionOptionStorage(*this));
274 /********************************************************************
275 * SelectionFileOptionStorage
278 SelectionFileOptionStorage::SelectionFileOptionStorage(const SelectionFileOption &settings)
279 : AbstractOptionStorage(settings, OptionFlags() | efOption_MultipleTimes
280 | efOption_DontCheckMinimumCount),
281 info_(this), manager_(NULL), bValueParsed_(false)
285 void SelectionFileOptionStorage::clearSet()
287 bValueParsed_ = false;
290 void SelectionFileOptionStorage::convertValue(const std::string &value)
292 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
296 GMX_THROW(InvalidInputError("More than one file name provided"));
298 bValueParsed_ = true;
299 // TODO: Should we throw an InvalidInputError if the file does not exist?
300 manager_->parseRequestedFromFile(value);
303 void SelectionFileOptionStorage::processSet()
307 GMX_THROW(InvalidInputError("No file name provided"));
312 /********************************************************************
313 * SelectionFileOptionInfo
316 SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage *option)
321 SelectionFileOptionStorage &SelectionFileOptionInfo::option()
323 return static_cast<SelectionFileOptionStorage &>(OptionInfo::option());
326 const SelectionFileOptionStorage &SelectionFileOptionInfo::option() const
328 return static_cast<const SelectionFileOptionStorage &>(OptionInfo::option());
331 void SelectionFileOptionInfo::setManager(SelectionOptionManager *manager)
333 option().setManager(manager);
337 /********************************************************************
338 * SelectionFileOption
341 SelectionFileOption::SelectionFileOption(const char *name)
342 : AbstractOption(name)
344 setDescription("Provide selections from files");
347 AbstractOptionStoragePointer SelectionFileOption::createStorage() const
349 return AbstractOptionStoragePointer(new SelectionFileOptionStorage(*this));