public ConfigurationSummary <TState, TTrigger> GetSummary()
        {
            var result = new ConfigurationSummary <TState, TTrigger>();

            addNonConfiguredStates(result);
            // Build a list of stateDetails
            var stateDetailsByState = _stateConfigurations.ToDictionary(config => config.Key, config => result.AddState(config.Key));

            foreach (var stateConfig in _stateConfigurations)
            {
                var fromStateDetails = stateDetailsByState[stateConfig.Key];

                if (stateConfig.Value.IsSubState())
                {
                    fromStateDetails.AddSuperState(stateDetailsByState[stateConfig.Value.SuperStateConfig.State]);
                }

                if (!(stateConfig.Value is StateConfigurationBase <T, TState, TTrigger> stateConfigBase))
                {
                    continue;
                }

                foreach (var transitions in stateConfigBase.Transitions)
                {
                    foreach (var transition in transitions.Value)
                    {
                        fromStateDetails.CreateTransitionFrom(transition.Name
                                                              , transitions.Key
                                                              , transition is StateTransitionNonDynamic <T, TState, TTrigger> nonDynamic
                                ? result.StateDetails.First(s => s.State.IsEqual(nonDynamic.ToState))
                                : stateDetailsByState.First().Value
                                                              , (transition is StateTransition <T, TState, TTrigger> condTransition) &&
                                                              condTransition.HasCondition
                                                              , transition.Priority
                                                              , transition is StateTransitionDynamicBase <T, TState, TTrigger>);
                    }
                }

                foreach (var transitions in stateConfigBase.AutoTransitions)
                {
                    foreach (var transition in transitions.Value)
                    {
                        fromStateDetails.CreateTransitionFrom(transition.Name
                                                              , transitions.Key
                                                              , transition is StateTransitionNonDynamic <T, TState, TTrigger> nonDynamic
                                ? result.StateDetails.First(s => s.State.IsEqual(nonDynamic.ToState))
                                : stateDetailsByState.First().Value
                                                              , (transition is StateTransition <T, TState, TTrigger> condTransition) &&
                                                              condTransition.HasCondition
                                                              , transition.Priority
                                                              , transition is StateTransitionDynamicBase <T, TState, TTrigger>);
                    }
                }
            }

            setStateLevels(result);

            return(result);
        }
Beispiel #2
0
        public void AddState_adds_stateDetails_to_list()
        {
            var sut = new ConfigurationSummary <SaleState, SaleEvent>();

            var stateDetails = sut.AddState(SaleState.Open);

            Assert.Contains(stateDetails, sut.StateDetails);
        }
Beispiel #3
0
        public void AddState_returns_stateDetails()
        {
            var sut = new ConfigurationSummary <SaleState, SaleEvent>();

            var stateDetails = sut.AddState(SaleState.Open);

            Assert.IsAssignableFrom <StateDetails <SaleState, SaleEvent> >(stateDetails);
        }
Beispiel #4
0
        public void FinalStates_includes_states_that_have_transitions_to()
        {
            var sut = new ConfigurationSummary <SaleState, SaleEvent>();

            var openStateDetails      = sut.AddState(SaleState.Open);
            var changeDueStateDetails = sut.AddState(SaleState.ChangeDue);

            openStateDetails.CreateTransitionFrom("openTransition", SaleEvent.Pay, changeDueStateDetails);

            Assert.Contains(changeDueStateDetails, sut.FinalStates);
        }
Beispiel #5
0
        public void StartingStates_does_not_includes_states_that_have_transitions_to()
        {
            var sut = new ConfigurationSummary <SaleState, SaleEvent>();

            var openStateDetails      = sut.AddState(SaleState.Open);
            var changeDueStateDetails = sut.AddState(SaleState.ChangeDue);

            openStateDetails.CreateTransitionFrom("openTransition", SaleEvent.Pay, changeDueStateDetails);
            changeDueStateDetails.CreateTransitionFrom("changeDueTransition", SaleEvent.ChangeGiven, openStateDetails);

            Assert.DoesNotContain(openStateDetails, sut.StartingStates);
            Assert.DoesNotContain(changeDueStateDetails, sut.StartingStates);
        }
