summaryrefslogtreecommitdiffstats
path: root/src/uscxml/transform/ChartToFSM.h
blob: 07531e2e456e107b0f3ed1a83bd49cd247fd683c (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
/**
 *  @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>


namespace uscxml {
class GlobalState;
class GlobalTransition;

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

	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;

	bool isFinal;
};


class USCXML_API GlobalTransition {
public:
	class Action {
	public:
		Arabica::DOM::Node<std::string> onEntry;
		Arabica::DOM::Node<std::string> onExit;
		Arabica::DOM::Node<std::string> transition;
		Arabica::DOM::Node<std::string> entered;
		Arabica::DOM::Node<std::string> exited;
		Arabica::DOM::Node<std::string> invoke;
		Arabica::DOM::Node<std::string> uninvoke;
	};

	GlobalTransition(const Arabica::XPath::NodeSet<std::string>& transitions, DataModel dataModel);

	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;

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

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

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

	// override to do nothing
	void send(const Arabica::DOM::Node<std::string>& element) {}
	void internalDoneSend(const Arabica::DOM::Node<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();

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

	GlobalState* _start;
	GlobalTransition* _currGlobalTransition;

	int maxDepth;
	int maxOrder;

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

class USCXML_API ChartToFSM {
public:
	static Interpreter flatten(const Interpreter& other);
};

}

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