Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.

Commit 6152492

Browse files
akashlalpdeligia
authored andcommitted
Akashl/monitor state inheritence wildcard (#446)
1 parent ce2e507 commit 6152492

File tree

10 files changed

+800
-52
lines changed

10 files changed

+800
-52
lines changed

Source/Core/Runtime/Machines/MachineState.cs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,21 @@ internal void InitializeState()
7777
this.IgnoredEvents = new HashSet<Type>();
7878
this.DeferredEvents = new HashSet<Type>();
7979

80-
var entryAttribute = this.GetType().GetCustomAttribute(typeof(OnEntryAttribute), true) as OnEntryAttribute;
81-
var exitAttribute = this.GetType().GetCustomAttribute(typeof(OnExitAttribute), true) as OnExitAttribute;
82-
83-
if (entryAttribute != null)
80+
if (this.GetType().GetCustomAttribute(typeof(OnEntryAttribute), true) is OnEntryAttribute entryAttribute)
8481
{
8582
this.EntryAction = entryAttribute.Action;
8683
}
8784

88-
if (exitAttribute != null)
85+
if (this.GetType().GetCustomAttribute(typeof(OnExitAttribute), true) is OnExitAttribute exitAttribute)
8986
{
9087
this.ExitAction = exitAttribute.Action;
9188
}
9289

90+
if (this.GetType().IsDefined(typeof(StartAttribute), false))
91+
{
92+
this.IsStart = true;
93+
}
94+
9395
// Events with already declared handlers.
9496
var handledEvents = new HashSet<Type>();
9597

@@ -99,11 +101,6 @@ internal void InitializeState()
99101
this.InstallActionHandlers(handledEvents);
100102
this.InstallIgnoreHandlers(handledEvents);
101103
this.InstallDeferHandlers(handledEvents);
102-
103-
if (this.GetType().IsDefined(typeof(StartAttribute), false))
104-
{
105-
this.IsStart = true;
106-
}
107104
}
108105

