Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bb3c9ded9fbf8c2684e190fe042240e8a2720754 (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
/*******************************************************************************
 * 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 org.eclipse.etrice.core.genmodel.etricegen.ActorInstance
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.PrimitiveType
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
import java.util.List
import java.util.ArrayList

class ConfigGenAddon {
	
	@Inject extension JavaExtensions stdExt
	@Inject extension TypeHelpers typeHelpers
	@Inject extension ProcedureHelpers helpers
	@Inject IDataConfiguration dataConfigExt
	@Inject extension RoomExtensions
	
	// For SubSystemClassGen
	
	def public genActorInstanceConfig(ActorInstance ai){'''
		«FOR a : ai.actorClass.attributes»
			«applyInstanceConfig(ai, "inst", ai.path+"/"+a.name, a)»
		«ENDFOR»
		'''
	}
	
	def public applyInstanceConfig(ActorInstance ai, String invokes, String instancePath, Attribute a){
		var aType = a.refType.type
		if(aType.primitive){
			var value = dataConfigExt.getAttrInstanceConfigValue(ai, instancePath)
			if(value == null)
				''''''
			else if(a.size == 0 || aType.characterType)
				'''«invokes».«a.name.invokeSetter(null, (aType as PrimitiveType).toValueLiteral(value))»;'''
			else if(a.size == value.split(",").size){
				var arrayExpr = '''«FOR s : value.split(",") SEPARATOR ','»«(aType as PrimitiveType).toValueLiteral(s.trim)»«ENDFOR»'''
				'''«invokes».«a.name.invokeSetter(null, '''new «aType.typeName»[]«arrayExpr»'''.toString)»'''
			} else
				'''{
						«aType.typeName»[] array = «invokes».«a.name.invokeGetter(null)»;
						for (int i=0;i<«a.size»;i++){
							array[i] = «(aType as PrimitiveType).toValueLiteral(value)»;
					}'''
		}
		else if (aType.dataClass)
			'''«FOR e : (aType as DataClass).attributes»
					«applyInstanceConfig(ai, invokes+"."+a.name.invokeGetter(null), instancePath+"/"+e.name, e)»
				«ENDFOR»'''
	}
	
	// For ActorClassGen
	
//	def public genDynConfigGetterSetter(ActorClass ac){'''
//		«FOR a : dataConfigExt.getAllDynConfigReadAttributes(ac)»
//			public «a.refType.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.refType.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.getAllDynConfigWriteAttributes(ac)»
//			public void setAndWrite«a.name.toFirstUpper»(«a.refType.type.typeName»«IF a.size>0»[]«ENDIF» «a.name»){
//					set«a.name.toFirstUpper»(«a.name»);
//					variableService.write(this.getInstancePath()+"/«a.name»", «a.name»);
//			}
//		«ENDFOR»
//	'''}

	def genDynConfigGetterSetter(ActorClass ac){
		
	}
	
	def public genMinMaxConstants(ActorClass ac){
		var result = '''
			«FOR a : ac.attributes»
				«genMinMaxConstantsRec(ac, a.name, new ArrayList<Attribute>().union(a))»
			«ENDFOR»
		'''
		if(result.length != 0)
			result = result+'''//--------------------- Attribute Specifications'''
		return result
	}
	
	def private genMinMaxConstantsRec(ActorClass ac, String varNamePath, List<Attribute> path){
		var temp = null as String
		if(path.last.refType.type.dataClass)
			'''
				«FOR e : (path.last.refType.type as DataClass).allAttributes»
					«genMinMaxConstantsRec(ac, varNamePath+"_"+e.name, path.union(e))»
				«ENDFOR»
			'''
		else {
			var aType = (path.last.refType.type as PrimitiveType)
			'''
				«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