Browse Source

Updated static generators

Casper Thule 3 years ago
parent
commit
d37481cde1

+ 197 - 176
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/StaticGenerators.xtend

@@ -4,6 +4,7 @@ class StaticGenerators {
 	def static String GenFmuCppFile(String adapClassName)
 	{
 		'''
+		#include <stdio.h>
 		#include "fmi2Functions.h"
 		#include <string>
 		
@@ -19,45 +20,53 @@ class StaticGenerators {
 		shared_ptr<adaptation::«adapClassName»> g_adaptation;
 		
 		template<class T>
-		static void log(const fmi2CallbackFunctions *functions,
-				fmi2ComponentEnvironment componentEnvironment, fmi2String instanceName,
-				fmi2Status status, fmi2String category, fmi2String message, T arg) {
-			if (functions != NULL && functions->logger != NULL) {
-				functions->logger(componentEnvironment, instanceName, status, category,
-						message, arg);
+		static void log(const fmi2CallbackFunctions *functions, fmi2ComponentEnvironment componentEnvironment,
+				fmi2String instanceName, fmi2Status status, fmi2String category, fmi2String message, T arg)
+		{
+			if (functions != NULL && functions->logger != NULL)
+			{
+				functions->logger(componentEnvironment, instanceName, status, category, message, arg);
 			}
 		}
 		
-		static void notimplemented(fmi2Component c, fmi2String message) {
+		static void notimplemented(fmi2Component c, fmi2String message)
+		{
 			std::string base("Not implemented: %s");
 			std::string m(message);
-			if (g_functions != NULL) {
-				log(g_functions, (void*) 2, "", fmi2Error, "error", (base + m).c_str(),
-						"");
+			if (g_functions != NULL)
+			{
+				log(g_functions, (void*) 2, "", fmi2Error, "error", (base + m).c_str(), "");
 			}
 		}
 		
 		template<class T>
-		static void fmiprintf(fmi2String message, T arg) {
-			if (g_functions != NULL) {
-				log(g_functions, (void*) 2, name->c_str(), fmi2OK, "logAll", message,
-						arg);
+		static void fmiprintf(fmi2String message, T arg)
+		{
+			if (g_functions != NULL)
+			{
+				log(g_functions, (void*) 2, name->c_str(), fmi2OK, "logAll", message, arg);
 			}
 		}
 		
 		// ---------------------------------------------------------------------------
 		// FMI functions
 		// ---------------------------------------------------------------------------
-		extern "C" fmi2Component fmi2Instantiate(fmi2String instanceName,
-				fmi2Type fmuType, fmi2String fmuGUID, fmi2String fmuResourceLocation,
-				const fmi2CallbackFunctions *functions, fmi2Boolean visible,
-				fmi2Boolean loggingOn) {
+		extern "C" fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2String fmuGUID,
+				fmi2String fmuResourceLocation, const fmi2CallbackFunctions *functions, fmi2Boolean visible,
+				fmi2Boolean loggingOn)
+		{
 			name = new std::string(instanceName);
 			g_functions = functions;
 			fmiprintf("instantiating %s\n", instanceName);
 			auto resourceLoc = make_shared<std::string>(fmuResourceLocation);
-			g_adaptation = make_shared<adaptation::«adapClassName»>(resourceLoc,functions);
-			g_adaptation->initialize();
+			g_adaptation = make_shared<adaptation::Window_SA>(resourceLoc, functions);
+			try
+			{
+				g_adaptation->initialize();
+			} catch (adaptation::SemanticAdaptationFmiException& e)
+			{
+				return NULL;
+			}
 		
 			if (g_adaptation->getLastErrorState() != fmi2OK)
 				return NULL;
@@ -65,31 +74,59 @@ class StaticGenerators {
 			return g_adaptation->getComponent();
 		}
 		
-		extern "C" fmi2Status fmi2SetupExperiment(fmi2Component c,
-				fmi2Boolean toleranceDefined, fmi2Real tolerance, fmi2Real startTime,
-				fmi2Boolean stopTimeDefined, fmi2Real stopTime) {
+		extern "C" fmi2Status fmi2SetupExperiment(fmi2Component c, fmi2Boolean toleranceDefined, fmi2Real tolerance,
+				fmi2Real startTime, fmi2Boolean stopTimeDefined, fmi2Real stopTime)
+		{
 		
-			return g_adaptation->fmi2SetupExperiment(toleranceDefined, tolerance,
-					startTime, stopTimeDefined, stopTime);
+			try
+			{
+				return g_adaptation->fmi2SetupExperiment(toleranceDefined, tolerance, startTime, stopTimeDefined, stopTime);
+			} catch (adaptation::SemanticAdaptationFmiException& e)
+			{
+				return e.status;
+			}
 		}
 		
-		extern "C" fmi2Status fmi2EnterInitializationMode(fmi2Component c) {
-			return g_adaptation->fmi2EnterInitializationMode();
+		extern "C" fmi2Status fmi2EnterInitializationMode(fmi2Component c)
+		{
+			try
+			{
+				return g_adaptation->fmi2EnterInitializationMode();
+			} catch (adaptation::SemanticAdaptationFmiException& e)
+			{
+				return e.status;
+			}
 		}
 		
-		extern "C" fmi2Status fmi2ExitInitializationMode(fmi2Component c) {
-			return g_adaptation->fmi2ExitInitializationMode();
+		extern "C" fmi2Status fmi2ExitInitializationMode(fmi2Component c)
+		{
+			try
+			{
+				return g_adaptation->fmi2ExitInitializationMode();
+			} catch (adaptation::SemanticAdaptationFmiException& e)
+			{
+				return e.status;
+			}
 		}
 		
-		extern "C" fmi2Status fmi2Terminate(fmi2Component c) {
-			return g_adaptation->fmi2Terminate();
+		extern "C" fmi2Status fmi2Terminate(fmi2Component c)
+		{
+			try
+			{
+				return g_adaptation->fmi2Terminate();
+			} catch (adaptation::SemanticAdaptationFmiException& e)
+			{
+				return e.status;
+			}
 		}
 		
-		extern "C" fmi2Status fmi2Reset(fmi2Component c) {
+		extern "C" fmi2Status fmi2Reset(fmi2Component c)
+		{
 			return fmi2OK;
 		}
 		
-		extern "C" void fmi2FreeInstance(fmi2Component c) {
+		extern "C" void fmi2FreeInstance(fmi2Component c)
+		{
 		
 		}
 		
@@ -97,11 +134,13 @@ class StaticGenerators {
 		// FMI functions: class methods not depending of a specific model instance
 		// ---------------------------------------------------------------------------
 		
-		extern "C" const char* fmi2GetVersion() {
+		extern "C" const char* fmi2GetVersion()
+		{
 			return fmi2Version;
 		}
 		
-		extern "C" const char* fmi2GetTypesPlatform() {
+		extern "C" const char* fmi2GetTypesPlatform()
+		{
 			return fmi2TypesPlatform;
 		}
 		
@@ -110,172 +149,148 @@ class StaticGenerators {
 		// Boolean, String
 		// ---------------------------------------------------------------------------
 		
-		extern "C" fmi2Status fmi2SetDebugLogging(fmi2Component c,
-				fmi2Boolean loggingOn, size_t nCategories,
-				const fmi2String categories[]) {
+		extern "C" fmi2Status fmi2SetDebugLogging(fmi2Component c, fmi2Boolean loggingOn, size_t nCategories,
+				const fmi2String categories[])
+		{
 			return fmi2OK;
 		}
 		
-		extern "C" fmi2Status fmi2GetReal(fmi2Component c,
-				const fmi2ValueReference vr[], size_t nvr, fmi2Real value[]) {
-			fmi2Status status = g_adaptation->executeOutRules();
-			if (status != fmi2OK)
-				return status;
-		
-			status = g_adaptation->flushAllEnabledOutRules();
-			if (status != fmi2OK)
-				return status;
-		
-			for (int i = 0; i < nvr; i++) {
-				value[i] = g_adaptation->getFmiValueReal(vr[i]);
-		
-				status = g_adaptation->getLastErrorState();
+		template<class T>
+		fmi2Status getX(const fmi2ValueReference vr[], size_t nvr, T value[],
+				T (adaptation::Window_SA::*getter)(fmi2ValueReference vr))
+		{
+			try
+			{
+				fmi2Status status = g_adaptation->executeOutRules();
 				if (status != fmi2OK)
 					return status;
-			}
-		
-			return fmi2OK;
-		}
 		
-		extern "C" fmi2Status fmi2GetInteger(fmi2Component c,
-				const fmi2ValueReference vr[], size_t nvr, fmi2Integer value[]) {
-		
-			fmi2Status status = g_adaptation->executeOutRules();
-			if (status != fmi2OK)
-				return status;
-			status = g_adaptation->flushAllEnabledOutRules();
-			if (status != fmi2OK)
-				return status;
-		
-			for (int i = 0; i < nvr; i++) {
-				value[i] = g_adaptation->getFmiValueInteger(vr[i]);
-		
-				status = g_adaptation->getLastErrorState();
+				status = g_adaptation->flushAllEnabledOutRules();
 				if (status != fmi2OK)
 					return status;
-			}
 		
-			return fmi2OK;
-		}
-		
-		extern "C" fmi2Status fmi2GetBoolean(fmi2Component c,
-				const fmi2ValueReference vr[], size_t nvr, fmi2Boolean value[]) {
-		
-			fmi2Status status = g_adaptation->executeOutRules();
-			if (status != fmi2OK)
-				return status;
-			status = g_adaptation->flushAllEnabledOutRules();
-			if (status != fmi2OK)
-				return status;
+				for (int i = 0; i < nvr; i++)
+				{
+					value[i] = ((*g_adaptation).*getter)(vr[i]);
 		
-			for (int i = 0; i < nvr; i++) {
-				value[i] = g_adaptation->getFmiValueBoolean(vr[i]);
+					status = g_adaptation->getLastErrorState();
+					if (status != fmi2OK)
+						return status;
+				}
 		
-				status = g_adaptation->getLastErrorState();
-				if (status != fmi2OK)
-					return status;
+				return fmi2OK;
+			} catch (adaptation::SemanticAdaptationFmiException& e)
+			{
+				return e.status;
 			}
-		
-			return fmi2OK;
 		}
 		
-		extern "C" fmi2Status fmi2GetString(fmi2Component c,
-				const fmi2ValueReference vr[], size_t nvr, fmi2String value[]) {
-		
-			fmi2Status status = g_adaptation->executeOutRules();
-			if (status != fmi2OK)
-				return status;
-			status = g_adaptation->flushAllEnabledOutRules();
-			if (status != fmi2OK)
-				return status;
+		extern "C" fmi2Status fmi2GetReal(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Real value[])
+		{
+			return getX(vr, nvr, value, &adaptation::Window_SA::getFmiValueReal);
+		}
 		
-			return fmi2OK;
+		extern "C" fmi2Status fmi2GetInteger(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Integer value[])
+		{
+			return getX(vr, nvr, value, &adaptation::Window_SA::getFmiValueInteger);
 		}
 		
-		extern "C" fmi2Status fmi2SetReal(fmi2Component c,
-				const fmi2ValueReference vr[], size_t nvr, const fmi2Real value[]) {
-			fmi2Status status = fmi2OK;
-			for (int i = 0; i < nvr; i++) {
-		//		g_functions->logger((void*) 2, name->c_str(), fmi2OK, "logAll", "setting real vr=%d, value: %f\n", vr[i], value[i]);
-				g_adaptation->setFmiValue(vr[i], value[i]);
+		extern "C" fmi2Status fmi2GetBoolean(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Boolean value[])
+		{
+			typedef bool (adaptation::Window_SA::*GB)(fmi2ValueReference);
+			typedef int (adaptation::Window_SA::*GI)(fmi2ValueReference);
 		
-				status = g_adaptation->getLastErrorState();
-				if (status != fmi2OK)
-					return status;
-			}
+			GB g = &adaptation::Window_SA::getFmiValueBoolean;
+			GI i = reinterpret_cast<GI>(g);
 		
-		//	g_functions->logger((void*) 2, name->c_str(), fmi2OK, "logAll", "setting real executing in rules");
-			status = g_adaptation->executeInRules();
-			return status;
+			return getX(vr, nvr, value, i);
 		}
 		
-		extern "C" fmi2Status fmi2SetInteger(fmi2Component c,
-				const fmi2ValueReference vr[], size_t nvr, const fmi2Integer value[]) {
-			fmi2Status status = fmi2OK;
-			for (int i = 0; i < nvr; i++) {
-				g_adaptation->setFmiValue(vr[i], value[i]);
+		extern "C" fmi2Status fmi2GetString(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2String value[])
+		{
+			return fmi2Discard;
+		}
 		
-				status = g_adaptation->getLastErrorState();
-				if (status != fmi2OK)
-					return status;
+		template<class T>
+		fmi2Status setX(const fmi2ValueReference vr[], size_t nvr, T value[])
+		{
+			try
+			{
+				fmi2Status status = fmi2OK;
+				for (int i = 0; i < nvr; i++)
+				{
+					g_adaptation->setFmiValue(vr[i], value[i]);
+		
+					status = g_adaptation->getLastErrorState();
+					if (status != fmi2OK)
+						return status;
+				}
+				status = g_adaptation->executeInRules();
+				return status;
+			} catch (adaptation::SemanticAdaptationFmiException& e)
+			{
+				return e.status;
 			}
-		
-			status = g_adaptation->executeInRules();
-			return status;
 		}
 		
-		extern "C" fmi2Status fmi2SetBoolean(fmi2Component c,
-				const fmi2ValueReference vr[], size_t nvr, const fmi2Boolean value[]) {
-			fmi2Status status = fmi2OK;
-			for (int i = 0; i < nvr; i++) {
-				g_adaptation->setFmiValue(vr[i], value[i]);
+		extern "C" fmi2Status fmi2SetReal(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Real value[])
+		{
+			return setX(vr, nvr, value);
+		}
 		
-				status = g_adaptation->getLastErrorState();
-				if (status != fmi2OK)
-					return status;
-			}
-			status = g_adaptation->executeInRules();
-			return status;
+		extern "C" fmi2Status fmi2SetInteger(fmi2Component c, const fmi2ValueReference vr[], size_t nvr,
+				const fmi2Integer value[])
+		{
+			return setX(vr, nvr, value);
 		}
 		
-		extern "C" fmi2Status fmi2SetString(fmi2Component c,
-				const fmi2ValueReference vr[], size_t nvr, const fmi2String value[]) {
+		extern "C" fmi2Status fmi2SetBoolean(fmi2Component c, const fmi2ValueReference vr[], size_t nvr,
+				const fmi2Boolean value[])
+		{
+			return setX(vr, nvr, value);
+		}
 		
-			fmi2Status status = g_adaptation->executeInRules();
-			return status;
+		extern "C" fmi2Status fmi2SetString(fmi2Component c, const fmi2ValueReference vr[], size_t nvr,
+				const fmi2String value[])
+		{
+			return fmi2Discard;
 		}
 		
-		extern "C" fmi2Status fmi2GetFMUstate(fmi2Component c, fmi2FMUstate* FMUstate) {
+		extern "C" fmi2Status fmi2GetFMUstate(fmi2Component c, fmi2FMUstate* FMUstate)
+		{
 			return fmi2OK;
 		}
-		extern "C" fmi2Status fmi2SetFMUstate(fmi2Component c, fmi2FMUstate FMUstate) {
+		extern "C" fmi2Status fmi2SetFMUstate(fmi2Component c, fmi2FMUstate FMUstate)
+		{
 			return fmi2OK;
 		}
-		extern "C" fmi2Status fmi2FreeFMUstate(fmi2Component c,
-				fmi2FMUstate* FMUstate) {
+		extern "C" fmi2Status fmi2FreeFMUstate(fmi2Component c, fmi2FMUstate* FMUstate)
+		{
 			return fmi2OK;
 		}
 		
-		extern "C" fmi2Status fmi2SerializedFMUstateSize(fmi2Component c,
-				fmi2FMUstate FMUstate, size_t *size) {
+		extern "C" fmi2Status fmi2SerializedFMUstateSize(fmi2Component c, fmi2FMUstate FMUstate, size_t *size)
+		{
 			notimplemented(c, "fmi2SerializedFMUstateSize");
 			return fmi2Error;
 		}
-		extern "C" fmi2Status fmi2SerializeFMUstate(fmi2Component c,
-				fmi2FMUstate FMUstate, fmi2Byte serializedState[], size_t size) {
+		extern "C" fmi2Status fmi2SerializeFMUstate(fmi2Component c, fmi2FMUstate FMUstate, fmi2Byte serializedState[],
+				size_t size)
+		{
 			notimplemented(c, "fmi2SerializeFMUstate");
 			return fmi2Error;
 		}
-		extern "C" fmi2Status fmi2DeSerializeFMUstate(fmi2Component c,
-				const fmi2Byte serializedState[], size_t size, fmi2FMUstate* FMUstate) {
+		extern "C" fmi2Status fmi2DeSerializeFMUstate(fmi2Component c, const fmi2Byte serializedState[], size_t size,
+				fmi2FMUstate* FMUstate)
+		{
 			notimplemented(c, "fmi2DeSerializeFMUstate");
 			return fmi2Error;
 		}
 		
-		extern "C" fmi2Status fmi2GetDirectionalDerivative(fmi2Component c,
-				const fmi2ValueReference vUnknown_ref[], size_t nUnknown,
-				const fmi2ValueReference vKnown_ref[], size_t nKnown,
-				const fmi2Real dvKnown[], fmi2Real dvUnknown[]) {
+		extern "C" fmi2Status fmi2GetDirectionalDerivative(fmi2Component c, const fmi2ValueReference vUnknown_ref[],
+				size_t nUnknown, const fmi2ValueReference vKnown_ref[], size_t nKnown, const fmi2Real dvKnown[],
+				fmi2Real dvUnknown[])
+		{
 			notimplemented(c, "fmi2GetDirectionalDerivative");
 			return fmi2Error;
 		}
@@ -308,10 +323,14 @@ class StaticGenerators {
 		extern "C" fmi2Status fmi2DoStep(fmi2Component c, fmi2Real currentCommunicationPoint, fmi2Real communicationStepSize,
 				fmi2Boolean noSetFMUStatePriorToCurrentPoint)
 		{
-		//	fmiprintf("doStep%s\n", "");
-		
-			g_adaptation->executeControlFlow(currentCommunicationPoint, communicationStepSize);
-			return fmi2OK;
+			try
+			{
+				g_adaptation->executeControlFlow(currentCommunicationPoint, communicationStepSize);
+				return fmi2OK;
+			} catch (adaptation::SemanticAdaptationFmiException& e)
+			{
+				return e.status;
+			}
 		}
 		
 		extern "C" fmi2Status fmi2GetStatus(fmi2Component c, const fmi2StatusKind s, fmi2Status *value)
@@ -350,57 +369,59 @@ class StaticGenerators {
 		// ---------------------------------------------------------------------------
 		#else
 		/* Enter and exit the different modes */
-		fmi2Status fmi2EnterEventMode(fmi2Component c) {
+		fmi2Status fmi2EnterEventMode(fmi2Component c)
+		{
 			return fmi2Error;
 		}
 		
-		fmi2Status fmi2NewDiscreteStates(fmi2Component c, fmi2EventInfo *eventInfo) {
+		fmi2Status fmi2NewDiscreteStates(fmi2Component c, fmi2EventInfo *eventInfo)
+		{
 			return fmi2Error;
 		}
 		
-		fmi2Status fmi2EnterContinuousTimeMode(fmi2Component c) {
+		fmi2Status fmi2EnterContinuousTimeMode(fmi2Component c)
+		{
 			return fmi2Error;
 		}
 		
-		fmi2Status fmi2CompletedIntegratorStep(fmi2Component c,
-				fmi2Boolean noSetFMUStatePriorToCurrentPoint,
-				fmi2Boolean *enterEventMode, fmi2Boolean *terminateSimulation) {
+		fmi2Status fmi2CompletedIntegratorStep(fmi2Component c, fmi2Boolean noSetFMUStatePriorToCurrentPoint,
+				fmi2Boolean *enterEventMode, fmi2Boolean *terminateSimulation)
+		{
 			return fmi2Error;
 		}
 		
 		/* Providing independent variables and re-initialization of caching */
-		fmi2Status fmi2SetTime(fmi2Component c, fmi2Real time) {
+		fmi2Status fmi2SetTime(fmi2Component c, fmi2Real time)
+		{
 			return fmi2Error;
 		}
 		
-		fmi2Status fmi2SetContinuousStates(fmi2Component c, const fmi2Real x[],
-				size_t nx) {
+		fmi2Status fmi2SetContinuousStates(fmi2Component c, const fmi2Real x[], size_t nx)
+		{
 			return fmi2Error;
 		}
 		
 		/* Evaluation of the model equations */
-		fmi2Status fmi2GetDerivatives(fmi2Component c, fmi2Real derivatives[],
-				size_t nx) {
+		fmi2Status fmi2GetDerivatives(fmi2Component c, fmi2Real derivatives[], size_t nx)
+		{
 			return fmi2Error;
 		}
 		
-		fmi2Status fmi2GetEventIndicators(fmi2Component c, fmi2Real eventIndicators[],
-				size_t ni) {
+		fmi2Status fmi2GetEventIndicators(fmi2Component c, fmi2Real eventIndicators[], size_t ni)
+		{
 			return fmi2Error;
 		}
 		
-		fmi2Status fmi2GetContinuousStates(fmi2Component c, fmi2Real states[],
-				size_t nx) {
+		fmi2Status fmi2GetContinuousStates(fmi2Component c, fmi2Real states[], size_t nx)
+		{
 			return fmi2Error;
 		}
 		
-		fmi2Status fmi2GetNominalsOfContinuousStates(fmi2Component c,
-				fmi2Real x_nominal[], size_t nx) {
+		fmi2Status fmi2GetNominalsOfContinuousStates(fmi2Component c, fmi2Real x_nominal[], size_t nx)
+		{
 			return fmi2Error;
 		}
 		#endif // Model Exchange
-		
-		
 		'''
 	}
 }

