Browse Source

changed examples

Bart Meyers 7 years ago
parent
commit
e25dbdc191

+ 1 - 0
.gitignore

@@ -6,3 +6,4 @@
 /SemanticAdaptationForFMI/FMIAbstraction/src/case_study/units/adaptations/*.pyc
 /SemanticAdaptationForFMI/FMIAbstraction/src/case_study/units/ct_based/*.pyc
 /SemanticAdaptationForFMI/FMIAbstraction/src/case_study/units/de_based/*.pyc
+/DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation/.antlr-generator-3.2.0-patch.jar

+ 2 - 2
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/input/powerwindow.sa

@@ -4,7 +4,7 @@ semantic adaptation controller_sa
 for fmu controller
 input ports armature_current -> obj_detected, passenger_up, passenger_down, driver_up, driver_down, next
 output ports up, down
-control rules { triggered by signal == true or t >= next_transition }
+control rules { triggered by signal == true or t >= get_next_time_step(controller) }
 in rules with crossing absolute tolerance = 1e-8 and relative tolerance = 0.0001, no hold {
 	armature_current >! 5 -> obj_detected := true;
 	otherwise(armature_current) -> obj_detected := false;
@@ -22,7 +22,7 @@ out rules {
 	reaction_torque := -reaction_torque;
 }
 
-semantic adaptation window_obstacle_sa with master
+semantic adaptation window_obstacle_sa
 for fmu window_sa, obstacle
 multiply rate 10 times with first order interpolation
 successive substitution starts at height with absolute tolerance = 1e-8 and relative tolerance = 0.0001

+ 1 - 2
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/input/powerwindow_algebraic_loop_delay_BASE.sa

@@ -9,11 +9,10 @@ out rules {
 }
 
 // algebraic loop, simply use delay... written in baseDSL
-semantic adaptation window_sa_delay with master
+semantic adaptation window_sa_delay //with master
 for fmu window_sa // inner SA, makes it possible to enforce order among adaptations this way
 param init_height = 0;
 param init_reaction_torque = 0;
-control rules { implied }
 out var stored_height := init_height;
 out var stored_reaction_torque := init_reaction_torque;
 out rules {

+ 59 - 2
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/input/powerwindow_algebraic_loop_iteration_BASE.sa

@@ -11,7 +11,7 @@ out rules {
 // algebraic loop
 semantic adaptation power_window_obstacle_sa // without master -> not necessary: custom control rules override master
 for fmu power, window_sa, obstacle
-input ports up (m/s^2), down
+input ports up, down
 output ports armature_current
 //algebraic loop fix point iteration at reaction_torque and height with absolute tolerance = 1e-8 and relative tolerance = 0.0001
 param MAXITER = 100;
@@ -39,11 +39,68 @@ control rules {
 		temp_reaction_torque := reaction_torque;
 		//local_step(power); // with delayed input for reaction_torque
 		//local_step(obstacle); // with delayed input for height
-		local_step(h, power, obstacle, window_sa); // acts like the Local master but cannot not advance time, so will always roll-back for safety (must be in one statements because a full step is done here), but how do we get the inner
+		local_step(power);
+		local_step(obstacle);
+		local_step(window_sa);
+		//local_step(H, power, obstacle, window_sa); // acts like the Local master but cannot not advance time, so will always roll-back for safety (must be in one statements because a full step is done here), but how do we get the inner
 		if (is_close(temp_height, height, REL_TOL, ABS_TOL) and is_close(temp_reaction_torque, reaction_torque, REL_TOL, ABS_TOL)) { // todo: set tolerance
 			converged := true;
 		}
 	}
+	return H;
+}
+
+// this is the one for the paper
+semantic adaptation window_obstacle_sa_loop// overrides master -> not necessary: custom control rules override master
+for fmu window_sa, obstacle
+input ports motor_speed
+output ports reaction_torque
+param MAXITER = 100;
+param REL_TOL = 1e-8;
+param ABS_TOL = 1e-8;
+control rules {
+	var prev_height; // to compare new value with previous value
+	var window_state;
+	var obstacle_state;
+	height := height; // do_step will always use the last values, local_step does not advance time so will replace the last signal values! therefore, we must set the signal at time t+h of reaction_torque and height. this is done by a very weird statement:
+	reaction_force := 0; // and just initialise other values at time t+h
+	for (var iter in 0 .. MAXITER) {
+		prev_height := height;
+		local_step(obstacle); // with delayed input for height. overwrite current time step: basically a set_state (state at previous time step), do_step (with time step H)
+		local_step(window_sa);
+		if (is_close(prev_height, height, REL_TOL, ABS_TOL)) {
+			break;
+		}
+	}
+	return H;
+}
+
+// other version without local_step
+semantic adaptation window_obstacle_sa_loop2// overrides master -> not necessary: custom control rules override master
+for fmu window_sa, obstacle
+input ports motor_speed
+output ports reaction_torque
+param MAXITER = 100;
+param REL_TOL = 1e-8;
+param ABS_TOL = 1e-8;
+control rules {
+	var prev_height; // to compare new value with previous value
+	var window_state;
+	var obstacle_state;
+	for (var iter in 0 .. MAXITER) {
+		window_state := get_state(window_sa);
+		obstacle_state := get_state(obstacle);
+		prev_height := height;
+		do_step(obstacle, H); // with delayed input for height
+		do_step(window_sa, H);
+		if (is_close(prev_height, height, REL_TOL, ABS_TOL)) {
+			break;
+		} else {
+			set_state(window_sa, window_state);
+			set_state(obstacle, obstacle_state);
+		}
+	}
+	return H;
 }
 
 

+ 37 - 5
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/input/powerwindow_controller_delay_BASE.sa

@@ -4,7 +4,7 @@ module Controller_SA
 
 semantic adaptation controller_sa
 for fmu controller
-input ports armature_current -> obj_detected, passenger_up, passenger_down, driver_up, driver_down, next
+input ports armature_current -> obj_detected, passenger_up, passenger_down, driver_up, driver_down
 output ports up, down
 param REL_TOL = 0.0001;
 param ABS_TOL = 1e-8;
@@ -12,8 +12,22 @@ param CROSSING = 5;
 param init_armature_current = CROSSING;
 param init_up = 0;
 param init_down = 0;
-control rules { triggered by signal == true or t >= next_transition }
-in var next_transition := -1;
+control var internal_transition;
+control rules { // overwriting master! What to do in case of roll back? How to reuse generic algorithm
+	if (not is_set(internal_transition)) { // only at start of simulation
+		var state := get_state(controller);
+		internal_transition := do_step(controller, MAX) + t; // may return internal transition time or MAX
+		set_state(controller, state);
+	}
+	var step_size; // may be used to determine roll-back
+	if (signal == true or t >= internal_transition) {
+		step_size := do_step(controller, H); // do a step, then decide next internal transition
+		var state := get_state(controller);
+		internal_transition := do_step(controller, MAX) + t;
+		set_state(controller, state);
+	}
+	return step_size;
+}
 in var stored_armature_current := init_armature_current;
 in rules {
 	true -> {
@@ -22,7 +36,7 @@ in rules {
 					and (not is_close(armature_current, CROSSING, REL_TOL, ABS_TOL) and armature_current > CROSSING)) { // crossing, but not within tolerance
 			var negative_value := stored_armature_current - CROSSING;
 			var positive_value := armature_current - CROSSING;
-			var new_step_size := (h * (- negative_value)) / (positive_value - negative_value);
+			var new_step_size := (H * (- negative_value)) / (positive_value - negative_value);
 			discard(new_step_size);
 		} else {
 			if (not (not is_close(stored_armature_current, CROSSING, REL_TOL, ABS_TOL) and stored_armature_current > CROSSING)
@@ -32,7 +46,6 @@ in rules {
 		}
 		stored_armature_current := armature_current;
 	} --> { };
-	next > 0 -> { next_transition := t + next; } --> { };
 }
 out var stored_up := init_up;
 out var stored_down := init_down;
@@ -46,3 +59,22 @@ out rules {
 	true -> { up := stored_up; stored_up := up; } --> { };
 	true -> { down := stored_down; stored_down := down; } --> { }; 
 }
+
+
+// other suggestion for implementing get_next_time_step, making it easier to generate baseDSL code
+/*control rules { // overwriting master! What to do in case of roll back? How to reuse generic algorithm
+       var step_size; // may be used to determine roll-back
+       if (signal == true or t >= internal_transition) {
+             step_size := do_step(controller, H); // do a step, then decide next internal transition
+       }
+       return step_size;
+}
+in var internal_transition;
+in rules {
+       not is_set(internal_transition) -> {
+             if (not is_set(internal_transition)) {
+                    internal_transition := get_next_step(controller) + t;
+              }
+       } --> { internal_transition := get_next_step(controller) + t; };
+}*/
+

