InterpreterImpl.h
Go to the documentation of this file.
1 
20 #ifndef INTERPRETERIMPL_H_2A79C83D
21 #define INTERPRETERIMPL_H_2A79C83D
22 
23 #include <memory>
24 #include <mutex>
25 #include <list>
26 #include <map>
27 #include <string>
28 #include <limits>
29 
30 #include "uscxml/Common.h"
31 #include "uscxml/util/URL.h"
32 #include "uscxml/plugins/Factory.h"
40 //#include "uscxml/util/DOM.h"
41 
42 namespace uscxml {
43 
44 class InterpreterMonitor;
45 class InterpreterIssue;
46 
51 class USCXML_API InterpreterImpl :
52  public MicroStepCallbacks,
53  public DataModelCallbacks,
54  public IOProcessorCallbacks,
57  public InvokerCallbacks,
58  public std::enable_shared_from_this<InterpreterImpl> {
59 public:
60  enum Binding {
61  EARLY = 0,
62  LATE = 1
63  };
64 
66  virtual ~InterpreterImpl();
67 
68  void cloneFrom(InterpreterImpl* other);
69  void cloneFrom(std::shared_ptr<InterpreterImpl> other);
70 
71  virtual InterpreterState step(size_t blockMs);
72  virtual void reset();
73 
74  virtual void cancel();
75 
76  virtual void deserialize(const std::string& encodedState);
77  virtual std::string serialize();
78 
79  InterpreterState getState() {
80  return _state;
81  }
82 
83  std::list<XERCESC_NS::DOMElement*> getConfiguration() {
84  return _microStepper.getConfiguration();
85  }
86 
87  void addMonitor(InterpreterMonitor* monitor) {
88  _monitors.insert(monitor);
89  }
90 
91  void removeMonitor(InterpreterMonitor* monitor) {
92  _monitors.erase(monitor);
93  }
94 
98  virtual Event dequeueInternal() {
99  _currEvent = _internalQueue.dequeue(0);
100  if (_currEvent)
101  _dataModel.setEvent(_currEvent);
102  return _currEvent;
103  }
104  virtual Event dequeueExternal(size_t blockMs);
105  virtual bool isTrue(const std::string& expr);
106 
107  virtual void raiseDoneEvent(XERCESC_NS::DOMElement* state, XERCESC_NS::DOMElement* doneData) {
108  _execContent.raiseDoneEvent(state, doneData);
109  }
110 
111  virtual void process(XERCESC_NS::DOMElement* block) {
112  _execContent.process(block);
113  }
114 
115  virtual bool isMatched(const Event& event, const std::string& eventDesc);
116  virtual void initData(XERCESC_NS::DOMElement* element);
117 
118  virtual void invoke(XERCESC_NS::DOMElement* invoke) {
119  _execContent.invoke(invoke);
120  }
121 
122  virtual void uninvoke(XERCESC_NS::DOMElement* invoke) {
123  _execContent.uninvoke(invoke);
124  }
125 
126  virtual std::set<InterpreterMonitor*> getMonitors() {
127  return _monitors;
128  }
129 
130  virtual Interpreter getInterpreter() {
131  return Interpreter(shared_from_this());
132  }
133 
134  virtual Data& getCache() {
135  return _cache;
136  }
137 
141  virtual const std::string& getName() {
142  return _name;
143  }
144  virtual const std::string& getSessionId() {
145  return _sessionId;
146  }
147  virtual const std::map<std::string, IOProcessor>& getIOProcessors() {
148  return _ioProcs;
149  }
150  virtual const std::map<std::string, Invoker>& getInvokers() {
151  return _invokers;
152  }
153 
154  virtual bool isInState(const std::string& stateId) {
155  return _microStepper.isInState(stateId);
156  }
157  virtual XERCESC_NS::DOMDocument* getDocument() const {
158  return _document;
159  }
160 
165  virtual void enqueueInternal(const Event& event) {
166  return _internalQueue.enqueue(event);
167  }
168  virtual void enqueueExternal(const Event& event) {
169  return _externalQueue.enqueue(event);
170  }
171  virtual void enqueueExternalDelayed(const Event& event, size_t delayMs, const std::string& eventUUID) {
172  return _delayQueue.enqueueDelayed(event, delayMs, eventUUID);
173  }
174  virtual void cancelDelayed(const std::string& eventId);
175 
176  virtual size_t getLength(const std::string& expr) {
177  return _dataModel.getLength(expr);
178  }
179 
180  virtual void setForeach(const std::string& item,
181  const std::string& array,
182  const std::string& index,
183  uint32_t iteration) {
184  return _dataModel.setForeach(item, array, index, iteration);
185  }
186  virtual Data evalAsData(const std::string& expr) {
187  return _dataModel.evalAsData(expr);
188  }
189 
190  virtual Data getAsData(const std::string& expr) {
191  return _dataModel.getAsData(expr);
192  }
193 
194  virtual void assign(const std::string& location, const Data& data, const std::map<std::string, std::string>& attrs);
195 
196  virtual std::string getInvokeId() {
197  return _invokeId;
198  }
199  virtual std::string getBaseURL() {
200  return _baseURL;
201  }
202 
203  virtual bool checkValidSendType(const std::string& type, const std::string& target);
204  virtual void invoke(const std::string& type, const std::string& src, bool autoForward, XERCESC_NS::DOMElement* finalize, const Event& invokeEvent);
205  virtual void uninvoke(const std::string& invokeId);
206  virtual void enqueue(const std::string& type, const std::string& target, size_t delayMs, const Event& sendEvent);
207 
208  virtual const Event& getCurrentEvent() {
209  return _currEvent;
210  }
211 
215  virtual void enqueueAtInvoker(const std::string& invokeId, const Event& event);
216  virtual void enqueueAtParent(const Event& event);
217 
222  virtual void eventReady(Event& event, const std::string& eventUUID);
223 
227  if (al.logger) // we intialized _logger as the default logger already
228  _logger = al.logger;
229  _execContent = al.execContent;
230  _microStepper = al.microStepper;
231  _dataModel = al.dataModel;
232  _internalQueue = al.internalQueue;
233  _externalQueue = al.externalQueue;
234  _delayQueue = al.delayQueue;
235  }
236 
237  ActionLanguage* getActionLanguage() {
238  _al.logger = _logger;
239  _al.execContent = _execContent;
240  _al.microStepper = _microStepper;
241  _al.dataModel = _dataModel;
242  _al.internalQueue = _internalQueue;
243  _al.externalQueue = _externalQueue;
244  _al.delayQueue = _delayQueue;
245  return &_al;
246  }
247 
248  void setFactory(Factory* factory) {
249  _factory = factory;
250  }
251 
252  virtual Logger getLogger() {
253  return _logger;
254  }
255 
256  static std::map<std::string, std::weak_ptr<InterpreterImpl> > getInstances();
257 
258  virtual XERCESC_NS::DOMDocument* getDocument() {
259  return _document;
260  }
261 
262 protected:
263  static void addInstance(std::shared_ptr<InterpreterImpl> instance);
264 
265  Binding _binding;
266  ActionLanguage _al;
267 
268  std::string _sessionId;
269  std::string _name;
270  std::string _invokeId; // TODO: Never set!
271 
272  bool _isInitialized;
273  XERCESC_NS::DOMDocument* _document;
274  XERCESC_NS::DOMElement* _scxml;
275 
276  std::map<std::string, std::tuple<std::string, std::string, std::string> > _delayedEventTargets;
277 
278  virtual void init();
279 
280  static std::map<std::string, std::weak_ptr<InterpreterImpl> > _instances;
281  static std::recursive_mutex _instanceMutex;
282  std::recursive_mutex _delayMutex;
283  std::recursive_mutex _serializationMutex;
284 
285  friend class Interpreter;
286  friend class InterpreterIssue;
287  friend class TransformerImpl;
288  friend class USCXMLInvoker;
289  friend class SCXMLIOProcessor;
290  friend class DebugSession;
291  friend class Debugger;
292 
293  std::string _xmlPrefix;
294  std::string _xmlNS;
295  Factory* _factory;
296 
297  URL _baseURL;
298  std::string _md5;
299 
300  MicroStep _microStepper;
301  DataModel _dataModel;
302  ContentExecutor _execContent;
303  Logger _logger = Logger::getDefault();
304 
305  InterpreterState _state;
306 
307  EventQueue _internalQueue;
308  EventQueue _externalQueue;
309  EventQueue _parentQueue;
310  DelayedEventQueue _delayQueue;
311 
312  Event _currEvent;
313  Event _invokeReq;
314 
315  std::map<std::string, IOProcessor> _ioProcs;
316  std::map<std::string, Invoker> _invokers;
317  std::map<std::string, XERCESC_NS::DOMElement*> _finalize;
318  std::set<std::string> _autoForwarders;
319  std::set<InterpreterMonitor*> _monitors;
320 
321  Data _cache;
322 
323 private:
324  void setupDOM();
325 };
326 
327 }
328 
329 #endif /* end of include guard: INTERPRETERIMPL_H_2A79C83D */
EventQueue internalQueue
The queue where internal events will be enqueued.
Definition: Interpreter.h:59
Definition: Breakpoint.cpp:26
Definition: InterpreterMonitor.h:57
virtual Event dequeueInternal()
MicrostepCallbacks.
Definition: InterpreterImpl.h:98
Definition: MicroStepImpl.h:40
void setActionLanguage(const ActionLanguage &al)
Definition: InterpreterImpl.h:226
virtual void process(XERCESC_NS::DOMElement *block)
Executable Content.
Definition: InterpreterImpl.h:111
virtual std::set< InterpreterMonitor * > getMonitors()
Monitoring.
Definition: InterpreterImpl.h:126
Central class to interpret and process SCXML documents.
Definition: Interpreter.h:79
Definition: EventQueueImpl.h:53
EventQueue externalQueue
The queue for external events.
Definition: Interpreter.h:60
MicroStep microStepper
The microstepper instance to use.
Definition: Interpreter.h:56
virtual const std::string & getName()
DataModelCallbacks.
Definition: InterpreterImpl.h:141
Collection of instances for interpreter that constitute its action language.
Definition: Interpreter.h:53
DataModel dataModel
The datamodel to use.
Definition: Interpreter.h:57
Definition: InterpreterImpl.h:51
virtual void enqueueInternal(const Event &event)
ContentExecutorCallbacks.
Definition: InterpreterImpl.h:165
ContentExecutor execContent
To process executable content elements.
Definition: Interpreter.h:58
Definition: Event.h:94
virtual Data & getCache()
Cache Data.
Definition: InterpreterImpl.h:134
Callbacks available for every invoker.
Definition: InvokerImpl.h:43
Logger logger
The logger instance to use for messages.
Definition: Interpreter.h:55
Callbacks available for every data-model.
Definition: DataModelImpl.h:47
Definition: ContentExecutorImpl.h:46
DelayedEventQueue delayQueue
The queue for delayed events.
Definition: Interpreter.h:61
virtual void invoke(XERCESC_NS::DOMElement *invoke)
Invocations.
Definition: InterpreterImpl.h:118
Callbacks available for every IO processor.
Definition: IOProcessorImpl.h:36
Definition: Data.h:44