Пример #1
0
 public void Serialize(StateMap sd, SerializationHelper helper)
 {
     sd.Set("IsSuccessfullyCompleted", IsSuccessfullyCompleted);
     sd.Set("Exception", _thrownException.GetOr(default(Exception)));
     sd.Set(nameof(_awaiters), _awaiters);
 }
Пример #2
0
 public void Serialize(StateMap sd, SerializationHelper helper)
 {
     sd.Set(nameof(_handler), _handler);
     sd.Set(nameof(_value), _value);
 }
Пример #3
0
 public QueuePendingState(IDelayQueueContext context, StateMap stateMap) : base(context, stateMap)
 {
 }
 public ClientConnectedState(TcpClientConnection context, StateMap stateMap) : base(context, stateMap, BinaryConnectionState.Connected)
 {
 }
Пример #5
0
        /// <summary>
        /// Create a state machine of the given base type and will set it on the given context
        /// Will internally called by the <see cref="StateMachine"/> wrapper class
        /// </summary>
        internal static void Create <TStateBase>(IStateContext context, int?initialKey)
            where TStateBase : StateBase
        {
            var stateBaseType = typeof(TStateBase);

            // Check the base type
            if (!stateBaseType.IsAbstract)
            {
                throw new ArgumentException("The state base class must be abstract!");
            }

            // Load all fields
            // 1. Get all fields which are static constant with the attribute
            // 2. let attribute and create an anonymous array
            var definedStates = (from stateField in GetStateFields(stateBaseType)
                                 let att = stateField.GetCustomAttribute <StateDefinitionAttribute>()
                                           select new { Key = (int)stateField.GetValue(null), att.IsInitial, att.Type }).ToArray();

            if (definedStates.Length == 0)
            {
                throw new InvalidOperationException("There was no state constant defined in the given base type." +
                                                    $"There musst be at least one constant integer attributed with the {nameof(StateDefinitionAttribute)}.");
            }

            // If a initial key is set, we check if it exists
            if (initialKey.HasValue && definedStates.All(s => s.Key != initialKey.Value))
            {
                throw new InvalidOperationException($"There was no state defined with key: {initialKey}");
            }

            // Group by type to find multiple defined state types
            var duplicates = definedStates.GroupBy(state => state.Type).Where(g => g.Count() > 1).Select(g => g.Key).ToArray();

            if (duplicates.Any())
            {
                var typeNames = string.Join(", ", duplicates.Select(type => type.Name));
                throw new InvalidOperationException($"State types are only allowed once: {typeNames}");
            }

            var       stateMap     = new StateMap();
            StateBase initialState = null;

            foreach (var definedState in definedStates)
            {
                var instance = (StateBase)Activator.CreateInstance(definedState.Type, context, stateMap);
                instance.Key = definedState.Key;

                if (initialKey.HasValue && initialKey.Value == definedState.Key)
                {
                    initialState = instance;
                }
                else if (definedState.IsInitial && initialState == null)
                {
                    initialState = instance;
                }
                else if (definedState.IsInitial && initialState != null)
                {
                    throw new InvalidOperationException("At least one state must be flagged as '" +
                                                        $"{nameof(StateDefinitionAttribute.IsInitial)} = true'.");
                }

                stateMap.Add(definedState.Key, instance);
            }

            if (initialState == null)
            {
                throw new InvalidOperationException("There is no state flagged with " +
                                                    $"'{nameof(StateDefinitionAttribute.IsInitial)} = true'.");
            }

            context.SetState(initialState);
            initialState.OnEnter();
        }
Пример #6
0
 public void Serialize(StateMap sd, SerializationHelper helper)
 {
     sd.Set(nameof(_any), _any);
     sd.Set(nameof(_currentMax), _currentMax);
 }
Пример #7
0
 public override void Serialize(StateMap sd, SerializationHelper helper)
 => sd.Set(nameof(_inner), _inner);
Пример #8
0
 public ReadyState(WorkflowEngine context, StateMap stateMap) : base(context, stateMap)
 {
 }
 public void Serialize(StateMap sd, SerializationHelper helper)
 => sd.Set(nameof(Value), Value);
Пример #10
0
 public override void Serialize(StateMap sd, SerializationHelper helper)
 {
     sd.Set(nameof(_isCompletedSuccessfully), _isCompletedSuccessfully);
     sd.Set(nameof(_thrownException), _thrownException.GetOr(default(Exception)));
     sd.Set(nameof(_continuation), _continuation);
 }
Пример #11
0
 public void Serialize(StateMap sd, SerializationHelper helper)
 {
     sd[nameof(Messages)] = Messages;
     sd[nameof(Network)]  = Network;
 }
Пример #12
0
 public override void Serialize(StateMap sd, SerializationHelper helper)
 => sd.Set("Exception", _thrownException);
Пример #13
0
 public override void Serialize(StateMap stateToSerialize, SerializationHelper helper)
 {
 }
Пример #14
0
 public abstract void Serialize(StateMap stateToSerialize, SerializationHelper helper);
Пример #15
0
 public void Serialize(StateMap sd, SerializationHelper helper)
 {
     sd.Set(nameof(_curr), _curr);
     sd.Set(nameof(_folder), _folder);
 }
Пример #16
0
 public RunningState(WorkflowEngine context, StateMap stateMap) : base(context, stateMap)
 {
 }
Пример #17
0
 public void Serialize(StateMap sd, SerializationHelper helper)
 {
 }
Пример #18
0
 protected ClientStateBase(TcpClientConnection context, StateMap stateMap, BinaryConnectionState connectionState) : base(context, stateMap)
 {
     Current = connectionState;
 }
Пример #19
0
 public void Serialize(StateMap sd, SerializationHelper helper) => sd.Set(nameof(_count), _count);
 private State FindStateNamed(string stateName, StateMap stateMap) => stateMap.OrderedStates.Single(s => s.Name == stateName);
Пример #21
0
 public void Serialize(StateMap sd, SerializationHelper helper)
 {
     sd.Set(nameof(Name), Name);
     sd.Set(nameof(Child), Child);
 }
 public void Serialize(StateMap sd, SerializationHelper helper)
 {
     sd.Set(nameof(ThrownException), ThrownException);
     sd.Set(nameof(Source), Source);
 }
Пример #23
0
 /// <summary>
 ///     Executes the behavior for this instance.
 /// </summary>
 /// <param name="container"> </param>
 /// <param name="stateMap"> </param>
 public void Execute(IContainer container, StateMap stateMap)
 {
     _action.Invoke( );
 }
Пример #24
0
 public void Serialize(StateMap sd, SerializationHelper helper)
 {
     sd.Set(nameof(Messages), Messages);
     sd.Set(nameof(Count), Count);
 }
Пример #25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StateBase"/> class.
 /// </summary>
 /// <param name="context">Context of the state machine</param>
 /// <param name="stateMap">Map of states to objects</param>
 protected StateBase(IStateContext context, StateMap stateMap)
 {
     Map     = stateMap;
     Context = context;
 }
 /// <summary>
 /// constructor
 /// </summary>
 public StartedState(ResourceWrapper context, StateMap stateMap) : base(context, stateMap)
 {
 }
Пример #27
0
 /// <summary>
 /// constructor
 /// </summary>
 protected ResourceStateBase(ResourceWrapper context, StateMap stateMap) : base(context, stateMap)
 {
 }
Пример #28
0
 protected EngineState(WorkflowEngine context, StateMap stateMap) : base(context, stateMap)
 {
 }
        public static void SerializeReferencesUsingReflectionInto(this object instance, StateMap sd, SerializationHelper helper)
        {
            var instanceType = instance.GetType();
            var fields       = instanceType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            var fieldValues = fields
                              .Select(f => new { Field = f, Value = f.GetValue(instance) })
                              .Where(a => a.Value == null ||
                                     a.Field.FieldType.IsPrimitive ||
                                     a.Field.FieldType == typeof(DateTime) ||
                                     a.Field.FieldType == typeof(string) ||
                                     a.Value is IPersistable ||
                                     a.Value is IPropertyPersistable ||
                                     a.Value.GetType().IsDisplayClass() ||
                                     a.Value is Delegate
                                     )
                              .Select(a => new FieldNameAndValue(a.Field.Name, a.Value));

            foreach (var fieldAndValue in fieldValues)
            {
                sd.Set(fieldAndValue.Name, helper.GetReference(fieldAndValue.Value));
            }
        }
Пример #30
0
        public void Begin()
        {
            Push();

            m_Map = new StateMap();
        }