forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task.mqh
324 lines (288 loc) · 8.41 KB
/
Task.mqh
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2020, 31337 Investments Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
* This file 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 3 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; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
* Provides integration with tasks (manages conditions and actions).
*/
// Prevents processing this includes file for the second time.
#ifndef TASK_MQH
#define TASK_MQH
// Forward class declaration.
class Task;
// Includes.
#include "Action.mqh"
#include "Condition.mqh"
// Enums.
// Defines task entry flags.
enum ENUM_TASK_ENTRY_FLAGS {
TASK_ENTRY_FLAG_NONE = 0,
TASK_ENTRY_FLAG_IS_ACTIVE = 1,
TASK_ENTRY_FLAG_IS_DONE = 2,
TASK_ENTRY_FLAG_IS_EXPIRED = 4,
TASK_ENTRY_FLAG_IS_FAILED = 8,
TASK_ENTRY_FLAG_IS_INVALID = 16
};
// Actions for action class.
enum ENUM_TASK_ACTION {
TASK_ACTION_NONE = 0, // Does nothing.
TASK_ACTION_PROCESS, // Process tasks.
FINAL_TASK_ACTION_ENTRY
};
// Action conditions.
enum ENUM_TASK_CONDITION {
TASK_COND_NONE = 0, // Empty condition.
TASK_COND_IS_ACTIVE, // Is active.
TASK_COND_IS_DONE, // Is done.
TASK_COND_IS_FAILED, // Is failed.
TASK_COND_IS_FINISHED, // Is finished.
TASK_COND_IS_INVALID, // Is invalid.
FINAL_TASK_CONDITION_ENTRY
};
// Structs.
struct TaskEntry {
Action *action; // Action of the task.
Condition *cond; // Condition of the task.
datetime expires; // Time of expiration.
datetime last_process; // Time of the last process.
datetime last_success; // Time of the last success.
unsigned char flags; // Action flags.
// Constructor.
void ActionEntry() {}
void Init() {
flags = TASK_ENTRY_FLAG_NONE;
AddFlags(TASK_ENTRY_FLAG_IS_ACTIVE);
expires = last_process = last_success = 0;
}
// Flag methods.
bool HasFlag(unsigned char _flag) { return bool(flags & _flag); }
void AddFlags(unsigned char _flags) { flags |= _flags; }
void RemoveFlags(unsigned char _flags) { flags &= ~_flags; }
void SetFlag(ENUM_TASK_ENTRY_FLAGS _flag, bool _value) {
if (_value)
AddFlags(_flag);
else
RemoveFlags(_flag);
}
void SetFlags(unsigned char _flags) { flags = _flags; }
// State methods.
bool IsActive() { return HasFlag(ACTION_ENTRY_FLAG_IS_ACTIVE); }
bool IsDone() { return HasFlag(ACTION_ENTRY_FLAG_IS_DONE); }
bool IsFailed() { return HasFlag(ACTION_ENTRY_FLAG_IS_FAILED); }
bool IsValid() { return !HasFlag(ACTION_ENTRY_FLAG_IS_INVALID); }
};
class Task {
protected:
// Class variables.
Ref<Log> logger;
public:
// Class variables.
DictStruct<short, TaskEntry> *tasks;
/* Special methods */
/**
* Class constructor.
*/
Task() { Init(); }
Task(TaskEntry &_entry) {
Init();
tasks.Push(_entry);
}
/**
* Class copy constructor.
*/
Task(Task &_task) {
Init();
tasks = _task.GetTasks();
}
/**
* Class deconstructor.
*/
~Task() {}
Log* Logger() { return logger.Ptr(); }
/**
* Initialize class variables.
*/
void Init() { tasks = new DictStruct<short, TaskEntry>(); }
/* Main methods */
/**
* Process tasks.
*
* @return
* Returns true when tasks has been processed.
*/
bool Process() {
bool _result = false;
for (DictStructIterator<short, TaskEntry> iter = tasks.Begin(); iter.IsValid(); ++iter) {
bool _curr_result = false;
TaskEntry _entry = iter.Value();
if (_entry.IsActive()) {
if (_entry.cond.Test()) {
_entry.action.Execute();
if (_entry.action.IsFinished()) {
_entry.SetFlag(TASK_ENTRY_FLAG_IS_DONE, _entry.action.IsDone());
_entry.SetFlag(TASK_ENTRY_FLAG_IS_FAILED, _entry.action.IsFailed());
_entry.SetFlag(TASK_ENTRY_FLAG_IS_INVALID, _entry.action.IsInvalid());
_entry.RemoveFlags(TASK_ENTRY_FLAG_IS_ACTIVE);
}
}
_entry.last_process = TimeCurrent();
_result = true;
}
}
return _result;
}
/* State methods */
/**
* Check if task is active.
*/
bool IsActive() {
// The whole task is active when at least one task is active.
return GetFlagCount(TASK_ENTRY_FLAG_IS_ACTIVE) > 0;
}
/**
* Check if task is done.
*/
bool IsDone() {
// The whole task is done when all tasks has been executed successfully.
return GetFlagCount(TASK_ENTRY_FLAG_IS_DONE) == tasks.Size();
}
/**
* Check if task is failed.
*/
bool IsFailed() {
// The whole task is failed when at least one task failed.
return GetFlagCount(TASK_ENTRY_FLAG_IS_FAILED) > 0;
}
/**
* Check if task is finished.
*/
bool IsFinished() {
// The whole task is finished when there are no more active tasks.
return GetFlagCount(TASK_ENTRY_FLAG_IS_ACTIVE) == 0;
}
/**
* Check if task is invalid.
*/
bool IsInvalid() {
// The whole task is invalid when at least one task is invalid.
return GetFlagCount(TASK_ENTRY_FLAG_IS_INVALID) > 0;
}
/* Getters */
/**
* Returns tasks.
*/
DictStruct<short, TaskEntry> *GetTasks() { return tasks; }
/**
* Count entry flags.
*/
unsigned int GetFlagCount(ENUM_TASK_ENTRY_FLAGS _flag) {
unsigned int _counter = 0;
for (DictStructIterator<short, TaskEntry> iter = tasks.Begin(); iter.IsValid(); ++iter) {
TaskEntry _entry = iter.Value();
if (_entry.HasFlag(_flag)) {
_counter++;
}
}
return _counter;
}
/* Setters */
/**
* Sets entry flags.
*/
bool SetFlags(ENUM_TASK_ENTRY_FLAGS _flag, bool _value = true) {
unsigned int _counter = 0;
for (DictStructIterator<short, TaskEntry> iter = tasks.Begin(); iter.IsValid(); ++iter) {
TaskEntry _entry = iter.Value();
switch (_value) {
case false:
if (_entry.HasFlag(_flag)) {
_entry.SetFlag(_flag, _value);
_counter++;
}
break;
case true:
if (!_entry.HasFlag(_flag)) {
_entry.SetFlag(_flag, _value);
_counter++;
}
break;
}
}
return _counter > 0;
}
/* Conditions and actions */
/**
* Checks for Task condition.
*
* @param ENUM_TASK_CONDITION _cond
* Task condition.
* @return
* Returns true when the condition is met.
*/
bool Condition(ENUM_TASK_CONDITION _cond, MqlParam &_args[]) {
switch (_cond) {
case TASK_COND_IS_ACTIVE:
// Is active;
return IsActive();
case TASK_COND_IS_DONE:
// Is done.
return IsDone();
case TASK_COND_IS_FAILED:
// Is failed.
return IsFailed();
case TASK_COND_IS_FINISHED:
// Is finished.
return IsFinished();
case TASK_COND_IS_INVALID:
// Is invalid.
return IsInvalid();
default:
Logger().Error(StringFormat("Invalid Task condition: %s!", EnumToString(_cond), __FUNCTION_LINE__));
return false;
}
}
bool Condition(ENUM_TASK_CONDITION _cond) {
MqlParam _args[] = {};
return Task::Condition(_cond, _args);
}
/**
* Execute Task action.
*
* @param ENUM_TASK_ACTION _action
* Task action to execute.
* @return
* Returns true when the action has been executed successfully.
*/
bool ExecuteAction(ENUM_TASK_ACTION _action, MqlParam &_args[]) {
bool _result = true;
switch (_action) {
case TASK_ACTION_PROCESS:
// Process tasks.
return Process();
default:
Logger().Error(StringFormat("Invalid Task action: %s!", EnumToString(_action), __FUNCTION_LINE__));
return false;
}
return _result;
}
bool ExecuteAction(ENUM_TASK_ACTION _action) {
MqlParam _args[] = {};
return Task::ExecuteAction(_action, _args);
}
/* Other methods */
};
#endif // TASK_MQH