Browse Source

Fixed issue with source dependencies: output ports tau <- loop_sa.tau
Only implemented for out var!

Added several opeartors
Added some type checks.

Implemented all case studies

Casper Thule 3 years ago
parent
commit
399f7dcc79
17 changed files with 851 additions and 507 deletions
  1. 12 0
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/tests/CgCppBasicTest.xtend
  2. BIN
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/controller/LazySA.fmu
  3. 71 0
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/controller/controller.sa
  4. 41 0
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/controller/modelDescription.xml
  5. BIN
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/power/Power.fmu
  6. 25 0
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/power/Power.mo
  7. 15 0
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/power/power.BASE.sa
  8. BIN
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/rollback_test/Rollbackend.fmu
  9. BIN
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/rollback_test/rollback-end.fmu
  10. 15 0
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/rollback_test/rollback_test.sa
  11. 46 0
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/BasicConditionSwitch.xtend
  12. 1 1
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/BuildUtilities.java
  13. 5 4
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/ControlConditionSwitch.xtend
  14. 608 498
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/CppGenerator.xtend
  15. 1 0
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/OutRulesConditionSwitch.xtend
  16. 6 4
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/RulesConditionSwitch.xtend
  17. 5 0
      DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/src/be/uantwerpen/ansymo/semanticadaptation/tests/SemanticAdaptationGeneratorTest.xtend

+ 12 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/tests/CgCppBasicTest.xtend

