-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
engine.hpp
175 lines (121 loc) · 5.08 KB
/
engine.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Copyright (C) 2009 - 2016 by Yurii Chernyi <terraninfo@terraninfo.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* AI Support engine - creating specific ai components from config
* @file
*/
#ifndef AI_COMPOSITE_ENGINE_HPP_INCLUDED
#define AI_COMPOSITE_ENGINE_HPP_INCLUDED
#include "component.hpp"
#include "ai/contexts.hpp"
#include <algorithm>
#include <iterator>
//============================================================================
namespace ai {
class rca_context;
class ai_context;
class component;
class engine : public component {
public:
engine( readonly_context &context, const config &cfg );
virtual ~engine();
virtual bool is_ok() const;
static void parse_aspect_from_config( readonly_context &context, const config &cfg, const std::string &id, std::back_insert_iterator<std::vector< aspect_ptr > > b );
static void parse_goal_from_config( readonly_context &context, const config &cfg, std::back_insert_iterator<std::vector< goal_ptr > > b );
static void parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr > > b );
static void parse_engine_from_config( readonly_context &context, const config &cfg, std::back_insert_iterator<std::vector< engine_ptr > > b );
static void parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr > > b );
//do not override that method in subclasses which cannot create aspects
virtual void do_parse_aspect_from_config( const config &cfg, const std::string &id, std::back_insert_iterator< std::vector< aspect_ptr> > b );
//do not override that method in subclasses which cannot create candidate_actions
virtual void do_parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr > > b );
//do not override that method in subclasses which cannot create goals
virtual void do_parse_goal_from_config( const config &cfg, std::back_insert_iterator<std::vector< goal_ptr > > b );
//do not override that method in subclasses which cannot create engines
virtual void do_parse_engine_from_config( const config &cfg, std::back_insert_iterator<std::vector< engine_ptr > > b );
//do not override that method in subclasses which cannot create stages
virtual void do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr > > b );
//do not override that method in subclasses which cannot evaluate formulas
virtual std::string evaluate(const std::string& str);
readonly_context& get_readonly_context();
/**
* set ai context (which is not available during early initialization)
*/
virtual void set_ai_context(ai_context_ptr context);
virtual ai_context_ptr get_ai_context();
/**
* serialize
*/
virtual config to_config() const;
virtual std::string get_id() const
{ return id_; }
virtual std::string get_engine() const
{ return engine_; }
virtual std::string get_name() const
{ return name_; }
protected:
readonly_context &ai_;
ai_context_ptr ai_context_;
/** name of the engine which has created this engine*/
std::string engine_;
std::string id_;
std::string name_;
};
class engine_factory;
class engine_factory{
bool is_duplicate(const std::string &name);
public:
typedef boost::shared_ptr< engine_factory > factory_ptr;
typedef std::map<std::string, factory_ptr> factory_map;
typedef std::pair<const std::string, factory_ptr> factory_map_pair;
static factory_map& get_list() {
static factory_map *engine_factories;
if (engine_factories==NULL) {
engine_factories = new factory_map;
}
return *engine_factories;
}
virtual engine_ptr get_new_instance( readonly_context &ai, const config &cfg ) = 0;
virtual engine_ptr get_new_instance( readonly_context &ai, const std::string& name ) = 0;
engine_factory( const std::string &name )
{
if (is_duplicate(name)) {
return;
}
factory_ptr ptr_to_this(this);
get_list().insert(make_pair(name,ptr_to_this));
}
virtual ~engine_factory() {}
};
template<class ENGINE>
class register_engine_factory : public engine_factory {
public:
register_engine_factory( const std::string &name )
: engine_factory( name )
{
}
virtual engine_ptr get_new_instance( readonly_context &ai, const config &cfg ){
engine_ptr e = engine_ptr(new ENGINE(ai,cfg));
if (!e->is_ok()) {
return engine_ptr();
}
return e;
}
virtual engine_ptr get_new_instance( readonly_context &ai, const std::string& name ){
config cfg;
cfg["name"] = name;
cfg["engine"] = "cpp"; // @Crab: what is the purpose of this line(neph)
return engine_ptr(new ENGINE(ai,cfg));
}
};
} //end of namespace ai
#endif