3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements gmx::SelectionOptionManager.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_selection
38 #include "selectionoptionmanager.h"
42 #include "gromacs/selection/selection.h"
43 #include "gromacs/selection/selectioncollection.h"
44 #include "gromacs/utility/exceptions.h"
45 #include "gromacs/utility/stringutil.h"
47 #include "selectionoptionstorage.h"
52 /********************************************************************
53 * SelectionOptionManager::Impl
57 * Private implemention class for SelectionOptionManager.
59 * \ingroup module_selection
61 class SelectionOptionManager::Impl
65 * Request for postponed parsing of selections.
67 struct SelectionRequest
69 //! Initializes a request for the given option.
70 explicit SelectionRequest(SelectionOptionStorage *storage)
75 //! Returns name of the requested selection optin.
76 const std::string &name() const
78 return storage_->name();
80 //! Returns description for the requested selection option.
81 const std::string &description() const
83 return storage_->description();
86 * Returns the number of selections requested in this request.
88 * -1 indicates no upper limit.
92 return storage_->maxValueCount();
95 //! Storage object to which the selections will be added.
96 SelectionOptionStorage *storage_;
99 //! Collection for a list of selection requests.
100 typedef std::vector<SelectionRequest> RequestList;
101 //! Collection for list of option storage objects.
102 typedef std::vector<SelectionOptionStorage *> OptionList;
105 * Helper class that clears a request list on scope exit.
107 * Methods in this class do not throw.
109 class RequestsClearer
112 //! Constructs an object that clears given list on scope exit.
113 explicit RequestsClearer(RequestList *requests)
114 : requests_(requests)
117 //! Clears the request list given to the constructor.
124 RequestList *requests_;
128 * Creates a new selection collection.
130 * \throws std::bad_alloc if out of memory.
132 explicit Impl(SelectionCollection *collection);
135 * Assign selections from a list to pending requests.
137 * \param[in] selections List of selections to assign.
138 * \throws std::bad_alloc if out of memory.
139 * \throws InvalidInputError if the assignment cannot be done
140 * (see parseRequestedFromFile() for documented conditions).
142 * Loops through \p selections and the pending requests lists in order,
143 * and for each requests, assigns the first yet unassigned selections
146 void placeSelectionsInRequests(const SelectionList &selections);
148 * Adds a request for each required option that is not yet set.
150 * \throws std::bad_alloc if out of memory.
152 void requestUnsetRequiredOptions();
154 //! Selection collection to which selections are stored.
155 SelectionCollection &collection_;
156 //! List of selection options (storage objects) this manager manages.
158 //! List of selections requested for later parsing.
159 RequestList requests_;
162 SelectionOptionManager::Impl::Impl(SelectionCollection *collection)
163 : collection_(*collection)
167 void SelectionOptionManager::Impl::placeSelectionsInRequests(
168 const SelectionList &selections)
170 if (requests_.empty())
172 requestUnsetRequiredOptions();
175 RequestsClearer clearRequestsOnExit(&requests_);
177 SelectionList::const_iterator first = selections.begin();
178 SelectionList::const_iterator last = first;
179 RequestList::const_iterator i;
180 for (i = requests_.begin(); i != requests_.end(); ++i)
182 const SelectionRequest &request = *i;
183 if (request.count() > 0)
185 int remaining = selections.end() - first;
186 if (remaining < request.count())
188 int assigned = first - selections.begin();
189 GMX_THROW(InvalidInputError(formatString(
190 "Too few selections provided for '%s': "
191 "Expected %d selections, but only %d left "
192 "after assigning the first %d to other selections.",
193 request.name().c_str(), request.count(),
194 remaining, assigned)));
196 last = first + request.count();
200 RequestList::const_iterator nextRequest = i;
202 if (nextRequest != requests_.end())
204 const char *name = request.name().c_str();
205 const char *conflictName = nextRequest->name().c_str();
206 GMX_THROW(InvalidInputError(formatString(
207 "Ambiguous selections for '%s' and '%s': "
208 "Any number of selections is acceptable for "
209 "'%s', but you have requested subsequent "
210 "selections to be assigned to '%s'. "
211 "Resolution for such cases is not implemented, "
212 "and may be impossible.",
213 name, conflictName, name, conflictName)));
215 last = selections.end();
217 SelectionList curr(first, last);
218 request.storage_->addSelections(curr, true);
221 if (last != selections.end())
223 int count = selections.end() - selections.begin();
224 int remaining = selections.end() - last;
225 int assigned = last - selections.begin();
226 GMX_THROW(InvalidInputError(formatString(
227 "Too many selections provided: "
228 "Expected %d selections, but %d provided. "
229 "Last %d selections could not be assigned to any option.",
230 assigned, count, remaining)));
234 void SelectionOptionManager::Impl::requestUnsetRequiredOptions()
236 OptionList::const_iterator i;
237 for (i = options_.begin(); i != options_.end(); ++i)
239 SelectionOptionStorage &storage = **i;
240 if (storage.isRequired() && !storage.isSet())
242 requests_.push_back(SelectionRequest(&storage));
248 /********************************************************************
249 * SelectionOptionManager
252 SelectionOptionManager::SelectionOptionManager(SelectionCollection *collection)
253 : impl_(new Impl(collection))
257 SelectionOptionManager::~SelectionOptionManager()
262 SelectionOptionManager::registerOption(SelectionOptionStorage *storage)
264 impl_->requests_.reserve(impl_->options_.size() + 1);
265 impl_->options_.push_back(storage);
269 SelectionOptionManager::convertOptionValue(SelectionOptionStorage *storage,
270 const std::string &value)
272 SelectionList selections = impl_->collection_.parseFromString(value);
273 storage->addSelections(selections, false);
277 SelectionOptionManager::requestOptionDelayedParsing(
278 SelectionOptionStorage *storage)
280 impl_->requests_.push_back(Impl::SelectionRequest(storage));
284 SelectionOptionManager::parseRequestedFromStdin(bool bInteractive)
286 Impl::RequestsClearer clearRequestsOnExit(&impl_->requests_);
288 Impl::RequestList::const_iterator i;
289 for (i = impl_->requests_.begin(); i != impl_->requests_.end(); ++i)
291 const Impl::SelectionRequest &request = *i;
294 std::fprintf(stderr, "\nSpecify ");
295 if (request.count() < 0)
297 std::fprintf(stderr, "any number of selections");
299 else if (request.count() == 1)
301 std::fprintf(stderr, "a selection");
305 std::fprintf(stderr, "%d selections", request.count());
307 std::fprintf(stderr, " for option '%s' (%s):\n",
308 request.name().c_str(), request.description().c_str());
309 std::fprintf(stderr, "(one selection per line, 'help' for help%s)\n",
310 request.count() < 0 ? ", Ctrl-D to end" : "");
312 SelectionList selections
313 = impl_->collection_.parseFromStdin(request.count(), bInteractive);
314 request.storage_->addSelections(selections, true);
319 SelectionOptionManager::parseRequestedFromFile(const std::string &filename)
321 SelectionList selections = impl_->collection_.parseFromFile(filename);
324 impl_->placeSelectionsInRequests(selections);
326 catch (GromacsException &ex)
328 ex.prependContext(formatString(
329 "Error in adding selections from file '%s'",
336 SelectionOptionManager::parseRequestedFromString(const std::string &str)
338 SelectionList selections = impl_->collection_.parseFromString(str);
339 impl_->placeSelectionsInRequests(selections);