private StateMachine<ImportFinancialGuaranteeStatus, Trigger> CreateStateMachine()
        {
            var stateMachine = new StateMachine<ImportFinancialGuaranteeStatus, Trigger>(() => Status, s => Status = s);

            stateMachine.OnTransitioned(OnTransitionAction);

            completeTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.Complete);

            stateMachine.Configure(ImportFinancialGuaranteeStatus.ApplicationReceived)
                .Permit(Trigger.Complete, ImportFinancialGuaranteeStatus.ApplicationComplete);

            stateMachine.Configure(ImportFinancialGuaranteeStatus.ApplicationComplete)
                .OnEntryFrom(completeTrigger, OnComplete)
                .Permit(Trigger.Approve, ImportFinancialGuaranteeStatus.Approved)
                .Permit(Trigger.Refuse, ImportFinancialGuaranteeStatus.Refused);

            stateMachine.Configure(ImportFinancialGuaranteeStatus.Approved)
                .Permit(Trigger.Release, ImportFinancialGuaranteeStatus.Released);
                
            return stateMachine;
        }
Пример #2
0
        static void Main(string[] args)
        {
            var phoneCall = new StateMachine<State, Trigger>(State.OffHook);
            setVolumeTrigger = phoneCall.SetTriggerParameters<int>(Trigger.SetVolume);

            phoneCall.Configure(State.OffHook)
                .Permit(Trigger.CallDialed, State.Ringing);

            phoneCall.Configure(State.Ringing)
                .Permit(Trigger.HungUp, State.OffHook)
                .Permit(Trigger.CallConnected, State.Connected);

            phoneCall.Configure(State.Connected)
                .OnEntry(t => StartCallTimer())
                .OnExit(t => StopCallTimer())
                .InternalTransition(Trigger.MuteMicrophone, t => OnMute())
                .InternalTransition(Trigger.UnmuteMicrophone, t => OnUnmute())
                .InternalTransition<int>(setVolumeTrigger, (volume, t) => OnSetVolume(volume))
                .Permit(Trigger.LeftMessage, State.OffHook)
                .Permit(Trigger.HungUp, State.OffHook)
                .Permit(Trigger.PlacedOnHold, State.OnHold);

            phoneCall.Configure(State.OnHold)
                .SubstateOf(State.Connected)
                .Permit(Trigger.TakenOffHold, State.Connected)
                .Permit(Trigger.HungUp, State.OffHook)
                .Permit(Trigger.PhoneHurledAgainstWall, State.PhoneDestroyed);

            Print(phoneCall);
            Fire(phoneCall, Trigger.CallDialed);
            Print(phoneCall);
            Fire(phoneCall, Trigger.CallConnected);
            Print(phoneCall);
            SetVolume(phoneCall, 2);
            Print(phoneCall);
            Fire(phoneCall, Trigger.PlacedOnHold);
            Print(phoneCall);
            Fire(phoneCall, Trigger.MuteMicrophone);
            Print(phoneCall);
            Fire(phoneCall, Trigger.UnmuteMicrophone);
            Print(phoneCall);
            Fire(phoneCall, Trigger.TakenOffHold);
            Print(phoneCall);
            SetVolume(phoneCall, 11);
            Print(phoneCall);
            Fire(phoneCall, Trigger.HungUp);
            Print(phoneCall);

            Console.WriteLine("Press any key...");
            Console.ReadKey(true);
        }
Пример #3
0
        protected Bug()
        {
            Machine = new StateMachine<States, Triggers>(() => State, s => State = s);
            AssignTrigger = Machine.SetTriggerParameters<string>(Triggers.Assign);

            Machine.Configure(States.Open)
               .Permit(Triggers.Assign, States.Assigned);

            Machine.Configure(States.Assigned)
                .SubstateOf(States.Open)
                .OnEntryFrom(AssignTrigger, OnAssigned)
                .PermitReentry(Triggers.Assign)
                .Permit(Triggers.Close, States.Closed)
                .Permit(Triggers.Defer, States.Deferred)
                .OnExit(OnDeassigned);

            Machine.Configure(States.Deferred)
                .OnEntry(() => Assignee = null)
                .Permit(Triggers.Assign, States.Assigned);
        }
