/// <summary> /// Initializes a new instance of the <see cref="Stateflow.Workflow.WorkflowEntity"/> class. /// </summary> /// <param name="workflowDefinition">Workflow definition.</param> protected WorkflowEntity(WorkflowDefinition <TIdentifier> workflowDefinition) { _workflowEngine = new WorkflowEngine <TIdentifier>(workflowDefinition, null, this); _workflowEngine.OnStateTransition += WorkflowEngine_OnStateTransition; }
private void ToStateMachine(WorkflowDefinition <TIdentifier> workflowDefinition, State <TIdentifier> currentState) { Enforce.ArgumentNotNull(workflowDefinition, "workflowDefinition"); _workflowDefinition = workflowDefinition; CurrentState = currentState ?? workflowDefinition.Transitions.First().FromState; _stateMachine = new StateMachine <State <TIdentifier>, Trigger <TIdentifier> > (() => CurrentState, s => CurrentState = s); // Get a distinct list of states with a trigger from state configuration // "State => Trigger => TargetState var states = workflowDefinition.Transitions.AsQueryable() .Select(x => x.FromState) .Distinct() .Select(x => x) .ToList(); // Assign triggers to states states.ForEach(state => { var triggers = workflowDefinition.Transitions.AsQueryable() .Where(config => config.FromState == state) .Select(config => new { Trigger = config.TriggerBy, TargetState = config.ToState, Condition = config.Condition, IsReentrant = config.IsReentrant }) .ToList(); var stateConfig = _stateMachine.Configure(state); triggers.ForEach(trig => { if (trig.Condition == null) { if (trig.IsReentrant) { stateConfig.PermitReentry(trig.Trigger); } else { stateConfig.Permit(trig.Trigger, trig.TargetState); } } else { stateConfig.PermitIf(trig.Trigger, trig.TargetState, ConditionalGuard(trig.Condition)); } }); if (state.SuperState != null) { stateConfig.SubstateOf(state.SuperState); } if (state.EntryAction != null) { stateConfig.OnEntry(t => ExecuteAction(t, state.EntryAction)); } if (state.ExitAction != null) { stateConfig.OnExit(t => ExecuteAction(t, state.ExitAction)); } }); // Handle exceptions _stateMachine.OnUnhandledTrigger(OnUnhandledTrigger); // For all the state transitions _stateMachine.OnTransitioned(OnTransitionAction); }
/// <summary> /// Initializes a new instance of the <see cref="Stateflow.Workflow.WorkflowEngine"/> class. /// </summary> /// <param name="workflowDefinition">Workflow definition.</param> /// <param name="currentState">Current state.</param> public WorkflowEngine(WorkflowDefinition <TIdentifier> workflowDefinition, State <TIdentifier> currentState) : this(workflowDefinition, currentState, null) { }
/// <summary> /// Initializes a new instance of the <see cref="WorkflowEngine"/> class. /// </summary> /// <param name="workflowDefinition">The workflow definition.</param> /// <param name="currentState">Current state of the workflow. If null, it will be first state found in the workflow definition.</param> /// <param name="workflowContext">The context that the workflow engine is bound to.</param> public WorkflowEngine(WorkflowDefinition <TIdentifier> workflowDefinition, State <TIdentifier> currentState, object workflowContext) { _workflowContext = workflowContext; ToStateMachine(workflowDefinition, currentState); }
/// <summary> /// Initializes a new instance of the <see cref="Stateflow.Workflow.WorkflowEngine`1"/> class. /// </summary> /// <param name="workflowDefinition">Workflow definition.</param> public WorkflowEngine(WorkflowDefinition <TIdentifier> workflowDefinition) : this(workflowDefinition, null) { }