Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 59d62aff9786c01c0ea92c5de3fefb7ea74263eb (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*******************************************************************************
 * 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 v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.generator.java.gen

import com.google.inject.Inject
import com.google.inject.Singleton
import java.util.HashSet
import org.eclipse.etrice.core.etmap.util.ETMapUtil
import org.eclipse.etrice.core.etphys.eTPhys.ExecMode
import org.eclipse.etrice.core.etphys.eTPhys.PhysicalThread
import org.eclipse.etrice.core.genmodel.etricegen.ActorInstance
import org.eclipse.etrice.core.genmodel.etricegen.IDiagnostician
import org.eclipse.etrice.core.genmodel.etricegen.Root
import org.eclipse.etrice.core.genmodel.etricegen.SubSystemInstance
import org.eclipse.etrice.core.room.CommunicationType
import org.eclipse.etrice.generator.base.IDataConfiguration
import org.eclipse.etrice.generator.base.IGeneratorFileIo
import org.eclipse.etrice.generator.base.IntelligentSeparator
import org.eclipse.etrice.generator.generic.ProcedureHelpers
import org.eclipse.etrice.generator.generic.RoomExtensions

import static extension org.eclipse.etrice.generator.base.Indexed.*
import org.eclipse.etrice.core.genmodel.etricegen.ActorInterfaceInstance
import org.eclipse.etrice.core.room.ActorClass
import com.google.common.collect.Sets
import com.google.common.collect.Lists
import org.eclipse.etrice.core.genmodel.etricegen.StructureInstance
import org.eclipse.etrice.generator.java.Main

@Singleton
class NodeGen {
	
	@Inject extension JavaExtensions
	@Inject extension RoomExtensions
	@Inject IDataConfiguration dataConfigExt
	@Inject ConfigGenAddon configGenAddon
	@Inject extension ProcedureHelpers

	@Inject IGeneratorFileIo fileIO
	@Inject VariableServiceGen varService
	@Inject IDiagnostician diagnostician
	
	def doGenerate(Root root) {
		for (nr : ETMapUtil::getNodeRefs()) {
			for (instpath : ETMapUtil::getSubSystemInstancePaths(nr)) {
				val ssi = root.getInstance(instpath) as SubSystemInstance
				val path = ssi.subSystemClass.generationTargetPath+ssi.subSystemClass.getPath
				val infopath = ssi.subSystemClass.generationInfoPath+ssi.subSystemClass.getPath
				val file = nr.getJavaFileName(ssi)
				
				checkDataPorts(ssi)
				
				val usedThreads = new HashSet<PhysicalThread>();
				for (thread: nr.type.threads) {
					val instancesOnThread = ssi.allContainedInstances.filter(ai|ETMapUtil::getPhysicalThread(ai)==thread)
					if (!instancesOnThread.empty)
						usedThreads.add(thread)
				}
				
				fileIO.generateFile("generating Node implementation", path, infopath, file, root.generate(ssi, usedThreads))
				if (dataConfigExt.hasVariableService(ssi))
					varService.doGenerate(root, ssi);
			}
		}
	}
	
	/**
	 * Recursively collect all possible interface instances below a given structure instance.
	 */
	def private HashSet<ActorClass> getOptionalActorClasses(Root root, StructureInstance si) {
		val aifs = Lists::newArrayList(si.eAllContents.filter(i|i instanceof ActorInterfaceInstance).map[aii|aii as ActorInterfaceInstance])
		val result = Sets::newHashSet(aifs.map[aii|(aii as ActorInterfaceInstance).actorClass])
		for (ai : aifs) {
			for (oi : ai.optionalInstances) {
				result.addAll(root.getOptionalActorClasses(oi))
			}
		}
		
		return result
	}
	def generate(Root root, SubSystemInstance comp, HashSet<PhysicalThread> usedThreads) {
		val cc = comp.subSystemClass
		val models = root.getReferencedModels(cc)
		val nr = ETMapUtil::getNodeRef(comp)
		val clsname = nr.getJavaClassName(comp)
		val threads = nr.type.threads.filter(t|usedThreads.contains(t))
		val opt = root.getOptionalActorClasses(comp)
		
	'''
		package «cc.getPackage()»;
		
		import org.eclipse.etrice.runtime.java.config.IVariableService;
		«IF Main::settings.generateMSCInstrumentation»
			import org.eclipse.etrice.runtime.java.debugging.DebuggingService;
		«ENDIF»
		import org.eclipse.etrice.runtime.java.messaging.IRTObject;
		import org.eclipse.etrice.runtime.java.messaging.IMessageService;
		import org.eclipse.etrice.runtime.java.messaging.MessageService;
		import org.eclipse.etrice.runtime.java.messaging.MessageServiceController;
		import org.eclipse.etrice.runtime.java.messaging.RTServices;
		import org.eclipse.etrice.runtime.java.modelbase.ActorClassBase;
		import org.eclipse.etrice.runtime.java.modelbase.OptionalActorInterfaceBase;
		import org.eclipse.etrice.runtime.java.modelbase.IOptionalActorFactory;
		import org.eclipse.etrice.runtime.java.modelbase.SubSystemClassBase;
		import org.eclipse.etrice.runtime.java.modelbase.InterfaceItemBase;
		import org.eclipse.etrice.runtime.java.modelbase.InterfaceItemBroker;
		
		«FOR model : models»
			import «model.name».*;
		«ENDFOR»
		
		«cc.userCode(1, false)»
		
		public class «clsname» extends SubSystemClassBase {
			
			«FOR thread : threads.indexed»
				public static final int «thread.value.threadId» = «thread.index0»;
			«ENDFOR»
		
			«cc.userCode(2, false)»
			
			public «clsname»(IRTObject parent, String name) {
				super(parent, name);
			}
			
			@Override
			public void receiveEvent(InterfaceItemBase ifitem, int evt, Object data){
			}
			
			@Override	
			public void instantiateMessageServices() {
			
				IMessageService msgService;
				«FOR thread: threads»
					«IF thread.execmode==ExecMode::POLLED || thread.execmode==ExecMode::MIXED»
						msgService = new MessageService(this, MessageService.ExecMode.«thread.execmode.name», «thread.time», 0, «thread.threadId», "MessageService_«thread.name»" /*, thread_prio */);
					«ELSE»
						msgService = new MessageService(this, MessageService.ExecMode.«thread.execmode.name», 0, «thread.threadId», "MessageService_«thread.name»" /*, thread_prio */);
					«ENDIF»
					RTServices.getInstance().getMsgSvcCtrl().addMsgSvc(msgService);
				«ENDFOR»
			}
		
			@Override
			public void instantiateActors() {
				
				// thread mappings
				«FOR ai : comp.allContainedInstances»
					addPathToThread("«ai.path»", «ETMapUtil::getPhysicalThread(ai).threadId»);
				«ENDFOR»
				
				// port to peer port mappings
				«FOR ai : comp.allSubInstances»
					«val ports = if (ai instanceof ActorInstance) (ai as ActorInstance).orderedIfItemInstances else ai.ports»
					«FOR pi : ports»
						«IF pi.peers.size>0»
							addPathToPeers("«pi.path»", «FOR peer : pi.peers SEPARATOR ","»"«peer.path»"«ENDFOR»);
						«ENDIF»
					«ENDFOR»
					«val services = if (ai instanceof ActorInterfaceInstance) (ai as ActorInterfaceInstance).providedServices else null»
					«IF services!=null»
						«FOR svc: services»
							addPathToPeers("«ai.path»/«svc.protocol.fullyQualifiedName»", "«svc.path»");
						«ENDFOR»
					«ENDIF»
				«ENDFOR»
		
				// sub actors
				«FOR sub : cc.actorRefs»
					«IF sub.multiplicity>1»
						for (int i=0; i<«sub.multiplicity»; ++i) {
							«IF Main::settings.generateMSCInstrumentation»
								DebuggingService.getInstance().addMessageActorCreate(this, "«sub.name»_"+i);
							«ENDIF»
							new «sub.type.name»(this, "«sub.name»_"+i);
						}
					«ELSE»
						«IF Main::settings.generateMSCInstrumentation»
							DebuggingService.getInstance().addMessageActorCreate(this, "«sub.name»");
						«ENDIF»
						new «sub.type.name»(this, "«sub.name»"); 
					«ENDIF»
				«ENDFOR»
				
				// wire optional actor interfaces with services
				«FOR aii: comp.allSubInstances.filter(inst|inst instanceof ActorInterfaceInstance).map(inst|inst as ActorInterfaceInstance)»
					{
						OptionalActorInterfaceBase oai = (OptionalActorInterfaceBase) getObject("«aii.getPath()»");
						«FOR svc: aii.providedServices»
							new InterfaceItemBroker(oai, "«svc.protocol.fullyQualifiedName»", 0);
						«ENDFOR»
					}
				«ENDFOR»
				
				// apply instance attribute configurations
				«FOR ai: comp.allContainedInstances»
					«val cfg = configGenAddon.genActorInstanceConfig(ai, "inst")»
					«IF cfg.length>0»
						{
							«ai.actorClass.name» inst = («ai.actorClass.name») getObject("«ai.path»");
							if (inst!=null) {
								«cfg»
							}
						}
					«ENDIF»
				«ENDFOR»
			}
			
			@Override
			public void init(){
				«IF Main::settings.generateMSCInstrumentation»
					DebuggingService.getInstance().addVisibleComment("begin sub system initialization");
				«ENDIF»
				«IF dataConfigExt.hasVariableService(comp)»
					variableService = new «clsname»VariableService(this);
				«ENDIF»
				super.init();
				«IF dataConfigExt.hasVariableService(comp)»
					variableService.init();
				«ENDIF»
				«IF Main::settings.generateMSCInstrumentation»
					DebuggingService.getInstance().addVisibleComment("done sub system initialization");
				«ENDIF»
			}
				
			@Override
			public void stop(){
				super.stop();
				«IF dataConfigExt.hasVariableService(comp)»
					variableService.stop();
				«ENDIF»
			}
			«IF Main::settings.generateMSCInstrumentation»
				
				@Override
				public void destroy() {
					DebuggingService.getInstance().addVisibleComment("begin sub system destruction");
					super.destroy();
					DebuggingService.getInstance().addVisibleComment("done sub system destruction");
				}
			«ENDIF»
			
			public IOptionalActorFactory getFactory(String optionalActorClass, String actorClass) {
				«val else1 = new IntelligentSeparator("else ")»
				«FOR oa : opt»
					«else1»if (optionalActorClass.equals("«oa.name»")) {
						«val else2 = new IntelligentSeparator("else ")»
						«FOR subcls : root.getSubClasses(oa).union(oa).filter(s|!s.abstract)»
							«else2»if ("«subcls.name»".equals(actorClass)) {
								return new «subcls.javaFactoryName»();
							}
						«ENDFOR»
					}
				«ENDFOR»
				
				return null;
			}
		};
	'''
	}

	def private getThreadId(PhysicalThread thread) {
		"THREAD_"+thread.name.toUpperCase
	}
	
	def private checkDataPorts(SubSystemInstance comp) {
		val found = new HashSet<String>()
		for (ai: comp.allContainedInstances) {
			val thread = ai.threadId
			for (pi: ai.orderedIfItemInstances) {
				if (pi.protocol.commType==CommunicationType::DATA_DRIVEN) {
					for (peer: pi.peers) {
						val peer_ai = peer.eContainer as ActorInstance
						val peer_thread = peer_ai.threadId
						if (thread!=peer_thread) {
							val path = pi.path
							val ppath = peer.path
							val pair = if (path.compareTo(ppath)<0) path+" and "+ppath
										else ppath+" and "+path
							if (!found.contains(pair)) {
								found.add(pair)
								diagnostician.error(pair+": data ports placed on different threads (not supported yet)",
									pi.interfaceItem, pi.interfaceItem.eContainingFeature)
							}
						}
					}
				}
			}
		}
	}

	def private isKindOf(ActorClass ac, HashSet<ActorClass> classes) {
		var a = ac
		
		while (a!=null) {
			if (classes.contains(a))
				return true
			a = a.base
		}
		
		false
	}
}

Back to the top