109106
/// <summary>
@@ -289,9 +286,7 @@ private void InheritActionHandlers(Type baseState, HashSet<Type> handledEvents)
289286
/// </summary>
290287
private void InstallIgnoreHandlers(HashSet<Type> handledEvents)
291288
{
292-
var ignoreEventsAttribute = this.GetType().GetCustomAttribute(typeof(IgnoreEventsAttribute), false) as IgnoreEventsAttribute;
293-
294-
if (ignoreEventsAttribute != null)
289+
if (this.GetType().GetCustomAttribute(typeof(IgnoreEventsAttribute), false) is IgnoreEventsAttribute ignoreEventsAttribute)
295290
{
296291
foreach (var e in ignoreEventsAttribute.Events)
297292
{
@@ -315,9 +310,7 @@ private void InheritIgnoreHandlers(Type baseState, HashSet<Type> handledEvents)
315310
return;
316311
}
317312

318-
var ignoreEventsAttribute = baseState.GetCustomAttribute(typeof(IgnoreEventsAttribute), false) as IgnoreEventsAttribute;
319-
320-
if (ignoreEventsAttribute != null)
313+
if (baseState.GetCustomAttribute(typeof(IgnoreEventsAttribute), false) is IgnoreEventsAttribute ignoreEventsAttribute)
321314
{
322315
foreach (var e in ignoreEventsAttribute.Events)
323316
{
@@ -341,8 +334,7 @@ private void InheritIgnoreHandlers(Type baseState, HashSet<Type> handledEvents)
341334
/// </summary>
342335
private void InstallDeferHandlers(HashSet<Type> handledEvents)
343336
{
344-
var deferEventsAttribute = this.GetType().GetCustomAttribute(typeof(DeferEventsAttribute), false) as DeferEventsAttribute;
345-
if (deferEventsAttribute != null)
337+
if (this.GetType().GetCustomAttribute(typeof(DeferEventsAttribute), false) is DeferEventsAttribute deferEventsAttribute)
346338
{
347339
foreach (var e in deferEventsAttribute.Events)
348340
{
@@ -366,8 +358,7 @@ private void InheritDeferHandlers(Type baseState, HashSet<Type> handledEvents)
366358
return;
367359
}
368360

369-
var deferEventsAttribute = baseState.GetCustomAttribute(typeof(DeferEventsAttribute), false) as DeferEventsAttribute;
370-
if (deferEventsAttribute != null)
361+
if (baseState.GetCustomAttribute(typeof(DeferEventsAttribute), false) is DeferEventsAttribute deferEventsAttribute)
371362
{
372363
foreach (var e in deferEventsAttribute.Events)
373364
{
@@ -393,7 +384,7 @@ private static void CheckEventHandlerAlreadyDeclared(Type e, HashSet<Type> handl
393384
{
394385
if (handledEvents.Contains(e))
395386
{
396-
throw new InvalidOperationException($"declared multiple handlers for '{e}'");
387+
throw new InvalidOperationException($"declared multiple handlers for event '{e}'");
397388
}
398389
}
399390

@@ -404,7 +395,7 @@ private static void CheckEventHandlerAlreadyInherited(Type e, Type baseState, Ha
404395
{
405396
if (handledEvents.Contains(e))
406397
{
407-
throw new InvalidOperationException($"inherited multiple handlers for '{e}' from state '{baseState}'");
398+
throw new InvalidOperationException($"inherited multiple handlers for event '{e}' from state '{baseState}'");
408399
}
409400
}
410401
}

Source/Core/Runtime/Specifications/Monitor.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ internal void MonitorEvent(Event e)
246246
private void HandleEvent(Event e)
247247
{
248248
// Do not process an ignored event.
249-
if (this.IgnoredEvents.Contains(e.GetType()))
249+
if (this.IsEventIgnoredInCurrentState(e))
250250
{
251251
return;
252252
}
@@ -283,17 +283,43 @@ private void HandleEvent(Event e)
283283
var transition = this.GotoTransitions[e.GetType()];
284284
this.GotoState(transition.TargetState, transition.Lambda);
285285
}
286+
else if (this.GotoTransitions.ContainsKey(typeof(WildCardEvent)))
287+
{
288+
// Checks if the event can trigger a goto state transition.
289+
var transition = this.GotoTransitions[typeof(WildCardEvent)];
290+
this.GotoState(transition.TargetState, transition.Lambda);
291+
}
286292
else if (this.ActionBindings.ContainsKey(e.GetType()))
287293
{
288294
// Checks if the event can trigger an action.
289295
var handler = this.ActionBindings[e.GetType()];
290296
this.Do(handler.Name);
291297
}
298+
else if (this.ActionBindings.ContainsKey(typeof(WildCardEvent)))
299+
{
300+
// Checks if the event can trigger an action.
301+
var handler = this.ActionBindings[typeof(WildCardEvent)];
302+
this.Do(handler.Name);
303+
}
292304

293305
break;
294306
}
295307
}
296308

309+
/// <summary>
310+
/// Checks if the specified event is ignored in the current monitor state.
311+
/// </summary>
312+
private bool IsEventIgnoredInCurrentState(Event e)
313+
{
314+
if (this.IgnoredEvents.Contains(e.GetType()) ||
315+
this.IgnoredEvents.Contains(typeof(WildCardEvent)))
316+
{
317+
return true;
318+
}
319+
320+
return false;
321+
}
322+
297323
/// <summary>
298324
/// Invokes an action.
299325
/// </summary>
@@ -417,7 +443,9 @@ private void GotoState(Type s, string onExitActionName)
417443
private bool CanHandleEvent(Type e)
418444
{
419445
if (this.GotoTransitions.ContainsKey(e) ||
446+
this.GotoTransitions.ContainsKey(typeof(WildCardEvent)) ||
420447
this.ActionBindings.ContainsKey(e) ||
448+
this.ActionBindings.ContainsKey(typeof(WildCardEvent)) ||
421449
e == typeof(GotoStateEvent))
422450
{
423451
return true;

0 commit comments

Comments
 (0)