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
|
/**
* @file
* @author 2016 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
* @copyright Simplified BSD
*
* @cond
* This program is free software: you can redistribute it and/or modify
* it under the terms of the FreeBSD license as published by the FreeBSD
* project.
*
* 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.
*
* You should have received a copy of the FreeBSD license along with this
* program. If not, see <http://www.opensource.org/licenses/bsd-license>.
* @endcond
*/
#ifndef INTERPRETERIMPL_H_2A79C83D
#define INTERPRETERIMPL_H_2A79C83D
#include <memory>
#include <mutex>
#include <list>
#include <map>
#include <string>
#include "uscxml/Common.h"
#include "uscxml/util/URL.h"
#include "uscxml/plugins/Factory.h"
#include "uscxml/plugins/DataModelImpl.h"
#include "uscxml/interpreter/MicroStepImpl.h"
#include "uscxml/interpreter/ContentExecutorImpl.h"
#include "uscxml/interpreter/EventQueue.h"
#include "uscxml/interpreter/EventQueueImpl.h"
#include "uscxml/util/DOM.h"
#include <xercesc/dom/DOM.hpp>
namespace uscxml {
class InterpreterMonitor;
class InterpreterIssue;
/**
* @ingroup interpreter
* @ingroup impl
*/
class USCXML_API InterpreterImpl :
public MicroStepCallbacks,
public DataModelCallbacks,
public ContentExecutorCallbacks,
public DelayedEventQueueCallbacks,
public std::enable_shared_from_this<InterpreterImpl> {
public:
enum Binding {
EARLY = 0,
LATE = 1
};
InterpreterImpl();
virtual ~InterpreterImpl();
void cloneFrom(InterpreterImpl* other);
void cloneFrom(std::shared_ptr<InterpreterImpl> other);
virtual InterpreterState step(size_t blockMs) {
if (!_isInitialized) {
init();
_state = USCXML_INITIALIZED;
} else {
_state = _microStepper.step(blockMs);
}
return _state;
}
virtual void reset() {///< Reset state machine
if (_microStepper)
_microStepper.reset();
_isInitialized = false;
_state = USCXML_INSTANTIATED;
// _dataModel.reset();
if (_delayQueue)
_delayQueue.reset();
// _contentExecutor.reset();
}
virtual void cancel(); ///< Cancel and finalize state machine
InterpreterState getState() {
return _state;
}
std::list<XERCESC_NS::DOMElement*> getConfiguration() {
return _microStepper.getConfiguration();
}
void addMonitor(InterpreterMonitor* monitor) {
_monitors.insert(monitor);
}
void removeMonitor(InterpreterMonitor* monitor) {
_monitors.erase(monitor);
}
/**
MicrostepCallbacks
*/
virtual Event dequeueInternal() {
_currEvent = _internalQueue.dequeue(false);
if (_currEvent)
_dataModel.setEvent(_currEvent);
return _currEvent;
}
virtual Event dequeueExternal(size_t blockMs);
virtual bool isTrue(const std::string& expr);
virtual void raiseDoneEvent(XERCESC_NS::DOMElement* state, XERCESC_NS::DOMElement* doneData) {
_execContent.raiseDoneEvent(state, doneData);
}
virtual void process(XERCESC_NS::DOMElement* block) {
_execContent.process(block, _xmlPrefix);
}
virtual bool isMatched(const Event& event, const std::string& eventDesc);
virtual void initData(XERCESC_NS::DOMElement* element);
virtual void invoke(XERCESC_NS::DOMElement* invoke) {
_execContent.invoke(invoke);
}
virtual void uninvoke(XERCESC_NS::DOMElement* invoke) {
_execContent.uninvoke(invoke);
}
virtual std::set<InterpreterMonitor*> getMonitors() {
return _monitors;
}
virtual Interpreter getInterpreter() {
return Interpreter(shared_from_this());
}
/**
DataModelCallbacks
*/
virtual const std::string& getName() {
return _name;
}
virtual const std::string& getSessionId() {
return _sessionId;
}
virtual const std::map<std::string, IOProcessor>& getIOProcessors() {
return _ioProcs;
}
virtual const std::map<std::string, Invoker>& getInvokers() {
return _invokers;
}
virtual bool isInState(const std::string& stateId) {
return _microStepper.isInState(stateId);
}
virtual XERCESC_NS::DOMDocument* getDocument() const {
return _document;
}
/**
ContentExecutorCallbacks
*/
virtual void enqueueInternal(const Event& event) {
return _internalQueue.enqueue(event);
}
virtual void enqueueExternal(const Event& event) {
return _externalQueue.enqueue(event);
}
virtual void enqueueExternalDelayed(const Event& event, size_t delayMs, const std::string& eventUUID) {
return _delayQueue.enqueueDelayed(event, delayMs, eventUUID);
}
virtual void cancelDelayed(const std::string& eventId);
virtual size_t getLength(const std::string& expr) {
return _dataModel.getLength(expr);
}
virtual void setForeach(const std::string& item,
const std::string& array,
const std::string& index,
uint32_t iteration) {
return _dataModel.setForeach(item, array, index, iteration);
}
virtual Data evalAsData(const std::string& expr) {
return _dataModel.evalAsData(expr);
}
virtual Data getAsData(const std::string& expr) {
return _dataModel.getAsData(expr);
}
virtual void assign(const std::string& location, const Data& data);
virtual std::string getInvokeId() {
return _invokeId;
}
virtual std::string getBaseURL() {
return _baseURL;
}
virtual bool checkValidSendType(const std::string& type, const std::string& target);
virtual void invoke(const std::string& type, const std::string& src, bool autoForward, XERCESC_NS::DOMElement* finalize, const Event& invokeEvent);
virtual void uninvoke(const std::string& invokeId);
virtual void enqueue(const std::string& type, const std::string& target, size_t delayMs, const Event& sendEvent);
virtual const Event& getCurrentEvent() {
return _currEvent;
}
/**
DelayedEventQueueCallbacks
*/
virtual void eventReady(Event& event, const std::string& eventUUID);
/** --- */
void setActionLanguage(const ActionLanguage& al) {
_execContent = al.execContent;
_microStepper = al.microStepper;
_dataModel = al.dataModel;
_internalQueue = al.internalQueue;
_externalQueue = al.externalQueue;
_delayQueue = al.delayedQueue;
}
void setFactory(Factory* factory) {
_factory = factory;
}
static std::map<std::string, std::weak_ptr<InterpreterImpl> > getInstances();
virtual XERCESC_NS::DOMDocument* getDocument() {
return _document;
}
protected:
static void addInstance(std::shared_ptr<InterpreterImpl> instance);
Binding _binding;
std::string _sessionId;
std::string _name;
std::string _invokeId; // TODO: Never set!
bool _isInitialized;
XERCESC_NS::DOMDocument* _document;
XERCESC_NS::DOMElement* _scxml;
std::map<std::string, std::tuple<std::string, std::string, std::string> > _delayedEventTargets;
virtual void init();
static std::map<std::string, std::weak_ptr<InterpreterImpl> > _instances;
static std::recursive_mutex _instanceMutex;
std::recursive_mutex _delayMutex;
friend class Interpreter;
friend class InterpreterIssue;
friend class TransformerImpl;
friend class USCXMLInvoker;
friend class SCXMLIOProcessor;
friend class DebugSession;
friend class Debugger;
X _xmlPrefix;
X _xmlNS;
Factory* _factory;
URL _baseURL;
MicroStep _microStepper;
DataModel _dataModel;
ContentExecutor _execContent;
InterpreterState _state;
EventQueue _internalQueue;
EventQueue _externalQueue;
EventQueue _parentQueue;
DelayedEventQueue _delayQueue;
Event _currEvent;
Event _invokeReq;
std::map<std::string, IOProcessor> _ioProcs;
std::map<std::string, Invoker> _invokers;
std::set<std::string> _autoForwarders;
std::set<InterpreterMonitor*> _monitors;
private:
void setupDOM();
};
}
#endif /* end of include guard: INTERPRETERIMPL_H_2A79C83D */
|