Browse Source

Refactored

Casper Thule 4 years ago
parent
commit
6f471010ec

+ 4 - 7
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/data/InOutRulesBlockResult.java

@@ -6,17 +6,14 @@ import java.util.List;
 import org.eclipse.xtext.xbase.lib.Pair;
 
 public class InOutRulesBlockResult extends RulesBlockResult {
-	public final LinkedHashMap<String, Pair<SVType, Object>> globalVars;
-	public final LinkedHashMap<String, GlobalInOutVariable> globalVars2;
+	public final LinkedHashMap<String, GlobalInOutVariable> gVars;
 	public final String constructorInitialization;
 
-	public InOutRulesBlockResult(String generatedCpp, List<String> functionSignatures, 
-			LinkedHashMap<String, Pair<SVType, Object>> globalVars, 
-			LinkedHashMap<String, GlobalInOutVariable> globalVars2, String constructorInitialization)
+	public InOutRulesBlockResult(String generatedCpp, List<String> functionSignatures,
+			LinkedHashMap<String, GlobalInOutVariable> gVars, String constructorInitialization)
 	{
 		super(generatedCpp, functionSignatures);
-		this.globalVars = globalVars;
-		this.globalVars2 = globalVars2;
+		this.gVars = gVars;
 		this.constructorInitialization = constructorInitialization;
 	}
 }

+ 23 - 19
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/CppGenerator.xtend

