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
42 #include "selectionoption.h"
43 #include "selectionfileoption.h"
44 #include "selectionoptionstorage.h"
45 #include "selectionfileoptionstorage.h"
49 #include "gromacs/options/optionmanagercontainer.h"
50 #include "gromacs/selection/selection.h"
51 #include "gromacs/selection/selectionoptionmanager.h"
52 #include "gromacs/utility/exceptions.h"
53 #include "gromacs/utility/gmxassert.h"
54 #include "gromacs/utility/messagestringcollector.h"
59 /********************************************************************
60 * SelectionOptionStorage
63 SelectionOptionStorage::SelectionOptionStorage(const SelectionOption &settings,
64 SelectionOptionManager *manager)
65 : MyBase(settings, OptionFlags() | efOption_NoDefaultValue
66 | efOption_DontCheckMinimumCount),
67 info_(this), manager_(*manager), defaultText_(settings.defaultText_),
68 selectionFlags_(settings.selectionFlags_)
70 GMX_RELEASE_ASSERT(manager != NULL,
71 "SelectionOptionManager must be added before SelectionOption");
72 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
73 "allowMultiple() is not supported for selection options");
74 manager_.registerOption(this);
78 std::string SelectionOptionStorage::formatSingleValue(const Selection &value) const
80 return value.selectionText();
84 void SelectionOptionStorage::addSelections(
85 const SelectionList &selections,
88 if (bFullValue && selections.size() < static_cast<size_t>(minValueCount()))
90 GMX_THROW(InvalidInputError("Too few selections provided"));
96 SelectionList::const_iterator i;
97 for (i = selections.begin(); i != selections.end(); ++i)
99 // TODO: Having this check in the parser would make interactive input
101 if (selectionFlags_.test(efSelection_OnlyStatic) && i->isDynamic())
103 GMX_THROW(InvalidInputError("Dynamic selections not supported"));
105 // Create a copy to allow modifications.
107 sel.data().setFlags(selectionFlags_);
118 void SelectionOptionStorage::convertValue(const std::string &value)
120 manager_.convertOptionValue(this, value, false);
123 void SelectionOptionStorage::processSetValues(ValueList *values)
125 if (values->size() == 0)
127 manager_.requestOptionDelayedParsing(this);
129 else if (values->size() < static_cast<size_t>(minValueCount()))
131 GMX_THROW(InvalidInputError("Too few (valid) values provided"));
135 void SelectionOptionStorage::processAll()
137 if (!isSet() && !defaultText_.empty())
139 manager_.convertOptionValue(this, defaultText_, true);
141 if (isRequired() && !isSet())
143 manager_.requestOptionDelayedParsing(this);
148 void SelectionOptionStorage::setAllowedValueCount(int count)
150 // TODO: It should be possible to have strong exception safety here.
151 MessageStringCollector errors;
152 errors.startContext("In option '" + name() + "'");
155 // Should not throw because efOption_DontCheckMinimumCount is set.
156 setMinValueCount(count);
157 if (valueCount() > 0 && valueCount() < count)
159 errors.append("Too few (valid) values provided");
164 setMaxValueCount(count);
166 catch (const UserInputError &ex)
168 errors.append(ex.what());
170 errors.finishContext();
171 if (!errors.isEmpty())
173 GMX_THROW(InvalidInputError(errors.toString()));
177 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
179 ValueList::iterator i;
180 for (i = values().begin(); i != values().end(); ++i)
182 if (flag == efSelection_OnlyStatic && bSet && i->isDynamic())
184 MessageStringCollector errors;
185 errors.startContext("In option '" + name() + "'");
186 errors.append("Dynamic selections not supported");
187 errors.finishContext();
188 GMX_THROW(InvalidInputError(errors.toString()));
191 selectionFlags_.set(flag, bSet);
192 for (i = values().begin(); i != values().end(); ++i)
194 i->data().setFlags(selectionFlags_);
199 /********************************************************************
200 * SelectionOptionInfo
203 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage *option)
208 SelectionOptionStorage &SelectionOptionInfo::option()
210 return static_cast<SelectionOptionStorage &>(OptionInfo::option());
213 const SelectionOptionStorage &SelectionOptionInfo::option() const
215 return static_cast<const SelectionOptionStorage &>(OptionInfo::option());
218 void SelectionOptionInfo::setValueCount(int count)
220 option().setAllowedValueCount(count);
223 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
225 option().setSelectionFlag(efSelection_EvaluateVelocities, bEnabled);
228 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
230 option().setSelectionFlag(efSelection_EvaluateForces, bEnabled);
233 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
235 option().setSelectionFlag(efSelection_OnlyAtoms, bEnabled);
238 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
240 option().setSelectionFlag(efSelection_OnlyStatic, bEnabled);
243 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
245 option().setSelectionFlag(efSelection_DynamicMask, bEnabled);
249 /********************************************************************
253 AbstractOptionStoragePointer
254 SelectionOption::createStorage(const OptionManagerContainer &managers) const
256 return AbstractOptionStoragePointer(
257 new SelectionOptionStorage(
258 *this, managers.get<SelectionOptionManager>()));
262 /********************************************************************
263 * SelectionFileOptionStorage
266 SelectionFileOptionStorage::SelectionFileOptionStorage(
267 const SelectionFileOption &settings, SelectionOptionManager *manager)
268 : AbstractOptionStorage(settings, OptionFlags() | efOption_MultipleTimes
269 | efOption_DontCheckMinimumCount),
270 info_(this), manager_(*manager), bValueParsed_(false)
272 GMX_RELEASE_ASSERT(manager != NULL,
273 "SelectionOptionManager must be added before SelectionFileOption");
276 void SelectionFileOptionStorage::clearSet()
278 bValueParsed_ = false;
281 void SelectionFileOptionStorage::convertValue(const std::string &value)
285 GMX_THROW(InvalidInputError("More than one file name provided"));
287 bValueParsed_ = true;
288 // TODO: Should we throw an InvalidInputError if the file does not exist?
289 manager_.parseRequestedFromFile(value);
292 void SelectionFileOptionStorage::processSet()
296 GMX_THROW(InvalidInputError("No file name provided"));
301 /********************************************************************
302 * SelectionFileOptionInfo
305 SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage *option)
311 /********************************************************************
312 * SelectionFileOption
315 SelectionFileOption::SelectionFileOption(const char *name)
316 : AbstractOption(name)
318 setDescription("Provide selections from files");
321 AbstractOptionStoragePointer
322 SelectionFileOption::createStorage(const OptionManagerContainer &managers) const
324 return AbstractOptionStoragePointer(
325 new SelectionFileOptionStorage(
326 *this, managers.get<SelectionOptionManager>()));