+ 12 - 4
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.testframework/src/be/uantwerpen/ansymo/semanticadaptation/testframework/StaticGenerators.xtend

@@ -164,10 +164,20 @@ class StaticGenerators {
 		        message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
 		endif()
 		
-		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFMI_COSIMULATION -g")
-		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFMI_COSIMULATION -g")
+		add_definitions(-DFMI_COSIMULATION)
 		
+		if (WIN32)
+			#windows, becuase windows just cannot figure out to do it correct. 
+			# must be a bug in msys mingw gcc 6.3	
+			# it doesnt read the rsp files.
+			set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES 0)
+			set(CMAKE_C_USE_RESPONSE_FILE_FOR_INCLUDES 0)
+			
+			set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_LIBRARIES 0)
+			set(CMAKE_C_USE_RESPONSE_FILE_FOR_LIBRARIES 0)
+		endif()
 		
+		add_subdirectory («frameworkPath»)
 		
 		include_directories(src)
 		
@@ -223,8 +233,6 @@ class StaticGenerators {
 		#target_link_libraries(${PROJECT_NAME}_main ${PTHREAD_LIBRARY} /C/msys64/mingw64/x86_64-w64-mingw32/lib/libwinpthread.a)
 		#endif()
 		
-		add_subdirectory («frameworkPath»)
-		
 		#add_custom_command(${PROJECT_NAME}_main POST_BUILD COMMAND "find . -name *.exe -exec echo {} \; -exec bash -c \"objdump -p {} | grep 'DLL Name:'\" \;")		
 		
 		'''