2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source 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
44 #include "selectionoption.h"
45 #include "selectionfileoption.h"
46 #include "selectionoptionstorage.h"
47 #include "selectionfileoptionstorage.h"
51 #include "gromacs/options/optionmanagercontainer.h"
52 #include "gromacs/selection/selection.h"
53 #include "gromacs/selection/selectionoptionmanager.h"
54 #include "gromacs/utility/exceptions.h"
55 #include "gromacs/utility/gmxassert.h"
56 #include "gromacs/utility/messagestringcollector.h"
61 /********************************************************************
62 * SelectionOptionStorage
65 SelectionOptionStorage::SelectionOptionStorage(const SelectionOption &settings,
66 SelectionOptionManager *manager)
67 : MyBase(settings, OptionFlags() | efOption_NoDefaultValue
68 | efOption_DontCheckMinimumCount),
69 info_(this), manager_(*manager), defaultText_(settings.defaultText_),
70 selectionFlags_(settings.selectionFlags_)
72 GMX_RELEASE_ASSERT(manager != NULL,
73 "SelectionOptionManager must be added before SelectionOption");
74 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
75 "allowMultiple() is not supported for selection options");
76 manager_.registerOption(this);
80 std::string SelectionOptionStorage::formatSingleValue(const Selection &value) const
82 return value.selectionText();
86 void SelectionOptionStorage::addSelections(
87 const SelectionList &selections,
90 if (bFullValue && selections.size() < static_cast<size_t>(minValueCount()))
92 GMX_THROW(InvalidInputError("Too few selections provided"));
98 SelectionList::const_iterator i;
99 for (i = selections.begin(); i != selections.end(); ++i)
101 // TODO: Having this check in the parser would make interactive input
103 if (selectionFlags_.test(efSelection_OnlyStatic) && i->isDynamic())
105 GMX_THROW(InvalidInputError("Dynamic selections not supported"));
107 // Create a copy to allow modifications.
109 sel.data().setFlags(selectionFlags_);
120 void SelectionOptionStorage::convertValue(const std::string &value)
122 manager_.convertOptionValue(this, value, false);
125 void SelectionOptionStorage::processSetValues(ValueList *values)
127 if (values->size() == 0)
129 manager_.requestOptionDelayedParsing(this);
131 else if (values->size() < static_cast<size_t>(minValueCount()))
133 GMX_THROW(InvalidInputError("Too few (valid) values provided"));
137 void SelectionOptionStorage::processAll()
139 if (!isSet() && !defaultText_.empty())
141 manager_.convertOptionValue(this, defaultText_, true);
143 if (isRequired() && !isSet())
145 manager_.requestOptionDelayedParsing(this);
150 void SelectionOptionStorage::setAllowedValueCount(int count)
152 // TODO: It should be possible to have strong exception safety here.
153 MessageStringCollector errors;
154 errors.startContext("In option '" + name() + "'");
157 // Should not throw because efOption_DontCheckMinimumCount is set.
158 setMinValueCount(count);
159 if (valueCount() > 0 && valueCount() < count)
161 errors.append("Too few (valid) values provided");
166 setMaxValueCount(count);
168 catch (const UserInputError &ex)
170 errors.append(ex.what());
172 errors.finishContext();
173 if (!errors.isEmpty())
175 GMX_THROW(InvalidInputError(errors.toString()));
179 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
181 ValueList::iterator i;
182 for (i = values().begin(); i != values().end(); ++i)
184 if (flag == efSelection_OnlyStatic && bSet && i->isDynamic())
186 MessageStringCollector errors;
187 errors.startContext("In option '" + name() + "'");
188 errors.append("Dynamic selections not supported");
189 errors.finishContext();
190 GMX_THROW(InvalidInputError(errors.toString()));
193 selectionFlags_.set(flag, bSet);
194 for (i = values().begin(); i != values().end(); ++i)
196 i->data().setFlags(selectionFlags_);
201 /********************************************************************
202 * SelectionOptionInfo
205 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage *option)
210 SelectionOptionStorage &SelectionOptionInfo::option()
212 return static_cast<SelectionOptionStorage &>(OptionInfo::option());
215 const SelectionOptionStorage &SelectionOptionInfo::option() const
217 return static_cast<const SelectionOptionStorage &>(OptionInfo::option());
220 void SelectionOptionInfo::setValueCount(int count)
222 option().setAllowedValueCount(count);
225 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
227 option().setSelectionFlag(efSelection_EvaluateVelocities, bEnabled);
230 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
232 option().setSelectionFlag(efSelection_EvaluateForces, bEnabled);
235 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
237 option().setSelectionFlag(efSelection_OnlyAtoms, bEnabled);
240 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
242 option().setSelectionFlag(efSelection_OnlyStatic, bEnabled);
245 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
247 option().setSelectionFlag(efSelection_DynamicMask, bEnabled);
251 /********************************************************************
255 AbstractOptionStorage *
256 SelectionOption::createStorage(const OptionManagerContainer &managers) const
258 return new SelectionOptionStorage(
259 *this, managers.get<SelectionOptionManager>());
263 /********************************************************************
264 * SelectionFileOptionStorage
267 SelectionFileOptionStorage::SelectionFileOptionStorage(
268 const SelectionFileOption &settings, SelectionOptionManager *manager)
269 : AbstractOptionStorage(settings, OptionFlags() | efOption_MultipleTimes
270 | efOption_DontCheckMinimumCount),
271 info_(this), manager_(*manager), bValueParsed_(false)
273 GMX_RELEASE_ASSERT(manager != NULL,
274 "SelectionOptionManager must be added before SelectionFileOption");
277 void SelectionFileOptionStorage::clearSet()
279 bValueParsed_ = false;
282 void SelectionFileOptionStorage::convertValue(const std::string &value)
286 GMX_THROW(InvalidInputError("More than one file name provided"));
288 bValueParsed_ = true;
289 // TODO: Should we throw an InvalidInputError if the file does not exist?
290 manager_.parseRequestedFromFile(value);
293 void SelectionFileOptionStorage::processSet()
297 GMX_THROW(InvalidInputError("No file name provided"));
302 /********************************************************************
303 * SelectionFileOptionInfo
306 SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage *option)
312 /********************************************************************
313 * SelectionFileOption
316 SelectionFileOption::SelectionFileOption(const char *name)
317 : AbstractOption(name)
319 setDescription("Provide selections from files");
322 AbstractOptionStorage *
323 SelectionFileOption::createStorage(const OptionManagerContainer &managers) const
325 return new SelectionFileOptionStorage(
326 *this, managers.get<SelectionOptionManager>());