Repository navigation
Server: make StateMachines the one state-machine builder - #5225
Conversation
The Fluent IStateMachineBuilder had its own transition-hook implementation next to StateMachines.StateMachineBuilder<T>, and both namespaces exposed an AsStateMachine() extension on INodeBuilder<T>, so a file importing both did not compile. - Fluent IStateMachineBuilder is now a thin adapter (FluentStateMachineBuilderAdapter) over StateMachineBuilder<T>. Its enter/exit/transition hooks, guards and causes go through the StateMachines dispatcher. The polled, retrying timed cause moved to PolledTimedCauseScheduler. The Fluent facade files now live under StateMachines/Fluent (namespace unchanged), so StateMachines owns all state-machine building. - StateMachineBuilder<T> gains WithCause(MethodState, causeId), WithTimedCause(from, timeout, causeId) and an internal source-state guard slot that runs after the machine's pre-existing OnBeforeTransition (the Fluent guard contract). - Fluent AsStateMachine takes an optional timedTransitionTickInterval. With both namespaces imported, the StateMachines overload is now the better match. The old single-parameter signature stays as a hidden non-extension method for binary compatibility. - Fluent path behaviour changes: callbacks run exit -> transition -> enter (was exit -> enter -> transition); transition-observer exceptions are logged; WithCause on a FluentFiniteStateMachineState adds HasCause references. Tests: compile test importing both namespaces, binary-compat entry point, Fluent-vs-StateMachines parity (results, callbacks, CurrentState/ LastTransition, transition and audit events, HasCause references), and the new WithCause/WithTimedCause overloads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Dispatcher installation can silently remove an existing rejecting guard, while exceptions in pre-existing after-transition handlers suppress Fluent lifecycle callbacks and timed-cause rearming.
2 open issues
What changed in this PR
Consolidates server state-machine building around StateMachines.StateMachineBuilder<T>, retaining the Fluent API as an adapter and resolving the ambiguous AsStateMachine() call when both namespaces are imported.
Changes:
- Adds explicit method-to-cause binding, one-shot timed causes, and Fluent-compatible source-state guards.
- Replaces the separate Fluent implementation with an adapter, preserving polling retries and the legacy binary entry point.
- Adds entry-point and behavior-parity tests, updates lifecycle tests, and documents overload selection.
| File | Description |
|---|---|
tests/Opc.Ua.Server.Tests/StateMachines/StateMachineLifetimeRegressionTests.cs |
Uses the adapter and direct scheduler access in existing tests. |
tests/Opc.Ua.Server.Tests/StateMachines/StateMachineBuilderEntryPointTests.cs |
Tests overload binding, behavior parity, and new cause APIs. |
src/Opc.Ua.Server/StateMachines/StateMachineBuilderExtensions.cs |
Clarifies an XML documentation reference to WithCause. |
src/Opc.Ua.Server/StateMachines/StateMachineBuilder.cs |
Adds explicit causes, timed causes, source-state guards, and polling logs. |
src/Opc.Ua.Server/StateMachines/Fluent/StateMachineBuilderExtensions.cs |
Routes Fluent entry points through the adapter and resolves overload ambiguity. |
src/Opc.Ua.Server/StateMachines/Fluent/PolledTimedCauseScheduler.cs |
Extracts Fluent timed-cause polling, retries, and warning suppression. |
src/Opc.Ua.Server/StateMachines/Fluent/IStateMachineBuilder.cs |
Updates the relocated interface’s entry-point documentation reference. |
src/Opc.Ua.Server/StateMachines/Fluent/FluentStateMachineBuilderAdapter.cs |
Delegates Fluent operations to the shared builder while retaining Fluent-specific behavior. |
src/Opc.Ua.Server/Fluent/StateMachineBuilder.cs |
Removes the separate Fluent builder implementation. |
docs/StateMachines.md |
Explains entry-point selection, semantic differences, and new cause APIs. |
Review effort: Balanced
…ailures - Capture the machine's transition callback factories when a dispatcher installs itself, not when it is constructed. Two builders created for the same machine before either registered a hook both captured the empty factory, so the second install silently dropped the first's guards and observers. - A throwing pre-existing OnAfterTransition (or synchronizer) no longer skips the exit/transition/enter callbacks and timer re-arming after the state has committed. The failure still reaches the caller once the lifecycle has been dispatched, matching the asynchronous path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Code coverage✅ Coverage gate passed.
Uncovered changed lines
Coverage is above the recorded baseline - consider ratcheting Thresholds live in |
…dard into romanett/md1-statemachine-builder-6e82b3 # Conflicts: # src/Opc.Ua.Server/Fluent/StateMachineBuilder.cs
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #5225 +/- ##
==========================================
- Coverage 83.88% 83.86% -0.03%
==========================================
Files 3879 3880 +1
Lines 575744 575760 +16
Branches 99275 99273 -2
==========================================
- Hits 482956 482848 -108
- Misses 60108 60206 +98
- Partials 32680 32706 +26
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|


