summaryrefslogtreecommitdiffstats
path: root/src/uscxml/interpreter/FastMicroStep.h
blob: 1b743ef6ed2697d86104238f0c4fb0bf1eaa2a2a (plain)
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
/**
 *  @file
 *  @author     2012-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 FASTMICROSTEP_H_065FE1F7
#define FASTMICROSTEP_H_065FE1F7

//#define USCXML_VERBOSE 1

#include "uscxml/Common.h"
#include "uscxml/util/DOM.h" // X

#include <vector>
#include <map>
#include <set>
#include "MicroStepImpl.h"

#include <boost/dynamic_bitset.hpp>

//#undef WITH_CACHE_FILES

#ifdef _WIN32
#define BITSET_BLOCKTYPE size_t
#else
#define BITSET_BLOCKTYPE
#endif

namespace uscxml {

/**
 * @ingroup microstep
 * @ingroup impl
 *
 * MicroStep implementation backed by indexed bit-arrays.
 */
class FastMicroStep : public MicroStepImpl {
public:
	FastMicroStep(MicroStepCallbacks* callbacks);
	virtual ~FastMicroStep();
	virtual std::shared_ptr<MicroStepImpl> create(MicroStepCallbacks* callbacks);

    std::string getName() { return "fast"; }
    
	virtual InterpreterState step(size_t blockMs);
	virtual void reset();
	virtual bool isInState(const std::string& stateId);
	virtual std::list<XERCESC_NS::DOMElement*> getConfiguration();
	void markAsCancelled();

	virtual void deserialize(const Data& encodedState);
	virtual Data serialize();

protected:
    FastMicroStep() {}; // only for factory
    
	class Transition {
	public:
		Transition() : element(NULL), source(0), onTrans(NULL), type(0) {}

		XERCESC_NS::DOMElement* element;
		boost::dynamic_bitset<BITSET_BLOCKTYPE> conflicts;
		boost::dynamic_bitset<BITSET_BLOCKTYPE> exitSet;

		uint32_t source;
		boost::dynamic_bitset<BITSET_BLOCKTYPE> target;

		XERCESC_NS::DOMElement* onTrans;

		std::string event;
		std::string cond;

		unsigned char type;

	};

	class State {
	public:
		State() : element(NULL), parent(0), documentOrder(0), doneData(NULL), type(0) {}

		XERCESC_NS::DOMElement* element;
		boost::dynamic_bitset<BITSET_BLOCKTYPE> completion;
		boost::dynamic_bitset<BITSET_BLOCKTYPE> children;
		boost::dynamic_bitset<BITSET_BLOCKTYPE> ancestors;
		uint32_t parent;
		uint32_t documentOrder;

		std::list<XERCESC_NS::DOMElement*> data;
		std::list<XERCESC_NS::DOMElement*> invoke;
		std::list<XERCESC_NS::DOMElement*> onEntry;
		std::list<XERCESC_NS::DOMElement*> onExit;
		XERCESC_NS::DOMElement* doneData;

		unsigned char type;
	};

	class CachedPredicates {
	public:
		std::map<const XERCESC_NS::DOMElement*, std::list<XERCESC_NS::DOMElement*> > exitSet;
	};

	virtual void init(XERCESC_NS::DOMElement* scxml);

	std::list<XERCESC_NS::DOMElement*> getCompletion(const XERCESC_NS::DOMElement* state);

	unsigned char _flags;
	std::map<std::string, int> _stateIds;

	std::vector<State*> _states;
	std::vector<Transition*> _transitions;
	std::list<XERCESC_NS::DOMElement*> _globalScripts;

	boost::dynamic_bitset<BITSET_BLOCKTYPE> _configuration;
	boost::dynamic_bitset<BITSET_BLOCKTYPE> _invocations;
	boost::dynamic_bitset<BITSET_BLOCKTYPE> _history;
	boost::dynamic_bitset<BITSET_BLOCKTYPE> _initializedData;

	std::set<boost::dynamic_bitset<BITSET_BLOCKTYPE> > _microstepConfigurations;

	Binding _binding;
	XERCESC_NS::DOMElement* _scxml;
	X _xmlPrefix;
	X _xmlNS;

	bool _isInitialized;
	bool _isCancelled;
	Event _event; // we do not care about the event's representation

private:
	std::list<XERCESC_NS::DOMElement*> getHistoryCompletion(const XERCESC_NS::DOMElement* state);
	void resortStates(XERCESC_NS::DOMElement* node, const X& xmlPrefix);

	bool conflictsCached(const XERCESC_NS::DOMElement* t1, const XERCESC_NS::DOMElement* t2, const XERCESC_NS::DOMElement* root); ///< overrides implementation Predicates::conflicts for speed

	std::string toBase64(const boost::dynamic_bitset<BITSET_BLOCKTYPE>& bitset);
	boost::dynamic_bitset<BITSET_BLOCKTYPE> fromBase64(const std::string& encoded);

	std::list<XERCESC_NS::DOMElement*> getExitSetCached(const XERCESC_NS::DOMElement* transition,
	        const XERCESC_NS::DOMElement* root);

	CachedPredicates _cache;

	boost::dynamic_bitset<BITSET_BLOCKTYPE> _exitSet;
	boost::dynamic_bitset<BITSET_BLOCKTYPE> _entrySet;
	boost::dynamic_bitset<BITSET_BLOCKTYPE> _targetSet;
	boost::dynamic_bitset<BITSET_BLOCKTYPE> _tmpStates;

	boost::dynamic_bitset<BITSET_BLOCKTYPE> _conflicts;
	boost::dynamic_bitset<BITSET_BLOCKTYPE> _transSet;

#ifdef USCXML_VERBOSE
	void printStateNames(const boost::dynamic_bitset<BITSET_BLOCKTYPE>& bitset);
#endif

    friend class Factory;
};

}

#endif /* end of include guard: FASTMICROSTEP_H_065FE1F7 */