How to use Navigator class of Microsoft.Coyote.Samples.DrinksServingRobot package

Best Coyote code snippet using Microsoft.Coyote.Samples.DrinksServingRobot.Navigator

Robot.cs

Source:Robot.cs Github

copy

Full Screen

...12{13 internal class Robot : StateMachine14 {15 internal ActorId CreatorId; // the Id of the Actor who created this instance.16 internal ActorId NavigatorId { get; set; }17 private readonly LogWriter Log = LogWriter.Instance;18 internal bool RunForever;19 private static readonly Location StartingLocation = new Location(1, 1);20 private Location Coordinates = StartingLocation;21 private List<Location> Route;22 private DrinkOrder CurrentOrder;23 private bool DrinkOrderPending;24 internal const double MoveDuration = 0.5;25 internal const int ServingDuration = 2;26 internal const int RetreatingDuration = 1;27 private readonly Dictionary<string, TimerInfo> Timers = new Dictionary<string, TimerInfo>();28 internal class ConfigEvent : Event29 {30 internal readonly bool RunForever;31 internal readonly ActorId CreatorId;32 public ConfigEvent(bool runForever, ActorId creatorId)33 {34 this.RunForever = runForever;35 this.CreatorId = creatorId;36 }37 }38 internal class RobotReadyEvent : Event39 {40 }41 internal class NavigatorResetEvent : Event { }42 internal class MoveTimerElapsedEvent : TimerElapsedEvent { }43 [Start]44 [OnEntry(nameof(OnInit))]45 [OnEventDoAction(typeof(Navigator.RegisterNavigatorEvent), nameof(OnSetNavigator))]46 [DeferEvents(typeof(Navigator.DrinkOrderProducedEvent))]47 internal class Init : State { }48 internal void OnInit(Event e)49 {50 if (e is ConfigEvent ce)51 {52 this.RunForever = ce.RunForever;53 this.CreatorId = ce.CreatorId;54 }55 }56 private void OnSetNavigator(Event e)57 {58 if (e is Navigator.RegisterNavigatorEvent sne)59 {60 // Note: the whole point of this sample is to test failover of the Navigator.61 // The Robot is designed to be robust in the face of failover, and that means62 // it needs to continue on with the new navigator object.63 if (this.NavigatorId == null)64 {65 this.NavigatorId = sne.NewNavigatorId;66 this.RaisePushStateEvent<Active>();67 }68 else69 {70 this.Log.WriteLine("<Robot> received a new Navigator, and pending drink order={0}!!!", this.DrinkOrderPending);71 // continue on with the new navigator.72 this.NavigatorId = sne.NewNavigatorId;73 if (this.DrinkOrderPending)74 {75 // stop any current driving and wait for DrinkOrderProducedEvent from new navigator76 // as it restarts the previous drink order request.77 this.StopMoving();78 this.RaiseGotoStateEvent<Active>();79 this.Monitor<LivenessMonitor>(new LivenessMonitor.IdleEvent());80 }81 }82 this.SendEvent(this.CreatorId, new NavigatorResetEvent());83 }84 }85 [OnEntry(nameof(OnInitActive))]86 [OnEventGotoState(typeof(Navigator.DrinkOrderProducedEvent), typeof(ExecutingOrder))]87 [OnEventDoAction(typeof(Navigator.DrinkOrderConfirmedEvent), nameof(OnDrinkOrderConfirmed))]88 internal class Active : State { }89 private void OnInitActive()90 {91 if (!this.DrinkOrderPending)92 {93 this.SendEvent(this.NavigatorId, new Navigator.GetDrinkOrderEvent(this.GetPicture()));94 this.Log.WriteLine("<Robot> Asked for a new Drink Order");95 }96 this.Monitor<LivenessMonitor>(new LivenessMonitor.BusyEvent());97 }98 private void OnDrinkOrderConfirmed()99 {100 this.DrinkOrderPending = true;101 this.SendEvent(this.CreatorId, new RobotReadyEvent());102 }103 public RoomPicture GetPicture()104 {105 var now = DateTime.UtcNow;106 this.Log.WriteLine($"<Robot> Obtained a Room Picture at {now} UTC");107 return new RoomPicture() { TimeTaken = now, Image = ReadCamera() };108 }109 private static byte[] ReadCamera()110 {111 return new byte[1]; // todo: plug in real camera code here.112 }113 [OnEntry(nameof(OnInitExecutingOrder))]114 [OnEventGotoState(typeof(DrivingInstructionsEvent), typeof(ReachingClient))]115 internal class ExecutingOrder : State { }116 private void OnInitExecutingOrder(Event e)117 {118 this.CurrentOrder = (e as Navigator.DrinkOrderProducedEvent)?.DrinkOrder;119 if (this.CurrentOrder != null)120 {121 this.Log.WriteLine("<Robot> Received new Drink Order. Executing ...");122 this.ExecuteOrder();123 }124 }125 private void ExecuteOrder()126 {127 var clientLocation = this.CurrentOrder.ClientDetails.Coordinates;128 this.Log.WriteLine($"<Robot> Asked for driving instructions from {this.Coordinates} to {clientLocation}");129 this.SendEvent(this.NavigatorId, new Navigator.GetDrivingInstructionsEvent(this.Coordinates, clientLocation));130 this.Monitor<LivenessMonitor>(new LivenessMonitor.BusyEvent());131 }132 [OnEntry(nameof(ReachClient))]133 internal class ReachingClient : State { }134 private void ReachClient(Event e)135 {136 var route = (e as DrivingInstructionsEvent)?.Route;137 if (route != null)138 {139 this.Route = route;140 // this.DrinkOrderPending = false; // this is where it really belongs.141 this.Timers["MoveTimer"] = this.StartTimer(TimeSpan.FromSeconds(MoveDuration), new MoveTimerElapsedEvent());142 }143 this.RaiseGotoStateEvent<MovingOnRoute>();144 }145 [OnEventDoAction(typeof(MoveTimerElapsedEvent), nameof(NextMove))]146 [IgnoreEvents(typeof(Navigator.DrinkOrderProducedEvent))]147 internal class MovingOnRoute : State { }148 private void NextMove()149 {150 this.DrinkOrderPending = false;151 if (this.Route == null)152 {153 return;154 }155 if (!this.Route.Any())156 {157 this.StopMoving();158 this.RaiseGotoStateEvent<ServingClient>();159 this.Log.WriteLine("<Robot> Reached Client.");160 Specification.Assert(...

Full Screen

Full Screen

FailoverDriver.cs

Source:FailoverDriver.cs Github

copy

Full Screen

...11 internal class FailoverDriver : StateMachine12 {13 private ActorId StorageId;14 private ActorId RobotId;15 private ActorId NavigatorId;16 private bool RunForever;17 private readonly LogWriter Log = LogWriter.Instance;18 private TimerInfo HaltTimer;19 private int Iterations;20 private const int NavigatorTimeToLive = 500; // milliseconds21 internal class ConfigEvent : Event22 {23 public bool RunForever;24 public ConfigEvent(bool runForever)25 {26 this.RunForever = runForever;27 }28 }29 [Start]30 [OnEntry(nameof(OnInit))]31 [DeferEvents(typeof(TimerElapsedEvent), typeof(Robot.RobotReadyEvent))]32 [IgnoreEvents(typeof(Robot.NavigatorResetEvent))]33 internal class Init : State { }34 internal void OnInit(Event e)35 {36 if (e is ConfigEvent ce)37 {38 this.RunForever = ce.RunForever;39 }40 this.Log.WriteLine("<FailoverDriver> #################################################################");41 this.Log.WriteLine("<FailoverDriver> Starting the Robot.");42 this.StorageId = this.CreateActor(typeof(MockStorage));43 this.NavigatorId = this.CreateNavigator();44 // Create the Robot.45 this.RobotId = this.CreateActor(typeof(Robot), new Robot.ConfigEvent(this.RunForever, this.Id));46 // Wake up the Navigator.47 this.SendEvent(this.NavigatorId, new Navigator.WakeUpEvent(this.RobotId));48 this.RaiseGotoStateEvent<Active>();49 }50 [OnEventGotoState(typeof(TimerElapsedEvent), typeof(TerminatingNavigator))]51 [OnEventDoAction(typeof(Robot.RobotReadyEvent), nameof(OnRobotReady))]52 [IgnoreEvents(typeof(Robot.NavigatorResetEvent))]53 internal class Active : State { }54 private void OnRobotReady()55 {56 // We have to wait for the robot to be ready before we test killing the Navigator otherwise57 // we end up killing the Navigator before the Robot has anything to do which is a waste of time.58 // Setup a timer to randomly kill the Navigator. When the timer fires we will restart the59 // Navigator and this is testing that the Navigator and Robot can recover gracefully when that happens.60 int duration = this.RandomInteger(NavigatorTimeToLive) + NavigatorTimeToLive;61 this.HaltTimer = this.StartTimer(TimeSpan.FromMilliseconds(duration));62 }63 private void StopTimer()64 {65 if (this.HaltTimer != null)66 {67 this.StopTimer(this.HaltTimer);68 this.HaltTimer = null;69 }70 }71 private ActorId CreateNavigator()72 {73 var cognitiveServiceId = this.CreateActor(typeof(MockCognitiveService));74 var routePlannerServiceId = this.CreateActor(typeof(MockRoutePlanner));75 return this.CreateActor(typeof(Navigator), new Navigator.NavigatorConfigEvent(this.Id, this.StorageId, cognitiveServiceId, routePlannerServiceId));76 }77 [OnEntry(nameof(OnTerminateNavigator))]78 [OnEventDoAction(typeof(Navigator.HaltedEvent), nameof(OnHalted))]79 [OnEventDoAction(typeof(Robot.NavigatorResetEvent), nameof(OnNavigatorReset))]80 [IgnoreEvents(typeof(TimerElapsedEvent))]81 internal class TerminatingNavigator : State { }82 private void OnTerminateNavigator()83 {84 this.StopTimer();85 this.Log.WriteLine("<FailoverDriver> #################################################################");86 this.Log.WriteLine("<FailoverDriver> # Starting the fail over of the Navigator #");87 this.Log.WriteLine("<FailoverDriver> #################################################################");88 this.SendEvent(this.NavigatorId, new Navigator.TerminateEvent());89 }90 private void OnHalted()91 {92 this.Log.WriteLine("<FailoverDriver> ***** The Navigator confirmed that it has terminated ***** ");93 // Create a new Navigator.94 this.Log.WriteLine("<FailoverDriver> ***** Created a new Navigator -- paused *****");95 this.NavigatorId = this.CreateNavigator();96 this.Log.WriteLine("<FailoverDriver> ***** Waking up the new Navigator *****");97 this.SendEvent(this.NavigatorId, new Navigator.WakeUpEvent(this.RobotId));98 }99 private void OnNavigatorReset()100 {101 this.Log.WriteLine("***** Robot confirmed it reset to the new Navigator *****");102 this.Iterations++;103 if (this.Iterations == 1 || this.RunForever)104 {105 // Continue on, we expect the WakeUpEvent to RegisterNavigator on the Robot which106 // will cause the Robot to raise another RobotReady event.107 }108 else109 {110 this.HaltSystem();111 }112 this.RaiseGotoStateEvent<Active>();113 }114 private void HaltSystem()115 {116 this.KillActors(this.RobotId, this.NavigatorId, this.StorageId);117 this.RaiseHaltEvent();118 }119 private void KillActors(params ActorId[] actors)120 {121 foreach (var actor in actors.Where(ac => ac != null))122 {123 this.SendEvent(actor, HaltEvent.Instance);124 }125 }126 private void WriteLine(string s)127 {128 this.Log.WriteLine(s);129 }130 }...

Full Screen

Full Screen

LivenessMonitor.cs

Source:LivenessMonitor.cs Github

copy

Full Screen

...3using Microsoft.Coyote.Specifications;4namespace Microsoft.Coyote.Samples.DrinksServingRobot5{6 /// <summary>7 /// This monitors the Robot and the Navigator to make sure the Robot always finishes the job,8 /// by serving a Drink.9 /// </summary>10 internal class LivenessMonitor : Monitor11 {12 public class BusyEvent : Event { }13 public class IdleEvent : Event { }14 [Start]15 [Cold]16 [OnEventGotoState(typeof(BusyEvent), typeof(Busy))]17 [IgnoreEvents(typeof(IdleEvent))]18 private class Idle : State { }19 [Hot]20 [OnEventGotoState(typeof(IdleEvent), typeof(Idle))]21 [IgnoreEvents(typeof(BusyEvent))]...

Full Screen

Full Screen

Navigator

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Samples.DrinksServingRobot;2using Microsoft.Coyote.Samples.DrinksServingRobot;3using Microsoft.Coyote.Samples.DrinksServingRobot;4using Microsoft.Coyote.Samples.DrinksServingRobot;5using Microsoft.Coyote.Samples.DrinksServingRobot;6using Microsoft.Coyote.Samples.DrinksServingRobot;7using Microsoft.Coyote.Samples.DrinksServingRobot;8using Microsoft.Coyote.Samples.DrinksServingRobot;9using Microsoft.Coyote.Samples.DrinksServingRobot;10using Microsoft.Coyote.Samples.DrinksServingRobot;11{12 static void Main(string[] args)13 {14 RobotEnvironment env = new RobotEnvironment();15 RobotController controller = new RobotController(env);16 Robot robot = new Robot(env, controller);17 User user = new User(env, controller);18 env.AddRobot(robot);19 env.AddUser(user);20 user.Start();21 robot.Start();

Full Screen

Full Screen

Navigator

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Samples.DrinksServingRobot;2using Microsoft.Coyote.Samples.DrinksServingRobot;3using Microsoft.Coyote.Samples.DrinksServingRobot;4using Microsoft.Coyote.Samples.DrinksServingRobot;5using Microsoft.Coyote.Samples.DrinksServingRobot;6using Microsoft.Coyote.Samples.DrinksServingRobot;7using Microsoft.Coyote.Samples.DrinksServingRobot;8using Microsoft.Coyote.Samples.DrinksServingRobot;9using Microsoft.Coyote.Samples.DrinksServingRobot;10using Microsoft.Coyote.Samples.DrinksServingRobot;11using Microsoft.Coyote.Samples.DrinksServingRobot;12using Microsoft.Coyote.Samples.DrinksServingRobot;13using Microsoft.Coyote.Samples.DrinksServingRobot;14using Microsoft.Coyote.Samples.DrinksServingRobot;

Full Screen

Full Screen

Navigator

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Samples.DrinksServingRobot;2using Microsoft.Coyote.Samples.DrinksServingRobot.DrinksServingRobot;3using Microsoft.Coyote.Samples.DrinksServingRobot.DrinksServingRobot.Events;4using Microsoft.Coyote.Samples.DrinksServingRobot.DrinksServingRobot.Machines;5using Microsoft.Coyote.Samples.DrinksServingRobot.DrinksServingRobot.Machines.Drinks;6using Microsoft.Coyote.Samples.DrinksServingRobot.DrinksServingRobot.Machines.Drinks.Drinks;7using Microsoft.Coyote.Samples.DrinksServingRobot.DrinksServingRobot.Machines.Drinks.Drinks.Drinks;8using Microsoft.Coyote.Samples.DrinksServingRobot.DrinksServingRobot.Machines.Drinks.Drinks.Drinks.Drinks;9using Microsoft.Coyote.Samples.DrinksServingRobot.DrinksServingRobot.Machines.Drinks.Drinks.Drinks.Drinks.Drinks;10using Microsoft.Coyote.Samples.DrinksServingRobot.DrinksServingRobot.Machines.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks;11using Microsoft.Coyote.Samples.DrinksServingRobot.DrinksServingRobot.Machines.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks;12using Microsoft.Coyote.Samples.DrinksServingRobot.DrinksServingRobot.Machines.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks;13using Microsoft.Coyote.Samples.DrinksServingRobot.DrinksServingRobot.Machines.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks;14using Microsoft.Coyote.Samples.DrinksServingRobot.DrinksServingRobot.Machines.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks.Drinks;

Full Screen

Full Screen

Navigator

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Samples.DrinksServingRobot;2using System;3using System.Threading.Tasks;4{5 {6 public async Task NavigateToDestinationAsync()7 {8 Console.WriteLine("Navigating to the destination");9 await Task.Delay(1000);10 }11 }12}13using Microsoft.Coyote.Samples.DrinksServingRobot;14using System;15using System.Threading.Tasks;16{17 {18 public async Task ServeDrinkAsync()19 {20 Console.WriteLine("Serving drink");21 await Task.Delay(1000);22 }23 }24}25using Microsoft.Coyote.Samples.DrinksServingRobot;26using System;27using System.Threading.Tasks;28{29 {30 public async Task ServeDrinkAsync()31 {32 Console.WriteLine("Serving drink");33 await Task.Delay(1000);34 }35 }36}37using Microsoft.Coyote.Samples.DrinksServingRobot;38using System;39using System.Threading.Tasks;40{41 {42 private readonly Navigator navigator;43 private readonly Robot robot;44 public DrinksServingRobot()45 {46 this.navigator = new Navigator();47 this.robot = new Robot();48 }49 public async Task ServeDrinkAsync()50 {51 await this.navigator.NavigateToDestinationAsync();52 await this.robot.ServeDrinkAsync();53 }54 }55}56using Microsoft.Coyote.Samples.DrinksServingRobot;57using System;58using System.Threading.Tasks;

Full Screen

Full Screen

Navigator

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Samples.DrinksServingRobot;2using System;3{4 {5 static void Main(string[] args)6 {7 var navigator = new Navigator();8 navigator.Start();9 Console.ReadLine();10 }11 }12}

Full Screen

Full Screen

Navigator

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Samples.DrinksServingRobot;2using Microsoft.Coyote.Samples.DrinksServingRobot.Machines;3{4 {5 public static void NavigateTo(Barista barista, string location)6 {7 barista.SendEvent(new Barista.Events.NavigateTo(location));8 }9 }10}11using Microsoft.Coyote.Samples.DrinksServingRobot;12using Microsoft.Coyote.Samples.DrinksServingRobot.Machines;13{14 {15 public static void NavigateTo(Barista barista, string location)16 {17 barista.SendEvent(new Barista.Events.NavigateTo(location));18 }19 }20}21using Microsoft.Coyote.Samples.DrinksServingRobot;22using Microsoft.Coyote.Samples.DrinksServingRobot.Machines;23{24 {25 public static void NavigateTo(Barista barista, string location)26 {27 barista.SendEvent(new Barista.Events.NavigateTo(location));28 }29 }30}31using Microsoft.Coyote.Samples.DrinksServingRobot;32using Microsoft.Coyote.Samples.DrinksServingRobot.Machines;33{34 {35 public static void NavigateTo(Barista barista, string location)36 {37 barista.SendEvent(new Barista.Events.NavigateTo(location));38 }39 }40}41using Microsoft.Coyote.Samples.DrinksServingRobot;42using Microsoft.Coyote.Samples.DrinksServingRobot.Machines;

Full Screen

Full Screen

Navigator

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Samples.DrinksServingRobot;2{3 {4 private static void Main(string[] args)5 {6 var navigator = new Navigator();7 navigator.Start();8 }9 }10}11using Microsoft.Coyote.Samples.DrinksServingRobot;12{13 {14 private static void Main(string[] args)15 {16 var navigator = new Navigator();17 navigator.Start();18 }19 }20}21using Microsoft.Coyote.Samples.DrinksServingRobot;22{23 {24 private static void Main(string[] args)25 {26 var navigator = new Navigator();27 navigator.Start();28 }29 }30}31using Microsoft.Coyote.Samples.DrinksServingRobot;32{33 {34 private static void Main(string[] args)35 {36 var navigator = new Navigator();37 navigator.Start();38 }39 }40}41using Microsoft.Coyote.Samples.DrinksServingRobot;42{43 {44 private static void Main(string[] args)45 {46 var navigator = new Navigator();47 navigator.Start();48 }49 }50}51using Microsoft.Coyote.Samples.DrinksServingRobot;52{53 {

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