summaryrefslogtreecommitdiffstats
path: root/src/uscxml/transform/FlatStateIdentifier.h
blob: 4afd9562831c77725cdcb8dd1db27a7827b18159 (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
/**
 *  @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 FLATSTATEIDENTIFIER_H_E9534AF9
#define FLATSTATEIDENTIFIER_H_E9534AF9

#include "uscxml/Common.h"
#include "uscxml/Convenience.h"
#include "uscxml/DOMUtils.h"

#include <XPath/XPath.hpp>

#include <sstream>
#include <string>
#include <list>
#include <map>

#include <boost/algorithm/string.hpp>

namespace uscxml {

class USCXML_API FlatStateIdentifier {
public:

	operator bool() const {
		return stateId.length() > 0;
	}

	bool operator<( const FlatStateIdentifier& other) const {
		return stateId < other.stateId;
	}

	FlatStateIdentifier(const Arabica::XPath::NodeSet<std::string>& activeStates,
	                    const Arabica::XPath::NodeSet<std::string>& alreadyEnteredStates,
	                    const std::map<std::string, Arabica::XPath::NodeSet<std::string> >& historyStates) {
		for (int i = 0; i < activeStates.size(); i++) {
			active.push_back(ATTR_CAST(activeStates[i], "id"));
		}

		for (int i = 0; i < alreadyEnteredStates.size(); i++) {
			const Arabica::DOM::NodeList<std::string>& children = alreadyEnteredStates[i].getChildNodes();
			bool isRelevant = false;
			for (int j = 0; j < children.getLength(); j++) {
				if (children.item(j).getNodeType() != Arabica::DOM::Node_base::ELEMENT_NODE)
					continue;
				if (iequals(LOCALNAME_CAST(children.item(j)), "data") || iequals(LOCALNAME_CAST(children.item(j)), "datamodel")) {
					isRelevant = true;
					break;
				}
			}
			if (isRelevant)
				visited.push_back(ATTR_CAST(alreadyEnteredStates[i], "id"));
		}

		std::map<std::string, Arabica::XPath::NodeSet<std::string> >::const_iterator histIter;
		for (histIter = historyStates.begin(); histIter != historyStates.end(); histIter++) {
			for (int i = 0; i < histIter->second.size(); i++) {
				histories[histIter->first].push_back(ATTR_CAST(histIter->second[i], "id"));
			}
		}

		initStateId();
	}


	FlatStateIdentifier(const std::list<std::string>& active,
	                    const std::list<std::string>& visited,
	                    const std::map<std::string, std::list<std::string> >& histories) : active(active), visited(visited), histories(histories) {
		initStateId();
	}

	static std::string toStateId(const std::list<std::string> active,
	                             const std::list<std::string> visited = std::list<std::string>(),
	                             const std::map<std::string, std::list<std::string> > histories = std::map<std::string, std::list<std::string> >()) {
		FlatStateIdentifier tmp(active, visited, histories);
		return tmp.getStateId();
	}

	static std::string toStateId(const Arabica::XPath::NodeSet<std::string> activeStates,
	                             const Arabica::XPath::NodeSet<std::string> alreadyEnteredStates = Arabica::XPath::NodeSet<std::string>(),
	                             const std::map<std::string, Arabica::XPath::NodeSet<std::string> > historyStates = std::map<std::string, Arabica::XPath::NodeSet<std::string> >()) {
		FlatStateIdentifier tmp(activeStates, alreadyEnteredStates, historyStates);
		return tmp.getStateId();
	}

	FlatStateIdentifier(const std::string& identifier) : stateId(identifier) {
		std::string parsedName;
		// parse unique state identifier
		std::stringstream elemNameSS(identifier);
		std::string section;
		while(std::getline(elemNameSS, section, ';')) {
			if (boost::starts_with(section, "active:{")) {
				// active:{s0,s1,s2}
				std::stringstream stateSS(section.substr(8, section.size() - 9));
				std::string state;
				while(std::getline(stateSS, state, ',')) {
					size_t closingBracketPos = state.find("}");
					if (closingBracketPos != std::string::npos) {
						state = state.substr(0, closingBracketPos);
					}
					boost::trim(state);
					if (state.length() > 0) {
						active.push_back(state);
					}
				}
			} else if (boost::starts_with(section, "visited:{")) {
				// entered:{s0,s1,s2}
				std::stringstream stateSS(section.substr(9, section.size() - 10));
				std::string state;
				while(std::getline(stateSS, state, ',')) {
					size_t closingBracketPos = state.find("}");
					if (closingBracketPos != std::string::npos) {
						state = state.substr(0, closingBracketPos);
					}
					boost::trim(state);
					if (state.length() > 0) {
						visited.push_back(state);
					}
				}
			} else if (boost::starts_with(section, "history:{")) {
				// history:{h0:{s1,s2},h1:{s2,s3}}
				std::string histEntries(section.substr(9, section.length() - 10));

				std::string state;
				size_t start = 0;
				size_t history = 0;

				while((history = histEntries.find(":", start)) != std::string::npos) {
					std::string histName = histEntries.substr(start, history - start);
					history++;

					size_t end = histEntries.find("}", start);
					if (end == std::string::npos)
						continue;

					std::stringstream stateSS(histEntries.substr(history + 1, end - history - 1));
					std::string state;
					while(std::getline(stateSS, state, ',')) {
						size_t closingBracketPos = state.find("}");
						if (closingBracketPos != std::string::npos) {
							state = state.substr(0, closingBracketPos);
						}
						boost::trim(state);
						if (state.length() > 0) {
							histories[histName].push_back(state);
						}

					}

					start = end + 2;
				}
			}
		}
		initStateId();
	}

	const std::string& getStateId() const {
		return stateId;
	}

	const std::list<std::string>& getActive() const {
		return active;
	}
	const std::string& getFlatActive() const {
		return flatActive;
	}


	const std::string& getFlatHistory() const {
		return flatHistories;
	}

	const std::list<std::string>& getVisited() const {
		return visited;
	}
	const std::string& getFlatVisited() const {
		return flatVisited;
	}


	const std::map<std::string, std::list<std::string> > & getHistory() const {
		return histories;
	}

	const std::map<std::string, std::set<std::string> > getHistorySets() {
		std::map<std::string, std::set<std::string> > histSet;
		std::map<std::string, std::list<std::string> >::const_iterator histIter = histories.begin();
		while(histIter != histories.end()) {
			histSet[histIter->first].insert(histIter->second.begin(), histIter->second.end());
			histIter++;
		}
		return histSet;
	}

protected:
	std::list<std::string> active;
	std::list<std::string> visited;
	std::map<std::string, std::list<std::string> > histories;

	std::string flatActive;
	std::string flatVisited;
	std::string flatHistories;

	std::string stateId;

	void initStateId() {
		std::stringstream stateIdSS;
		std::string seperator;

		std::stringstream flatActiveSS;
		flatActiveSS << "active:{";
		for (std::list<std::string>::const_iterator actIter = active.begin(); actIter != active.end(); actIter++) {
			flatActiveSS << seperator << *actIter;
			seperator = ",";
		}
		flatActiveSS << "}";
		flatActive = flatActiveSS.str();
		stateIdSS << flatActive;

		if (visited.size() > 0) {
			std::stringstream flatVisitedSS;
			seperator = "";
			flatVisitedSS << "visited:{";
			for (std::list<std::string>::const_iterator visitIter = visited.begin(); visitIter != visited.end(); visitIter++) {
				flatVisitedSS << seperator << *visitIter;
				seperator = ",";
			}
			flatVisitedSS << "}";
			flatVisited = flatVisitedSS.str();
			stateIdSS << ";" << flatVisited;
		}

		if (histories.size() > 0) {
			std::stringstream flatHistorySS;
			seperator = "";
			flatHistorySS << "history:{";
			for (std::map<std::string, std::list<std::string> >::const_iterator histIter = histories.begin(); histIter != histories.end(); histIter++) {
				flatHistorySS << seperator << histIter->first << ":{";
				seperator = ",";
				std::string itemSeperator;
				for (std::list<std::string>::const_iterator histItemIter = histIter->second.begin(); histItemIter != histIter->second.end(); histItemIter++) {
					flatHistorySS << itemSeperator << *histItemIter;
					itemSeperator = ",";
				}
				flatHistorySS << "}";
			}
			flatHistorySS << "}";
			flatHistories = flatHistorySS.str();
			stateIdSS << ";" << flatHistories;
		}

		stateId = stateIdSS.str();
	}

#if 0
	std::string activeId() {
		std::stringstream activeSS;
		activeSS << "active-";
		for (std::list<std::string>::const_iterator activeIter = active.begin(); activeIter != active.end(); activeIter++) {
			activeSS << *activeIter << "-";
		}
		return activeSS.str();
	}

#endif

};

}

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