Overpass API regression suite coverage (Saxony) / overpass_api/statements/evaluator.h

overpass_api/statements/evaluator.h

88 of 109 instrumented lines run by the suite (80.7%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#ifndef DE__OSM3S___OVERPASS_API__STATEMENTS__EVALUATOR_H
20#define DE__OSM3S___OVERPASS_API__STATEMENTS__EVALUATOR_H
23#include "statement.h"
24#include "../data/collect_members.h"
25#include "../data/meta_collector.h"
26#include "../data/relation_geometry_store.h"
27#include "../data/tag_store.h"
28#include "../data/utils.h"
29#include "../data/way_geometry_store.h"
31#include <map>
32#include <string>
33#include <vector>
36template< typename Object >
37struct Element_With_Context
39 Element_With_Context(const Object* object_,
40 const std::vector< std::pair< std::string, std::string > >* tags_,
41 const Opaque_Geometry* geometry_,
42 const OSM_Element_Metadata_Skeleton< typename Object::Id_Type >* meta_,
43 const std::string* user_name_)
449664487189 q : object(object_), tags(tags_), geometry(geometry_), meta(meta_), user_name(user_name_) {}
46 const Object* object;
47 const std::vector< std::pair< std::string, std::string > >* tags;
48 const Opaque_Geometry* geometry;
49 const OSM_Element_Metadata_Skeleton< typename Object::Id_Type >* meta;
50 const std::string* user_name;
51};
54struct Set_Usage
56618147 q Set_Usage(const std::string& set_name_, uint usage_) : set_name(set_name_), usage(usage_) {}
58 std::string set_name;
59 uint usage;
61 static const uint SKELETON;
62 static const uint TAGS;
63 static const uint GEOMETRY;
64 static const uint META;
65 static const uint SET_KEY_VALUES;
670 bool operator<(const Set_Usage& rhs) const { return this->set_name < rhs.set_name; }
68};
71bool assert_element_in_context(Error_Output* error_output,
72 const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context);
75bool assert_member_in_context(Error_Output* error_output,
76 const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context);
79struct Requested_Context
811970229 q Requested_Context() : object_usage(0), role_names_requested(false), user_names_requested(false) {}
82 Requested_Context& add_usage(const std::string& set_name, uint usage);
83 Requested_Context& add_usage(uint usage);
84 Requested_Context& add_role_names();
85 Requested_Context& add_user_names();
86 void add(const Requested_Context& rhs);
87 void bind(const std::string& set_name);
89 std::vector< Set_Usage > set_usage;
90 uint object_usage;
91 bool role_names_requested;
92 bool user_names_requested;
93};
96struct Prepare_Task_Context;
99struct Set_With_Context
101private:
102 Set_With_Context(const Set_With_Context&);
103 Set_With_Context& operator=(const Set_With_Context&);
105public:
106739220 q Set_With_Context() : base(0), set_key_values(0), parent(0),
107739220 q tag_store_nodes(0), tag_store_attic_nodes(0),
108739220 q tag_store_ways(0), tag_store_attic_ways(0),
109739220 q tag_store_relations(0), tag_store_attic_relations(0),
110739220 q tag_store_areas(0), tag_store_deriveds(0),
111739220 q use_geometry(false), current_geometry(0),
112739220 q way_geometry_store(0), attic_way_geometry_store(0),
113739220 q relation_geometry_store(0), attic_relation_geometry_store(0),
114739220 q meta_collector_nodes(0), meta_collector_attic_nodes(0),
115739220 q meta_collector_ways(0), meta_collector_attic_ways(0),
116739220 q meta_collector_relations(0), meta_collector_attic_relations(0) {}
118 ~Set_With_Context()
120739220 q delete tag_store_nodes;
121739220 q delete tag_store_attic_nodes;
122739220 q delete tag_store_ways;
123739220 q delete tag_store_attic_ways;
124739220 q delete tag_store_relations;
125739220 q delete tag_store_attic_relations;
126739220 q delete tag_store_areas;
127739220 q delete tag_store_deriveds;
129739220 q delete current_geometry;
130739220 q delete way_geometry_store;
131739220 q delete attic_way_geometry_store;
132739220 q delete relation_geometry_store;
133739220 q delete attic_relation_geometry_store;
135739220 q delete meta_collector_nodes;
136739220 q delete meta_collector_attic_nodes;
137739220 q delete meta_collector_ways;
138739220 q delete meta_collector_attic_ways;
139739220 q delete meta_collector_relations;
140739220 q delete meta_collector_attic_relations;
143 Element_With_Context< Node_Skeleton > get_context(const Uint32_Index& index, const Node_Skeleton& elem);
144 Element_With_Context< Attic< Node_Skeleton > > get_context(
145 const Uint32_Index& index, const Attic< Node_Skeleton >& elem);
146 Element_With_Context< Way_Skeleton > get_context(const Uint31_Index& index, const Way_Skeleton& elem);
147 Element_With_Context< Attic< Way_Skeleton > > get_context(
148 const Uint31_Index& index, const Attic< Way_Skeleton >& elem);
149 Element_With_Context< Relation_Skeleton > get_context(const Uint31_Index& index, const Relation_Skeleton& elem);
150 Element_With_Context< Attic< Relation_Skeleton > > get_context(
151 const Uint31_Index& index, const Attic< Relation_Skeleton >& elem);
152 Element_With_Context< Area_Skeleton > get_context(const Uint31_Index& index, const Area_Skeleton& elem);
153 Element_With_Context< Derived_Skeleton > get_context(const Uint31_Index& index, const Derived_Structure& elem);
155 void prefetch(uint usage, const Set& set, const Statement& query, Resource_Manager& rman);
157 std::string name;
158 const Set* base;
159 const std::map< std::string, std::string >* set_key_values;
160 const Prepare_Task_Context* parent;
162 Tag_Store< Uint32_Index, Node_Skeleton >* tag_store_nodes;
163 Tag_Store< Uint32_Index, Node_Skeleton >* tag_store_attic_nodes;
164 Tag_Store< Uint31_Index, Way_Skeleton >* tag_store_ways;
165 Tag_Store< Uint31_Index, Way_Skeleton >* tag_store_attic_ways;
166 Tag_Store< Uint31_Index, Relation_Skeleton >* tag_store_relations;
167 Tag_Store< Uint31_Index, Relation_Skeleton >* tag_store_attic_relations;
168 Tag_Store< Uint31_Index, Area_Skeleton >* tag_store_areas;
169 Tag_Store< Uint31_Index, Derived_Structure >* tag_store_deriveds;
171 bool use_geometry;
172 Opaque_Geometry* current_geometry;
173 Way_Geometry_Store* way_geometry_store;
174 Way_Geometry_Store* attic_way_geometry_store;
175 Relation_Geometry_Store* relation_geometry_store;
176 Relation_Geometry_Store* attic_relation_geometry_store;
178 Meta_Collector< Node::Index, Node_Skeleton::Id_Type >* meta_collector_nodes;
179 Attic_Meta_Collector< Node::Index, Node_Skeleton >* meta_collector_attic_nodes;
180 Meta_Collector< Way::Index, Way_Skeleton::Id_Type >* meta_collector_ways;
181 Attic_Meta_Collector< Way::Index, Way_Skeleton >* meta_collector_attic_ways;
182 Meta_Collector< Relation::Index, Relation_Skeleton::Id_Type >* meta_collector_relations;
183 Attic_Meta_Collector< Relation::Index, Relation_Skeleton >* meta_collector_attic_relations;
184};
187struct Prepare_Task_Context
189 Prepare_Task_Context(const Requested_Context& requested, const Statement& stmt, Resource_Manager& rman);
191 Set_With_Context* get_set(const std::string& set_name);
192 uint32 get_role_id(const std::string& role) const;
193 const std::string* get_user_name(uint32 user_id) const;
19433 q const std::map< uint32, std::string >* get_roles() const { return relation_member_roles_; }
196private:
197 Array< Set_With_Context > contexts;
198 const std::map< uint32, std::string >* relation_member_roles_;
199 const std::map< uint32, std::string >* users;
200};
203/* == Evaluators ==
205Evaluators are building blocks that yield on execution a value.
206The use of which of the evaluators makes sense depends on the context.
208Evaluators help to filter for elements within a query statement.
209They allow to get statistical information about query results.
210And they allow to remove or add tags from elements.
212Currently only tag evaluators are supported.
213Geometry evaluators are planned but not implemented in this version.
215The following types of evaluators exist and are explained further down:
216* Const evaluators deliver always the same value independent of context.
217* Element dependent evaluators deliver information about an individual object.
218They only make sense in the context of a single element.
219* Statistical evaluators deliver information about a set as a whole.
220* Aggregators let an element dependent evaluator loop over all elements of a set and combine its results.
221* Operators and endomorphisms combine the result of one or two evaluator executions into a new result.
223*/
226struct Eval_Task
2283120208 q virtual ~Eval_Task() {}
230 virtual std::string eval(const std::string* key) const = 0;
232 virtual std::string eval(const Element_With_Context< Node_Skeleton >& data, const std::string* key) const
233303352942 q { return eval(key); }
234 virtual std::string eval(const Element_With_Context< Attic< Node_Skeleton > >& data, const std::string* key) const
2351497614 q { return eval(key); }
236 virtual std::string eval(const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
237143279545 q { return eval(key); }
238 virtual std::string eval(const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
2396680616 q { return eval(key); }
240 virtual std::string eval(const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
241146364235 q { return eval(key); }
242 virtual std::string eval(const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const
243288467 q { return eval(key); }
244 virtual std::string eval(const Element_With_Context< Area_Skeleton >& data, const std::string* key) const
2453824 q { return eval(key); }
246 virtual std::string eval(const Element_With_Context< Derived_Skeleton >& data, const std::string* key) const
247114122316 q { return eval(key); }
249 virtual std::string eval(uint pos, const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
250627714 q { return eval(data, key); }
251 virtual std::string eval(uint pos, const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
25268891 q { return eval(data, key); }
253 virtual std::string eval(uint pos, const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
2542846553 q { return eval(data, key); }
255 virtual std::string eval(uint pos, const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const
2560 { return eval(data, key); }
257};
260struct Const_Eval_Task : public Eval_Task
2621102168 q Const_Eval_Task(const std::string& value_) : value(value_) {}
2647138114159 q virtual std::string eval(const std::string* key) const { return value; }
266private:
267 std::string value;
268};
271struct Eval_Container_Task
27377 q virtual ~Eval_Container_Task() {}
275 virtual std::vector< std::string > eval(const std::string* key) const = 0;
277 virtual std::vector< std::string > eval(
278 const Element_With_Context< Node_Skeleton >& data, const std::string* key) const
2790 { return eval(key); }
280 virtual std::vector< std::string > eval(
281 const Element_With_Context< Attic< Node_Skeleton > >& data, const std::string* key) const
2820 { return eval(key); }
283 virtual std::vector< std::string > eval(
284 const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
2850 { return eval(key); }
286 virtual std::vector< std::string > eval(
287 const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
2880 { return eval(key); }
289 virtual std::vector< std::string > eval(
290 const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
2910 { return eval(key); }
292 virtual std::vector< std::string > eval(
293 const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const
2940 { return eval(key); }
295 virtual std::vector< std::string > eval(
296 const Element_With_Context< Area_Skeleton >& data, const std::string* key) const
2970 { return eval(key); }
298 virtual std::vector< std::string > eval(
299 const Element_With_Context< Derived_Skeleton >& data, const std::string* key) const
3000 { return eval(key); }
301};
304struct Eval_Geometry_Task
30613946 q virtual ~Eval_Geometry_Task() {}
308 virtual Opaque_Geometry* eval() const = 0;
310 virtual Opaque_Geometry* eval(const Element_With_Context< Node_Skeleton >& data) const
3110 { return eval(); }
312 virtual Opaque_Geometry* eval(const Element_With_Context< Attic< Node_Skeleton > >& data) const
3130 { return eval(); }
314 virtual Opaque_Geometry* eval(const Element_With_Context< Way_Skeleton >& data) const
3150 { return eval(); }
316 virtual Opaque_Geometry* eval(const Element_With_Context< Attic< Way_Skeleton > >& data) const
3170 { return eval(); }
318 virtual Opaque_Geometry* eval(const Element_With_Context< Relation_Skeleton >& data) const
3190 { return eval(); }
320 virtual Opaque_Geometry* eval(const Element_With_Context< Attic< Relation_Skeleton > >& data) const
3210 { return eval(); }
322 virtual Opaque_Geometry* eval(const Element_With_Context< Area_Skeleton >& data) const
3230 { return eval(); }
324 virtual Opaque_Geometry* eval(const Element_With_Context< Derived_Skeleton >& data) const
3250 { return eval(); }
326};
329struct Const_Eval_Geometry_Task : public Eval_Geometry_Task
3311918 q Const_Eval_Geometry_Task(Opaque_Geometry* geometry_) : geometry(geometry_) {}
3331918 q virtual Opaque_Geometry* eval() const { return geometry ? geometry->clone() : 0; }
335private:
336 Owner< Opaque_Geometry > geometry;
337};
340struct Evaluator : public Statement
34211928225 q Evaluator(int line_number) : Statement(line_number) {}
344 virtual Requested_Context request_context() const = 0;
346 virtual Eval_Task* get_string_task(Prepare_Task_Context& context, const std::string* key) = 0;
3470 virtual Eval_Container_Task* get_container_task(Prepare_Task_Context& context, const std::string* key) { return 0; }
3480 virtual Eval_Geometry_Task* get_geometry_task(Prepare_Task_Context& context) { return 0; }
349 virtual Statement::Eval_Return_Type return_type() const = 0;
3513434 q virtual std::string dump_pretty_ql(const std::string& indent) const { return dump_compact_ql(indent); }
3521566151 q virtual int get_operator_priority() const { return std::numeric_limits< int >::max(); }
353};
356template< typename Evaluator_ >
357struct Element_Function_Maker : public Statement::Evaluator_Maker
359 virtual Statement* create_evaluator(const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context,
360 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output)
3612000172 q {
3622000172 q if (!tree_it.assert_is_function(error_output) || !tree_it.assert_has_input_set(error_output, false)
3632000172 q || !tree_it.assert_has_arguments(error_output, false)
3642000172 q || !assert_element_in_context(error_output, tree_it, tree_context))
36541 q return 0;
3671996172 q return new Evaluator_(tree_it->line_col.first, std::map< std::string, std::string >(), global_settings);
3682000172 q }
36935984692 q Element_Function_Maker() { Statement::maker_by_func_name()[Evaluator_::stmt_func_name()].push_back(this); }
370};
373template< typename Evaluator_ >
374struct Member_Function_Maker : public Statement::Evaluator_Maker
376 virtual Statement* create_evaluator(const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context,
377 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output)
37811220 q {
37911220 q if (!tree_it.assert_is_function(error_output) || !tree_it.assert_has_input_set(error_output, false)
38011220 q || !tree_it.assert_has_arguments(error_output, false)
38111220 q || !assert_member_in_context(error_output, tree_it, tree_context))
3820 return 0;
38411220 q return new Evaluator_(tree_it->line_col.first, std::map< std::string, std::string >(), global_settings);
38511220 q }
38613840692 q Member_Function_Maker() { Statement::maker_by_func_name()[Evaluator_::stmt_func_name()].push_back(this); }
387};
390template< typename Evaluator_ >
391struct Operator_Stmt_Maker : public Generic_Statement_Maker< Evaluator_ >
39341520692 q Operator_Stmt_Maker() : Generic_Statement_Maker< Evaluator_ >(Evaluator_::stmt_name()) {}
394};
397template< typename Evaluator_ >
398struct Operator_Eval_Maker : public Statement::Evaluator_Maker
400 virtual Statement* create_evaluator(const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context,
401 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output)
4022136156 q {
4032136156 q if (tree_context != Statement::evaluator_expected && tree_context != Statement::elem_eval_possible
4042136156 q && tree_context != Statement::member_eval_possible)
4059624 q return 0;
4062040153 q if (!Evaluator_::applicable_by_subtree_structure(tree_it))
40718838 q return 0;
4091852152 q std::map< std::string, std::string > attributes;
4101852152 q Statement* result = new Evaluator_(tree_it->line_col.first, attributes, global_settings);
4111852152 q Evaluator_::add_substatements(result, Evaluator_::stmt_operator(), tree_it, tree_context,
4121852152 q stmt_factory, error_output);
4131852152 q return result;
4142040153 q }
416 Operator_Eval_Maker()
41741520692 q {
41841520692 q Statement::maker_by_token()[Evaluator_::stmt_operator()].push_back(this);
41941520692 q }
420};
423#endif