summaryrefslogtreecommitdiffstats
path: root/src/uscxml/transform/ChartToFSM.h
blob: 923304c778220cd02763948ee0e64fe1fe75e942 (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
/**
 *  @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/InterpreterDraft6.h"
#include <DOM/Document.hpp>
#include <DOM/Node.hpp>
#include <XPath/XPath.hpp>
#include <ostream>
#include <list>

namespace uscxml {
class GlobalState;
class GlobalTransition;
class FlatteningInterpreter;
	
class USCXML_API GlobalState {
public:

	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);

	Arabica::XPath::NodeSet<std::string> activeStates;
	Arabica::XPath::NodeSet<std::string> alreadyEnteredStates;
	std::map<std::string, Arabica::XPath::NodeSet<std::string> > historyStates;

	std::map<std::string, GlobalTransition*> incoming;
	std::map<std::string, GlobalTransition*> outgoing;
	std::string stateId;

	static int gIndex;
	
	std::string index;
	bool isFinal;
};


class USCXML_API GlobalTransition {
public:
	class Action {
	public:
		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, FlatteningInterpreter* flattener);

	bool isValid; // constructor will determine, calling code will delete if not
	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

	std::vector<long> firstElemPerLevel;
	std::vector<long> nrElemPerLevel;
	std::vector<long> prioPerLevel;

	Arabica::XPath::NodeSet<std::string> transitions; // constituting transitions

	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

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

	Arabica::XPath::NodeSet<std::string> entered;
	Arabica::XPath::NodeSet<std::string> exited;

	Arabica::XPath::NodeSet<std::string> invoke;
	Arabica::XPath::NodeSet<std::string> uninvoke;

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

	std::string index;
	FlatteningInterpreter* interpreter;
	
	bool operator< (const GlobalTransition& other) const;
	
protected:
	std::list<std::string> getCommonEvents(const Arabica::XPath::NodeSet<std::string>& transitions);
};

class USCXML_API FlatteningInterpreter : public InterpreterDraft6, public InterpreterMonitor {
public:
	FlatteningInterpreter(const Arabica::DOM::Document<std::string>& doc);
	virtual ~FlatteningInterpreter();

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

	std::list<Arabica::DOM::Element<std::string> > indexedTransitions;

protected:
	// 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 labelTransitions();
	void weightTransitions();
	void createDocument();

	void indexTransitions(const Arabica::DOM::Element<std::string>& root);
	std::list<GlobalTransition*> sortTransitions(std::list<GlobalTransition*> list);

	void appendGlobalStateNode(GlobalState* globalState);
	Arabica::DOM::Node<std::string> globalTransitionToNode(GlobalTransition* globalTransition);

	GlobalState* _start;
	GlobalTransition* _currGlobalTransition;

	uint64_t _perfProcessed;
	uint64_t _perfTotal;
	uint64_t _lastTimeStamp;

	int maxDepth;
	int maxOrder;

	size_t _lastTransientStateId;
	
	Arabica::DOM::Document<std::string> _flatDoc;
	std::map<std::string, GlobalState*> _globalConf;
};

class USCXML_API ChartToFSM {
public:
	static Interpreter flatten(const Interpreter& other);
	static uint64_t stateMachineComplexity(const Arabica::DOM::Element<std::string>& root);

protected:
	class USCXML_API Complexity {
	public:
		Complexity() : value(0) {}
		Complexity(uint64_t value) : value(value) {}

		Complexity& operator+=(const Complexity& rhs) {
			value += rhs.value;
			history.insert(history.end(), rhs.history.begin(), rhs.history.end());
			return *this;
		}

		Complexity& operator*=(const Complexity& rhs) {
			value *= rhs.value;
			history.insert(history.end(), rhs.history.begin(), rhs.history.end());
			return *this;
		}

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

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

};

}

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