Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2dc1894dc8138584177ab041ac39f07715595bcf (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
/*******************************************************************************
 * Copyright (c) 2012 Juergen Haug
 * 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:
 * 		Juergen Haug
 * 
 *******************************************************************************/

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

import com.google.inject.Inject
import java.util.ArrayList
import java.util.List
import org.eclipse.etrice.core.genmodel.etricegen.ActorInstance
import org.eclipse.etrice.core.genmodel.etricegen.InstanceBase
import org.eclipse.etrice.core.room.ActorClass
import org.eclipse.etrice.core.room.Attribute
import org.eclipse.etrice.core.room.DataClass
import org.eclipse.etrice.core.room.EnumerationType
import org.eclipse.etrice.core.room.PrimitiveType
import org.eclipse.etrice.core.room.util.RoomHelpers
import org.eclipse.etrice.generator.base.IDataConfiguration
import org.eclipse.etrice.generator.generic.ProcedureHelpers
import org.eclipse.etrice.generator.generic.RoomExtensions
import org.eclipse.etrice.generator.generic.TypeHelpers

class ConfigGenAddon {
	
	@Inject extension JavaExtensions stdExt
	@Inject extension TypeHelpers typeHelpers
	@Inject extension ProcedureHelpers helpers
	@Inject IDataConfiguration dataConfigExt
	@Inject extension RoomHelpers
	@Inject extension RoomExtensions
	@Inject Initialization initGen
	
	// For SubSystemClassGen
	
	def public genActorInstanceConfig(ActorInstance ai, String aiVariableName){'''
			«FOR a : ai.actorClass.allAttributes»
				«applyInstanceConfig(ai, aiVariableName, new ArrayList<Attribute>().union(a))»
			«ENDFOR»
			«FOR pi : ai.orderedIfItemInstances»
				«var attribs = getPortClass(pi.interfaceItem)?.attributes»
				«IF attribs != null»
					«FOR a : attribs»
						«applyInstanceConfig(pi, aiVariableName+"."+invokeGetter(pi.name, null), new ArrayList<Attribute>().union(a))»
					«ENDFOR»
				«ENDIF»
			«ENDFOR»
		'''
	}
	
	def private CharSequence applyInstanceConfig(InstanceBase instance, String invokes, List<Attribute> path){
		var a = path.last
		var aType = a.type.type		
		switch aType {
			PrimitiveType: {
				var value = typeHelpers.getAttrInstanceConfigValue(path, instance)
				if(value != null)
					initGen.genAttributeInitializer(a, toValueLiteral(aType, value), invokes)	
			}
			EnumerationType: {
				var value = typeHelpers.getAttrInstanceConfigValue(path, instance)
				if(value != null)
					initGen.genAttributeInitializer(a, value, invokes)
			}
			DataClass:
				'''
					«FOR e : (aType as DataClass).allAttributes»
						«applyInstanceConfig(instance, invokes+"."+a.name.invokeGetter(null), path.union(e))»
					«ENDFOR»
				'''	
		}	
	}
	
	// For ActorClassGen
	
	def public genDynConfigGetterSetter(ActorClass ac){'''
		«FOR a : dataConfigExt.getDynConfigReadAttributes(ac)»
			public «a.type.type.typeName»«IF a.size>0»[]«ENDIF» get«a.name.toFirstUpper»(){
				if(lock_«a.name» == null)
					return «a.name»;
				else
					synchronized(lock_«a.name»){
						return «a.name»;
					}
			}
			public void set«a.name.toFirstUpper»(«a.type.type.typeName»«IF a.size>0»[]«ENDIF» «a.name»){
				if(lock_«a.name» == null)
					this.«a.name» = «a.name»;
				else
					synchronized(lock_«a.name»){
						this.«a.name» = «a.name»;
					}
			}
			public DynConfigLock get«a.name.toFirstUpper»Lock(){
				return lock_«a.name»;
			}	
		«ENDFOR»
		«FOR a : dataConfigExt.getDynConfigWriteAttributes(ac)»
			public void setAndWrite«a.name.toFirstUpper»(«a.type.type.typeName»«IF a.size>0»[]«ENDIF» «a.name»){
					set«a.name.toFirstUpper»(«a.name»);
					getVariableService().write(this.getInstancePath()+"/«a.name»", «a.name»);
			}
		«ENDFOR»
	'''}
	
	def public genMinMaxConstants(ActorClass ac){
		var result = '''
			«FOR a : ac.allAttributes»
				«genMinMaxConstantsRec(ac, a.name, new ArrayList<Attribute>().union(a))»
			«ENDFOR»
		'''
		if(result.length != 0)
			result = result+'''//--------------------- Attribute Specifications'''
		return result
	}
	
	def private CharSequence genMinMaxConstantsRec(ActorClass ac, String varNamePath, List<Attribute> path){
		var aType = path.last.type.type
		switch aType {
			DataClass:
				'''
					«FOR e : (aType as DataClass).allAttributes»
						«genMinMaxConstantsRec(ac, varNamePath+"_"+e.name, path.union(e))»
					«ENDFOR»
				'''
			PrimitiveType:{
				var temp = null as String
				'''
					«IF (temp = dataConfigExt.getAttrClassConfigMinValue(ac, path)) != null»
						public static «aType.minMaxType» MIN_«varNamePath» = «aType.toValueLiteral(temp)»;
					«ENDIF»
					«IF (temp = dataConfigExt.getAttrClassConfigMaxValue(ac, path)) != null»
						public static «aType.minMaxType» MAX_«varNamePath» = «aType.toValueLiteral(temp)»;
					«ENDIF»
				'''
			}
		}
	}
	
	def private getMinMaxType(PrimitiveType type){
		return switch(type.typeName){
			case "byte":
				"int"
			case "short":
				"int"
			case "float":
				"double"
			default:
				type.typeName
		}
	}
	
}

Back to the top