Proposed changes
Opc.Ua.Serverhad two state-machine builders: the FluentIStateMachineBuilder(namespaceOpc.Ua.Server.Fluent) andStateMachineBuilder<T>(namespaceOpc.Ua.Server.StateMachines). Both namespaces define anAsStateMachine()extension onINodeBuilder<T>, so a file that imports both fails with an ambiguous-call error (MD-1).StateMachineBuilder<T>is now the only implementation, and the Fluent builder forwards to it.What changed
WithCause(MethodState method, uint causeId, bool reportExecutable = true)maps a method to an explicit cause id. The method can be anywhere in the node manager.WithTimedCause(uint fromStateId, TimeSpan timeout, uint causeId)sets up a one-shot timed cause.OnBeforeTransitionhandler, which is the order Fluent guards ran in.FluentStateMachineBuilderAdapter<T>wrapsStateMachineBuilder<T>. It keeps the Fluent argument validation and the "method already has a handler" check.PolledTimedCauseScheduler.StateMachines/Fluent/. The namespace is unchanged.AsStateMachinegained an optionalTimeSpan? timedTransitionTickInterval = nullparameter.[EditorBrowsable(Never)]) non-extension method, so assemblies compiled against 2.0.x keep working.Public API changes (all additions)
StateMachineBuilder<T>.WithCause(MethodState, uint, bool = true)StateMachineBuilder<T>.WithTimedCause(uint, TimeSpan, uint)Opc.Ua.Server.Fluent.StateMachineBuilderExtensions.AsStateMachine<T>(this INodeBuilder<T>, TimeSpan? = null); the old one-parameter signature is kept, hidden.FluentStateMachineBuilder<T>is removed.Behaviour changes on the Fluent path only
WithCauseon aFluentFiniteStateMachineStatenow adds the Part 16HasCausereferences.Transition-path unification
ProgramRuntimealready commits throughFiniteStateMachineState.PrepareTransition/CommitTransition, so nothing changes there.FiniteStateMachineDispatcher(used by Di, Extrusion, MachineTool and Robotics) still writesCurrentState/LastTransitiondirectly. Moving it onto the transition core needs state tables from the generator, and it changes what those servers emit. That is tracked in Server: route FiniteStateMachineDispatcher through the FiniteStateMachineState transition core #5224.Related Issues
Types of changes
Checklist
Further comments
Tests. The new
StateMachineBuilderEntryPointTestsfile imports both namespaces. It covers:CurrentState/LastTransition, transition and audit events, andHasCausereferences,WithCauseandWithTimedCauseoverloads.Local results on net10.0:
Opc.Ua.Server.Testsdocs/StateMachines.mdnow explains whichAsStateMachine()you get when both namespaces are imported.🤖 Generated with Claude Code