+ 1 - 1
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/input/powerwindow_model_only.sa

@@ -8,7 +8,7 @@ full internal dependencies
 fmu controller
 type discrete event
 input ports obj_detected, passenger_up, passenger_down, driver_up, driver_down
-output ports up, down, stop, next
+output ports up, down, stop
 full internal dependencies
 
 fmu power

+ 1 - 1
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/input/powerwindow_multi_rate.sa

@@ -3,7 +3,7 @@ import Controller_SA
 import Algebraic_Loop_SA
 
 // multi-rate for window and object
-semantic adaptation window_obstacle_sa with master
+semantic adaptation window_obstacle_sa
 for fmu window_sa, obstacle
 multiply rate 10 times with first order interpolation
 successive substitution starts at height with absolute tolerance = 1e-8 and relative tolerance = 0.0001

+ 12 - 2
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/input/powerwindow_multi_rate_BASE.sa

@@ -10,12 +10,22 @@ semantic adaptation window_obstacle_sa_loop
 for fmu window_sa, obstacle
 // just for the sake of declaring this, look at algebraic loop iteration
 
-semantic adaptation window_obstacle_sa_multirate with master // multi-rate for window and object
+semantic adaptation window_obstacle_sa_multirate //with master // multi-rate for window and object
 for fmu window_obstacle_sa_loop
 input ports motor_speed
 output ports reaction_torque
 param init_motor_speed = 0;
