Browse Source

Updated to match naming of fmu file

Casper Thule 3 years ago
parent
commit
926e939d7d

+ 19 - 3
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/tests/CgCppBasicTest.xtend

@@ -20,6 +20,10 @@ import org.eclipse.xtext.generator.InMemoryFileSystemAccess
 import org.eclipse.xtext.generator.IGeneratorContext
 import org.eclipse.emf.ecore.resource.ResourceSet
 import org.junit.Ignore
+import java.io.File
+import java.io.PrintWriter
+import java.nio.file.Files
+import java.io.FileWriter
 
 @RunWith(XtextRunner)
 @InjectWith(SemanticAdaptationInjectorProvider)
@@ -42,9 +46,21 @@ class CgCppBasicTest extends AbstractSemanticAdaptationTest {
 		
 		for(files : fsa.allFiles.entrySet)
 		{
-			System.out.println("########################")
-			System.out.println("Filename: " + files.key.substring(14))
-			System.out.println(files.value)
+//			System.out.println("########################")
+//			System.out.println("Filename: " + files.key.substring(14))
+//			System.out.println(files.value)
+			var path = new File("generated");
+			if(path.exists)
+				path.delete
+			else
+				path.mkdir;
+				
+			path = new File(path, files.key.substring(14))	
+			
+			val FileWriter writer = new FileWriter(path);
+			writer.write(files.value.toString);
+			writer.close;
+			System.out.println("Stored file: " + files.key.substring(14) + " at: " + path.absolutePath);
 		}
 		//System.out.println(fsa.allFiles)		
 	}

+ 212 - 210
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/CppGenerator.xtend