@@ -57,8 +57,7 @@ class CppGenerator extends SemanticAdaptationGenerator {
 			// List of FMUs with a pairing between its name and its type.name.
 			var ArrayList<Pair<String, String>> fmus = newArrayList();
 
-			// The scalar variables above with additional data
-			var LinkedHashMap<String, LinkedHashMap<String, MappedScalarVariable>> mappedScalarVariables = newLinkedHashMap();
+
 
 			// TODO: Currently only 1 inner fmu is supported
 			val innerFmus = type.inner.eAllContents.toList.filter(InnerFMU);
@@ -69,12 +68,17 @@ class CppGenerator extends SemanticAdaptationGenerator {
 				throw new IncorrectAmountOfElementsException("The adaptation does not contain any InnerFMUs.")
 			}
 
+			/*
+			 * This map will contain scalar variables from the FMUs defined in InnerFMU.
+			 * The structure is fmuName -> (SVName -> mappedSV) where SVName = mappedSV.name for easy lookup.
+			 * The mappedSV contains the original scalar variable and extra data such as define name.
+			 */
+			var LinkedHashMap<String, LinkedHashMap<String, MappedScalarVariable>> mappedScalarVariables = newLinkedHashMap();
+
 			/*
 			 * Loading the FMU defined in InnerFMU, the related model description file and its scalar variables.
-			 * This is stored in a map of fmuName -> (SVName -> mappedSV)
-			 * where the mappedSV contains the original scalar variable and some extra data such as define name. 
 			 */
-			// TODO: Currently only 1 model description is supported
+			// TODO: Add support for multiple inner fmus
 			var ModelDescription md;
 			for (fmu : type.inner.eAllContents.toList.filter(InnerFMU)) {
 				md = new ModelDescription(fmu.name, fmu.type.name, new File(fmu.path.replace('\"', '')));
@@ -88,10 +92,10 @@ class CppGenerator extends SemanticAdaptationGenerator {
 				mappedScalarVariables.put(fmu.name, mSV);
 			}
 
-			// Defines for accessing FMU scalar variables.
-			val String fmusDefines = calcDefines2(mappedScalarVariables);
+			// C++ Defines for accessing FMU scalar variables.
+			val String fmusDefines = calcDefines(mappedScalarVariables);
 
-			// Compile Params 
+			// Compile Params
 			var LinkedHashMap<String, GlobalInOutVariable> params = newLinkedHashMap;
 			val String paramsConstructorSource = compileParams(params, type.params);
 
@@ -102,7 +106,7 @@ class CppGenerator extends SemanticAdaptationGenerator {
 			var LinkedHashMap<String, SAScalarVariable> SASVs = calcSASVsFromInportsOutports(adapInteralRefName,
 				type.inports, type.outports)
 
-			// Generate defines for the scalar variables of the semantic adaptation 
+			// C++ defines for accessing semantic adaptation scalar variables 
 			val String SADefines = calcSADefines(SASVs.values);
 
 			// Compile the in rules
@@ -114,13 +118,6 @@ class CppGenerator extends SemanticAdaptationGenerator {
 				filter(OutRulesBlock).map[x|x as InOutRules], adapClassName, adapInteralRefName, mappedScalarVariables,
 				SASVs, params);
 
-			// Merge the global variables for h file and cpp file
-			// TODO: Check for duplicates
-			var LinkedHashMap<String, GlobalInOutVariable> globalVariables = newLinkedHashMap();
-			globalVariables.putAll(params);
-			globalVariables.putAll(outRuleResult.globalVars2);
-			globalVariables.putAll(inRuleResult.globalVars2);
-
 			// Compile the Control Rules
 			val crtlRuleResult = compileControlRuleBlock(adaptation.eAllContents.toIterable.filter(ControlRuleBlock),
 				adapClassName, adapInteralRefName, SASVs);
@@ -163,6 +160,13 @@ class CppGenerator extends SemanticAdaptationGenerator {
 			);
 			fsa.generateFile(adapClassName + ".cpp", sourceFile);
 
+			// Merge the global variables for use in compiling the header file.
+			// TODO: Check for duplicates
+			var LinkedHashMap<String, GlobalInOutVariable> allGVars = newLinkedHashMap();
+			allGVars.putAll(params);
+			allGVars.putAll(outRuleResult.gVars);
+			allGVars.putAll(inRuleResult.gVars);
+
 			// Compile the header file
 			val headerFile = compileHeader(
 				adapClassName,
@@ -171,7 +175,7 @@ class CppGenerator extends SemanticAdaptationGenerator {
 				inRuleResult.functionSignatures,
 				outRuleResult.functionSignatures,
 				crtlRuleResult.functionSignatures,
-				globalVariables,
+				allGVars,
 				params,
 				fmus,
 				SASVs.values.map[CalcSVar()].toList
@@ -210,7 +214,7 @@ class CppGenerator extends SemanticAdaptationGenerator {
 		return defines.join("\n");
 	}
 
-	def calcDefines2(LinkedHashMap<String, LinkedHashMap<String, MappedScalarVariable>> map) {
+	def calcDefines(LinkedHashMap<String, LinkedHashMap<String, MappedScalarVariable>> map) {
 		var ArrayList<String> defines = newArrayList();
 
 		for (fmuEntries : map.entrySet) {
@@ -535,7 +539,7 @@ class CppGenerator extends SemanticAdaptationGenerator {
 				'''
 			}
 		}
-		return new InOutRulesBlockResult(cpp, visitor.functionSignatures, visitor.vars, visitor.getGlobalVars, visitor.constructorInitialization);
+		return new InOutRulesBlockResult(cpp, visitor.functionSignatures, visitor.getGlobalVars, visitor.constructorInitialization);
 	}
 
 	/*

+ 50 - 40
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/InOutRulesConditionSwitch.xtend

@@ -29,28 +29,46 @@ import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.LValueDeclarat
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Declaration
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.IsSet
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Div
+import org.eclipse.emf.common.util.EList
+import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.InOutRules
 
 abstract class InOutRulesConditionSwitch extends SemanticAdaptationSwitch<ReturnInformation> {
+	// Global in and out variables
+	protected var LinkedHashMap<String, GlobalInOutVariable> gVars = newLinkedHashMap();
 
-	protected var LinkedHashMap<String, Pair<SVType, Object>> globalVars = newLinkedHashMap();
-	protected var LinkedHashMap<String, GlobalInOutVariable> globalVars2 = newLinkedHashMap();
+	// Global params
 	protected var LinkedHashMap<String, GlobalInOutVariable> params;
-	private var Pair<SVType, Object> lastVal;
+
 	protected final String adaptationName;
+
 	protected final String adaptationClassName;
+
 	private Integer count = 0;
+
+	/** See the method {@link #createFunctionSignature(String, String)} and subclasses */
 	private final String functionPrefix;
+
 	protected List<String> functionSignatures = newArrayList();
+
+	/*
+	 * Intermediate variable used for referencing external FMU.
+	 * It is necessary because of parsing error
+	 */
 	protected String externalVariableOwner;
+
 	protected final LinkedHashMap<String, LinkedHashMap<String, MappedScalarVariable>> mSVars;
 	protected final LinkedHashMap<String, SAScalarVariable> SASVs;
+
 	protected boolean inRuleCondition;
 	protected boolean inRuleTransition;
 	protected boolean inRuleOutput;
-	protected String constructorInitialization = "";
 	protected boolean inControlRule;
+
+	// This is used for storing initialization information for global declarations.
+	protected String constructorInitialization = "";
+	// Flag to signal whether the declarations to be processed are global or local.
 	protected boolean globalDeclaration = false;
-// Add scope information to this.
+	// Add scope information to this.
 	protected var LinkedHashMap<String, SVType> localDeclarations = newLinkedHashMap();
 
 	new(
@@ -72,6 +90,12 @@ abstract class InOutRulesConditionSwitch extends SemanticAdaptationSwitch<Return
 	/*
 	 * UTILITY FUNCTIONS 
 	 */
+	override ReturnInformation defaultCase(EObject object) {
+		var retVal = new ReturnInformation();
+		retVal.code = '''[«object.class»]''';
+		return retVal;
+	}
+
 	private def Object convertTypeToObject(SVType type, Literal object) {
 		switch (type) {
 			case Real: {
@@ -88,9 +112,7 @@ abstract class InOutRulesConditionSwitch extends SemanticAdaptationSwitch<Return
 		}
 	}
 
-	public def getVars() { return this.globalVars; }
-
-	public def getGlobalVars() { return this.globalVars2; }
+	public def getGlobalVars() { return this.gVars; }
 
 	public def getConstructorInitialization() { return this.constructorInitialization; }
 
@@ -111,41 +133,34 @@ abstract class InOutRulesConditionSwitch extends SemanticAdaptationSwitch<Return
 	/*
 	 * COMPILATION FUNCTIONS
 	 */
-	override ReturnInformation caseOutRulesBlock(OutRulesBlock object) {
+	protected def ReturnInformation doSwitch(EList<Declaration> gVars, InOutRules object) {
+
 		this.globalDeclaration = true;
 
 		var retVal = new ReturnInformation();
 
 		// Get the global variables added to globalVars
-		for (gVar : object.globalOutVars) {
+		for (gVar : gVars) {
 			constructorInitialization += doSwitch(gVar).code;
 		}
+
+		this.globalDeclaration = false;
+
 		for (dataRule : object.eAllContents.toIterable.filter(DataRule)) {
 			this.incrementCount;
 			retVal.appendCode(doSwitch(dataRule).code);
 		}
-		this.globalDeclaration = false;
+
 		return retVal;
 	}
 
 	override ReturnInformation caseInRulesBlock(InRulesBlock object) {
-		this.globalDeclaration = true;
-
-		var retVal = new ReturnInformation();
-
-		// Get the global variables added to globalVars
-		for (gVar : object.globalInVars) {
-			constructorInitialization += doSwitch(gVar).code
-		}
+		return this.doSwitch(object.globalInVars, object);
+	}
 
-		for (DataRule dataRule : object.eAllContents.toIterable.filter(DataRule)) {
-			// This is used for naming each datarule
-			this.incrementCount;
-			retVal.appendCode(doSwitch(dataRule).code);
-		}
+	override ReturnInformation caseOutRulesBlock(OutRulesBlock object) {
 
-		this.globalDeclaration = false;
-		return retVal;
+		return this.doSwitch(object.globalOutVars, object);
 	}
 
 	override ReturnInformation caseDataRule(DataRule object) {
@@ -204,12 +219,6 @@ abstract class InOutRulesConditionSwitch extends SemanticAdaptationSwitch<Return
 		return retVal;
 	}
 
-	override ReturnInformation defaultCase(EObject object) {
-		var retVal = new ReturnInformation();
-		retVal.code = '''[«object.class»]''';
-		return retVal;
-	}
-
 	override ReturnInformation caseIf(If object) {
 		var retVal = new ReturnInformation();
 		retVal.code = '''
@@ -310,16 +319,16 @@ abstract class InOutRulesConditionSwitch extends SemanticAdaptationSwitch<Return
 		var retVal = new ReturnInformation();
 
 		if (object.owner === null || object.owner.name == this.adaptationName) {
-			retVal.code = '''this->«object.ref.name»''';
-			if (SASVs.containsKey(object.ref.name) || globalVars2.containsKey(object.ref.name) ||
+
+			if (SASVs.containsKey(object.ref.name) || gVars.containsKey(object.ref.name) ||
 				params.containsKey(object.ref.name)) {
-					
+
 				retVal.code = '''this->«object.ref.name»''';
-				
+
 				if (SASVs.containsKey(object.ref.name)) {
 					retVal.conSaSv = SASVs.get(object.ref.name);
-				} else if (globalVars2.containsKey(object.ref.name)) {
-					retVal.conGlobVar = globalVars2.get(object.ref.name);
+				} else if (gVars.containsKey(object.ref.name)) {
+					retVal.conGlobVar = gVars.get(object.ref.name);
 				} else if (params.containsKey(object.ref.name)) {
 					retVal.conGlobVar = params.get(object.ref.name);
 				}
@@ -327,8 +336,8 @@ abstract class InOutRulesConditionSwitch extends SemanticAdaptationSwitch<Return
 				retVal.code = '''«object.ref.name»'''
 				retVal.type = localDeclarations.get(object.ref.name);
 			}
+
 		} else {
-			// TODO: Extract the correct variable here
 			this.externalVariableOwner = object.owner.name;
 			retVal.code = '''«doSwitch(object.ref).code»''';
 		}
@@ -344,6 +353,7 @@ abstract class InOutRulesConditionSwitch extends SemanticAdaptationSwitch<Return
 
 	override ReturnInformation caseDeclaration(Declaration object) {
 		var retVal = new ReturnInformation();
+
 		for (SingleVarDeclaration decl : object.declarations) {
 			var doSwitchRes = doSwitch(decl.expr);
 			var String code = "";
@@ -358,7 +368,7 @@ abstract class InOutRulesConditionSwitch extends SemanticAdaptationSwitch<Return
 				globVar.value = doSwitchRes.value;
 				globVar.type = doSwitchRes.type;
 
-				globalVars2.put(decl.name, globVar);
+				gVars.put(decl.name, globVar);
 			} else {
 				// This is a local declaration.
 				val String type = Conversions.fmiTypeToCppType(doSwitchRes.type)

+ 10 - 10
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/OutRulesConditionSwitch.xtend

@@ -1,27 +1,27 @@
 package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.generation;
 
-import java.util.LinkedHashMap
+import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.GlobalInOutVariable
 import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.MappedScalarVariable
-import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.SAScalarVariable
 import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.ReturnInformation
+import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.SAScalarVariable
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Port
-import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.GlobalInOutVariable
+import java.util.LinkedHashMap
 
 public class OutRulesConditionSwitch extends InOutRulesConditionSwitch {
-	new(String adaptationClassName, 
-		String adaptationName,  
+	new(String adaptationClassName, String adaptationName,
 		LinkedHashMap<String, LinkedHashMap<String, MappedScalarVariable>> mSVars,
-		LinkedHashMap<String,SAScalarVariable> SASVs, LinkedHashMap<String, GlobalInOutVariable> params) {
+		LinkedHashMap<String, SAScalarVariable> SASVs, LinkedHashMap<String, GlobalInOutVariable> params) {
 		super(adaptationClassName, adaptationName, "out_rule_", mSVars, SASVs, params);
 	}
-	
-	override ReturnInformation casePort(Port object){
+
+	override ReturnInformation casePort(Port object) {
 		var retVal = new ReturnInformation();
-		
+
 		val type = mSVars.get(this.externalVariableOwner).get(object.name).mappedSv.type;
 		val define = mSVars.get(this.externalVariableOwner).get(object.name).define;
 		retVal.code = '''getValue«Conversions.fmiTypeToCppTypeCapitalized(type)»(«this.externalVariableOwner»,«define»)''';
-		
+
 		return retVal;
 	}
+
 }

+ 1 - 1
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/ParamConditionSwitch.xtend

@@ -35,7 +35,7 @@ class ParamConditionSwitch extends GeneralConditionSwitch {
 		val retInfo = new ReturnInformation();
 		retInfo.code = 
 		'''
-			«gVar.name» = «doSwitchRes.code»;
+			this->«gVar.name» = «doSwitchRes.code»;
 		'''
 		return retInfo;