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 modify6 * it under the terms of the GNU Affero General Public License as7 * published by the Free Software Foundation, either version 3 of the8 * 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 of12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13 * GNU General Public License for more details.14 *15 * You should have received a copy of the GNU Affero General Public License16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.17 */19#ifndef DE__OSM3S___OVERPASS_API__STATEMENTS__EVALUATOR_H20#define DE__OSM3S___OVERPASS_API__STATEMENTS__EVALUATOR_H23#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_Context38{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_Usage55{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;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_Context80{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_Context100{101private:102 Set_With_Context(const Set_With_Context&);103 Set_With_Context& operator=(const Set_With_Context&);105public:118 ~Set_With_Context()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_Context188{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;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_Task227{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) const234 virtual std::string eval(const Element_With_Context< Attic< Node_Skeleton > >& data, const std::string* key) const236 virtual std::string eval(const Element_With_Context< Way_Skeleton >& data, const std::string* key) const238 virtual std::string eval(const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const240 virtual std::string eval(const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const242 virtual std::string eval(const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const244 virtual std::string eval(const Element_With_Context< Area_Skeleton >& data, const std::string* key) const246 virtual std::string eval(const Element_With_Context< Derived_Skeleton >& data, const std::string* key) const249 virtual std::string eval(uint pos, const Element_With_Context< Way_Skeleton >& data, const std::string* key) const251 virtual std::string eval(uint pos, const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const253 virtual std::string eval(uint pos, const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const255 virtual std::string eval(uint pos, const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const257};260struct Const_Eval_Task : public Eval_Task261{266private:267 std::string value;268};271struct Eval_Container_Task272{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) const280 virtual std::vector< std::string > eval(281 const Element_With_Context< Attic< Node_Skeleton > >& data, const std::string* key) const283 virtual std::vector< std::string > eval(284 const Element_With_Context< Way_Skeleton >& data, const std::string* key) const286 virtual std::vector< std::string > eval(287 const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const289 virtual std::vector< std::string > eval(290 const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const292 virtual std::vector< std::string > eval(293 const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const295 virtual std::vector< std::string > eval(296 const Element_With_Context< Area_Skeleton >& data, const std::string* key) const298 virtual std::vector< std::string > eval(299 const Element_With_Context< Derived_Skeleton >& data, const std::string* key) const301};304struct Eval_Geometry_Task305{308 virtual Opaque_Geometry* eval() const = 0;310 virtual Opaque_Geometry* eval(const Element_With_Context< Node_Skeleton >& data) const312 virtual Opaque_Geometry* eval(const Element_With_Context< Attic< Node_Skeleton > >& data) const314 virtual Opaque_Geometry* eval(const Element_With_Context< Way_Skeleton >& data) const316 virtual Opaque_Geometry* eval(const Element_With_Context< Attic< Way_Skeleton > >& data) const318 virtual Opaque_Geometry* eval(const Element_With_Context< Relation_Skeleton >& data) const320 virtual Opaque_Geometry* eval(const Element_With_Context< Attic< Relation_Skeleton > >& data) const322 virtual Opaque_Geometry* eval(const Element_With_Context< Area_Skeleton >& data) const324 virtual Opaque_Geometry* eval(const Element_With_Context< Derived_Skeleton >& data) const326};329struct Const_Eval_Geometry_Task : public Eval_Geometry_Task330{335private:336 Owner< Opaque_Geometry > geometry;337};340struct Evaluator : public Statement341{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; }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_Maker358{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)3622000172 q if (!tree_it.assert_is_function(error_output) || !tree_it.assert_has_input_set(error_output, false)3671996172 q return new Evaluator_(tree_it->line_col.first, std::map< std::string, std::string >(), global_settings);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_Maker375{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)37911220 q if (!tree_it.assert_is_function(error_output) || !tree_it.assert_has_input_set(error_output, false)38411220 q return new Evaluator_(tree_it->line_col.first, std::map< std::string, std::string >(), global_settings);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_ >392{39341520692 q Operator_Stmt_Maker() : Generic_Statement_Maker< Evaluator_ >(Evaluator_::stmt_name()) {}394};397template< typename Evaluator_ >398struct Operator_Eval_Maker : public Statement::Evaluator_Maker399{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)4032136156 q if (tree_context != Statement::evaluator_expected && tree_context != Statement::elem_eval_possible4101852152 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,416 Operator_Eval_Maker()420};423#endif