@@ -36,7 +36,7 @@ class CppGenerator extends SemanticAdaptationGenerator {
 		for (Adaptation type : adaptation.elements.filter(Adaptation)) {
 
 			var LinkedHashMap<String, Pair<String, Integer>> svDefs = newLinkedHashMap();
-			var ArrayList<Pair<String,String>> fmus = newArrayList();
+			var ArrayList<Pair<String, String>> fmus = newArrayList();
 			var LinkedHashMap<String, ScalarVariable> sVars = newLinkedHashMap();
 			var String genSource = "";
 			var ModelDescription md;
@@ -70,7 +70,8 @@ class CppGenerator extends SemanticAdaptationGenerator {
 
 			// Compile the includes and constructor 
 			val String include = '''#include "«type.name».h"''';
-			val String constructor = compileConstructor(type.name, outRuleResult, inRuleResult, sVars, fmus.head.key, md.guid);
+			val String constructor = compileConstructor(type.name, outRuleResult, inRuleResult, sVars, fmus.head.key, fmus.head.value,
+				md.guid);
 			val String getRuleThis = compileGetRuleThis(type.name);
 			genSource = include + "\n\n" + constructor + "\n\n" + getRuleThis + "\n\n" + genSource;
 
@@ -96,259 +97,260 @@ class CppGenerator extends SemanticAdaptationGenerator {
 	}
 
 	def String compileHeader(String name, InOutRulesBlockResult inRulesResult, InOutRulesBlockResult outRulesResult,
-		RulesBlockResult crtlRulesResult, ArrayList<Pair<String,String>> fmus, LinkedHashMap<String, ScalarVariable> sVars) {
-		return '''
-			#include "SemanticAdaptation.h"
-			#include <memory>
-			#include "Fmu.h"
-			
-			using namespace std;
-			using namespace "Fmu.h"
-			
-			class «name» : public SemanticAdaptation<«name»>{
-				public:
-					«name»();
-					virtual ~«name»();
-					
-					void setFmiValue(int id, int value);
-					void setFmiValue(int id, bool value);
-					void setFmiValue(int id, double value);
+		RulesBlockResult crtlRulesResult, ArrayList<Pair<String, String>> fmus,
+		LinkedHashMap<String, ScalarVariable> sVars) {
+			return '''
+				#include "SemanticAdaptation.h"
+				#include <memory>
+				#include "Fmu.h"
 				
-					int getFmiValueInteger(int id);
-					bool getFmiValueBoolean(int id);
-					double getFmiValueDouble(int id);
-				private:
-					
-					«name»* getRuleThis();
-					
-					/*in rules*/
-					«inRulesResult.functionSignatures.join("\n")»
-					
-					/*out rules*/
-					«outRulesResult.functionSignatures.join("\n")»
-					
-					«crtlRulesResult.functionSignatures.join("\n")»
+				using namespace std;
+				using namespace fmi2
+				
+				class «name» : public SemanticAdaptation<«name»>{
+					public:
+						«name»();
+						virtual ~«name»();
+						
+						void setFmiValue(fmi2ValueReference id, int value);
+						void setFmiValue(fmi2ValueReference id, bool value);
+						void setFmiValue(fmi2ValueReference id, double value);
 					
-					«FOR fmu : fmus»
-						shared_ptr<Fmu> «fmu.key»;
+						int getFmiValueInteger(fmi2ValueReference id);
+						bool getFmiValueBoolean(fmi2ValueReference id);
+						double getFmiValueDouble(fmi2ValueReference id);
+					private:
+						
+						«name»* getRuleThis();
+						
+						/*in rules*/
+						«inRulesResult.functionSignatures.join("\n")»
+						
+						/*out rules*/
+						«outRulesResult.functionSignatures.join("\n")»
+						
+						«crtlRulesResult.functionSignatures.join("\n")»
+						
+						«FOR fmu : fmus»
+							shared_ptr<Fmu> «fmu.key»;
+						«ENDFOR»
+						
+						«FOR sv : sVars.entrySet»
+							«Conversions.fmiTypeToCppType(sv.value.type)» «sv.value.name»;
+							«IF sv.value.causality == SVCausality.input»
+								bool isSet«sv.value.name»;								
+							«ENDIF»
+						«ENDFOR»
+						
+						«FOR v : outRulesResult.globalVars.entrySet»
+							«Conversions.fmiTypeToCppType(v.value.key)» «v.key»;
+						«ENDFOR»
+						
+						«FOR v : inRulesResult.globalVars.entrySet»
+							«Conversions.fmiTypeToCppType(v.value.key)» «v.key»;
+						«ENDFOR»
+				}
+			''';
+		}
+
+		def compileConstructor(String name, InOutRulesBlockResult outRuleResult, InOutRulesBlockResult inRuleResult,
+			LinkedHashMap<String, ScalarVariable> sVars, String fmuName, String fmuTypeName, String guid) {
+			return '''
+				«name»::«name»(shared_ptr<std::string> resourceLocation) : SemanticAdaptation(createInputRules(),createOutputRules())
+				{
+					«FOR v : outRuleResult.globalVars.entrySet»
+						«(v.key)» = «v.value.value»;
 					«ENDFOR»
 					
-					«FOR sv : sVars.entrySet»
-						«Conversions.fmiTypeToCppType(sv.value.type)» «sv.value.name»;
-						«IF sv.value.causality == SVCausality.input»
-							bool isSet«sv.value.name»;								
-						«ENDIF»
+					«FOR v : inRuleResult.globalVars.entrySet»
+						this->«(v.key)» = «v.value.value»;
 					«ENDFOR»
 					
-					«FOR v : outRulesResult.globalVars.entrySet»
-						«Conversions.fmiTypeToCppType(v.value.key)» «v.key»;
+					«FOR v : sVars.entrySet»
+						«IF v.value.start !== null»
+							this->«(v.key)» = «v.value.start»;
+						«ENDIF»
 					«ENDFOR»
 					
-					«FOR v : inRulesResult.globalVars.entrySet»
-						«Conversions.fmiTypeToCppType(v.value.key)» «v.key»;
-					«ENDFOR»
-			}
-		''';
-	}
-
-	def compileConstructor(String name, InOutRulesBlockResult outRuleResult, InOutRulesBlockResult inRuleResult,
-		LinkedHashMap<String, ScalarVariable> sVars, String fmu, String guid) {
-		return '''
-			«name»::«name»(shared_ptr<std::string> resourceLocation) : SemanticAdaptation(createInputRules(),createOutputRules())
-			{
-				«FOR v : outRuleResult.globalVars.entrySet»
-					«(v.key)» = «v.value.value»;
-				«ENDFOR»
-				
-				«FOR v : inRuleResult.globalVars.entrySet»
-					this->«(v.key)» = «v.value.value»;
-				«ENDFOR»
-				
-				«FOR v : sVars.entrySet»
-					«IF v.value.start !== null»
-						this->«(v.key)» = «v.value.start»;
-					«ENDIF»
-				«ENDFOR»
-				
-				const char* path = Fmu::combinePath(resourceLocation, make_shared<string>("«fmu».fmu"))->c_str();
-				auto «fmu»Fmu = make_shared<fmi2::Fmu>(path);
-				«fmu»Fmu->initialize();
-				this->«fmu» = «fmu»Fmu->instantiate("«fmu»",fmi2CoSimulation, "«guid»", true, true, make_shared<Callback>()); 
-			}
-		''';
-	}
+					const char* path = Fmu::combinePath(resourceLocation, make_shared<string>("«fmuTypeName».fmu"))->c_str();
+					auto «fmuName»Fmu = make_shared<fmi2::Fmu>(path);
+					«fmuName»Fmu->initialize();
+					this->«fmuName» = «fmuName»Fmu->instantiate("«fmuTypeName»",fmi2CoSimulation, "«guid»", true, true, make_shared<Callback>()); 
+				}
+			''';
+		}
 
-	def compileGetRuleThis(String name)
-	{
-		return 
-			'''
+		def compileGetRuleThis(String name) {
+			return '''
 				«name»* «name»::getRuleThis()
 				{
 					return this;
 				}
 			'''
-	}
+		}
 
-	def compileGetFmuValueCppFuncSig(String adaptationName, SVType type) {
-		return '''«Conversions.fmiTypeToCppType(type)»  «adaptationName»::getFmiValue«Conversions.fmiTypeToCppTypeName(type)»(int id)''';
-	}
+		def compileGetFmuValueCppFuncSig(String adaptationName, SVType type) {
+			return '''«Conversions.fmiTypeToCppType(type)»  «adaptationName»::getFmiValue«Conversions.fmiTypeToCppTypeName(type)»(fmi2ValueReference id)''';
+		}
 
-	def compileGetFmuValueCppFuncReturn(SVType type) {
-		return '''return «Conversions.fmiTypeToCppDefaultValue(type)»''';
-	}
+		def compileGetFmuValueCppFuncReturn(SVType type) {
+			return '''return «Conversions.fmiTypeToCppDefaultValue(type)»''';
+		}
 
-	def compileGetFmuValue(String adaptationName, LinkedHashMap<String, ScalarVariable> sVars, LinkedHashMap<String, Pair<String, Integer>> sVarDefs) {
-		var ArrayList<String> cpp = newArrayList();
-		var sVarsOrdered = sVars.entrySet.filter[value.causality === SVCausality.output].groupBy[value.type];
+		def compileGetFmuValue(String adaptationName, LinkedHashMap<String, ScalarVariable> sVars,
+			LinkedHashMap<String, Pair<String, Integer>> sVarDefs) {
+			var ArrayList<String> cpp = newArrayList();
+			var sVarsOrdered = sVars.entrySet.filter[value.causality === SVCausality.output].groupBy[value.type];
 
-		for (SVType type : SVType.values) {
-			if (sVarsOrdered.containsKey(type)) {
-				cpp.add(
-					'''
-						«compileGetFmuValueCppFuncSig(adaptationName,type)»
-						{
-							switch (id)
+			for (SVType type : SVType.values) {
+				if (sVarsOrdered.containsKey(type)) {
+					cpp.add(
+						'''
+							«compileGetFmuValueCppFuncSig(adaptationName,type)»
 							{
-								«FOR svInner : sVarsOrdered.get(type)»
-									case «sVarDefs.get(svInner.value.owner + svInner.value.name).key»:
+								switch (id)
+								{
+									«FOR svInner : sVarsOrdered.get(type)»
+										case «sVarDefs.get(svInner.value.owner + svInner.value.name).key»:
+										{
+											return this->«svInner.key»;
+										}
+									«ENDFOR»
+									default:
 									{
-										return this->«svInner.key»;
+										«compileGetFmuValueCppFuncReturn(type)»;
 									}
-								«ENDFOR»
-								default:
-								{
-									«compileGetFmuValueCppFuncReturn(type)»;
 								}
+								
 							}
-							
-						}
-					'''
-				);
-			} else {
-				cpp.add(
-					'''
-						«compileGetFmuValueCppFuncSig(adaptationName,type)»
-						{
-							«compileGetFmuValueCppFuncReturn(type)»;
-						}
-					'''
-				);
+						'''
+					);
+				} else {
+					cpp.add(
+						'''
+							«compileGetFmuValueCppFuncSig(adaptationName,type)»
+							{
+								«compileGetFmuValueCppFuncReturn(type)»;
+							}
+						'''
+					);
+				}
 			}
-		}
 
-		return cpp.join("\n");
-	}
+			return cpp.join("\n");
+		}
 
-	def compileSetFmuValueCppFuncSig(String adaptationName, SVType type) {
-		return '''«Conversions.fmiTypeToCppType(type)»  «adaptationName»::getFmiValue«Conversions.fmiTypeToCppTypeName(type)»(int id)''';
-	}
+		def compileSetFmuValueCppFuncSig(String adaptationName, SVType type) {
+			return '''«Conversions.fmiTypeToCppType(type)»  «adaptationName»::getFmiValue«Conversions.fmiTypeToCppTypeName(type)»(fmi2ValueReference id)''';
+		}
 
-	def compileSetFmuValue(String adaptationName, LinkedHashMap<String, ScalarVariable> sVars, LinkedHashMap<String, Pair<String, Integer>> sVarDefs) {
-		var ArrayList<String> cpp = newArrayList();
-		var sVarsOrdered = sVars.entrySet.filter[value.causality === SVCausality.input].groupBy[value.type];
+		def compileSetFmuValue(String adaptationName, LinkedHashMap<String, ScalarVariable> sVars,
+			LinkedHashMap<String, Pair<String, Integer>> sVarDefs) {
+			var ArrayList<String> cpp = newArrayList();
+			var sVarsOrdered = sVars.entrySet.filter[value.causality === SVCausality.input].groupBy[value.type];
 
-		for (SVType type : SVType.values) {
+			for (SVType type : SVType.values) {
 
-			cpp.add(
-				'''
-					void «adaptationName»::setFmiValue(int id, «Conversions.fmiTypeToCppType(type)» value)
-					{
-						«IF sVarsOrdered.containsKey(type)»
-							switch (id)	
-								{
-									«FOR svInner : sVarsOrdered.get(type)»
-										case «sVarDefs.get(svInner.value.owner + svInner.value.name).key»:
+				cpp.add(
+					'''
+						void «adaptationName»::setFmiValue(fmi2ValueReference id, «Conversions.fmiTypeToCppType(type)» value)
+						{
+							«IF sVarsOrdered.containsKey(type)»
+								switch (id)	
+									{
+										«FOR svInner : sVarsOrdered.get(type)»
+											case «sVarDefs.get(svInner.value.owner + svInner.value.name).key»:
+											{
+												this->«svInner.key» = value;
+												this->isSet«svInner.key» = true;
+												break;
+											}
+										«ENDFOR»
+										default:
 										{
-											this->«svInner.key» = value;
-											this->isSet«svInner.key» = true;
-											break;
 										}
-									«ENDFOR»
-									default:
-									{
 									}
-								}
-						«ENDIF»
-					}
-				'''
-			);
+							«ENDIF»
+						}
+					'''
+				);
 
+			}
+
+			return cpp.join("\n");
 		}
 
-		return cpp.join("\n");
-	}
+		def RulesBlockResult compileControlRuleBlock(Iterable<ControlRuleBlock> crtlRuleBlocks, String name,
+			LinkedHashMap<String, Pair<String, Integer>> svDefs) {
+			var cpp = "";
+			val visitor = new ControlConditionSwitch(name, svDefs);
+			for (crtlRule : crtlRuleBlocks) {
+				cpp += visitor.doSwitch(crtlRule);
+			}
 
-	def RulesBlockResult compileControlRuleBlock(Iterable<ControlRuleBlock> crtlRuleBlocks, String name,
-		LinkedHashMap<String, Pair<String, Integer>> svDefs) {
-		var cpp = "";
-		val visitor = new ControlConditionSwitch(name, svDefs);
-		for (crtlRule : crtlRuleBlocks) {
-			cpp += visitor.doSwitch(crtlRule);
+			return new RulesBlockResult(cpp, visitor.functionSignatures);
 		}
 
-		return new RulesBlockResult(cpp, visitor.functionSignatures);
-	}
-
-	def InOutRulesBlockResult compileOutRuleBlock(Iterable<OutRulesBlock> rulesBlocks,
-		LinkedHashMap<String, Pair<String, Integer>> svDefs, String name) {
-		return compileInOutRuleBlocks(new OutRulesConditionSwitch(name, svDefs), rulesBlocks.map[x|x as InOutRules],
-			svDefs, name, "createOutputRules");
-	}
+		def InOutRulesBlockResult compileOutRuleBlock(Iterable<OutRulesBlock> rulesBlocks,
+			LinkedHashMap<String, Pair<String, Integer>> svDefs, String name) {
+			return compileInOutRuleBlocks(new OutRulesConditionSwitch(name, svDefs), rulesBlocks.map[x|x as InOutRules],
+				svDefs, name, "createOutputRules");
+		}
 
-	def InOutRulesBlockResult compileInRuleBlock(Iterable<InRulesBlock> rulesBlocks,
-		LinkedHashMap<String, Pair<String, Integer>> svDefs, String name) {
-		return compileInOutRuleBlocks(new InRulesConditionSwitch(name, svDefs), rulesBlocks.map[x|x as InOutRules],
-			svDefs, name, "createInputRules");
-	}
+		def InOutRulesBlockResult compileInRuleBlock(Iterable<InRulesBlock> rulesBlocks,
+			LinkedHashMap<String, Pair<String, Integer>> svDefs, String name) {
+			return compileInOutRuleBlocks(new InRulesConditionSwitch(name, svDefs), rulesBlocks.map[x|x as InOutRules],
+				svDefs, name, "createInputRules");
+		}
 
-	def InOutRulesBlockResult compileInOutRuleBlocks(InOutRulesConditionSwitch visitor,
-		Iterable<InOutRules> rulesBlocks, LinkedHashMap<String, Pair<String, Integer>> svDefs, String name,
-		String functionName) {
-			var String cpp = "";
-			val ruleBlock = rulesBlocks.head;
-			if (ruleBlock !== null) {
-				cpp += visitor.doSwitch(ruleBlock);
-				if (!visitor.functionSignatures.empty) {
-					var ArrayList<String> createRulesFunction = newArrayList();
-					for (var int i = 0; i < (visitor.functionSignatures.length); i += 3) {
-						createRulesFunction.add( 
+		def InOutRulesBlockResult compileInOutRuleBlocks(InOutRulesConditionSwitch visitor,
+			Iterable<InOutRules> rulesBlocks, LinkedHashMap<String, Pair<String, Integer>> svDefs, String name,
+			String functionName) {
+				var String cpp = "";
+				val ruleBlock = rulesBlocks.head;
+				if (ruleBlock !== null) {
+					cpp += visitor.doSwitch(ruleBlock);
+					if (!visitor.functionSignatures.empty) {
+						var ArrayList<String> createRulesFunction = newArrayList();
+						for (var int i = 0; i < (visitor.functionSignatures.length); i += 3) {
+							createRulesFunction.add( 
 					'''
-							list->push_back(
-								(Rule<«name»>){
-									&«name»::«visitor.functionSignatures.get(i)»,
-									&«name»::«visitor.functionSignatures.get(i+1)»
-									&«name»::«visitor.functionSignatures.get(i+2)»
-								});
+								list->push_back(
+									(Rule<«name»>){
+										&«name»::«visitor.functionSignatures.get(i)»,
+										&«name»::«visitor.functionSignatures.get(i+1)»
+										&«name»::«visitor.functionSignatures.get(i+2)»
+									});
+								
+							''');
+						}
+						val functionPrefix = '''shared_ptr<list<Rule<«name»>>>''';
+						visitor.functionSignatures.add(functionPrefix + " " + functionName + ";")
+						cpp += '''
+							«functionPrefix» «name»::«functionName»()
+							{
+								auto list = make_shared<list<Rule<«name»>>>()
+								
+								«createRulesFunction.join("\n")»
+								
+								return list;
+								
+							}
 							
-						''');
+						'''
 					}
-					val functionPrefix = '''shared_ptr<list<Rule<«name»>>>''';
-					visitor.functionSignatures.add(functionPrefix + " " + functionName + ";")
-					cpp += '''
-						«functionPrefix» «name»::«functionName»()
-						{
-							auto list = make_shared<list<Rule<«name»>>>()
-							
-							«createRulesFunction.join("\n")»
-							
-							return list;
-							
-						}
-						
-					'''
 				}
+				return new InOutRulesBlockResult(cpp, visitor.functionSignatures, visitor.globalVars);
 			}
-			return new InOutRulesBlockResult(cpp, visitor.functionSignatures, visitor.globalVars);
-		}
 
-	def calcDefines(LinkedHashMap<String, Pair<String, Integer>> svDefs) {
-			// Create Defines for the scalar values
-			var defines = newArrayList();
-			for (scalar : svDefs.entrySet) {
-				val definition = scalar.value;
-				defines.add("#define " + definition.key + " " + definition.value);
+			def calcDefines(LinkedHashMap<String, Pair<String, Integer>> svDefs) {
+				// Create Defines for the scalar values
+				var defines = newArrayList();
+				for (scalar : svDefs.entrySet) {
+					val definition = scalar.value;
+					defines.add("#define " + definition.key + " " + definition.value);
+				}
+				return defines;
 			}
-			return defines;
 		}
-	}
-	
+