Пример #4
0
        static void Main()
        {
            var sm = new StateMachine<State, Trigger>(State.A);

            sm.Configure(State.A)
              .Permit(Trigger.GoToB, State.B)
              .OnEntry(OnEnterA); // This never gets called.

            sm.Configure(State.B)
              .OnEntry(OnEnterB)
              .Permit(Trigger.GoToC, State.C);

            var goToCTrigger = sm.SetTriggerParameters<string>(Trigger.GoToC);

            sm.Configure(State.C)
              .OnEntryFrom(goToCTrigger, OnEnterC);

            Console.WriteLine(sm);
            sm.Fire(Trigger.GoToB);
            Console.WriteLine(sm);
            sm.Fire(goToCTrigger, "foo"); // Using Trigger.GoToC will throw an exception.
            Console.WriteLine(sm);
        }
Пример #5
0
        public Bug(string title)
        {
            _title = title;

            _machine = new StateMachine<State, Trigger>(() => _state, s => _state = s);

            _assignTrigger = _machine.SetTriggerParameters<string>(Trigger.Assign);

            _machine.Configure(State.Open)
                .Permit(Trigger.Assign, State.Assigned);

            _machine.Configure(State.Assigned)
                .SubstateOf(State.Open)
                .OnEntryFrom(_assignTrigger, assignee => OnAssigned(assignee))
                .PermitReentry(Trigger.Assign)
                .Permit(Trigger.Close, State.Closed)
                .Permit(Trigger.Defer, State.Deferred)
                .OnExit(() => OnDeassigned());

            _machine.Configure(State.Deferred)
                .OnEntry(() => _assignee = null)
                .Permit(Trigger.Assign, State.Assigned);
        }
        private StateMachine<NotificationStatus, Trigger> CreateStateMachine()
        {
            var stateMachine = new StateMachine<NotificationStatus, Trigger>(() => Status, s => Status = s);

            receivedTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.NotificationReceived);
            commencedTrigger = stateMachine.SetTriggerParameters<DateTime, string>(Trigger.AssessmentCommenced);
            completeTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.NotificationComplete);
            transmitTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.Transmit);
            acknowledgedTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.Acknowledged);
            withdrawTrigger = stateMachine.SetTriggerParameters<DateTime, string>(Trigger.Withdraw);
            objectTrigger = stateMachine.SetTriggerParameters<DateTime, string>(Trigger.Object);
            withdrawConsentTrigger = stateMachine.SetTriggerParameters<DateTime, string>(Trigger.WithdrawConsent);
            consentedTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.Consent);
            archiveTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.Archive);

            stateMachine.OnTransitioned(OnTransitionAction);

            stateMachine.Configure(NotificationStatus.NotSubmitted)
                .Permit(Trigger.Submit, NotificationStatus.Submitted)
                .Permit(Trigger.Archive, NotificationStatus.FileClosed);

            stateMachine.Configure(NotificationStatus.Submitted)
                .SubstateOf(NotificationStatus.InDetermination)
                .OnEntryFrom(Trigger.Submit, OnSubmit)
                .Permit(Trigger.NotificationReceived, NotificationStatus.NotificationReceived);

            stateMachine.Configure(NotificationStatus.NotificationReceived)
                .SubstateOf(NotificationStatus.InDetermination)
                .OnEntryFrom(receivedTrigger, OnReceived)
                .PermitIf(Trigger.AssessmentCommenced, NotificationStatus.InAssessment, () => Dates.PaymentReceivedDate.HasValue)
                .Permit(Trigger.Object, NotificationStatus.Objected);

            stateMachine.Configure(NotificationStatus.InAssessment)
                .SubstateOf(NotificationStatus.InDetermination)
                .OnEntryFrom(commencedTrigger, OnInAssessment)
                .Permit(Trigger.NotificationComplete, NotificationStatus.ReadyToTransmit)
                .Permit(Trigger.Object, NotificationStatus.Objected);

            stateMachine.Configure(NotificationStatus.ReadyToTransmit)
                .SubstateOf(NotificationStatus.InDetermination)
                .OnEntryFrom(completeTrigger, OnCompleted)
                .Permit(Trigger.Transmit, NotificationStatus.Transmitted)
                .Permit(Trigger.Object, NotificationStatus.Objected);

            stateMachine.Configure(NotificationStatus.Transmitted)
                .SubstateOf(NotificationStatus.InDetermination)
                .OnEntryFrom(transmitTrigger, OnTransmitted)
                .Permit(Trigger.Acknowledged, NotificationStatus.DecisionRequiredBy)
                .Permit(Trigger.Object, NotificationStatus.Objected);

            stateMachine.Configure(NotificationStatus.DecisionRequiredBy)
                .OnEntryFrom(acknowledgedTrigger, OnAcknowledged)
                .Permit(Trigger.Unlock, NotificationStatus.Unlocked)
                .Permit(Trigger.Consent, NotificationStatus.Consented)
                .Permit(Trigger.Object, NotificationStatus.Objected)
                .Permit(Trigger.Withdraw, NotificationStatus.Withdrawn)
                .Permit(Trigger.Archive, NotificationStatus.FileClosed);

            stateMachine.Configure(NotificationStatus.InDetermination)
                .Permit(Trigger.Withdraw, NotificationStatus.Withdrawn)
                .Permit(Trigger.Archive, NotificationStatus.FileClosed);

            stateMachine.Configure(NotificationStatus.Withdrawn)
                .OnEntryFrom(withdrawTrigger, OnWithdrawn)
                .Permit(Trigger.Archive, NotificationStatus.FileClosed);

            stateMachine.Configure(NotificationStatus.Objected)
                .OnEntryFrom(objectTrigger, OnObjected)
                .Permit(Trigger.Archive, NotificationStatus.FileClosed);

            stateMachine.Configure(NotificationStatus.Consented)
                .OnEntryFrom(consentedTrigger, OnConsented)
                .Permit(Trigger.WithdrawConsent, NotificationStatus.ConsentWithdrawn)
                .Permit(Trigger.Archive, NotificationStatus.FileClosed);

            stateMachine.Configure(NotificationStatus.ConsentWithdrawn)
                .OnEntryFrom(withdrawConsentTrigger, OnConsentWithdrawn)
                .Permit(Trigger.Archive, NotificationStatus.FileClosed);

            stateMachine.Configure(NotificationStatus.Unlocked)
                .SubstateOf(NotificationStatus.InDetermination)
                .Permit(Trigger.Resubmit, NotificationStatus.Reassessment);

            stateMachine.Configure(NotificationStatus.Reassessment)
                .SubstateOf(NotificationStatus.InDetermination)
                .Permit(Trigger.AcceptChanges, NotificationStatus.DecisionRequiredBy)
                .Permit(Trigger.RejectChanges, NotificationStatus.Unlocked);

            stateMachine.Configure(NotificationStatus.FileClosed)
                .OnEntryFrom(archiveTrigger, OnFileClosed);

            return stateMachine;
        }
        private StateMachine<FinancialGuaranteeStatus, Trigger> CreateStateMachine()
        {
            var stateMachine = new StateMachine<FinancialGuaranteeStatus, Trigger>(() => Status, s => Status = s);

            completedTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.Completed);
            approvedTrigger = stateMachine.SetTriggerParameters<ApprovalData>(Trigger.Approved);
            refusedTrigger = stateMachine.SetTriggerParameters<DateTime, string>(Trigger.Refused);
            releasedTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.Released);

            stateMachine.Configure(FinancialGuaranteeStatus.ApplicationReceived)
                .Permit(Trigger.Completed, FinancialGuaranteeStatus.ApplicationComplete);

            stateMachine.Configure(FinancialGuaranteeStatus.ApplicationComplete)
                .OnEntryFrom(completedTrigger, OnCompleted)
                .PermitIf(Trigger.Approved, FinancialGuaranteeStatus.Approved, () => CompletedDate.HasValue)
                .PermitIf(Trigger.Refused, FinancialGuaranteeStatus.Refused, () => CompletedDate.HasValue);

            stateMachine.Configure(FinancialGuaranteeStatus.Approved)
                .OnEntryFrom(approvedTrigger, OnApproved)
                .Permit(Trigger.Released, FinancialGuaranteeStatus.Released)
                .Permit(Trigger.Superseded, FinancialGuaranteeStatus.Superseded);

            stateMachine.Configure(FinancialGuaranteeStatus.Refused)
                .OnEntryFrom(refusedTrigger, OnRefused);

            stateMachine.Configure(FinancialGuaranteeStatus.Released)
                .OnEntryFrom(releasedTrigger, OnReleased);

            stateMachine.OnTransitioned(OnTransitionAction);

            return stateMachine;
        }
