Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0e5d92c07a6861ae7d9d0541b9145ebd02895bbe (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
/*******************************************************************************
 * 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 org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.JavaIoFileSystemAccess
import com.google.inject.Inject

import static extension org.eclipse.xtext.xtend2.lib.ResourceExtensions.*
import static extension org.eclipse.xtext.xtend2.lib.*

import org.eclipse.etrice.core.room.*
import org.eclipse.etrice.generator.etricegen.*
import org.eclipse.etrice.generator.java.Logger

class SubSystemClassGen {
	
	@Inject extension JavaIoFileSystemAccess fileAccess
	@Inject extension StdExtensions stdExt
	@Inject extension ProcedureHelpers helpers
	@Inject Logger logger
	
	def doGenerate(Root root) {
		for (ssi: root.subSystemInstances) {
			var path = ssi.subSystemClass.generationTargetPath+ssi.subSystemClass.getPath
			var file = ssi.subSystemClass.fileName
			logger.logInfo("generating SubSystemClass implementation: '"+file+"' in '"+path+"'")
			fileAccess.setOutputPath(path)
			fileAccess.generateFile(file, root.generate(ssi, ssi.subSystemClass))
		}
	}
	
	def generate(Root root, SubSystemInstance comp, SubSystemClass cc) {'''
		package «cc.getPackage()»;
		
		import org.eclipse.etrice.runtime.java.messaging.MessageService;
		import org.eclipse.etrice.runtime.java.messaging.RTServices;
		import org.eclipse.etrice.runtime.java.messaging.Address;
		import org.eclipse.etrice.runtime.java.messaging.IRTObject;
		import org.eclipse.etrice.runtime.java.messaging.RTSystemServicesProtocol.*;
		import org.eclipse.etrice.runtime.java.modelbase.ActorClassBase;
		import org.eclipse.etrice.runtime.java.modelbase.SubSystemClassBase;
		import org.eclipse.etrice.runtime.java.modelbase.InterfaceItemBase;
		
		«var models = root.getReferencedModels(cc)»
		«FOR model : models»import «model.name».*;«ENDFOR»
		
		
		«helpers.UserCode(cc.userCode1)»
		
		public class «comp.name» extends SubSystemClassBase{
		
			«helpers.UserCode(cc.userCode2)»
			
			public «comp.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, new Address(0, 0, 0),"MessageService_Main"));
				«FOR thread : cc.threads»
					RTServices.getInstance().getMsgSvcCtrl().addMsgSvc(new MessageService(this, new Address(0, «cc.threads.indexOf(thread)+1», 0),"MessageService_«thread.name»", «thread.prio»));
				«ENDFOR»
				}
		
			@Override
			public void instantiateActors(){
				// all addresses
				// Addresses for the Subsystem Systemport
				«FOR ai : comp.allContainedInstances»
					Address addr_item_SystemPort_«comp.allContainedInstances.indexOf(ai)» = new Address(0,0,«comp.objCounter.getAndIncrementCount()»);
				«ENDFOR»
				
				«FOR ai : comp.allContainedInstances»
					// actor instance «ai.path» itself => Systemport Address
					// TODOTJ: For each Actor, multiple addresses should be generated (actor?, systemport, debugport)
					Address addr_item_«ai.path.getPathName()» = new Address(0,«ai.threadId»,«ai.objId»);
					// interface items of «ai.path»
					«FOR pi : ai.orderedIfItemInstances»
						«IF pi instanceof ServiceImplInstance || pi.peers.size>1»
							«FOR peer : pi.peers»
								«var i = pi.peers.indexOf(peer)»
								Address addr_item_«pi.path.getPathName()»_«i» = new Address(0,«pi.threadId»,«pi.objId+i»);
							«ENDFOR»
						«ELSE»
							Address addr_item_«pi.path.getPathName()» = new Address(0,«ai.threadId»,«pi.objId»);
						«ENDIF»
					«ENDFOR»
				«ENDFOR»
		
				// instantiate all actor instances
				instances = new ActorClassBase[«comp.allContainedInstances.size»];
				«FOR ai : comp.allContainedInstances»
					instances[«comp.allContainedInstances.indexOf(ai)»] = new «ai.actorClass.name»(
						«IF ai.eContainer instanceof SubSystemInstance»
							this,
						«ELSE»
							instances[«comp.allContainedInstances.indexOf(ai.eContainer)»],
						«ENDIF»
						"«ai.name»",
						// own interface item addresses
						new Address[][] {{addr_item_«ai.path.getPathName()»}«IF !ai.orderedIfItemInstances.empty»,«ENDIF»
						«FOR pi : ai.orderedIfItemInstances SEPARATOR ","»
							{
								«IF pi instanceof ServiceImplInstance || pi.peers.size>1»
									«FOR peer : pi.peers SEPARATOR ","»
										addr_item_«pi.path.getPathName()»_«pi.peers.indexOf(peer)»
									«ENDFOR»
								«ELSE»
									addr_item_«pi.path.getPathName()»
								«ENDIF»
							}
						«ENDFOR»
						},
						// peer interface item addresses
						new Address[][] {{addr_item_SystemPort_«comp.allContainedInstances.indexOf(ai)»}«IF !ai.orderedIfItemInstances.empty»,«ENDIF»
							«FOR pi : ai.orderedIfItemInstances SEPARATOR ","»
							{
								«IF !(pi instanceof ServiceImplInstance) && pi.peers.isEmpty»
									null
								«ELSE»
									«FOR pp : pi.peers SEPARATOR ","»
										«IF pp instanceof ServiceImplInstance || pp.peers.size>1»
											addr_item_«pp.path.getPathName()»_«pp.peers.indexOf(pi)»
										«ELSE»
											addr_item_«pp.path.getPathName()»
										«ENDIF»
									«ENDFOR»
								«ENDIF»
							}
							«ENDFOR»
						}
					); 
				«ENDFOR»
		
				// create the subsystem system port	
				RTSystemPort = new RTSystemServicesProtocolConjPortRepl(this, "RTSystemPort",
						0, //local ID
						// own addresses
						new Address[]{
							«FOR ai : comp.allContainedInstances SEPARATOR ","»
								addr_item_SystemPort_«comp.allContainedInstances.indexOf(ai)»
							«ENDFOR»
						},
						// peer addresses
						new Address[]{
							«FOR ai : comp.allContainedInstances SEPARATOR ","»
								addr_item_«ai.path.getPathName()»
							«ENDFOR»
						});
						
			}
		};
	'''
	}
	
}

Back to the top