-control rules { multiplied 10 times }
+control rules {
+	var h := H/10;
+	var proposed_h := H;
+	for (var iter in 0 .. 10) {
+		var returned_h := do_step(window_obstacle_sa_loop, h);
+		if (returned_h != h) { // for backtracking
+			 proposed_h := iter*h + returned_h;
+		}
+	}
+	return proposed_h;
+}
 in var stored_motor_speed := init_motor_speed;
 in var linear_increment;
 in rules {

+ 13 - 7
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/input/window_obstacle_sa_loop_BASE_new.sa

@@ -1,4 +1,4 @@
-semantic adaptation window_obstacle_sa_loop// overrides master -> not necessary: custom control rules override master
+semantic adaptation window_obstacle_sa_loop
 for fmu window_sa, obstacle
 input ports motor_speed
 output ports reaction_torque
@@ -6,15 +6,21 @@ param MAXITER = 100;
 param REL_TOL = 1e-8;
 param ABS_TOL = 1e-8;
 control rules {
-	height := height; // do_step will always use the last values, local_step does not advance time so will replace the last signal values! therefore, we must set the signal at time t+h of reaction_torque and height. this is done by a very weird statement:
-	reaction_force := 0; // and just initialise other values at time t+h
-	var prev_height; // to compare new value with previous value
+	var prev_height;
+	var window_state;
+	var obstacle_state;
 	for (var iter in 0 .. MAXITER) {
+		window_state := get_state(window_sa);
+		obstacle_state := get_state(obstacle);
 		prev_height := height;
-		local_step(obstacle); // with delayed input for height
-		local_step(window_sa);
+		do_step(obstacle, H); // with delayed input for height
+		do_step(window_sa, H);
 		if (is_close(prev_height, height, REL_TOL, ABS_TOL)) {
 			break;
+		} else {
+			set_state(window_sa, window_state);
+			set_state(obstacle, obstacle_state);
 		}
 	}
-}
+	return H;
+}

+ 1 - 1
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/src/be/uantwerpen/ansymo/semanticadaptation/tests/SemanticAdaptationParsingTest.xtend