Пример #8
0
        private StateMachine<MovementStatus, Trigger> CreateStateMachine()
        {
            var stateMachine = new StateMachine<MovementStatus, Trigger>(() => Status, s => Status = s);

            submittedTrigger = stateMachine.SetTriggerParameters<Guid>(Trigger.Submit);
            internallySubmittedTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.SubmitInternal);
            completedTrigger = stateMachine.SetTriggerParameters<DateTime, Guid, Guid>(Trigger.Complete);
            internallyCompletedTrigger = stateMachine.SetTriggerParameters<DateTime, Guid>(Trigger.CompleteInternal);

            acceptedTrigger = stateMachine.SetTriggerParameters<AcceptedTriggerParameters>(Trigger.Receive);
            internallyAcceptedTrigger = stateMachine.SetTriggerParameters<InternallyAcceptedTriggerParameters>(Trigger.ReceiveInternal);

            stateMachine.OnTransitioned(OnTransitionAction);

            stateMachine.Configure(MovementStatus.New)
                .Permit(Trigger.Submit, MovementStatus.Submitted);

            stateMachine.Configure(MovementStatus.Submitted)
                .OnEntryFrom(submittedTrigger, OnSubmitted)
                .OnEntryFrom(internallySubmittedTrigger, OnInternallySubmitted)
                .Permit(Trigger.Receive, MovementStatus.Received)
                .Permit(Trigger.ReceiveInternal, MovementStatus.Received)
                .Permit(Trigger.Reject, MovementStatus.Rejected)
                .Permit(Trigger.Cancel, MovementStatus.Cancelled);

            stateMachine.Configure(MovementStatus.Received)
                .OnEntryFrom(acceptedTrigger, OnReceived)
                .OnEntryFrom(internallyAcceptedTrigger, OnInternallyReceived)
                .Permit(Trigger.Complete, MovementStatus.Completed)
                .Permit(Trigger.CompleteInternal, MovementStatus.Completed);

            stateMachine.Configure(MovementStatus.Completed)
                .OnEntryFrom(completedTrigger, OnCompleted)
                .OnEntryFrom(internallyCompletedTrigger, OnInternallyCompleted);

            stateMachine.Configure(MovementStatus.Captured)
                .Permit(Trigger.ReceiveInternal, MovementStatus.Received)
                .Permit(Trigger.Reject, MovementStatus.Rejected)
                .Permit(Trigger.SubmitInternal, MovementStatus.Submitted);

            return stateMachine;
        }
        private StateMachine<ImportNotificationStatus, Trigger> CreateStateMachine()
        {
            var stateMachine = new StateMachine<ImportNotificationStatus, Trigger>(() => Status, s => Status = s);

            stateMachine.OnTransitioned(OnTransition);

            receivedTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.Receive);
            fullyPaidTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.FullyPaid);
            beginAssessmentTrigger = stateMachine.SetTriggerParameters<DateTime, string>(Trigger.BeginAssessment);
            completeNotificationTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.CompleteNotification);
            acknowledgeTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.Acknowledge);
            consentedTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.Consent);
            withdrawConsentTrigger = stateMachine.SetTriggerParameters<DateTime, string>(Trigger.WithdrawConsent);
            fileClosedTrigger = stateMachine.SetTriggerParameters<DateTime>(Trigger.FileClosed);

            stateMachine.Configure(ImportNotificationStatus.New)
                .Permit(Trigger.Receive, ImportNotificationStatus.NotificationReceived);

            stateMachine.Configure(ImportNotificationStatus.NotificationReceived)
                .OnEntryFrom(receivedTrigger, OnReceived)
                .Permit(Trigger.Submit, ImportNotificationStatus.AwaitingPayment)
                .Permit(Trigger.Withdraw, ImportNotificationStatus.Withdrawn)
                .Permit(Trigger.Object, ImportNotificationStatus.Objected)
                .Permit(Trigger.FileClosed, ImportNotificationStatus.FileClosed);

            stateMachine.Configure(ImportNotificationStatus.AwaitingPayment)
                .SubstateOf(ImportNotificationStatus.Submitted)
                .Permit(Trigger.FullyPaid, ImportNotificationStatus.AwaitingAssessment)
                .Permit(Trigger.Withdraw, ImportNotificationStatus.Withdrawn)
                .Permit(Trigger.Object, ImportNotificationStatus.Objected)
                .Permit(Trigger.FileClosed, ImportNotificationStatus.FileClosed);

            stateMachine.Configure(ImportNotificationStatus.AwaitingAssessment)
                .SubstateOf(ImportNotificationStatus.Submitted)
                .OnEntryFrom(fullyPaidTrigger, OnFullyPaid)
                .Permit(Trigger.BeginAssessment, ImportNotificationStatus.InAssessment)
                .Permit(Trigger.Withdraw, ImportNotificationStatus.Withdrawn)
                .Permit(Trigger.Object, ImportNotificationStatus.Objected)
                .Permit(Trigger.FileClosed, ImportNotificationStatus.FileClosed);
            
            stateMachine.Configure(ImportNotificationStatus.InAssessment)
                .OnEntryFrom(beginAssessmentTrigger, OnAssessmentStarted)
                .Permit(Trigger.CompleteNotification, ImportNotificationStatus.ReadyToAcknowledge)
                .Permit(Trigger.Object, ImportNotificationStatus.Objected)
                .Permit(Trigger.Withdraw, ImportNotificationStatus.Withdrawn)
                .Permit(Trigger.FileClosed, ImportNotificationStatus.FileClosed);

            stateMachine.Configure(ImportNotificationStatus.ReadyToAcknowledge)
                .OnEntryFrom(completeNotificationTrigger, OnNotificationCompleted)
                .Permit(Trigger.Acknowledge, ImportNotificationStatus.DecisionRequiredBy)
                .Permit(Trigger.Withdraw, ImportNotificationStatus.Withdrawn)
                .Permit(Trigger.Object, ImportNotificationStatus.Objected)
                .Permit(Trigger.FileClosed, ImportNotificationStatus.FileClosed);

            stateMachine.Configure(ImportNotificationStatus.DecisionRequiredBy)
                .OnEntryFrom(acknowledgeTrigger, OnAcknowledged)
                .Permit(Trigger.Consent, ImportNotificationStatus.Consented)
                .Permit(Trigger.Object, ImportNotificationStatus.Objected)
                .Permit(Trigger.Withdraw, ImportNotificationStatus.Withdrawn)
                .Permit(Trigger.FileClosed, ImportNotificationStatus.FileClosed);

            stateMachine.Configure(ImportNotificationStatus.Consented)
                .OnEntryFrom(consentedTrigger, OnConsented)
                .Permit(Trigger.WithdrawConsent, ImportNotificationStatus.ConsentWithdrawn)
                .Permit(Trigger.FileClosed, ImportNotificationStatus.FileClosed);

            stateMachine.Configure(ImportNotificationStatus.ConsentWithdrawn)
                .OnEntryFrom(withdrawConsentTrigger, OnConsentWithdrawn)
                .Permit(Trigger.FileClosed, ImportNotificationStatus.FileClosed);

            stateMachine.Configure(ImportNotificationStatus.Objected)
                .Permit(Trigger.FileClosed, ImportNotificationStatus.FileClosed);

            stateMachine.Configure(ImportNotificationStatus.Withdrawn)
                .Permit(Trigger.FileClosed, ImportNotificationStatus.FileClosed);

            stateMachine.Configure(ImportNotificationStatus.FileClosed)
                .OnEntryFrom(fileClosedTrigger, OnFileClosed);

            return stateMachine;
        }