Browse Source

add in params

Cláudio Gomes 3 years ago
parent
commit
0e479c9a35

+ 9 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/src/be/uantwerpen/ansymo/semanticadaptation/tests/SemanticAdaptationGeneratorTest.xtend

@@ -5,6 +5,7 @@ package be.uantwerpen.ansymo.semanticadaptation.tests
 
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Adaptation
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.AtomicUnity
+import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.BoolLiteral
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.InnerFMU
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.MultiplyUnity
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Port
@@ -48,6 +49,14 @@ class SemanticAdaptationGeneratorTest extends AbstractSemanticAdaptationTest{
 			}
 		}) }
 	
+	@Test def test_addParams_sample1() { __generate('input/canonical_generation/sample1.sa', new IAcceptor<CompilationTestHelper.Result>(){
+			override accept(Result t) {
+				var Adaptation sa = t.resourceSet.resources.head.allContents.toIterable.filter(SemanticAdaptation).last.elements.filter(Adaptation).head
+				sa.params.head.declarations.filter[p | p.name=="INIT_EXT_INPUT_PORT3"].head.type == "Real"
+				sa.params.head.declarations.filter[p | p.name=="INIT_INNERFMU1__INPUT_PORT2"].head.expr instanceof BoolLiteral
+			}
+		}) }
+	
 	@Test def window_SA_parseNoExceptions() { __generate('input/power_window_case_study/window_sa.BASE.sa', new IAcceptor<CompilationTestHelper.Result>(){
 			override accept(Result t) { }
 		}) }

+ 63 - 20
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation/src/be/uantwerpen/ansymo/semanticadaptation/generator/SemanticAdaptationCanonicalGenerator.xtend

@@ -209,7 +209,8 @@ class SemanticAdaptationCanonicalGenerator extends AbstractGenerator {
 		addInPorts(sa)
 		
 		// Add in params
-		//addInParams(sa)
+		addInParams(sa)
+		
 		Log.pop("Canonicalize")
 	}
 	
@@ -671,36 +672,78 @@ class SemanticAdaptationCanonicalGenerator extends AbstractGenerator {
 		
 		val PARAM_PREFIX = "INIT_"
 		
-		var inputPort_to_parameterDeclaration_Map = new HashMap<Port, SingleParamDeclaration>(sa.inports.size)
+		var inputPort2parameterDeclaration = new HashMap<Port, SingleParamDeclaration>(sa.inports.size)
 		
 		for (inputPortDeclaration : sa.inports) {
 			Log.println("Generating parameter for port " + inputPortDeclaration.name)
 			var paramname = PARAM_PREFIX + inputPortDeclaration.name.toUpperCase()
-			var paramAlreadyDeclared = false
-			for(paramDeclarations : sa.params){
-				for(paramDeclaration : paramDeclarations.declarations){
-					if(paramDeclaration.name == paramname){
-						paramAlreadyDeclared = true
-					}
-				}
-			}
-			if (paramAlreadyDeclared){
+			
+			if (paramAlreadyDeclared(paramname, sa)){
 				Log.println("Parameter " + paramname + " already declared for port " + inputPortDeclaration.name)
 			} else {
 				Log.println("Declaring new parameter " + paramname + " for port " + inputPortDeclaration.name)
-				var factory = SemanticAdaptationFactory.eINSTANCE
-				if (sa.params.size == 0){
-					sa.params.add(factory.createParamDeclarations())
-				}
-				var paramDeclaration = factory.createSingleParamDeclaration()
-				
-				// TODO Continue here after solving problem with ports.
-				
-				//adaptation.params.head.declarations.add()
+				var paramDeclaration = addNewParamDeclaration(paramname, inputPortDeclaration, sa)
+				inputPort2parameterDeclaration.put(inputPortDeclaration, paramDeclaration)
 			}
 		}
 		
 		Log.pop("Adding input parameters... DONE")
+		return inputPort2parameterDeclaration
+	}
+	
+	def addNewParamDeclaration(String name, Port fromPort, Adaptation sa) {
+		var factory = SemanticAdaptationFactory.eINSTANCE
+		var paramDeclaration = factory.createSingleParamDeclaration()
+		
+		paramDeclaration.name = name
+		paramDeclaration.type = fromPort.type
+		paramDeclaration.expr = getDefaultTypeExpression(paramDeclaration.type)
+		
+		if (sa.params.size == 0){
+			sa.params.add(factory.createParamDeclarations())
+		}
+		
+		sa.params.head.declarations.add(paramDeclaration)
+		return paramDeclaration
+	}
+	
+	def getDefaultTypeExpression(String type) {
+		switch (type) {
+			case "Integer": {
+				val result = SemanticAdaptationFactory.eINSTANCE.createIntLiteral
+				result.value = 0
+				return result
+			}
+			case "Real": {
+				val result = SemanticAdaptationFactory.eINSTANCE.createRealLiteral
+				result.value = 0.0f
+				return result
+			}
+			case "Bool": {
+				val result = SemanticAdaptationFactory.eINSTANCE.createBoolLiteral
+				result.value = "false"
+				return result
+			}
+			case "String": {
+				val result = SemanticAdaptationFactory.eINSTANCE.createStringLiteral
+				result.value = ""
+				return result
+			}
+			default: {
+				throw new Exception("Unexpected type.")
+			}
+		}
+	}
+	
+	def paramAlreadyDeclared(String name, Adaptation sa) {
+		for(paramDeclarations : sa.params){
+			for(paramDeclaration : paramDeclarations.declarations){
+				if(paramDeclaration.name == name){
+					return true
+				}
+			}
+		}
+		return false
 	}
 	
 }