@@ -41,7 +41,7 @@ class SemanticAdaptationParsingTest extends AbstractSemanticAdaptationTest{
 	@Test def powerwindow_algebraic_loop_iteration_BASE() { __parseNoErrors('input/powerwindow_algebraic_loop_iteration_BASE.sa') }
 	@Test def powerwindow_algebraic_loop_iteration() { __parseNoErrors('input/powerwindow_algebraic_loop_iteration.sa') }
 	@Test def powerwindow_controller_delay() { __parseNoErrors('input/powerwindow_controller_delay.sa') }
-	@Test def powerwindow_controller_delay_BASE() { __parseNoErrorsPrint('input/powerwindow_controller_delay_BASE.sa') }
+	@Test def powerwindow_controller_delay_BASE() { __parseNoErrors('input/powerwindow_controller_delay_BASE.sa') }
 	@Test def powerwindow_multi_rate() { __parseNoErrors('input/powerwindow_multi_rate.sa') }
 	@Test def powerwindow_multi_rate_BASE() { __parseNoErrors('input/powerwindow_multi_rate_BASE.sa') }
 	@Test def powerwindow() { __parseNoErrors('input/powerwindow.sa') }

BIN
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation/.antlr-generator-3.2.0-patch.jar


+ 12 - 6
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation/src/be/uantwerpen/ansymo/semanticadaptation/SemanticAdaptation.xtext

@@ -88,7 +88,7 @@ OutRulesBlock:
 	{OutRulesBlock} ('out' globalvars+=Declaration)* 'out' 'rules' ('with' setting+=GeneralSetting (',' setting+=GeneralSetting)*)? '{' iterationvars+=Declaration* rules+=DataRule* '}';
 
 ControlRuleBlock:
-	{ControlRuleBlock} 'control' 'rules' '{' controlrule=ControlRule '}'; // TODO: multiple rules or just one?
+	{ControlRuleBlock} ('control' globalvars+=Declaration)*  'control' 'rules' '{' controlrule=ControlRule '}'; // TODO: multiple rules or just one?
 
 DataRule: // condition -> state transition --> output function
 	(((condition=RuleCondition "->" statetransitionfunction=StateTransitionFunction) | ConditionStateTransition | ConditionLessRule) ("-->" outputfunction=OutputFunction)? | AlgebraicLoopSolution) ';';
@@ -142,7 +142,7 @@ ControlRule:
 	{ImpliedControlRule} 'implied' | // the default
 	{PeriodicControlRule} 'periodic' ('at' init_time=REALTYPE)? 'every' period=REALTYPE | 
 	{MultipliedControlRule} 'multiplied' multiplication=INT 'times' | 
-	StateTransitionFunction;
+	{CustomControlRule} statements+=Statement* returnstatement=ReturnStatement;
 
 AnonymousFunction: // has access to ports
 	{FunctionExpression} '{=' code=Expression '}'| 
@@ -314,18 +314,24 @@ BuiltinFunction:
 	{Max} name='max' '(' args+=ArithmeticExpression ',' args+=ArithmeticExpression ')' |
 	{Min} name='min' '(' args+=ArithmeticExpression ',' args+=ArithmeticExpression ')' |
 	{Abs} name='abs' '(' args=ArithmeticExpression ')' |
-	{Close} name='is_close' '(' args+=ArithmeticExpression ',' args+=ArithmeticExpression ',' args+=ArithmeticExpression ',' args+=ArithmeticExpression ')' | 
-	{IsSet} name='is_set' '(' args=Variable ')'// a, b, relative tolerance, absolute tolerance 
+	{Close} name='is_close' '(' args+=ArithmeticExpression ',' args+=ArithmeticExpression ',' args+=ArithmeticExpression ',' args+=ArithmeticExpression ')' |  // a, b, relative tolerance, absolute tolerance 
+	{IsSet} name='is_set' '(' args=Variable ')' | 
+	{DoStepFun} name='do_step' '(' fmu=[FMU] ',' H=ArithmeticExpression ')' |
+	{GetState} name='get_state' '(' fmu=[FMU] ')' |
+	{GetNextInternalTimeStep} name='get_next_time_step' '(' fmu=[FMU] ')' // is actually the time step returned by do_step(fmu), preceded by get_state and succeeded by set_state
 	;
 	
 Procedure:
 	{Reject} name='reject' |
 	{Step} name='local_step' '(' fmu=[FMU] ')' |
-	{Discard} name='discard' '(' args=Variable ')'; // arg: new proposed step size
+	{Discard} name='discard' '(' args=Variable ')' |
+	{DoStep} name='do_step' '(' fmu=[FMU] ',' H=ArithmeticExpression ')' |
+	{SetState} name='set_state' '(' fmu=[FMU] ',' state=Var ')'; // arg: new proposed step size
 	
 Var:
-	{StepSize} name='h' | 
+	{StepSize} name='H' | 
 	{CurrentTime} name='t' | 
+	{Max} name='MAX' |
 	{GenericSignal} name='signal' ('[' (
 		index=NOW | 
 		index=PREV |