Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6104fe4a910bdfe9f288e003426d91c89dd2b976 (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
/*******************************************************************************
 * 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.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.core.room.LogicalThread
import org.eclipse.etrice.generator.base.IDataConfiguration
import org.eclipse.etrice.generator.base.IGeneratorFileIo
import org.eclipse.etrice.generator.generic.ProcedureHelpers
import org.eclipse.etrice.generator.generic.RoomExtensions

import static extension org.eclipse.etrice.generator.base.Indexed.*

@Singleton
class SubSystemClassGen {
	
	@Inject IGeneratorFileIo fileIO
	@Inject extension JavaExtensions
	@Inject extension RoomExtensions
	@Inject IDataConfiguration dataConfigExt
	@Inject ConfigGenAddon configGenAddon
	@Inject extension ProcedureHelpers

	@Inject VariableServiceGen varService
	@Inject IDiagnostician diagnostician
	
	def doGenerate(Root root) {
		for (ssi: root.subSystemInstances) {
			val path = ssi.subSystemClass.generationTargetPath+ssi.subSystemClass.getPath
			val infopath = ssi.subSystemClass.generationInfoPath+ssi.subSystemClass.getPath
			val file = ssi.subSystemClass.getJavaFileName
			checkDataPorts(ssi)
			fileIO.generateFile("generating SubSystemClass implementation", path, infopath, file, root.generate(ssi))
			if (dataConfigExt.hasVariableService(ssi))
				varService.doGenerate(root, ssi);
		}
	}

	def generate(Root root, SubSystemInstance comp) {
		val cc = comp.subSystemClass
		val models = root.getReferencedModels(cc)
		
	'''
		package «cc.getPackage()»;
		
		import org.eclipse.etrice.runtime.java.config.IVariableService;
		import org.eclipse.etrice.runtime.java.messaging.IRTObject;
		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.SubSystemClassBase;
		import org.eclipse.etrice.runtime.java.modelbase.InterfaceItemBase;
		
		«FOR model : models»
			import «model.name».*;
		«ENDFOR»
		
		«cc.userCode(1)»
		
		public class «cc.name» extends SubSystemClassBase {
			
			public final int THREAD__DEFAULT = 0;
			«FOR thread : cc.threads.indexed»
				public final int «thread.value.threadId» = «thread.index1»;
			«ENDFOR»
		
			«cc.userCode(2)»
			
			public «cc.name»(IRTObject parent, String name) {
				super(parent, name);
			}
			
			@Override
			public void receiveEvent(InterfaceItemBase ifitem, int evt, Object data){
			}
			
			@Override	
			public void instantiateMessageServices() {
			
				RTServices.getInstance().getMsgSvcCtrl().addMsgSvc(new MessageService(this, 0, THREAD__DEFAULT, "MessageService_Main"));
				«FOR thread : cc.threads»
					RTServices.getInstance().getMsgSvcCtrl().addMsgSvc(new MessageService(this, 0, «thread.threadId», "MessageService_«thread.name»" /*, thread_prio */));
				«ENDFOR»
			}
		
			@Override
			public void instantiateActors() {
				
				MessageServiceController msgSvcCtrl = RTServices.getInstance().getMsgSvcCtrl();

				// thread mappings
				msgSvcCtrl.addPathToThread("«comp.path»", THREAD__DEFAULT);
				«FOR ai : comp.allContainedInstances»
					«IF ai.threadId!=0»
						msgSvcCtrl.addPathToThread("«ai.path»", «cc.threads.get(ai.threadId-1).threadId»);
					«ENDIF»
				«ENDFOR»
				
				// port to peer port mappings
				«FOR ai : comp.allContainedInstances»
					«FOR pi : ai.orderedIfItemInstances»
						«IF pi.peers.size>0»
							msgSvcCtrl.addPathToPeers("«pi.path»", «FOR peer : pi.peers SEPARATOR ","»"«peer.path»"«ENDFOR»);
						«ENDIF»
					«ENDFOR»
				«ENDFOR»
		
				// sub actors
				«FOR sub : cc.actorRefs»
					«IF sub.size>1»
						for (int i=0; i<«sub.size»; ++i)
							new «sub.type.name»(this, "«sub.name»_"+i); 
					«ELSE»
						new «sub.type.name»(this, "«sub.name»"); 
					«ENDIF»
				«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 dataConfigExt.hasVariableService(comp)»
					variableService = new «cc.name»VariableService(this);
				«ENDIF»
				super.init();
				«IF dataConfigExt.hasVariableService(comp)»
					variableService.init();
				«ENDIF»
			}
				
			@Override
			public void stop(){
				super.stop();
				«IF dataConfigExt.hasVariableService(comp)»
					variableService.stop();
				«ENDIF»
			}
				
		};
	'''
	}

	def private getThreadId(LogicalThread 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)
							}
						}
					}
				}
			}
		}
	}
}

Back to the top