summaryrefslogtreecommitdiffstats
path: root/src/uscxml/transform/ChartToFSM.h.new
blob: 2ec2f4442c7b4d439c4fc130c5baa8aa44ca591c (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
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
/**
 *  @file
 *  @author     2012-2014 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 CHARTTOFSM_H_IOKPYEBY
#define CHARTTOFSM_H_IOKPYEBY

#include "uscxml/DOMUtils.h"
#include "uscxml/interpreter/InterpreterRC.h"
#include <DOM/Document.hpp>
#include <DOM/Node.hpp>
#include <XPath/XPath.hpp>
#include <ostream>
#include <list>

namespace uscxml {
class GlobalState;
class GlobalTransition;
class ChartToFSM;

class USCXML_API Complexity {
public:
	
	enum Variant {
		IGNORE_NOTHING,
		IGNORE_HISTORY,
		IGNORE_NESTED_DATA,
		IGNORE_HISTORY_AND_NESTED_DATA,
	};
	
	Complexity() : value(0), nestedData(0) {}
	Complexity(uint64_t value) : value(value), nestedData(0) {}
	
	Complexity& operator+=(const Complexity& rhs) {
		value += rhs.value;
		nestedData += rhs.nestedData;
		history.insert(history.end(), rhs.history.begin(), rhs.history.end());
		return *this;
	}
	
	Complexity& operator*=(const Complexity& rhs) {
		value *= rhs.value;
		nestedData += rhs.nestedData;
		history.insert(history.end(), rhs.history.begin(), rhs.history.end());
		return *this;
	}
	
	static uint64_t stateMachineComplexity(const Arabica::DOM::Element<std::string>& root, Complexity::Variant variant = IGNORE_NOTHING);

protected:
	static Complexity calculateStateMachineComplexity(const Arabica::DOM::Element<std::string>& root);

	uint64_t value;
	uint64_t nestedData;
	std::list<uint64_t> history;
};

class USCXML_API GlobalState {
public:

	GlobalState() {}
	GlobalState(const Arabica::DOM::Node<std::string>& globalState);
	GlobalState(const Arabica::XPath::NodeSet<std::string>& activeStates,
	            const Arabica::XPath::NodeSet<std::string>& alreadyEnteredStates, // we need to remember for binding=late
	            const std::map<std::string, Arabica::XPath::NodeSet<std::string> >& historyStates,
	            const std::string& xmlNSPrefix,
							ChartToFSM* flattener);

	std::set<int> activeStatesRefs;
	std::set<int> alreadyEnteredStatesRefs;
	std::map<std::string, std::set<int> > historyStatesRefs;
	
	std::list<GlobalTransition*> sortedOutgoing;
	std::string stateId;
	std::string activeId;

	unsigned long activeIndex;
	unsigned long index;
	bool isFinal;
	
	ChartToFSM* interpreter;
	
	Arabica::XPath::NodeSet<std::string> getActiveStates();
	Arabica::XPath::NodeSet<std::string> getAlreadyEnteredStates();
	std::map<std::string, Arabica::XPath::NodeSet<std::string> > getHistoryStates();

};


class USCXML_API GlobalTransition {
public:
	enum InvalidReason {
		MIXES_EVENT_SPONTANEOUS,
		NO_COMMON_EVENT,
		CHILD_ENABLED,
		SAME_SOURCE_STATE,
		UNCONDITIONAL_SUPERSET,
		UNCONDITIONAL_MATCH,
		PREEMPTING_MEMBERS
	};
	
	class Action {
	public:
		bool operator<(const Action& other) const {
			if (onEntry < other.onEntry)
				return onEntry < other.onEntry;
			if (onExit < other.onExit)
				return onExit < other.onExit;
			if (transition < other.transition)
				return transition < other.transition;
			if (entered < other.entered)
				return entered < other.entered;
			if (exited < other.exited)
				return exited < other.exited;
			if (invoke < other.invoke)
				return invoke < other.invoke;
			if (uninvoke < other.uninvoke)
				return uninvoke < other.uninvoke;
			return false;
		}

		bool operator==(const Action& other) const {
			return !(other < *this) && !(*this < other);
		}
		bool operator!=(const Action& other) const {
			return !operator==(other);
		}
		
		typedef std::list<GlobalTransition::Action>::iterator iter_t;
		
		Arabica::DOM::Element<std::string> onEntry;
		Arabica::DOM::Element<std::string> onExit;
		Arabica::DOM::Element<std::string> transition;
		Arabica::DOM::Element<std::string> entered;
		Arabica::DOM::Element<std::string> exited;
		Arabica::DOM::Element<std::string> invoke;
		Arabica::DOM::Element<std::string> uninvoke;
		
	};

	GlobalTransition(const Arabica::XPath::NodeSet<std::string>& transitions, DataModel dataModel, ChartToFSM* flattener);
	static GlobalTransition* copyWithoutExecContent(GlobalTransition* other);
	
	bool isValid; // constructor will determine, calling code will delete if not
	std::string invalidMsg;
	InvalidReason invalidReason;
	
	bool isEventless; // whether or not all our transitions are eventless
	bool isTargetless; // whether or not all our transitions are eventless
	bool isSubset; // there is a superset to this set
	bool hasExecutableContent;
	
	uint32_t eventsRaised; // internal events this transition will raise
	uint32_t eventsSent; // external events this transition will send
	uint32_t eventsChainRaised; // maximum number of internal events raised when taking this transition in a chain
	uint32_t eventsChainSent; // maximum number of external events raised when taking this transition in a chain
	
	std::set<int> startTransitionRefs; // indices of eventful transitions that might trigger this transition
	
	std::set<int> transitionRefs; // indizes of constituting transitions
	Arabica::XPath::NodeSet<std::string> getTransitions() const;
	
	std::list<std::string> eventNames; // the list of longest event names that will enable this set
	std::string eventDesc; // space-seperated eventnames for convenience
	std::string condition; // conjunction of all the set's conditions
	std::string members; // a convenience string listing all constituting transitions

	// executable content we gathered when we took the transition
	std::list<Action> actions;

	std::string transitionId;
	std::string source;
	std::string destination;
	std::string activeDestination;

	GlobalTransition* historyBase; // we have a base transition that left our source with no history (-> we are a history transition)
	std::list<GlobalTransition*> historyTrans; // transitions from the same source but different histories
	std::set<std::string> histTargets; // constituting targets to history states

	long index;
	ChartToFSM* interpreter;

	bool operator< (const GlobalTransition& other) const;

protected:
	std::list<std::string> getCommonEvents(const Arabica::XPath::NodeSet<std::string>& transitions);
};


class USCXML_API ChartToFSM : public InterpreterRC, public InterpreterMonitor {
public:
	ChartToFSM(const Interpreter& other);
	virtual ~ChartToFSM();

	void indexTransitions();
	Arabica::DOM::Document<std::string> getDocument() const; // overwrite to return flat FSM

protected:

	InterpreterState interpret();
	
	GlobalState* _start;
	Arabica::DOM::Document<std::string> _flatDoc;
	std::map<std::string, GlobalState*> _globalConf;
	std::map<std::string, GlobalState*> _activeConf; // potentially enabled transition sets per active configuration
	std::map<std::string, Arabica::DOM::Element<std::string> > _historyTargets; // ids of all history states

	uint32_t getMinInternalQueueLength(uint32_t defaultVal);
	uint32_t getMinExternalQueueLength(uint32_t defaultVal);

	bool _keepInvalidTransitions;
	
private:
	Arabica::XPath::NodeSet<std::string> refsToStates(const std::set<int>&);
	Arabica::XPath::NodeSet<std::string> refsToTransitions(const std::set<int>&);
	
	std::vector<Arabica::DOM::Element<std::string> > indexedTransitions;
	std::vector<Arabica::DOM::Element<std::string> > indexedStates;

	// gather executable content per microstep
	void executeContent(const Arabica::DOM::Element<std::string>& content, bool rethrow = false);

	// invoke and uninvoke
	virtual void invoke(const Arabica::DOM::Element<std::string>& element);
	virtual void cancelInvoke(const Arabica::DOM::Element<std::string>& element);

	// override to do nothing
	void send(const Arabica::DOM::Element<std::string>& element) {}
	void internalDoneSend(const Arabica::DOM::Element<std::string>& state);

	// InterpreterMonitor
	virtual void beforeMicroStep(Interpreter interpreter);
	virtual void onStableConfiguration(Interpreter interpreter);
	virtual void beforeExitingState(Interpreter interpreter, const Arabica::DOM::Element<std::string>& state, bool moreComing);
	virtual void beforeEnteringState(Interpreter interpreter, const Arabica::DOM::Element<std::string>& state, bool moreComing);
	virtual void beforeTakingTransition(Interpreter interpreter, const Arabica::DOM::Element<std::string>& transition, bool moreComing);

	void explode();
	void getPotentialTransitionsForConf(const Arabica::XPath::NodeSet<std::string>& conf, std::map<std::string, GlobalTransition*>& outMap);
//	void labelTransitions();

	void indexTransitions(const Arabica::DOM::Element<std::string>& root);
	void annotateRaiseAndSend(const Arabica::DOM::Element<std::string>& root);
	bool hasForeachInBetween(const Arabica::DOM::Node<std::string>& ancestor, const Arabica::DOM::Node<std::string>& child);
	void updateRaisedAndSendChains(GlobalState* state, GlobalTransition* source, std::set<GlobalTransition*> visited);

	void reassembleFromFlat();
	
	std::list<GlobalTransition*> sortTransitions(std::list<GlobalTransition*> list);

	// we need this static as we use it in a sort function
	static std::map<Arabica::DOM::Node<std::string>, Arabica::DOM::Node<std::string> > _transParents;
	
	static bool filterSameState(const Arabica::XPath::NodeSet<std::string>& transitions);

	uint64_t _perfTransProcessed;
	uint64_t _perfTransTotal;
	uint64_t _perfTransUsed;
	uint64_t _perfStatesProcessed;
	uint64_t _perfStatesSkippedProcessed;
	uint64_t _perfStatesSkippedTotal;
	uint64_t _perfStatesCachedProcessed;
	uint64_t _perfStatesCachedTotal;
	uint64_t _perfMicroStepProcessed;
	uint64_t _perfMicroStepTotal;
	uint64_t _lastTimeStamp;

	size_t _lastTransientStateId;
	size_t _lastStateIndex;
	size_t _lastActiveIndex;
	size_t _lastTransIndex;

	bool _alreadyFlat;

	bool _skipEventChainCalculations;
	size_t _maxEventSentChain;
	size_t _maxEventRaisedChain;
	size_t _doneEventRaiseTolerance;
	
	GlobalTransition* _currGlobalTransition;
	
	friend class GlobalTransition;
	friend class GlobalState;
};

}

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