@@ -61,6 +61,18 @@ class CgCppBasicTest extends AbstractSemanticAdaptationTest {
 	@Test def rate_canonical() {
 		__parseNoErrors('test_input/single_folder_spec/rate/rate_canonical.sa', 'generated', "rate_canonical");
 	}
+	
+	@Test def power(){
+		__parseNoErrors('test_input/single_folder_spec/power/power.BASE.sa', 'generated','power');
+	}
+	
+	@Test def rollback_test(){
+		__parseNoErrors('test_input/single_folder_spec/rollback_test/rollback_test.sa', 'generated','rollback_test');
+	}
+	
+	@Test def controller_test(){
+		__parseNoErrors('test_input/single_folder_spec/controller/controller.sa', 'generated','controller');
+	}
 
 	def __parseNoErrorsWithValidation(String directory, String filename) {
 		val model = __parse(filename);

BIN
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/controller/LazySA.fmu


+ 71 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/controller/controller.sa

@@ -0,0 +1,71 @@
+semantic adaptation reactive moore ControllerSA controller_sa
+at "./path/to/ControllerSA.fmu"
+
+	for inner fmu LazySA lazy
+	at "test_input/single_folder_spec/controller/LazySA.fmu"
+	with input ports obj_detected, passenger_up, passenger_down, passenger_stop, driver_up, driver_down, driver_stop
+	with output ports up, down, stop
+
+input ports armature_current -> lazy.obj_detected, 
+			passenger_up -> lazy.passenger_up, 
+			passenger_down -> lazy.passenger_down,
+			passenger_stop -> lazy.passenger_stop,
+			driver_up -> lazy.driver_up, 
+			driver_down -> lazy.driver_down,
+			driver_stop -> lazy.driver_stop
+
+output ports	u,
+				d
+
+param	RTOL := 0.0001,
+		ATOL := 1e-8,
+		T := 5.0,
+		INIT_V := 0.0;
+
+control var	c := false,
+			p_v := INIT_V;
+control rules {
+	var step_size := H;
+	var aux_obj_detected := false;
+	var crossedTooFar := false;
+	if ((not is_close(p_v, T, RTOL, ATOL) and p_v < T)
+				and (not is_close(f_v, T, RTOL, ATOL) and f_v > T)) {
+		crossedTooFar := true;
+		var negative_value := p_v - T;
+		var positive_value := f_v - T;
+		step_size := (H * (- negative_value)) / (positive_value - negative_value);
+	} else {
+		if ((not is_close(p_v, T, RTOL, ATOL) and p_v < T)
+					and is_close(f_v, T, RTOL, ATOL )) { 
+			c := true;
+		}
+	}
+	
+	if (not crossedTooFar){
+		step_size := do_step(lazy, t, H);
+	}
+	
+	if (is_close(step_size, H, RTOL, ATOL)) {
+		p_v := f_v;
+	}
+	return step_size;
+}
+
+in var	f_v := INIT_V;
+in rules {
+	true -> {
+		f_v := controller_sa.armature_current;
+	} --> {
+		lazy.obj_detected := c;
+	};
+}
+
+out rules {
+	lazy.up -> { } --> {controller_sa.u := 1.0; };
+	not lazy.up -> { } --> {controller_sa.u := 0.0; };
+	
+	lazy.down -> { } --> {controller_sa.d := 1.0; };
+	not lazy.down -> { } --> {controller_sa.d := 0.0; };
+	
+	lazy.stop -> { } --> {controller_sa.u := 0.0 ; controller_sa.d := 0.0; };
+}

+ 41 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/controller/modelDescription.xml

@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?><fmiModelDescription fmiVersion="2.0" guid="3767c7d8-b6f5-40f4-b646-4691b9287f55" modelName="LazySA" variableNamingConvention="flat">
+    <ModelVariables>
+        <ScalarVariable causality="input" name="obj_detected" valueReference="0" variability="discrete">
+            <Boolean/>
+        </ScalarVariable>
+        <ScalarVariable causality="input" name="passenger_up" valueReference="1" variability="discrete">
+            <Boolean/>
+        </ScalarVariable>
+        <ScalarVariable causality="input" name="passenger_down" valueReference="2" variability="discrete">
+            <Boolean/>
+        </ScalarVariable>
+        <ScalarVariable causality="input" name="passenger_stop" valueReference="3" variability="discrete">
+            <Boolean/>
+        </ScalarVariable>
+        <ScalarVariable causality="input" name="driver_up" valueReference="4" variability="discrete">
+            <Boolean/>
+        </ScalarVariable>
+        <ScalarVariable causality="input" name="driver_down" valueReference="5" variability="discrete">
+            <Boolean/>
+        </ScalarVariable>
+        <ScalarVariable causality="input" name="driver_stop" valueReference="6" variability="discrete">
+            <Boolean/>
+        </ScalarVariable>
+        <ScalarVariable causality="output" name="up" valueReference="7" variability="discrete">
+            <Boolean/>
+        </ScalarVariable>
+        <ScalarVariable causality="output" name="down" valueReference="8" variability="discrete">
+            <Boolean/>
+        </ScalarVariable>
+        <ScalarVariable causality="output" name="stop" valueReference="9" variability="discrete">
+            <Boolean/>
+        </ScalarVariable>
+    </ModelVariables>
+    <ModelStructure>
+        <Outputs>
+            <Unknown dependencies="" index="8"/>
+            <Unknown dependencies="" index="9"/>
+            <Unknown dependencies="" index="10"/>
+        </Outputs>
+    </ModelStructure>
+</fmiModelDescription>

BIN
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/power/Power.fmu


+ 25 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/power/Power.mo

@@ -0,0 +1,25 @@
+model Power
+  parameter Real J = 0.085;
+  parameter Real b = 5;
+  parameter Real K = 7.45;
+  parameter Real R = 0.15;
+  parameter Real L = 0.036;
+  parameter Real V_abs = 12;
+  Real V;
+  output Real displacement;
+  output Real armature_current;
+  output Real speed;
+  input Real tau;
+  input Real u;
+  input Real d;
+equation
+  V = if u > 0.5 then V_abs 
+      else if d > 0.5 then -V_abs
+      else 0.0;
+  J * der(speed) + b * speed = K * armature_current - tau;
+  L * der(armature_current) + R * armature_current = V - K * speed;
+  der(displacement) = speed;
+annotation(
+  experiment(StartTime = 0, StopTime = 1, Tolerance = 1e-6, Interval = 0.002)
+);
+end Power;

+ 15 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/power/power.BASE.sa

@@ -0,0 +1,15 @@
+semantic adaptation delayed moore PowerSA power_sa
+at "./path/to/PowerSA.fmu"
+
+	for inner fmu Power power
+	at "test_input/single_folder_spec/power/Power.fmu"
+	with input ports u, d, tau (N.m)
+	with output ports armature_current (A), speed (rad/s), displacement (rad)
+
+input ports u -> power.u,
+			d -> power.d,
+			tau -> power.tau	
+	
+output ports armature_current <- power.armature_current, 
+			speed <- power.speed,
+			displacement <- power.displacement

BIN
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/rollback_test/Rollbackend.fmu


BIN
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/rollback_test/rollback-end.fmu


+ 15 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/single_folder_spec/rollback_test/rollback_test.sa

@@ -0,0 +1,15 @@
+semantic adaptation reactive moore RollbackTest rollbackTest_sa
+at "./path/to/RollbackTest.fmu"
+
+	for inner fmu rollbackEnd rollbackend
+		at "./test_input/single_folder_spec/rollback_test/rollback-end.fmu"
+		with input ports level
+
+input ports level -> rollbackend.level
+		
+control rules {
+	save_state(rollbackend);
+	rollback(rollbackend);
+	
+	return 1;
+}

+ 46 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/BasicConditionSwitch.xtend

@@ -19,6 +19,9 @@ import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.exceptions.TypeException
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Or
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.NotEquals
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.GreaterThanOrEquals
+import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.And
+import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Not
+import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.GreaterThan
 
 class BasicConditionSwitch extends SemanticAdaptationSwitch<ReturnInformation> {
 
@@ -124,6 +127,21 @@ class BasicConditionSwitch extends SemanticAdaptationSwitch<ReturnInformation> {
 		return retVal;
 	}
 	
+	override ReturnInformation caseGreaterThan(GreaterThan object)
+	{
+		var ret = new ReturnInformation();
+		val left = doSwitch(object.left);
+		val right = doSwitch(object.right);
+		val code = '''«left.code» > «right.code»'''
+		if(!Conversions.isTypeANumber(left.type) || !Conversions.isTypeANumber(right.type))
+		{
+			throw new TypeException('''Wrong types at: «code»''');
+		}
+		ret.type = SVType.Boolean;
+		ret.code = code;
+		return ret;
+	}
+	
 	override ReturnInformation caseGreaterThanOrEquals(GreaterThanOrEquals object)
 	{
 		var ret = new ReturnInformation();
@@ -169,6 +187,34 @@ class BasicConditionSwitch extends SemanticAdaptationSwitch<ReturnInformation> {
 		return ret;
 	}
 	
+	override ReturnInformation caseNot(Not object)
+	{
+		var ret = new ReturnInformation();
+		val res = doSwitch(object.left);
+		if(res.type !== SVType.Boolean)
+		{
+			throw new TypeException('''Wrong types at: «res.code»''');
+		}
+		ret.type = SVType.Boolean;
+		ret.code = res.code;
+		return ret;
+	}
+	
+	override ReturnInformation caseAnd(And object)
+	{
+		var ret = new ReturnInformation();
+		val left = doSwitch(object.left);
+		val right = doSwitch(object.right);
+		val code = '''«left.code» && «right.code»'''
+		if(left.type !== SVType.Boolean || right.type !== SVType.Boolean)
+		{
+			throw new TypeException('''Wrong types at: «code»''');
+		}
+		ret.type = SVType.Boolean;
+		ret.code = code;
+		return ret;
+	}
+	
 	override ReturnInformation caseOr(Or object)
 	{
 		var ret = new ReturnInformation();

+ 1 - 1
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/BuildUtilities.java

@@ -33,7 +33,7 @@ public class BuildUtilities {
 		libFiles = new LinkedList<>();
 
 		try {
-			jarfile = new FileInputStream(new File("target/classes/jars/cppFramework.jar"));
+			jarfile = new FileInputStream(new File("jars/cppFramework.jar"));
 			jarstream = new JarInputStream(jarfile);
 			filejarentry = jarstream.getNextJarEntry();
 

+ 5 - 4
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/ControlConditionSwitch.xtend

@@ -97,7 +97,7 @@ class ControlConditionSwitch extends RulesConditionSwitch {
 
 	override ReturnInformation caseDoStepFun(DoStepFun object) {
 		var retVal = new ReturnInformation();
-		retVal.code = '''this->do_step(«object.fmu.name»,«doSwitch(object.h).code»,«doSwitch(object.t).code»)''';
+		retVal.code = '''this->do_step(this->«object.fmu.name»,«doSwitch(object.h).code»,«doSwitch(object.t).code»)''';
 		retVal.type = SVType.Integer;
 		return retVal;
 	}
@@ -126,13 +126,14 @@ class ControlConditionSwitch extends RulesConditionSwitch {
 
 	override ReturnInformation caseSaveState(SaveState object) {
 		var retVal = new ReturnInformation();
-		retVal.appendCode('''saveState(«object.fmu.name»)''')
+		retVal.appendCode('''save_state(«object.fmu.name»)''')
 		return retVal;
 	}
 
 	override ReturnInformation caseClose(Close object) {
 		var retVal = new ReturnInformation();
-		retVal.code = '''is_close(«object.args.map[e | doSwitch(e).code].join(", ")»)''';
+		retVal.code = '''HyfMath::is_close(«object.args.map[e | doSwitch(e).code].join(", ")»)''';
+		retVal.type = SVType.Boolean;
 		return retVal;
 	}
 
@@ -152,7 +153,7 @@ class ControlConditionSwitch extends RulesConditionSwitch {
 	{
 		var retVal = new ReturnInformation();
 		retVal.type = SVType.Real;
-		retVal.code = '''getMextTimeStep(«object.fmu.name»)''';
+		retVal.code = '''getNextTimeStep(«object.fmu.name»)''';
 		return retVal;
 	}
 }

File diff suppressed because it is too large
+ 608 - 498
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/CppGenerator.xtend


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

@@ -38,6 +38,7 @@ public class OutRulesConditionSwitch extends RulesConditionSwitch {
 		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»)''';
+		retVal.type = type;
 		return retVal;
 	}
 }

+ 6 - 4
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/generation/RulesConditionSwitch.xtend

@@ -282,7 +282,7 @@ class RulesConditionSwitch extends BasicConditionSwitch {
 		var String code = "";
 		if (globalDeclaration) {
 			// This is an in var, out var or crtl var declaration
-			code = '''this->«object.name» = «exprRes.code»''';
+			code = '''this->internalState.«object.name» = «exprRes.code»''';
 			var globVar = new GlobalInOutVariable(object.name, exprRes.type);
 			gVars.put(object.name, globVar)
 		} else {
@@ -322,7 +322,7 @@ class RulesConditionSwitch extends BasicConditionSwitch {
 				(inVars !== null && inVars.containsKey(object.ref.name)) ||
 				(crtlVars !== null && crtlVars.containsKey(object.ref.name))) {
 
-				retVal.code = '''this->«object.ref.name»''';
+				retVal.code = '''this->internalState.«object.ref.name»''';
 
 				if (SASVs !== null && SASVs.containsKey(object.ref.name)) {
 					retVal.conSaSv = SASVs.get(object.ref.name);
@@ -344,7 +344,9 @@ class RulesConditionSwitch extends BasicConditionSwitch {
 
 		} else {
 			this.externalVariableOwner = object.owner.name;
-			retVal.code = '''«doSwitch(object.ref).code»''';
+			val res =  doSwitch(object.ref);
+			retVal.code = res.code;
+			retVal.type = res.type;
 		}
 		return retVal;
 	}
@@ -378,7 +380,7 @@ class RulesConditionSwitch extends BasicConditionSwitch {
 
 	override ReturnInformation caseIsSet(IsSet object) {
 		var retInfo = new ReturnInformation();
-		retInfo.code = '''this->isSet«(object.args as Variable).ref.name»''';
+		retInfo.code = '''this->internalState.isSet«(object.args as Variable).ref.name»''';
 		return retInfo;
 	}
 

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

@@ -222,6 +222,11 @@ class SemanticAdaptationGeneratorTest extends AbstractSemanticAdaptationTest{
 		@Test def rate_SA_parseNoExceptions() { __generate('input/power_window_case_study/rate.sa', new IAcceptor<CompilationTestHelper.Result>(){
 			override accept(Result t) { }
 		}) }
+		
+	@Ignore
+	@Test def power_base_SA_parseNoExceptions() { __generate('input/power_window_case_study/power.BASE.sa', new IAcceptor<CompilationTestHelper.Result>(){
+			override accept(Result t) { }
+		}) }
 	
 	def void __generate(String filename, IAcceptor<CompilationTestHelper.Result> acceptor) {
 		//readFile(filename).assertCompilesTo('oracles/power_window_case_study/lazy.BASE.sa')