Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6cbb9d3e4e2a20d4b0e3691de92b1c89faaf6891 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 		Peter Karlitschek
 *
 *******************************************************************************/

package org.eclipse.etrice.generator.cpp.gen

import com.google.inject.Singleton
import java.util.Comparator
import org.eclipse.etrice.core.fsm.fSM.State
import org.eclipse.etrice.core.genmodel.fsm.fsmgen.GraphContainer
import org.eclipse.etrice.core.genmodel.fsm.fsmgen.Node
import org.eclipse.etrice.generator.cpp.Main
import org.eclipse.etrice.generator.generic.GenericStateMachineGenerator

import static extension org.eclipse.etrice.core.genmodel.fsm.FsmGenExtensions.*
import com.google.inject.Inject
import org.eclipse.etrice.generator.cpp.setup.GeneratorOptionsHelper

@Singleton
class StateMachineGen extends GenericStateMachineGenerator {

	@Inject protected extension GeneratorOptionsHelper

	val nodeComparator = new NodeComparator
	
	override genExtra(GraphContainer gc, boolean generateImplementation) {
		var ac = gc.component
		val clsName = ac.componentName
		val orderedStateNodes = gc.graph.allStateNodes.toList.sortWith(nodeComparator)

		if(generateImplementation)
			'''
				«IF Main::settings.generateMSCInstrumentation || Main::settings.generateWithVerboseOutput»
					// state names
					const String «clsName»::s_stateStrings[] = {
						"<no state>",
						"<top>",
						«FOR state : orderedStateNodes SEPARATOR ","»
							"«(state.stateGraphNode as State).genStatePathName»"
						«ENDFOR»
					};
				«ENDIF»
				const int «clsName»::s_numberOfStates = «2+orderedStateNodes.size»;

				void «clsName»::setState(int new_state) {
					«IF Main::settings.generateMSCInstrumentation»
						DebuggingService::getInstance().addActorState(*this, s_stateStrings[new_state].c_str());
					«ENDIF»
					«IF Main::settings.generateWithVerboseOutput»
						if (s_stateStrings[new_state] != "Idle") {
							std::cout << getInstancePath().c_str() << " -> " << s_stateStrings[new_state].c_str() << std::endl;
						}
					«ENDIF»
					m_state = new_state;
				}
			'''
		else
			'''
				«IF Main::settings.generateMSCInstrumentation || Main::settings.generateWithVerboseOutput»
				static const String s_stateStrings[];
				«ENDIF»
				static const int s_numberOfStates;

				int history[«2+orderedStateNodes.size»];

				void setState(int new_state);
			'''
	}

	override public stateType() {
		"etInt16"
	}

	override boolType() {
		"etBool"
	}

	private static class NodeComparator implements Comparator<Node> {
		
		// it is crucial that we obey the order that is used for state IDs
		// that means we have to collect base classes first and each base class list with leaf states last
		override int compare(Node o1, Node o2) {
			if (o1.inheritanceLevel==o2.inheritanceLevel) {
				if (o1.isLeaf && o2.isLeaf) {
					return 0
				}
				if (o1.isLeaf) {
					return 1
				}
				if (o2.isLeaf) {
					return -1
				}
				return 0
			}
			else {
				return Integer.compare(o1.inheritanceLevel, o1.inheritanceLevel)
			}
		}
		
	}
}

Back to the top