Best Coyote code snippet using Microsoft.Coyote.Actors.Coverage.ActorRuntimeLogGraphBuilder.GetOrCreateEventLink
ActorRuntimeLogGraphBuilder.cs
Source:ActorRuntimeLogGraphBuilder.cs  
...101                if (!string.IsNullOrEmpty(creatorName))102                {103                    var creatorId = this.GetResolveActorId(creatorName, creatorType);104                    GraphNode creator = this.Graph.GetOrCreateNode(creatorId);105                    this.GetOrCreateEventLink(creator, node, new EventInfo() { Event = "CreateActor" });106                }107            }108        }109        /// <inheritdoc/>110        public void OnCreateStateMachine(ActorId id, string creatorName, string creatorType)111        {112            lock (this.Inbox)113            {114                var resolvedId = this.GetResolveActorId(id?.Name, id?.Type);115                GraphNode node = this.Graph.GetOrCreateNode(resolvedId);116                node.Category = StateMachineCategory;117                if (!string.IsNullOrEmpty(creatorName))118                {119                    var creatorId = this.GetResolveActorId(creatorName, creatorType);120                    GraphNode creator = this.Graph.GetOrCreateNode(creatorId);121                    this.GetOrCreateEventLink(creator, node, new EventInfo() { Event = "CreateActor" });122                }123            }124        }125        /// <inheritdoc/>126        public void OnSendEvent(ActorId targetActorId, string senderName, string senderType, string senderStateName,127            Event e, Guid eventGroupId, bool isTargetHalted)128        {129            string eventName = e.GetType().FullName;130            this.AddEvent(targetActorId.Name, targetActorId.Type, senderName, senderType, senderStateName, eventName);131        }132        /// <inheritdoc/>133        public void OnRaiseEvent(ActorId id, string stateName, Event e)134        {135            string eventName = e.GetType().FullName;136            // Raising event to self.137            this.AddEvent(id.Name, id.Type, id.Name, id.Type, stateName, eventName);138        }139        /// <inheritdoc/>140        public void OnEnqueueEvent(ActorId id, Event e)141        {142        }143        /// <inheritdoc/>144        public void OnDequeueEvent(ActorId id, string stateName, Event e)145        {146            lock (this.Inbox)147            {148                var resolvedId = this.GetResolveActorId(id?.Name, id?.Type);149                string eventName = e.GetType().FullName;150                EventInfo info = this.PopEvent(resolvedId, eventName);151                if (info != null)152                {153                    this.Dequeued[id] = info;154                }155            }156        }157        private EventInfo PopEvent(string resolvedId, string eventName)158        {159            EventInfo result = null;160            lock (this.Inbox)161            {162                if (this.Inbox.TryGetValue(resolvedId, out List<EventInfo> inbox))163                {164                    for (int i = inbox.Count - 1; i >= 0; i--)165                    {166                        if (inbox[i].Event == eventName)167                        {168                            result = inbox[i];169                            inbox.RemoveAt(i);170                        }171                    }172                }173            }174            return result;175        }176        /// <inheritdoc/>177        public void OnReceiveEvent(ActorId id, string stateName, Event e, bool wasBlocked)178        {179            string resolvedId = this.GetResolveActorId(id?.Name, id?.Type);180            lock (this.Inbox)181            {182                if (this.Inbox.TryGetValue(resolvedId, out List<EventInfo> inbox))183                {184                    string eventName = e.GetType().FullName;185                    for (int i = inbox.Count - 1; i >= 0; i--)186                    {187                        EventInfo info = inbox[i];188                        if (info.Event == eventName)189                        {190                            // Yay, found it so we can draw the complete link connecting the Sender state to this state!191                            string category = string.IsNullOrEmpty(stateName) ? ActorCategory : StateMachineCategory;192                            var source = this.GetOrCreateChild(info.Name, info.Type, info.State);193                            var target = this.GetOrCreateChild(id?.Name, id?.Type, category, stateName);194                            this.GetOrCreateEventLink(source, target, info);195                            inbox.RemoveAt(i);196                            break;197                        }198                    }199                }200            }201        }202        /// <inheritdoc/>203        public void OnWaitEvent(ActorId id, string stateName, Type eventType)204        {205        }206        /// <inheritdoc/>207        public void OnWaitEvent(ActorId id, string stateName, params Type[] eventTypes)208        {209        }210        /// <inheritdoc/>211        public void OnStateTransition(ActorId id, string stateName, bool isEntry)212        {213            if (isEntry)214            {215                // record the fact we have entered this state216                this.GetOrCreateChild(id?.Name, id?.Type, stateName);217            }218        }219        /// <inheritdoc/>220        public void OnExecuteAction(ActorId id, string handlingStateName, string currentStateName, string actionName)221        {222            this.LinkTransition(typeof(DoActionEvent), id, handlingStateName, currentStateName, null);223        }224        /// <inheritdoc/>225        public void OnGotoState(ActorId id, string currentStateName, string newStateName)226        {227            this.LinkTransition(typeof(GotoStateEvent), id, currentStateName, currentStateName, newStateName);228        }229        /// <inheritdoc/>230        public void OnPushState(ActorId id, string currentStateName, string newStateName)231        {232            this.LinkTransition(typeof(PushStateEvent), id, currentStateName, currentStateName, newStateName);233        }234        /// <inheritdoc/>235        public void OnPopState(ActorId id, string currentStateName, string restoredStateName)236        {237            if (!string.IsNullOrEmpty(currentStateName))238            {239                this.LinkTransition(typeof(PopStateEvent), id, currentStateName,240                    currentStateName, restoredStateName);241            }242        }243        /// <inheritdoc/>244        public void OnHalt(ActorId id, int inboxSize)245        {246            lock (this.Inbox)247            {248                this.HaltedStates.TryGetValue(id, out string stateName);249                var target = this.GetOrCreateChild(id?.Name, id?.Type, "Halt", "Halt");250                // Transition to the Halt state.251                if (!string.IsNullOrEmpty(stateName))252                {253                    var source = this.GetOrCreateChild(id?.Name, id?.Type, stateName);254                    this.GetOrCreateEventLink(source, target, new EventInfo() { Event = typeof(HaltEvent).FullName });255                }256            }257        }258        private int? GetLinkIndex(GraphNode source, GraphNode target, string id)259        {260            if (this.MergeEventLinks)261            {262                return null;263            }264            return this.Graph.GetUniqueLinkIndex(source, target, id);265        }266        /// <inheritdoc/>267        public void OnDefaultEventHandler(ActorId id, string stateName)268        {269            lock (this.Inbox)270            {271                string resolvedId = this.GetResolveActorId(id?.Name, id?.Type);272                string eventName = typeof(DefaultEvent).FullName;273                this.AddEvent(id.Name, id.Type, id.Name, id.Type, stateName, eventName);274                this.Dequeued[id] = this.PopEvent(resolvedId, eventName);275            }276        }277        /// <inheritdoc/>278        public void OnHandleRaisedEvent(ActorId id, string stateName, Event e)279        {280            lock (this.Inbox)281            {282                // We used the inbox to store raised event, but it should be the first one handled since283                // raised events are highest priority.284                string resolvedId = this.GetResolveActorId(id?.Name, id?.Type);285                lock (this.Inbox)286                {287                    if (this.Inbox.TryGetValue(resolvedId, out List<EventInfo> inbox))288                    {289                        string eventName = e.GetType().FullName;290                        for (int i = inbox.Count - 1; i >= 0; i--)291                        {292                            EventInfo info = inbox[i];293                            if (info.Event == eventName)294                            {295                                this.Dequeued[id] = info;296                                break;297                            }298                        }299                    }300                }301            }302        }303        /// <inheritdoc/>304        public void OnPopStateUnhandledEvent(ActorId actorId, string currentStateName, Event e)305        {306            lock (this.Inbox)307            {308                if (e is HaltEvent)309                {310                    this.HaltedStates[actorId] = currentStateName;311                }312            }313        }314        /// <inheritdoc/>315        public void OnExceptionThrown(ActorId id, string stateName, string actionName, Exception ex)316        {317        }318        /// <inheritdoc/>319        public void OnExceptionHandled(ActorId id, string stateName, string actionName, Exception ex)320        {321        }322        /// <inheritdoc/>323        public void OnCreateTimer(TimerInfo info)324        {325            // TODO: figure out how to graph timers when we have no "timer id" at this point...326        }327        /// <inheritdoc/>328        public void OnStopTimer(TimerInfo info)329        {330        }331        /// <inheritdoc/>332        public void OnCreateMonitor(string monitorType)333        {334            lock (this.Inbox)335            {336                GraphNode node = this.Graph.GetOrCreateNode(monitorType, monitorType);337                node.Category = MonitorCategory;338            }339        }340        /// <inheritdoc/>341        public void OnMonitorExecuteAction(string monitorType, string stateName, string actionName)342        {343            // Monitors process actions immediately, so this state transition is a result of the only event in the inbox.344            lock (this.Inbox)345            {346                if (this.Inbox.TryGetValue(monitorType, out List<EventInfo> inbox) && inbox.Count > 0)347                {348                    var e = inbox[inbox.Count - 1];349                    inbox.RemoveAt(inbox.Count - 1);350                    // Draw the link connecting the Sender state to this state!351                    var source = this.GetOrCreateChild(e.Name, e.Type, e.State);352                    var target = this.GetOrCreateChild(monitorType, monitorType, stateName);353                    this.GetOrCreateEventLink(source, target, e);354                }355            }356        }357        /// <inheritdoc/>358        public void OnMonitorProcessEvent(string monitorType, string stateName, string senderName, string senderType,359            string senderStateName, Event e)360        {361            lock (this.Inbox)362            {363                string eventName = e.GetType().FullName;364                // Now add a fake event for internal monitor state transition that might now happen as a result of this event,365                // storing the monitor's current state in this event.366                var info = this.AddEvent(monitorType, monitorType, monitorType, monitorType, stateName, eventName);367                // Draw the link connecting the Sender state to this state!368                var source = this.GetOrCreateChild(senderName, senderType, senderStateName);369                var target = this.GetOrCreateChild(monitorType, monitorType, stateName);370                this.GetOrCreateEventLink(source, target, info);371            }372        }373        /// <inheritdoc/>374        public void OnMonitorRaiseEvent(string monitorType, string stateName, Event e)375        {376            // Raising event to self.377            string eventName = e.GetType().FullName;378            this.AddEvent(monitorType, monitorType, monitorType, monitorType, stateName, eventName);379        }380        /// <inheritdoc/>381        public void OnMonitorStateTransition(string monitorType, string stateName, bool isEntry, bool? isInHotState)382        {383            if (isEntry)384            {385                lock (this.Inbox)386                {387                    // Monitors process events immediately (and does not call OnDequeue), so this state transition is a result of388                    // the fake event we created in OnMonitorProcessEvent.389                    if (this.Inbox.TryGetValue(monitorType, out List<EventInfo> inbox) && inbox.Count > 0)390                    {391                        var info = inbox[inbox.Count - 1];392                        inbox.RemoveAt(inbox.Count - 1);393                        // draw the link connecting the current state to this new state!394                        var source = this.GetOrCreateChild(monitorType, monitorType, info.State);395                        var shortStateName = this.GetLabel(monitorType, monitorType, stateName);396                        string suffix = string.Empty;397                        if (isInHotState.HasValue)398                        {399                            suffix = (isInHotState is true) ? "[hot]" : "[cold]";400                            shortStateName += suffix;401                        }402                        string label = shortStateName;403                        var target = this.GetOrCreateChild(monitorType, monitorType, shortStateName, label);404                        // In case this node was already created, we may need to override the label here now that405                        // we know this is a hot state. This is because, unfortunately, other OnMonitor* methods406                        // do not provide the isInHotState parameter.407                        target.Label = label;408                        this.GetOrCreateEventLink(source, target, info);409                    }410                }411            }412        }413        /// <inheritdoc/>414        public void OnMonitorError(string monitorType, string stateName, bool? isInHotState)415        {416            var source = this.GetOrCreateChild(monitorType, monitorType, stateName);417            source.Category = "Error";418        }419        /// <inheritdoc/>420        public void OnRandom(object result, string callerName, string callerType)421        {422        }423        /// <inheritdoc/>424        public void OnAssertionFailure(string error)425        {426        }427        /// <inheritdoc/>428        public void OnStrategyDescription(string strategyName, string description)429        {430        }431        /// <inheritdoc/>432        public void OnCompleted()433        {434        }435        /// <summary>436        /// Return current graph and reset for next iteration.437        /// </summary>438        /// <param name="reset">Set to true will reset the graph for the next iteration.</param>439        /// <returns>The graph.</returns>440        public Graph SnapshotGraph(bool reset)441        {442            Graph result = this.CurrentGraph;443            if (reset)444            {445                // start fresh.446                this.CurrentGraph = null;447            }448            return result;449        }450        private string GetResolveActorId(string name, string type)451        {452            if (type is null)453            {454                // The sender id can be null if an event is fired from non-actor code.455                return ExternalCodeName;456            }457            if (this.CollapseMachineInstances)458            {459                return type;460            }461            return name;462        }463        private EventInfo AddEvent(string targetName, string targetType, string senderName, string senderType,464            string senderStateName, string eventName)465        {466            string targetId = this.GetResolveActorId(targetName, targetType);467            EventInfo info = null;468            lock (this.Inbox)469            {470                if (!this.Inbox.TryGetValue(targetId, out List<EventInfo> inbox))471                {472                    inbox = new List<EventInfo>();473                    this.Inbox[targetId] = inbox;474                }475                info = new EventInfo()476                {477                    Name = senderName ?? ExternalCodeName,478                    Type = senderType ?? ExternalCodeName,479                    State = senderStateName,480                    Event = eventName481                };482                inbox.Add(info);483            }484            return info;485        }486        private void LinkTransition(Type transitionType, ActorId id, string handlingStateName,487            string currentStateName, string newStateName)488        {489            string name = id.Name;490            string type = id.Type;491            lock (this.Inbox)492            {493                if (this.Dequeued.TryGetValue(id, out EventInfo info))494                {495                    // Event was dequeued, but now we know what state is handling this event, so connect the dots...496                    if (info.Type != type || info.Name != name || info.State != currentStateName)497                    {498                        var source = this.GetOrCreateChild(info.Name, info.Type, info.State);499                        var target = this.GetOrCreateChild(name, type, currentStateName);500                        info.HandlingState = handlingStateName;501                        this.GetOrCreateEventLink(source, target, info);502                    }503                }504                if (newStateName != null)505                {506                    // Then this is a goto or push and we can draw that link also.507                    var source = this.GetOrCreateChild(name, type, currentStateName);508                    var target = this.GetOrCreateChild(name, type, newStateName);509                    if (info is null)510                    {511                        info = new EventInfo { Event = transitionType.FullName };512                    }513                    this.GetOrCreateEventLink(source, target, info);514                }515                this.Dequeued.Remove(id);516            }517        }518        private GraphNode GetOrCreateChild(string name, string type, string stateName, string label = null)519        {520            GraphNode child = null;521            lock (this.Inbox)522            {523                this.AddNamespace(type);524                var initalStateName = stateName;525                // make label relative to fully qualified actor id (it's usually a nested class).526                stateName = this.GetLabel(name, type, stateName);527                string id = this.GetResolveActorId(name, type);528                GraphNode parent = this.Graph.GetOrCreateNode(id);529                parent.AddAttribute("Group", "Expanded");530                if (string.IsNullOrEmpty(label))531                {532                    label = stateName ?? ExternalStateName;533                }534                if (!string.IsNullOrEmpty(stateName))535                {536                    id += "." + stateName;537                }538                child = this.Graph.GetOrCreateNode(id, label);539                this.Graph.GetOrCreateLink(parent, child, null, null, "Contains");540            }541            return child;542        }543        private GraphLink GetOrCreateEventLink(GraphNode source, GraphNode target, EventInfo e)544        {545            GraphLink link = null;546            lock (this.Inbox)547            {548                string label = this.GetEventLabel(e.Event);549                var index = this.GetLinkIndex(source, target, label);550                var category = GetEventCategory(e.Event);551                link = this.Graph.GetOrCreateLink(source, target, index, label, category);552                if (this.MergeEventLinks)553                {554                    if (link.AddListAttribute("EventIds", e.Event) > 1)555                    {556                        link.Label = "*";557                    }...GetOrCreateEventLink
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.Coyote;7using Microsoft.Coyote.Actors;8using Microsoft.Coyote.Actors.Coverage;9using Microsoft.Coyote.Actors.Coverage.Runtime;10using Microsoft.Coyote.IO;11using Microsoft.Coyote.Specifications;12using Microsoft.Coyote.Testing;13using Microsoft.Coyote.Testing.Services;14using Microsoft.Coyote.Testing.Systematic;15using Microsoft.Coyote.Testing.Systematic.Strategies;16using Microsoft.Coyote.Testing.Systematic.TestReports;17using Microsoft.Coyote.Tasks;18using Microsoft.Coyote.Timers;19using Microsoft.Coyote.Timers.Coverage;20{21    {22        static void Main(string[] args)23        {24            var runtime = new SystematicTestingRuntime();25            var configuration = Configuration.Create();26            configuration.Verbose = 3;27            configuration.SchedulingStrategy = SchedulingStrategy.DFS;28            configuration.MaxFairSchedulingSteps = 1000;29            configuration.MaxUnfairSchedulingSteps = 1000;30            configuration.TestingIterations = 1;31            configuration.ReportActivityCoverage = true;32            configuration.ReportStateCoverage = true;33            configuration.ReportEventCoverage = true;34            configuration.ReportCoverage = true;35            runtime.CreateActor(typeof(Master));36            runtime.Run(configuration);37        }38    }39    {40        protected override async Task OnInitializeAsync(Event initialEvent)41        {42            ActorRuntimeLogGraphBuilder builder = new ActorRuntimeLogGraphBuilder();43            builder.GetOrCreateEventLink(1, 2);44            builder.GetOrCreateEventLink(2, 3);45            builder.GetOrCreateEventLink(3, 4);46            builder.GetOrCreateEventLink(4, 5);47            builder.GetOrCreateEventLink(5, 6);48            builder.GetOrCreateEventLink(6, 7);49            builder.GetOrCreateEventLink(7, 8);50            builder.GetOrCreateEventLink(8, 9);51            builder.GetOrCreateEventLink(9, 10);52            builder.GetOrCreateEventLink(10, 11);53            builder.GetOrCreateEventLink(11, 12);54            builder.GetOrCreateEventLink(12, 13);55            builder.GetOrCreateEventLink(13, 14);56            builder.GetOrCreateEventLink(14, 15);GetOrCreateEventLink
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.Coyote.Actors;7using Microsoft.Coyote.Actors.Coverage;8using Microsoft.Coyote.Actors.SharedObjects;9using Microsoft.Coyote.Actors.Timers;10using Microsoft.Coyote.Specifications;11using Microsoft.Coyote.SystematicTesting;12using Microsoft.Coyote.Tests.Common;13using Microsoft.Coyote.Tests.Common.Actors;14using Microsoft.Coyote.Tests.Common.Coverage;15using Microsoft.Coyote.Tests.Common.Runtime;16using Microsoft.Coyote.Tests.Common.TestingServices;17using Microsoft.Coyote.Tests.Common.Utilities;18using Microsoft.Coyote.Tests.Systematic;19using Microsoft.Coyote.Tests.Systematic.Coverage;20using Microsoft.Coyote.Tests.Systematic.TestingServices;21using Microsoft.Coyote.Tests.Systematic.Threading;22using Microsoft.Coyote.Tests.Systematic.Threading.Actors;23using Microsoft.Coyote.Tests.Systematic.Threading.Tasks;24using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.Coverage;25using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.SharedObjects;26using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.Timers;27using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.Timers.Coverage;28using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.WaitTasks;29using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.WaitTasks.Coverage;30using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.WaitTasks.SharedObjects;31using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.WaitTasks.Timers;32using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.WaitTasks.Timers.Coverage;33using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.WaitTasks.Timers.SharedObjects;34using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.WaitTasks.Timers.SharedObjects.Coverage;35using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.WaitTasks.Timers.SharedObjects.Timers;36using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.WaitTasks.Timers.SharedObjects.Timers.Coverage;37using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.WaitTasks.Timers.SharedObjects.Timers.SharedObjects;38using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.WaitTasks.Timers.SharedObjects.Timers.SharedObjects.Coverage;39using Microsoft.Coyote.Tests.Systematic.Threading.Tasks.WaitTasks.Timers.SharedObjects.Timers.SharedObjects.SharedObjects;GetOrCreateEventLink
Using AI Code Generation
1        {2            public static void GetOrCreateEventLink(this ActorRuntimeLogGraphBuilder builder, string source, string target, string eventName)3            {4                if (builder.GetEventLink(source, target, eventName) == null)5                {6                    builder.CreateEventLink(source, target, eventName);7                }8            }9        }10        [OnEventDoAction(typeof(Start), nameof(StartHandler))]11        [OnEventDoAction(typeof(Stop), nameof(StopHandler))]12        [OnEventDoAction(typeof(Reset), nameof(ResetHandler))]13        [OnEventDoAction(typeof(TimerElapsedEvent), nameof(TimerElapsedHandler))]14        [OnEventDoAction(typeof(ReceiveRequest), nameof(ReceiveRequestHandler))]15        [OnEventDoAction(typeof(ReceiveResponse), nameof(ReceiveResponseHandler))]16        {17            private int _count = 0;18            private int _max = 0;19            private int _num = 0;20            private int _numOfRequests = 0;21            private int _numOfResponses = 0;22            private List<int> _responseTimes = new List<int>();23            private List<int> _responseTimes2 = new List<int>();24            private List<int> _responseTimes3 = new List<int>();25            private List<int> _responseTimes4 = new List<int>();26            private List<int> _responseTimes5 = new List<int>();27            private List<int> _responseTimes6 = new List<int>();28            private List<int> _responseTimes7 = new List<int>();29            private List<int> _responseTimes8 = new List<int>();30            private List<int> _responseTimes9 = new List<int>();31            private List<int> _responseTimes10 = new List<int>();32            private List<int> _responseTimes11 = new List<int>();33            private List<int> _responseTimes12 = new List<int>();34            private List<int> _responseTimes13 = new List<int>();35            private List<int> _responseTimes14 = new List<int>();36            private List<int> _responseTimes15 = new List<int>();37            private List<int> _responseTimes16 = new List<int>();38            private List<int> _responseTimes17 = new List<int>();39            private List<int> _responseTimes18 = new List<int>();40            private List<int> _responseTimes19 = new List<int>();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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
