2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019, 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 internal selection methods for numeric and string keyword
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_selection
51 #include "gromacs/utility/arraysize.h"
52 #include "gromacs/utility/cstringutil.h"
53 #include "gromacs/utility/exceptions.h"
54 #include "gromacs/utility/gmxassert.h"
55 #include "gromacs/utility/smalloc.h"
56 #include "gromacs/utility/stringutil.h"
59 #include "parsetree.h"
63 #include "selmethod.h"
66 * Allocates data for integer keyword evaluation.
68 * \param[in] npar Not used.
69 * \param param Not used.
70 * \returns Pointer to the allocated data (\ref t_methoddata_kwint).
72 * Allocates memory for a \ref t_methoddata_kwint structure.
75 init_data_kwint(int npar
, gmx_ana_selparam_t
* param
);
77 * Allocates data for real keyword evaluation.
79 * \param[in] npar Not used.
80 * \param param Not used.
81 * \returns Pointer to the allocated data (\ref t_methoddata_kwreal).
83 * Allocates memory for a \ref t_methoddata_kwreal structure.
86 init_data_kwreal(int npar
, gmx_ana_selparam_t
* param
);
88 * Allocates data for string keyword evaluation.
90 * \param[in] npar Not used.
91 * \param param Not used.
92 * \returns Pointer to the allocated data (t_methoddata_kwstr).
94 * Allocates memory for a t_methoddata_kwstr structure.
97 init_data_kwstr(int npar
, gmx_ana_selparam_t
* param
);
98 /** /brief Initializes data for integer keyword evaluation.
100 * \param[in] top Not used.
101 * \param[in] npar Not used (should be 2).
102 * \param[in] param Method parameters (should point to \ref smparams_keyword_int).
103 * \param[in] data Should point to \ref t_methoddata_kwint.
106 init_kwint(const gmx_mtop_t
*top
, int npar
, gmx_ana_selparam_t
*param
, void *data
);
108 * Initializes data for real keyword evaluation.
110 * \param[in] top Not used.
111 * \param[in] npar Not used (should be 2).
112 * \param[in] param Method parameters (should point to \ref smparams_keyword_real).
113 * \param[in] data Should point to \ref t_methoddata_kwreal.
114 * \returns 0 (the initialization always succeeds).
117 init_kwreal(const gmx_mtop_t
*top
, int npar
, gmx_ana_selparam_t
*param
, void *data
);
119 * Initializes data for string keyword evaluation.
121 * \param[in] top Not used.
122 * \param[in] npar Not used (should be 2).
123 * \param[in] param Method parameters (should point to \ref smparams_keyword_str).
124 * \param[in] data Should point to t_methoddata_kwstr.
127 init_kwstr(const gmx_mtop_t
*top
, int npar
, gmx_ana_selparam_t
*param
, void *data
);
128 /** Frees the memory allocated for string keyword evaluation. */
130 free_data_kwstr(void *data
);
131 /** Evaluates integer selection keywords. */
133 evaluate_keyword_int(const gmx::SelMethodEvalContext
&context
,
134 gmx_ana_index_t
*g
, gmx_ana_selvalue_t
*out
, void *data
);
135 /** Evaluates real selection keywords. */
137 evaluate_keyword_real(const gmx::SelMethodEvalContext
&context
,
138 gmx_ana_index_t
*g
, gmx_ana_selvalue_t
*out
, void *data
);
139 /** Evaluates string selection keywords. */
141 evaluate_keyword_str(const gmx::SelMethodEvalContext
&context
,
142 gmx_ana_index_t
*g
, gmx_ana_selvalue_t
*out
, void *data
);
145 * Data structure for integer keyword expression evaluation.
147 typedef struct t_methoddata_kwint
149 /** Array of values for the keyword. */
151 /** Number of ranges in the \p r array. */
154 * Array of sorted integer ranges to match against.
156 * Each range is made of two integers, giving the endpoints (inclusive).
157 * This field stores the pointer to the ranges allocated by the
158 * parameter parser; see \ref SPAR_RANGES for more information.
161 } t_methoddata_kwint
;
164 * Data structure for real keyword expression evaluation.
166 typedef struct t_methoddata_kwreal
168 /** Array of values for the keyword. */
170 /** Number of ranges in the \p r array. */
173 * Array of sorted ranges to match against.
175 * Each range is made of two values, giving the endpoints (inclusive).
176 * This field stores the pointer to the ranges allocated by the
177 * parameter parser; see \ref SPAR_RANGES for more information.
180 } t_methoddata_kwreal
;
186 * Single item in the list of strings/regular expressions to match.
188 * \ingroup module_selection
190 class StringKeywordMatchItem
194 * Constructs a matcher from a string.
196 * \param[in] matchType String matching type.
197 * \param[in] str String to use for matching.
199 StringKeywordMatchItem(gmx::SelectionStringMatchType matchType
,
203 useRegExp_
= (matchType
== gmx::eStringMatchType_RegularExpression
);
204 if (matchType
== gmx::eStringMatchType_Auto
)
206 for (size_t j
= 0; j
< std::strlen(str
); ++j
)
208 if (std::ispunct(str
[j
]) && str
[j
] != '?' && str
[j
] != '*')
219 regex_
.assign(str
, std::regex::nosubs
| std::regex::extended
);
221 catch (const std::regex_error
& /*ex*/)
223 // TODO: Better error messages.
224 GMX_THROW(gmx::InvalidInputError
225 (gmx::formatString("Error in regular expression \"%s\"", str
)));
231 * Checks whether this item matches a string.
233 * \param[in] matchType String matching type.
234 * \param[in] value String to match.
235 * \returns true if this item matches \p value.
237 bool match(gmx::SelectionStringMatchType matchType
,
238 const char *value
) const
240 if (matchType
== gmx::eStringMatchType_Exact
)
242 return str_
== value
;
246 return std::regex_match(value
, regex_
);
250 return gmx_wcmatch(str_
.c_str(), value
) == 0;
255 //! The raw string passed for the matcher.
257 //! Whether a regular expression match is used.
259 //! Regular expression compiled from \p str_, if applicable.
264 * Data structure for string keyword expression evaluation.
266 struct t_methoddata_kwstr
268 /** Matching type for the strings. */
269 gmx::SelectionStringMatchType matchType
;
270 /** Array of values for the keyword. */
272 /** Array of strings/regular expressions to match against.*/
273 std::vector
<StringKeywordMatchItem
> matches
;
278 /** Parameters for integer keyword evaluation. */
279 static gmx_ana_selparam_t smparams_keyword_int
[] = {
280 {nullptr, {INT_VALUE
, -1, {nullptr}}, nullptr, SPAR_ATOMVAL
},
281 {nullptr, {INT_VALUE
, -1, {nullptr}}, nullptr, SPAR_RANGES
| SPAR_VARNUM
},
284 /** Parameters for real keyword evaluation. */
285 static gmx_ana_selparam_t smparams_keyword_real
[] = {
286 {nullptr, {REAL_VALUE
, -1, {nullptr}}, nullptr, SPAR_ATOMVAL
| SPAR_DYNAMIC
},
287 {nullptr, {REAL_VALUE
, -1, {nullptr}}, nullptr, SPAR_RANGES
| SPAR_VARNUM
},
290 /** Parameters for string keyword evaluation. */
291 static gmx_ana_selparam_t smparams_keyword_str
[] = {
292 {nullptr, {STR_VALUE
, -1, {nullptr}}, nullptr, SPAR_ATOMVAL
},
293 {nullptr, {STR_VALUE
, -1, {nullptr}}, nullptr, SPAR_VARNUM
},
296 /** Selection method data for integer keyword evaluation. */
297 gmx_ana_selmethod_t sm_keyword_int
= {
298 "kw_int", GROUP_VALUE
, SMETH_SINGLEVAL
,
299 asize(smparams_keyword_int
), smparams_keyword_int
,
306 &evaluate_keyword_int
,
308 {nullptr, nullptr, 0, nullptr},
311 /** Selection method data for real keyword evaluation. */
312 gmx_ana_selmethod_t sm_keyword_real
= {
313 "kw_real", GROUP_VALUE
, SMETH_SINGLEVAL
,
314 asize(smparams_keyword_real
), smparams_keyword_real
,
321 &evaluate_keyword_real
,
323 {nullptr, nullptr, 0, nullptr},
326 /** Selection method data for string keyword evaluation. */
327 gmx_ana_selmethod_t sm_keyword_str
= {
328 "kw_str", GROUP_VALUE
, SMETH_SINGLEVAL
,
329 asize(smparams_keyword_str
), smparams_keyword_str
,
336 &evaluate_keyword_str
,
338 {nullptr, nullptr, 0, nullptr},
342 * Initializes keyword evaluation for an arbitrary group.
344 * \param[in] top Not used.
345 * \param[in] npar Not used.
346 * \param[in] param Not used.
347 * \param[in] data Should point to \ref t_methoddata_kweval.
348 * \returns 0 on success, a non-zero error code on return.
350 * Calls the initialization method of the wrapped keyword.
353 init_kweval(const gmx_mtop_t
*top
, int npar
, gmx_ana_selparam_t
* param
, void *data
);
355 * Initializes output for keyword evaluation in an arbitrary group.
357 * \param[in] top Not used.
358 * \param[in,out] out Pointer to output data structure.
359 * \param[in,out] data Should point to \c t_methoddata_kweval.
360 * \returns 0 for success.
363 init_output_kweval(const gmx_mtop_t
*top
, gmx_ana_selvalue_t
*out
, void *data
);
364 /** Frees the data allocated for keyword evaluation in an arbitrary group. */
366 free_data_kweval(void *data
);
367 /** Initializes frame evaluation for keyword evaluation in an arbitrary group. */
369 init_frame_kweval(const gmx::SelMethodEvalContext
&context
, void *data
);
371 * Evaluates keywords in an arbitrary group.
373 * See sel_updatefunc() for description of the parameters.
374 * \p data should point to a \c t_methoddata_kweval.
376 * Calls the evaluation function of the wrapped keyword with the given
377 * parameters, with the exception of using \c t_methoddata_kweval::g for the
381 evaluate_kweval(const gmx::SelMethodEvalContext
&context
, gmx_ana_index_t
*g
,
382 gmx_ana_selvalue_t
*out
, void *data
);
384 * Evaluates keywords in an arbitrary set of positions.
386 * See sel_updatefunc() for description of the parameters.
387 * \p data should point to a \c t_methoddata_kweval.
389 * Calls the evaluation function of the wrapped keyword with the given
390 * parameters, with the exception of using \c t_methoddata_kweval::p for the
391 * evaluation positions.
394 evaluate_kweval_pos(const gmx::SelMethodEvalContext
&context
, gmx_ana_index_t
*g
,
395 gmx_ana_selvalue_t
*out
, void *data
);
398 * Data structure for keyword evaluation in arbitrary groups.
400 struct t_methoddata_kweval
402 //! Initialize new keyword evaluator for the given keyword.
403 t_methoddata_kweval(gmx_ana_selmethod_t
*method
, void *data
)
404 : kwmethod(method
), kwmdata(data
)
406 gmx_ana_index_clear(&g
);
408 ~t_methoddata_kweval()
410 _gmx_selelem_free_method(kwmethod
, kwmdata
);
413 //! Wrapped keyword method for evaluating the values.
414 gmx_ana_selmethod_t
*kwmethod
;
415 //! Method data for \p kwmethod.
417 //! Group in which \p kwmethod should be evaluated.
419 //! Positions for which \p kwmethod should be evaluated.
423 /** Parameters for keyword evaluation in an arbitrary group. */
424 static gmx_ana_selparam_t smparams_kweval_group
[] = {
425 {nullptr, {GROUP_VALUE
, 1, {nullptr}}, nullptr, SPAR_DYNAMIC
},
427 /** Parameters for keyword evaluation from positions. */
428 static gmx_ana_selparam_t smparams_kweval_pos
[] = {
429 {nullptr, {POS_VALUE
, 1, {nullptr}}, nullptr, SPAR_DYNAMIC
},
433 /********************************************************************
434 * INTEGER KEYWORD EVALUATION
435 ********************************************************************/
438 init_data_kwint(int /* npar */, gmx_ana_selparam_t
* /* param */)
440 t_methoddata_kwint
*data
;
447 init_kwint(const gmx_mtop_t
* /* top */, int /* npar */, gmx_ana_selparam_t
*param
, void *data
)
449 t_methoddata_kwint
*d
= static_cast<t_methoddata_kwint
*>(data
);
451 d
->v
= param
[0].val
.u
.i
;
452 d
->n
= param
[1].val
.nr
;
453 d
->r
= param
[1].val
.u
.i
;
457 * See sel_updatefunc() for description of the parameters.
458 * \p data should point to a \c t_methoddata_kwint.
460 * Does a binary search to find which atoms match the ranges in the
461 * \c t_methoddata_kwint structure for this selection.
462 * Matching atoms are stored in \p out->u.g.
465 evaluate_keyword_int(const gmx::SelMethodEvalContext
& /*context*/,
466 gmx_ana_index_t
*g
, gmx_ana_selvalue_t
*out
, void *data
)
468 t_methoddata_kwint
*d
= static_cast<t_methoddata_kwint
*>(data
);
469 int n
, i
, j
, jmin
, jmax
;
474 for (i
= 0; i
< g
->isize
; ++i
)
477 if (d
->r
[0] > val
|| d
->r
[2*n
-1] < val
)
483 while (jmax
- jmin
> 1)
485 j
= jmin
+ (jmax
- jmin
) / 2;
493 if (val
<= d
->r
[2*j
+1])
500 if (val
<= d
->r
[2*jmin
+1])
502 out
->u
.g
->index
[out
->u
.g
->isize
++] = g
->index
[i
];
508 /********************************************************************
509 * REAL KEYWORD EVALUATION
510 ********************************************************************/
513 init_data_kwreal(int /* npar */, gmx_ana_selparam_t
* /* param */)
515 t_methoddata_kwreal
*data
;
522 init_kwreal(const gmx_mtop_t
* /* top */, int /* npar */, gmx_ana_selparam_t
*param
, void *data
)
524 t_methoddata_kwreal
*d
= static_cast<t_methoddata_kwreal
*>(data
);
526 d
->v
= param
[0].val
.u
.r
;
527 d
->n
= param
[1].val
.nr
;
528 d
->r
= param
[1].val
.u
.r
;
532 * See sel_updatefunc() for description of the parameters.
533 * \p data should point to a \c t_methoddata_kwreal.
535 * Does a binary search to find which atoms match the ranges in the
536 * \c t_methoddata_kwreal structure for this selection.
537 * Matching atoms are stored in \p out->u.g.
540 evaluate_keyword_real(const gmx::SelMethodEvalContext
& /*context*/,
541 gmx_ana_index_t
*g
, gmx_ana_selvalue_t
*out
, void *data
)
543 t_methoddata_kwreal
*d
= static_cast<t_methoddata_kwreal
*>(data
);
544 int n
, i
, j
, jmin
, jmax
;
549 for (i
= 0; i
< g
->isize
; ++i
)
552 if (d
->r
[0] > val
|| d
->r
[2*n
-1] < val
)
558 while (jmax
- jmin
> 1)
560 j
= jmin
+ (jmax
- jmin
) / 2;
568 if (val
<= d
->r
[2*j
+1])
575 if (val
<= d
->r
[2*jmin
+1])
577 out
->u
.g
->index
[out
->u
.g
->isize
++] = g
->index
[i
];
583 /********************************************************************
584 * STRING KEYWORD EVALUATION
585 ********************************************************************/
588 init_data_kwstr(int /* npar */, gmx_ana_selparam_t
* /* param */)
590 t_methoddata_kwstr
*data
= new t_methoddata_kwstr();
591 data
->matchType
= gmx::eStringMatchType_Auto
;
596 * \param[in,out] sel Selection element to initialize.
597 * \param[in] matchType Method to use to match string values.
599 * Sets the string matching method for string keyword matching.
602 _gmx_selelem_set_kwstr_match_type(const gmx::SelectionTreeElementPointer
&sel
,
603 gmx::SelectionStringMatchType matchType
)
605 t_methoddata_kwstr
*d
= static_cast<t_methoddata_kwstr
*>(sel
->u
.expr
.mdata
);
607 if (sel
->type
!= SEL_EXPRESSION
|| !sel
->u
.expr
.method
608 || sel
->u
.expr
.method
->name
!= sm_keyword_str
.name
)
612 d
->matchType
= matchType
;
616 init_kwstr(const gmx_mtop_t
* /* top */, int /* npar */, gmx_ana_selparam_t
*param
, void *data
)
618 t_methoddata_kwstr
*d
= static_cast<t_methoddata_kwstr
*>(data
);
620 d
->v
= param
[0].val
.u
.s
;
621 /* Return if this is not the first time */
622 if (!d
->matches
.empty())
626 int n
= param
[1].val
.nr
;
627 d
->matches
.reserve(n
);
628 for (int i
= 0; i
< n
; ++i
)
630 const char *s
= param
[1].val
.u
.s
[i
];
631 d
->matches
.emplace_back(d
->matchType
, s
);
636 * \param data Data to free (should point to a t_methoddata_kwstr).
639 free_data_kwstr(void *data
)
641 t_methoddata_kwstr
*d
= static_cast<t_methoddata_kwstr
*>(data
);
646 * See sel_updatefunc() for description of the parameters.
647 * \p data should point to a \c t_methoddata_kwstr.
649 * Does a linear search to find which atoms match the strings in the
650 * \c t_methoddata_kwstr structure for this selection.
651 * Wildcards are allowed in the strings.
652 * Matching atoms are stored in \p out->u.g.
655 evaluate_keyword_str(const gmx::SelMethodEvalContext
& /*context*/,
656 gmx_ana_index_t
*g
, gmx_ana_selvalue_t
*out
, void *data
)
658 t_methoddata_kwstr
*d
= static_cast<t_methoddata_kwstr
*>(data
);
661 for (int i
= 0; i
< g
->isize
; ++i
)
663 for (size_t j
= 0; j
< d
->matches
.size(); ++j
)
665 if (d
->matches
[j
].match(d
->matchType
, d
->v
[i
]))
667 out
->u
.g
->index
[out
->u
.g
->isize
++] = g
->index
[i
];
675 /********************************************************************
676 * KEYWORD EVALUATION FOR ARBITRARY GROUPS
677 ********************************************************************/
680 init_kweval(const gmx_mtop_t
*top
, int /* npar */, gmx_ana_selparam_t
* /* param */, void *data
)
682 t_methoddata_kweval
*d
= static_cast<t_methoddata_kweval
*>(data
);
684 d
->kwmethod
->init(top
, 0, nullptr, d
->kwmdata
);
688 init_output_kweval(const gmx_mtop_t
* /* top */, gmx_ana_selvalue_t
*out
, void *data
)
690 t_methoddata_kweval
*d
= static_cast<t_methoddata_kweval
*>(data
);
692 out
->nr
= d
->g
.isize
;
693 _gmx_selvalue_reserve(out
, out
->nr
);
697 * \param data Data to free (should point to a \c t_methoddata_kweval).
699 * Frees the memory allocated for all the members of \c t_methoddata_kweval.
702 free_data_kweval(void *data
)
704 t_methoddata_kweval
*d
= static_cast<t_methoddata_kweval
*>(data
);
710 * \param[in] context Evaluation context.
711 * \param data Should point to a \ref t_methoddata_kweval.
714 init_frame_kweval(const gmx::SelMethodEvalContext
&context
, void *data
)
716 t_methoddata_kweval
*d
= static_cast<t_methoddata_kweval
*>(data
);
718 d
->kwmethod
->init_frame(context
, d
->kwmdata
);
722 evaluate_kweval(const gmx::SelMethodEvalContext
&context
,
723 gmx_ana_index_t
* /* g */, gmx_ana_selvalue_t
*out
, void *data
)
725 t_methoddata_kweval
*d
= static_cast<t_methoddata_kweval
*>(data
);
727 d
->kwmethod
->update(context
, &d
->g
, out
, d
->kwmdata
);
731 evaluate_kweval_pos(const gmx::SelMethodEvalContext
&context
,
732 gmx_ana_index_t
* /* g */, gmx_ana_selvalue_t
*out
, void *data
)
734 t_methoddata_kweval
*d
= static_cast<t_methoddata_kweval
*>(data
);
736 d
->kwmethod
->pupdate(context
, &d
->p
, out
, d
->kwmdata
);
740 * Initializes a selection element for evaluating a keyword in a given group.
742 * \param[in] method Keyword selection method to evaluate.
743 * \param[in] params Parameters to pass to initialization (the child group).
744 * \param[in] scanner Scanner data structure.
745 * \returns Pointer to the created selection element.
747 * Implements _gmx_sel_init_keyword_evaluator() for \ref GROUP_VALUE input
750 static gmx::SelectionTreeElementPointer
751 init_evaluator_group(gmx_ana_selmethod_t
*method
,
752 const gmx::SelectionParserParameterList
¶ms
,
755 if ((method
->flags
& (SMETH_SINGLEVAL
| SMETH_VARNUMVAL
))
756 || method
->outinit
|| method
->pupdate
)
759 = gmx::formatString("Keyword '%s' cannot be evaluated in this context",
761 GMX_THROW(gmx::InvalidInputError(message
));
764 // TODO: For same ... as ..., some other location could be more intuitive.
765 gmx::SelectionTreeElementPointer
sel(
766 new gmx::SelectionTreeElement(
767 SEL_EXPRESSION
, _gmx_sel_lexer_get_current_location(scanner
)));
768 _gmx_selelem_set_method(sel
, method
, scanner
);
770 t_methoddata_kweval
*data
771 = new t_methoddata_kweval(sel
->u
.expr
.method
, sel
->u
.expr
.mdata
);
773 snew(sel
->u
.expr
.method
, 1);
774 sel
->u
.expr
.method
->name
= data
->kwmethod
->name
;
775 sel
->u
.expr
.method
->type
= data
->kwmethod
->type
;
776 sel
->u
.expr
.method
->flags
= data
->kwmethod
->flags
| SMETH_VARNUMVAL
;
777 sel
->u
.expr
.method
->init_data
= nullptr;
778 sel
->u
.expr
.method
->set_poscoll
= nullptr;
779 sel
->u
.expr
.method
->init
= method
->init
? &init_kweval
: nullptr;
780 sel
->u
.expr
.method
->outinit
= &init_output_kweval
;
781 sel
->u
.expr
.method
->free
= &free_data_kweval
;
782 sel
->u
.expr
.method
->init_frame
= method
->init_frame
? &init_frame_kweval
: nullptr;
783 sel
->u
.expr
.method
->update
= &evaluate_kweval
;
784 sel
->u
.expr
.method
->pupdate
= nullptr;
785 sel
->u
.expr
.method
->nparams
= asize(smparams_kweval_group
);
786 sel
->u
.expr
.method
->param
= smparams_kweval_group
;
787 _gmx_selelem_init_method_params(sel
, scanner
);
788 sel
->u
.expr
.mdata
= data
;
790 sel
->u
.expr
.method
->param
[0].val
.u
.g
= &data
->g
;
792 _gmx_sel_parse_params(params
, sel
->u
.expr
.method
->nparams
,
793 sel
->u
.expr
.method
->param
, sel
, scanner
);
798 * Initializes a selection element for evaluating a keyword in given positions.
800 * \param[in] method Keyword selection method to evaluate.
801 * \param[in] params Parameters to pass to initialization (the child positions).
802 * \param[in] scanner Scanner data structure.
803 * \returns Pointer to the created selection element.
805 * Implements _gmx_sel_init_keyword_evaluator() for \ref POS_VALUE input
808 static gmx::SelectionTreeElementPointer
809 init_evaluator_pos(gmx_ana_selmethod_t
*method
,
810 const gmx::SelectionParserParameterList
¶ms
,
813 if ((method
->flags
& (SMETH_SINGLEVAL
| SMETH_VARNUMVAL
))
814 || method
->outinit
|| method
->pupdate
== nullptr)
817 = gmx::formatString("Keyword '%s' cannot be evaluated in this context",
819 GMX_THROW(gmx::InvalidInputError(message
));
822 gmx::SelectionTreeElementPointer
sel(
823 new gmx::SelectionTreeElement(
824 SEL_EXPRESSION
, _gmx_sel_lexer_get_current_location(scanner
)));
825 _gmx_selelem_set_method(sel
, method
, scanner
);
827 t_methoddata_kweval
*data
828 = new t_methoddata_kweval(sel
->u
.expr
.method
, sel
->u
.expr
.mdata
);
830 snew(sel
->u
.expr
.method
, 1);
831 sel
->u
.expr
.method
->name
= data
->kwmethod
->name
;
832 sel
->u
.expr
.method
->type
= data
->kwmethod
->type
;
833 sel
->u
.expr
.method
->flags
= data
->kwmethod
->flags
| SMETH_SINGLEVAL
;
834 sel
->u
.expr
.method
->init_data
= nullptr;
835 sel
->u
.expr
.method
->set_poscoll
= nullptr;
836 sel
->u
.expr
.method
->init
= method
->init
? &init_kweval
: nullptr;
837 sel
->u
.expr
.method
->outinit
= nullptr;
838 sel
->u
.expr
.method
->free
= &free_data_kweval
;
839 sel
->u
.expr
.method
->init_frame
= method
->init_frame
? &init_frame_kweval
: nullptr;
840 sel
->u
.expr
.method
->update
= &evaluate_kweval_pos
;
841 sel
->u
.expr
.method
->pupdate
= nullptr;
842 sel
->u
.expr
.method
->nparams
= asize(smparams_kweval_pos
);
843 sel
->u
.expr
.method
->param
= smparams_kweval_pos
;
844 _gmx_selelem_init_method_params(sel
, scanner
);
845 sel
->u
.expr
.mdata
= data
;
847 sel
->u
.expr
.method
->param
[0].val
.u
.p
= &data
->p
;
849 _gmx_sel_parse_params(params
, sel
->u
.expr
.method
->nparams
,
850 sel
->u
.expr
.method
->param
, sel
, scanner
);
854 gmx::SelectionTreeElementPointer
855 _gmx_sel_init_keyword_evaluator(gmx_ana_selmethod_t
*method
,
856 const gmx::SelectionTreeElementPointer
&child
,
859 gmx::SelectionParserParameterList params
;
861 gmx::SelectionParserParameter::createFromExpression(nullptr, child
));
862 if (child
->v
.type
== GROUP_VALUE
)
864 return init_evaluator_group(method
, params
, scanner
);
866 else if (child
->v
.type
== POS_VALUE
)
868 return init_evaluator_pos(method
, params
, scanner
);
872 std::string
text(_gmx_sel_lexer_get_text(scanner
, child
->location()));
874 = gmx::formatString("Expression '%s' cannot be used to evaluate keywords",
876 GMX_THROW(gmx::InvalidInputError(message
));