semantic adaptation reactive moore LazySA lazy_sa
at "./path/to/LazySA.fmu"

	for inner fmu Controller controller
	at "./path/to/Controller.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 obj_detected -> controller.obj_detected,
			passenger_up -> controller.passenger_up,
			passenger_down -> controller.passenger_down,
			passenger_stop -> controller.passenger_stop,
			driver_up -> controller.driver_up,
			driver_down -> controller.driver_down,
			driver_stop -> controller.driver_stop

output ports up, down, stop

param 	INIT_OBJ_DETECTED := false,
		INIT_PASSENGER_UP := false,
		INIT_PASSENGER_DOWN := false,
		INIT_PASSENGER_STOP := false,
		INIT_DRIVER_UP := false,
		INIT_DRIVER_DOWN := false,
		INIT_DRIVER_STOP := false;

control var	tn := -1.0,
			tl := -1.0,
			prev_obj_detected := INIT_OBJ_DETECTED,
			prev_passenger_up := INIT_PASSENGER_UP,
			prev_passenger_down := INIT_PASSENGER_DOWN,
			prev_passenger_stop := INIT_PASSENGER_STOP,
			prev_driver_up := INIT_DRIVER_UP,
			prev_driver_down := INIT_DRIVER_DOWN,
			prev_driver_stop := INIT_DRIVER_STOP;

control rules {
	if (tl < 0.0){
		tl := t;
	}
	
	var step_size := min(H, tn - t); 
	if (lazy_sa.obj_detected != prev_obj_detected or
		lazy_sa.passenger_up != prev_passenger_up or
		lazy_sa.passenger_down != prev_passenger_down or
		lazy_sa.passenger_stop != prev_passenger_stop or
		lazy_sa.driver_up != prev_driver_up or
		lazy_sa.driver_down != prev_driver_down or
		lazy_sa.driver_stop != prev_driver_stop or
		(t+H) >= tn
	){
		var step_to_be_done := (t+H-tl);
		var step_done := do_step(controller, t, step_to_be_done); 
		tn := tl + step_done + get_next_time_step(controller); 
		step_size := tl + step_done - t; 
		tl := tl + step_done; 
	}
	
	prev_obj_detected := lazy_sa.obj_detected;
	prev_passenger_up := lazy_sa.passenger_up;
	prev_passenger_down := lazy_sa.passenger_down;
	prev_passenger_stop := lazy_sa.passenger_stop;
	prev_driver_up := lazy_sa.driver_up;
	prev_driver_down := lazy_sa.driver_down;
	prev_driver_stop := lazy_sa.driver_stop;
	
	return step_size;
}

