Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ae5fce6a9182f6a3e40e10e6e844919f065f0132 (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
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
/*
 * generated by Xtext
 */
package org.eclipse.etrice.core.fsm.scoping

import com.google.inject.Inject
import java.util.HashMap
import java.util.List
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EReference
import org.eclipse.etrice.core.fsm.fSM.ChoicepointTerminal
import org.eclipse.etrice.core.fsm.fSM.RefinedState
import org.eclipse.etrice.core.fsm.fSM.SimpleState
import org.eclipse.etrice.core.fsm.fSM.State
import org.eclipse.etrice.core.fsm.fSM.StateGraph
import org.eclipse.etrice.core.fsm.fSM.StateTerminal
import org.eclipse.etrice.core.fsm.fSM.SubStateTrPointTerminal
import org.eclipse.etrice.core.fsm.fSM.TrPointTerminal
import org.eclipse.etrice.core.fsm.util.FSMHelpers
import org.eclipse.xtext.naming.QualifiedName
import org.eclipse.xtext.resource.EObjectDescription
import org.eclipse.xtext.resource.IEObjectDescription
import org.eclipse.xtext.scoping.IScope
import org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider
import org.eclipse.xtext.scoping.impl.SimpleScope
import org.eclipse.etrice.core.fsm.fSM.ModelComponent
import java.util.HashSet
import java.util.ArrayList
import org.eclipse.etrice.core.fsm.fSM.RefinedTransition
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.eclipse.etrice.core.fsm.fSM.Transition

/**
 * This class contains custom scoping description.
 * 
 * see : http://www.eclipse.org/Xtext/documentation.html#scoping
 * on how and when to use it 
 *
 */
class FSMScopeProvider extends AbstractDeclarativeScopeProvider {
	
	@Inject extension FSMHelpers

	@Inject extension IQualifiedNameProvider

	/**
	 * returns a flat list of State scopes for a {@link StateTerminal}
	 * @param st - the transition endpoint or terminal
	 * @param ref - not used
	 * @return a list of scopes
	 */
	def IScope scope_StateTerminal_state(StateTerminal st, EReference ref) {
		val List<IEObjectDescription> scopes = newArrayList

		// first state graph in container hierarchy
		val parent = getStateGraph(st)
		getStateScopes(parent, scopes)

		return new SimpleScope(IScope.NULLSCOPE, scopes)
	}

	/**
	 * returns a flat list of TransitionPoint scopes for a {@link TrPointTerminal}
	 * @param ep - the transition endpoint or terminal
	 * @param ref - not used
	 * @return a list of scopes
	 */
	def IScope scope_TrPointTerminal_trPoint(TrPointTerminal ep, EReference ref) {
		val List<IEObjectDescription> scopes = newArrayList
		
		val parent = getStateGraph(ep)
		val tps = parent.allTrPoints
		for (tp : tps) {
			scopes.add(EObjectDescription.create(tp.name, tp))
		}
		
		return new SimpleScope(IScope.NULLSCOPE, scopes)
	}

	/**
	 * returns a flat list of TransitionPoint scopes for a {@link SubStateTrPointTerminal}
	 * @param ep - the transition endpoint or terminal
	 * @param ref - not used
	 * @return a list of scopes
	 */
	def IScope scope_SubStateTrPointTerminal_trPoint(SubStateTrPointTerminal ep, EReference ref) {
		val List<IEObjectDescription> scopes = newArrayList
		
		val parent = getStateGraph(ep)
		if (ep.state!==null) {
			var epState = ep.state
			
			// check if there is a refined state for this state
			epState = parent.getRefinedStateFor(epState)
			
			if (epState.getSubgraph()!==null) {
				val tps = epState.subgraph.getAllTrPoints
				for (tp : tps) {
					scopes.add(EObjectDescription.create(tp.name, tp))
				}
			}
		}
		
		return new SimpleScope(IScope.NULLSCOPE, scopes)
	}

	/**
	 * returns a flat list of State scopes for a {@link SubStateTrPointTerminal}
	 * @param st - the transition endpoint or terminal
	 * @param ref - not used
	 * @return a list of scopes
	 */
	def IScope scope_SubStateTrPointTerminal_state(SubStateTrPointTerminal st, EReference ref) {
		val List<IEObjectDescription> scopes = newArrayList
		
		val parent = getStateGraph(st)
		getStateScopes(parent, scopes)
		
		return new SimpleScope(IScope.NULLSCOPE, scopes)
	}

	/**
	 * returns a flat list of Choicepoint scopes for a {@link SubStateTrPointTerminal}
	 * @param ct - the transition endpoint or terminal
	 * @param ref - not used
	 * @return a list of scopes
	 */
	def IScope scope_ChoicepointTerminal_cp(ChoicepointTerminal ct, EReference ref) {
		val List<IEObjectDescription> scopes = newArrayList
		
		val parent = getStateGraph(ct)
		val choicePoints = parent.allChoicePoints
		for (cp : choicePoints) {
			scopes.add(EObjectDescription.create(cp.name, cp))
		}
		
		return new SimpleScope(IScope.NULLSCOPE, scopes)
	}
	
	/**
	 * returns a flat list of BaseState scopes for a {@link RefinedState}
	 * @param rs - the refined state
	 * @param ref - not used
	 * @return a list of scopes
	 */
	def IScope scope_RefinedState_target(RefinedState rs, EReference ref) {
		val List<IEObjectDescription> scopes = newArrayList
		
		var sg = getStateGraph(rs)
		if (sg.eContainer instanceof ModelComponent) {
			var comp = sg.eContainer as ModelComponent
			if (comp.base !== null && !comp.isCircularClassHierarchy) {
				comp = comp.base
				val HashSet<State> covered = newHashSet
				val ArrayList<State> states = newArrayList
				while (comp!==null) {
					recursivelyAddStates(comp.stateMachine, covered, states)
					comp = comp.base
				}
				for (s : states) {
					scopes.add(EObjectDescription.create(s.statePath, s))
				}
			}
		}
		else if (sg.eContainer instanceof RefinedState) {
			sg = (sg.eContainer as RefinedState).target.subgraph
			if (sg!==null)
				for (s : sg.states) {
					scopes.add(EObjectDescription.create(s.name, s))
				}
		}
		
		return new SimpleScope(IScope.NULLSCOPE, scopes)
	}
	
	/**
	 * returns a flat list of Transition scopes for a {@link RefinedTransition}
	 * @param trans - the refined transition
	 * @param ref - not used
	 * @return a list of scopes
	 */
	def IScope scope_RefinedTransition_target(RefinedTransition trans, EReference ref) {
		val List<IEObjectDescription> scopes = newArrayList
		
		var comp = trans.eContainer.eContainer as ModelComponent
		if(!comp.isCircularClassHierarchy){
			comp = comp.base
			while (comp!==null) {
				if (comp.stateMachine!==null) {
					val acNameSegments = comp.fullyQualifiedName.segmentCount
					val iter = comp.stateMachine.eAllContents
					while (iter.hasNext) {
						val EObject obj = iter.next
						if (obj instanceof Transition) {
							// use qualified name but skip ac FQN and also omit state machine name 'sm'
							scopes.add(EObjectDescription.create(obj.fullyQualifiedName.skipFirst(acNameSegments+1), obj))
						}
					}
				}
				comp = comp.base
			}
		}
		return new SimpleScope(IScope.NULLSCOPE, scopes)
	}
	
	/**
	 * @param sg
	 * @param covered
	 * @param states
	 */
	def private void recursivelyAddStates(StateGraph sg, HashSet<State> covered, ArrayList<State> states) {
		for (s : sg.states) {
			if (s instanceof SimpleState && !covered.contains(s)) {
				states.add(s)
			}
			else if (s instanceof RefinedState && !covered.contains(s)) {
				states.add(s)
				covered.add((s as RefinedState).target)
			}
		}
		
		// recursion
		for (s : sg.states) {
			if (s.getSubgraph()!==null)
				recursivelyAddStates(s.getSubgraph(), covered, states)
		}
	}
	
	/**
	 * first container of type {@link StateGraph} ({@link State}, {@link StateMachine})
	 * @param obj
	 * @return StateGraph Container
	 */
	def private StateGraph getStateGraph(EObject obj) {
		var EObject ctx = obj.eContainer
		while (!(ctx instanceof StateGraph) && ctx.eContainer!==null) {
			ctx = ctx.eContainer
		}
		if (ctx instanceof StateGraph)
			return ctx
		
		return null
	}

	/**
	 * compute the path of a {@link BaseState}
	 * @param bs
	 * @return the path
	 */
	def private QualifiedName getStatePath(State bs) {
		val EObject parent = bs.eContainer.eContainer
		if (parent instanceof SimpleState)
			return getStatePath(parent as SimpleState).append(bs.name)
		else if (parent instanceof RefinedState) {
			val State target = (parent as RefinedState).target
			if (target!==null)
				return getStatePath(target).append(bs.name)
		}
		return QualifiedName.create(bs.name)
	}

	/**
	 * @param parent
	 * @param scopes
	 */
	def private void getStateScopes(StateGraph parent, List<IEObjectDescription> scopes) {
		val states = parent.allStates
		val HashMap<String, SimpleState> name2state = newHashMap
		for (s : states) {
			// returning SimpleStates only simplifies the relocation task in the generator model:
			// there we shuffle RefinedState contents to SimpleStates and remove the RefinedStates.
			// If we had references to RefinedStates we had to redirect those
			name2state.put(s.getName(), s.baseState)
		}
		for (entry : name2state.entrySet) {
			scopes.add(EObjectDescription.create(entry.key, entry.value))
		}
	}

}

Back to the top