Beispiel #6
0
        public void Transitions_includes_transitions_from_all_states()
        {
            var sut = new ConfigurationSummary <SaleState, SaleEvent>();

            var openStateDetails      = sut.AddState(SaleState.Open);
            var changeDueStateDetails = sut.AddState(SaleState.ChangeDue);
            var openTransition        = openStateDetails.CreateTransitionFrom("openTransition"
                                                                              , SaleEvent.Pay
                                                                              , changeDueStateDetails);
            var changeDueTransition = changeDueStateDetails.CreateTransitionFrom("changeDueTransition"
                                                                                 , SaleEvent.ChangeGiven
                                                                                 , openStateDetails);

            Assert.Contains(openTransition, sut.Transitions);
            Assert.Contains(changeDueTransition, sut.Transitions);
        }
        public ConfigurationSummary <TState, TTrigger> GetSummary()
        {
            var result = new ConfigurationSummary <TState, TTrigger>();

            AddNonConfiguredStates(result);
            // Build a list of stateDetails
            var stateDetailsByState = _stateConfigurations.ToDictionary(config => config.Key, config => result.AddState(config.Key));

            foreach (var config in _stateConfigurations)
            {
                var fromStateDetails = stateDetailsByState[config.Key];
                foreach (var transitions in (config.Value as StateConfigurationBase <T, TState, TTrigger>).Transitions)
                {
                    foreach (var transition in transitions.Value)
                    {
                        fromStateDetails.CreateTransitionFrom(transition.Name
                                                              , transitions.Key
                                                              , transition is StateTransitionNonDynamic <T, TState, TTrigger> nonDynamic
                                ? result.StateDetails.First(s => s.State.IsEqual(nonDynamic.ToState))
                                : stateDetailsByState.First().Value
                                                              , (transition is StateTransitionAsync <T, TState, TTrigger> condTransition) &&
                                                              condTransition.HasCondition
                                                              , transition.Priority
                                                              , transition is StateTransitionDynamicBase <T, TState, TTrigger>);
                    }
                }

                foreach (var transitions in (config.Value as StateConfigurationBase <T, TState, TTrigger>).AutoTransitions)
                {
                    foreach (var transition in transitions.Value)
                    {
                        fromStateDetails.CreateTransitionFrom(transition.Name
                                                              , transitions.Key
                                                              , transition is StateTransitionNonDynamic <T, TState, TTrigger> nonDynamic
                                ? result.StateDetails.First(s => s.State.IsEqual(nonDynamic.ToState))
                                : stateDetailsByState.First().Value
                                                              , (transition is StateTransitionAsync <T, TState, TTrigger> condTransition) &&
                                                              condTransition.HasCondition
                                                              , transition.Priority
                                                              , transition is StateTransitionDynamicBase <T, TState, TTrigger>);
                    }
                }
            }

            return(result);
        }
        private void AddNonConfiguredStates(ConfigurationSummary <TState, TTrigger> summary)
        {
            var dictionaryType = summary.GetType();
            var stateType      = dictionaryType.GetGenericArguments().First();

            if (stateType.IsEnum)
            {
                var enumTypes = Enum.GetValues(stateType);

                foreach (var enumType in enumTypes)
                {
                    var realEnumType = (TState)enumType;
                    summary.AddState(realEnumType);
                }
            }

            //TODO Non-configured non-enums
        }
        private static void setStateLevels(ConfigurationSummary <TState, TTrigger> configurationSummary)
        {
            foreach (var state in configurationSummary.StateDetails)
            {
                if (!state.IsSuperState)
                {
                    continue;
                }

                if (state.StateLevel == 0)
                {
                    state.StateLevel = 1;
                }

                foreach (var subState in state.SubStates)
                {
                    subState.StateLevel = state.StateLevel + 1;
                }
            }
        }