How to use OnTerminate method of Microsoft.Coyote.Samples.DrinksServingRobot.Paused class

Best Coyote code snippet using Microsoft.Coyote.Samples.DrinksServingRobot.Paused.OnTerminate

Navigator.cs

Source:Navigator.cs Github

copy

Full Screen

...70 }71 internal class HaltedEvent : Event { }72 [Start]73 [OnEntry(nameof(OnInit))]74 [OnEventDoAction(typeof(TerminateEvent), nameof(OnTerminate))]75 [DeferEvents(typeof(WakeUpEvent), typeof(GetDrinkOrderEvent), typeof(GetDrivingInstructionsEvent))]76 internal class Init : State { }77 internal void OnInit(Event e)78 {79 if (e is NavigatorConfigEvent configEvent)80 {81 this.CreatorId = configEvent.CreatorId;82 this.StorageId = configEvent.StorageId;83 this.CognitiveServiceId = configEvent.CognitiveServiceId;84 this.RoutePlannerServiceId = configEvent.RoutePlannerId;85 }86 this.RaisePushStateEvent<Paused>();87 }88 private void SaveGetDrinkOrderEvent(GetDrinkOrderEvent e)89 {90 this.SendEvent(this.StorageId, new KeyValueEvent(this.Id, DrinkOrderStorageKey, e));91 }92 internal class WakeUpEvent : Event93 {94 internal readonly ActorId ClientId;95 public WakeUpEvent(ActorId clientId)96 {97 this.ClientId = clientId;98 }99 }100 internal class RegisterNavigatorEvent : Event101 {102 internal ActorId NewNavigatorId;103 public RegisterNavigatorEvent(ActorId newNavigatorId)104 {105 this.NewNavigatorId = newNavigatorId;106 }107 }108 [OnEventDoAction(typeof(WakeUpEvent), nameof(OnWakeUp))]109 [OnEventDoAction(typeof(KeyValueEvent), nameof(RestartPendingJob))]110 [DeferEvents(typeof(TerminateEvent), typeof(GetDrinkOrderEvent), typeof(GetDrivingInstructionsEvent))]111 internal class Paused : State { }112 private void OnWakeUp(Event e)113 {114 this.Log.WriteLine("<Navigator> starting");115 if (e is WakeUpEvent wpe)116 {117 this.Log.WriteLine("<Navigator> Got RobotId");118 this.RobotId = wpe.ClientId;119 // tell this client robot about this new navigator. During failover testing120 // of the Navigator, this can be swapping out the Navigator that the robot is using.121 this.SendEvent(this.RobotId, new RegisterNavigatorEvent(this.Id));122 }123 // Check storage to see if we have a pending request already.124 this.SendEvent(this.StorageId, new ReadKeyEvent(this.Id, DrinkOrderStorageKey));125 }126 internal void RestartPendingJob(Event e)127 {128 if (e is KeyValueEvent kve)129 {130 var key = kve.Key;131 object value = kve.Value;132 Specification.Assert(key != null, $"Error: KeyValueEvent contains a null key");133 if (key == DrinkOrderStorageKey)134 {135 this.RestartPendingGetDrinkOrderRequest(value as GetDrinkOrderEvent);136 }137 this.RaiseGotoStateEvent<Active>();138 }139 }140 private void RestartPendingGetDrinkOrderRequest(GetDrinkOrderEvent e)141 {142 if (e != null)143 {144 this.ProcessDrinkOrder(e);145 this.Log.WriteLine("<Navigator> Restarting the pending Robot's request to find drink clients ...");146 }147 else148 {149 this.Log.WriteLine("<Navigator> There was no prior pending request to find drink clients ...");150 }151 }152 [OnEntry(nameof(InitActive))]153 [OnEventDoAction(typeof(GetDrinkOrderEvent), nameof(GetDrinkOrder))]154 [OnEventDoAction(typeof(ConfirmedEvent), nameof(OnStorageConfirmed))]155 [OnEventDoAction(typeof(GetDrivingInstructionsEvent), nameof(GetDrivingInstructions))]156 [OnEventDoAction(typeof(DrinksClientDetailsEvent), nameof(SendClientDetailsToRobot))]157 [OnEventDoAction(typeof(DrivingInstructionsEvent), nameof(SendDrivingInstructionsToRobot))]158 [IgnoreEvents(typeof(KeyValueEvent))]159 internal class Active : State { }160 private void InitActive()161 {162 this.Log.WriteLine("<Navigator> initialized.");163 }164 private void GetDrinkOrder(Event e)165 {166 if (e is GetDrinkOrderEvent getDrinkOrderEvent)167 {168 this.SaveGetDrinkOrderEvent(getDrinkOrderEvent);169 }170 }171 private void OnStorageConfirmed(Event e)172 {173 if (e is ConfirmedEvent ce && ce.Key == DrinkOrderStorageKey)174 {175 Specification.Assert(176 !ce.Existing,177 $"Error: The storage `{DrinkOrderStorageKey}` was already set which means we lost a GetDrinkOrderEvent");178 this.SendEvent(this.RobotId, new DrinkOrderConfirmedEvent());179 this.ProcessDrinkOrder(ce.Value as GetDrinkOrderEvent);180 }181 }182 private void ProcessDrinkOrder(GetDrinkOrderEvent e)183 {184 // continue on...185 var picture = e.Picture;186 this.SendEvent(this.CognitiveServiceId, new RecognizeDrinksClientEvent(this.Id, picture));187 }188 private void SendClientDetailsToRobot(Event e)189 {190 // When the cognitive service recognizes someone in the picture it sends us a191 // DrinksClientDetailsEvent containing information about who is in the picture and where192 // they are located.193 if (e is DrinksClientDetailsEvent drinksClientDetailsEvent)194 {195 var details = drinksClientDetailsEvent.Details;196 this.SendEvent(this.RobotId, new DrinkOrderProducedEvent(new DrinkOrder(details)));197 }198 }199 private void GetDrivingInstructions(Event e)200 {201 // When the DrinkOrderProducedEvent is received by the Robot it calls back with202 // this event to request driving instructions. This operation is not restartable. Instead,203 // during failover of the navigator the robot will re-request any driving instructions.204 if (e is GetDrivingInstructionsEvent getDrivingInstructionsEvent)205 {206 this.ProcessDrivingInstructions(getDrivingInstructionsEvent);207 }208 }209 private void SendDrivingInstructionsToRobot(Event e)210 {211 if (e is DrivingInstructionsEvent drivingInstructionsEvent)212 {213 this.SendEvent(this.RobotId, drivingInstructionsEvent);214 // The drink order is now completed, so we can delete the persistent job.215 this.Log.WriteLine("<Navigator> drink order is complete, deleting the job record.");216 this.SendEvent(this.StorageId, new DeleteKeyEvent(this.Id, DrinkOrderStorageKey));217 }218 }219 private void ProcessDrivingInstructions(GetDrivingInstructionsEvent e)220 {221 this.SendEvent(this.RoutePlannerServiceId, new GetRouteEvent(this.Id, e.StartPoint, e.EndPoint));222 }223 private void OnTerminate(Event e)224 {225 if (e is TerminateEvent)226 {227 this.TerminateMyself();228 }229 }230 private void TerminateMyself()231 {232 if (!this.Terminating)233 {234 this.Terminating = true;235 this.Log.WriteLine("<Navigator> Terminating as previously ordered ...");236 this.SendEvent(this.CognitiveServiceId, HaltEvent.Instance);237 this.SendEvent(this.RoutePlannerServiceId, HaltEvent.Instance);...

Full Screen

Full Screen

OnTerminate

Using AI Code Generation

copy

Full Screen

1{2 using System;3 using System.Collections.Generic;4 using System.Linq;5 using System.Text;6 using System.Threading.Tasks;7 using Microsoft.Coyote;8 using Microsoft.Coyote.Actors;9 using Microsoft.Coyote.Tasks;10 {11 public override Task OnEntryAsync(Event e, StateMachine stateMachine)12 {13 var robot = stateMachine as Robot;14 robot.SendEvent(robot.Id, new OnPause());15 return Task.CompletedTask;16 }17 public override Task OnExitAsync(Event e, StateMachine stateMachine)18 {19 var robot = stateMachine as Robot;20 robot.SendEvent(robot.Id, new OnResume());21 return Task.CompletedTask;22 }23 public override Task OnReentryAsync(Event e, StateMachine stateMachine)24 {25 return Task.CompletedTask;26 }27 public override Task OnEventRaisedAsync(Event e, StateMachine stateMachine)28 {29 return Task.CompletedTask;30 }31 }32}33{34 using System;35 using System.Collections.Generic;36 using System.Linq;37 using System.Text;38 using System.Threading.Tasks;39 using Microsoft.Coyote;40 using Microsoft.Coyote.Actors;41 using Microsoft.Coyote.Tasks;42 {43 public override Task OnEntryAsync(Event e, StateMachine stateMachine)44 {45 return Task.CompletedTask;46 }47 public override Task OnExitAsync(Event e, StateMachine stateMachine)48 {49 return Task.CompletedTask;50 }51 public override Task OnReentryAsync(Event e, StateMachine stateMachine)52 {53 return Task.CompletedTask;54 }55 public override Task OnEventRaisedAsync(Event e, StateMachine stateMachine)56 {57 var robot = stateMachine as Robot;58 robot.SendEvent(robot.Id, new OnPause());59 return Task.CompletedTask;60 }61 }62}

Full Screen

Full Screen

OnTerminate

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4using Microsoft.Coyote.Actors;5using Microsoft.Coyote.Samples.DrinksServingRobot;6using Microsoft.Coyote.Tasks;7{8 {9 public override Task OnEntryAsync(Event e, StateMachine stateMachine)10 {11 var machine = stateMachine as Robot;12 machine.SendEvent(machine.Control, new PausedEvent());13 return Task.CompletedTask;14 }15 public override async Task OnExitAsync(Event e, StateMachine stateMachine)16 {17 var machine = stateMachine as Robot;18 machine.SendEvent(machine.Control, new ResumedEvent());19 }20 }21}22using System;23using System.Threading.Tasks;24using Microsoft.Coyote;25using Microsoft.Coyote.Actors;26using Microsoft.Coyote.Samples.DrinksServingRobot;27using Microsoft.Coyote.Tasks;28{29 {30 public override Task OnEntryAsync(Event e, StateMachine stateMachine)31 {32 var machine = stateMachine as Robot;33 machine.SendEvent(machine.Control, new PausedEvent());34 return Task.CompletedTask;35 }36 public override async Task OnExitAsync(Event e, StateMachine stateMachine)37 {38 var machine = stateMachine as Robot;39 machine.SendEvent(machine.Control, new ResumedEvent());40 }41 public override async Task OnHaltAsync(Event e, StateMachine stateMachine)42 {43 var machine = stateMachine as Robot;44 machine.SendEvent(machine.Control, new HaltedEvent());45 }46 }47}48using System;49using System.Threading.Tasks;50using Microsoft.Coyote;51using Microsoft.Coyote.Actors;52using Microsoft.Coyote.Samples.DrinksServingRobot;53using Microsoft.Coyote.Tasks;54{55 {

Full Screen

Full Screen

OnTerminate

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4{5 {6 protected override Task OnTerminate()7 {8 Console.WriteLine("Paused state is being terminated");9 return Task.CompletedTask;10 }11 }12}13using System;14using System.Threading.Tasks;15using Microsoft.Coyote;16{17 {18 protected override Task OnEvent(Event e)19 {20 if (e is EvStart)21 {22 Console.WriteLine("Robot is now serving drinks");23 this.Raise(new EvServingDrinks());24 return Task.CompletedTask;25 }26 return base.OnEvent(e);27 }28 }29}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful