Administrator
2021-04-14 620df6c0ddff4cda0bf27da97fc854b7d2732c8c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
 
namespace Bro.Common.AbstractFSM
{
    //actions that are performed when state changed
    public delegate void StateAction(object sender, StateEventArgs eventArgs);
 
    public class Transition
    {
        private State m_initialState;
        public State initialState
        {
            get { return m_initialState; }
        }
        private StateEventArgs m_eventArgs;
        public StateEventArgs eventArgs
        {
            get { return m_eventArgs; }
        }
        private State m_finalState;
        public State finalState
        {
            get { return m_finalState; }
        }
        private StateAction m_state_action;
        public StateAction action
        {
            get { return m_state_action; }
        }
        private bool m_autoMode = false;
        public bool AutoMode
        {
            get { return m_autoMode; }
        }
 
        private Transition m_autoTransition = null;
        public Transition AutoTransition
        {
            get { return m_autoTransition; }
        }
 
        public Transition(State initialState, StateEventArgs sevent, State finalState, StateAction action)
        {
            m_initialState = initialState;
            m_eventArgs = sevent;
            m_finalState = finalState;
            m_state_action = action;
        }
        public Transition(State initialState, StateEventArgs sevent, State finalState, StateAction action, bool autoMode, Transition autoTransition) 
                         : this(initialState, sevent, finalState, action)    
        {
            m_autoMode = autoMode;
            m_autoTransition = autoTransition; 
        }
        public override int GetHashCode()
        {
            //create a unique transition key
            return GetHashCode(m_initialState, m_eventArgs);
        }
 
        public static int GetHashCode(State state, StateEventArgs sevent)
        {
            return (state.GetHashCode() << 8) + sevent.Id;
        }
    }
    /// <summary>
    /// Represents a collection of transition objects.
    /// </summary>
    public class Transitions : System.Collections.Generic.Dictionary <int, Transition>   
    {
        /// <summary>Adds the specified transition to the collection.</summary> 
        /// <param name="transition">Transition object</param>      
        /// <see cref="System.Collections.Generic.Dictionary {int, Transition}"/>
        /// <exception cref="System.ArgumentNullException">Key is null</exception>
        /// <exception cref="System.ArgumentException">An transition with the same key already exists.</exception>
        public void Add(Transition transition)
        {
            // The Add method throws an exception if the new key is already in the dictionary.
            try
            {
                base.Add(transition.GetHashCode(), transition);
            }
            catch (ArgumentException)
            {
                throw new ArgumentException("A transition with the key (Initials state " + 
                        transition.initialState + ", Event " + 
                        transition.eventArgs + ") already exists.");
            }  
        }
        //
        public Transition this[State state, StateEventArgs sevent]
        {
            get
            {
                try
                {
                    return this[Transition.GetHashCode(state, sevent)];
                }
                catch(KeyNotFoundException)
                {
                    throw new KeyNotFoundException("The given transition was not found.");
                }
            }
            set
            {
                this[Transition.GetHashCode(state, sevent)] = value;
            }
        }
        //
        public bool Remove(State state, StateEventArgs sevent)
        {
            return base.Remove(Transition.GetHashCode(state, sevent));   
        }
    }
}