Beispiel #1
0
        internal void setTransition(State begin, State end, double p)
        {
            if (!States.Contains(begin) || !States.Contains(end))
            {
                throw new InvalidTransitionException();
            }

            if (p < 0 || p > 1)
            {
                throw new OutOfRangeProbabilityException();
            }


            var transition = new Transition(begin, end, p);

            if (Transitions.ContainsKey(begin))
            {
                Transitions[begin].Add(transition);
            }
            else
            {
                Transitions.Add(begin, new List <Transition>()
                {
                    transition
                });
            }
        }
Beispiel #2
0
        public HashSet <T> epsilonClosure(T state)
        {
            HashSet <T> eClosure = new HashSet <T>();

            if (!Transitions.ContainsKey(state))
            {
                eClosure.Add(state);
                return(eClosure);
            }
            if (Transitions[state].ContainsKey(Epsilon))
            {
                eClosure.UnionWith(Transitions[state][Epsilon]);
            }
            HashSet <T> eClosure2 = new HashSet <T>();

            do
            {
                foreach (T eState in eClosure)
                {
                    eClosure2.UnionWith(epsilonClosure(eState));
                }
                eClosure.UnionWith(eClosure2);
            }while (eClosure2.Except(eClosure).Count() > 0);
            eClosure.Add(state);
            return(eClosure);
        }
Beispiel #3
0
        private bool IsSequenceAccepted(string sequence)
        {
            var currentState = InitialState;

            while (sequence != "")
            {
                var transitionKey = new Tuple <string, string>(currentState, sequence[0].ToString());

                if (Transitions.ContainsKey(transitionKey))
                {
                    currentState = Transitions[transitionKey];
                    sequence     = sequence.Substring(1);
                }
                else
                {
                    return(false);
                }
            }

            if (!FinalStates.Contains(currentState))
            {
                return(false);
            }

            return(true);
        }
Beispiel #4
0
        public bool addTransition(U input, T fromState, T toState)
        {
            // Add from state if non existant
            if (!Transitions.ContainsKey(fromState))
            {
                Transitions.Add(fromState, new Dictionary <U, HashSet <T> >());
            }

            if (Transitions[fromState].ContainsKey(input))
            {
                // Add to state to existing input.
                return(Transitions[fromState][input].Add(toState));
            }
            else
            {
                // Add input to alphabet (except for epsilon) so the alphabet can dynamically be changed
                if (!input.Equals(Epsilon))
                {
                    Alphabet.Add(input);
                }
                // Add input with to state.
                Transitions[fromState].Add(input, new HashSet <T>()
                {
                    toState
                });
                return(true);
            }
        }
Beispiel #5
0
 private void AddNewTransition(BaseGcTransition transition)
 {
     if (!Transitions.ContainsKey(transition.CodeOffset))
     {
         Transitions[transition.CodeOffset] = new List <BaseGcTransition>();
     }
     Transitions[transition.CodeOffset].Add(transition);
 }
Beispiel #6
0
 public void AddTransitions(int lineId, int[] t)
 {
     if (Transitions.ContainsKey(lineId))
     {
         Transitions.Remove(lineId);
     }
     Transitions.Add(lineId, t);
 }
Beispiel #7
0
    public override IState ProcessInput()
    {
        var sqrDistance = (_player.transform.position - transform.position).sqrMagnitude;

        if (sqrDistance < meleeDistance * meleeDistance && Transitions.ContainsKey("OnMeleeAttackState"))
        {
            return(Transitions["OnMeleeAttackState"]);
        }

        return(this);
    }
        public void AddTransition(string from, string to, Predicate <Character> condition)
        {
            if (!Transitions.ContainsKey(from))
            {
                Transitions.Add(from, new Dictionary <string, Predicate <Character> >());
            }

            else if (Transitions[from].ContainsKey(to))
            {
                throw new InvalidOperationException("The state machine already contains this transition.");
            }

            Transitions[from].Add(to, condition);
        }
Beispiel #9
0
 public void AddTransition(char value, bool isFinal)
 {
     if (Transitions.ContainsKey(value))
     {
         if (!Transitions[value])
         {
             Transitions[value] = isFinal;
         }
     }
     else
     {
         Transitions.Add(value, isFinal);
     }
 }
        public StateMachine AddTransition(Type transitionType, Transition transitionInstance)
        {
            if (Transitions.ContainsKey(transitionType) ||
                Transitions.Values.Any(t => t.SourceStateType == transitionInstance.SourceStateType && t.TargetStateType == transitionInstance.TargetStateType))
            {
                throw new TransitionCurrentlyExistsException(transitionInstance);
            }

            CheckTransitionState(transitionInstance.SourceStateType);
            CheckTransitionState(transitionInstance.TargetStateType);

            Transitions.Add(transitionType, transitionInstance);
            return(this);
        }
        /// <summary>
        /// Translates between nodes with the specified token.
        /// </summary>
        /// <param name="token">The transition token.</param>
        /// <param name="nextState">The transition end state.</param>
        /// <param name="inputObject">The input object.</param>
        /// <param name="sizes">The Riviera given sizes.</param>
        /// <returns>The sowed element</returns>
        public RivieraObject Transition(ArrowDirection token, out int nextState, RivieraObject inputObject, params RivieraMeasure[] sizes)
        {
            RivieraObject obj = null;

            if (Transitions.ContainsKey(token))
            {
                nextState = Connections[token];
                obj       = Transitions[token](inputObject, token, sizes);
            }
            else
            {
                nextState = Connections[ArrowDirection.NONE];
            }
            return(obj);
        }
        public bool AddOutgoing(FSMState <TInput, TOutput> sourceState, TInput action, FSMState <TInput, TOutput> destinationState, TOutput output, double probability)
        {
            if ((probability <= 0) || (probability > 1))
            {
                throw new ArgumentException("Probability must be in (0;1]", "probability");
            }

            bool result = false;
            var  st     = stateSet.FirstOrDefault(s => s.StateCore == sourceState.StateCore);

            if (st != null)
            {
                Transition <TInput, TOutput> transition = new Transition <TInput, TOutput>(this)
                {
                    SourceState = sourceState,
                    Input       = action,
                    //DestinationState = destinationState,
                    //Output = output
                };
                transition.AddDestination(destinationState, output, probability);
                if (!Transitions.ContainsKey(transition.ToString()))
                {
                    if (st.AddOutgoing(action, destinationState, output, probability))
                    {
                        Transitions.Add(transition.ToString(), transition);
                        result = true;
                    }
                }
                else
                {
                    if (st.AddOutgoing(action, destinationState, output, probability))
                    {
                        result = Transitions[transition.ToString()].AddDestination(destinationState, output, probability);
                    }
                }
            }

            if (result)
            {
                if (probability < 1)
                {
                    IsProbabilityMachine = true;
                }
            }

            return(result);
        }
Beispiel #13
0
        public void AggregateTransitions()
        {
            //clear existing transitions
            Transitions.Clear();

            for (int i = 0; i < RawStates.Count - 1; i++)
            {
                //starting state
                TimelineState currentState = RawStates[i];

                //non-social state?
                if (currentState.IsSocialEvent == true)
                {
                    //try again
                    continue;
                }

                //pointer to next index
                int nextIndex = i + 1;

                //and the next one
                TimelineState nextState = RawStates[nextIndex];

                //handle social events differently.  Like normal, place them in a bucket that is of the form
                //State -> Social Event.  However, do not increment currentState until we find a matching
                //non-social event.
                while (nextIndex < RawStates.Count && nextState.IsSocialEvent == true)
                {
                    KeyValuePair <string, string> transition = new KeyValuePair <string, string>(currentState.State, currentState.State);
                    if (Transitions.ContainsKey(transition) == false)
                    {
                        Transitions.Add(transition, 0);
                    }
                    Transitions[transition]++;
                    nextIndex++;
                }

                //at this point, currentState and nextState should both be non-social events.  Record transition.
                KeyValuePair <string, string> key = new KeyValuePair <string, string>(currentState.State, nextState.State);
                if (Transitions.ContainsKey(key) == false)
                {
                    Transitions.Add(key, 0);
                }
                Transitions[key]++;
            }
        }
Beispiel #14
0
        /// <summary>
        /// 设置单一的状态过渡
        ///     同一个状态机下的状态之间的状态过渡
        /// </summary>
        /// <param name="toStateName"></param>
        /// <param name="transitionName"></param>
        /// <returns></returns>
        protected Transition AddTransition(StateName toStateName, TransitionName transitionName)
        {
            StateMachine machine = StateManager.GetMachineWithName(Machine.Name);
            State        toState = machine.GetStateWithName(toStateName);

            if (toState == null)
            {
                throw new Exception("从本状态切换到 " + toStateName + "状态,不存在");
            }
            if (Transitions.ContainsKey(transitionName))
            {
                throw new Exception("此过渡状态已存在");
            }
            Transition transition = new Transition(transitionName, toState);

            Transitions.Add(transitionName, transition);
            return(transition);
        }
        public bool AddOutgoing(FSMState <TInput, TOutput> sourceState, TInput action)
        {
            bool result = false;
            var  st     = stateSet.FirstOrDefault(s => s.StateCore == sourceState.StateCore);

            if (st != null)
            {
                Transition <TInput, TOutput> transition = new Transition <TInput, TOutput>(this)
                {
                    SourceState = sourceState,
                    Input       = action
                };
                if (!Transitions.ContainsKey(transition.ToString()))
                {
                    Transitions.Add(transition.ToString(), transition);
                    result = true;
                }
            }
            return(result);
        }
Beispiel #16
0
 public bool addTransition(U input, T fromState, T toState)
 {
     // Check if state and input already exists
     if (!Transitions.ContainsKey(fromState))
     {
         // Add the transition to the automaton
         Transitions.Add(fromState, new Dictionary <U, T>());
     }
     // Check if input already exists
     if (Transitions[fromState].ContainsKey(input))
     {
         // if it already exists we don't need to add it, so the method returns false
         return(false);
     }
     // Add input to alphabet so the alphabet can dynamically be changed
     Alphabet.Add(input);
     Transitions[fromState].Add(input, toState);
     States.Add(fromState);
     States.Add(toState);
     return(true);
 }
Beispiel #17
0
        public bool accept(U[] input)
        {
            if (!isValid())
            {
                throw new AutomatonInvalidException();
            }

            var currStates = StartStates;

            foreach (U u in input)
            {
                if (!Alphabet.Contains(u))
                {
                    throw new System.ArgumentException($"{u} is not part of alphabet");
                }
                HashSet <T> newStates = new HashSet <T>();
                foreach (T state in currStates)
                {
                    if (!Transitions.ContainsKey(state))
                    {
                        continue;
                    }
                    if (Transitions[state].ContainsKey(Epsilon))
                    {
                        newStates.UnionWith(Transitions[state][Epsilon]);
                    }
                    if (Transitions[state].ContainsKey(u))
                    {
                        newStates.UnionWith(Transitions[state][u]);
                    }
                }
                currStates = newStates;
            }

            return(EndStates.Intersect(currStates).Count() > 0);
        }
Beispiel #18
0
        public DFA <IEnumerable <T>, U> toDFA()
        {
            DFA <IEnumerable <T>, U> result = new DFA <IEnumerable <T>, U>();
            HashSet <StateSet <T> >  curr   = new HashSet <StateSet <T> >();

            // Add start state
            StateSet <T> start = new StateSet <T>();

            foreach (T startState in StartStates)
            {
                start.UnionWith(epsilonClosure(startState));
            }
            curr.Add(start);
            result.addStartState(start);

            // Add trap state
            StateSet <T> trap = new StateSet <T>();

            foreach (U terminal in Alphabet)
            {
                result.addTransition(terminal, trap, trap);
            }

            // Add transitions
            bool done = false;

            while (!done)
            {
                bool addedNew = false;
                HashSet <StateSet <T> > next = new HashSet <StateSet <T> >();
                // For each current state in set of current states.
                foreach (StateSet <T> currState in curr)
                {
                    // For each terminal in the alphabet.
                    foreach (U terminal in Alphabet)
                    {
                        // nextState will become the actual dfa to state.
                        StateSet <T> nextState = new StateSet <T>();
                        // For each ndfa state of which the dfa state is made.
                        foreach (T subState in currState)
                        {
                            // Get the epsilon closure of the sub state.
                            HashSet <T> preClosure = epsilonClosure(subState);
                            // For each state in the epsilon closure.
                            foreach (T preClosureState in preClosure)
                            {
                                // Check if exists.
                                if (!Transitions.ContainsKey(preClosureState) || !Transitions[preClosureState].ContainsKey(terminal))
                                {
                                    continue;
                                }
                                // Get all the to states for this terminal.
                                HashSet <T> follow = Transitions[preClosureState][terminal];
                                // Accumulate the epsilon closure of each followed state.
                                foreach (T followedState in follow)
                                {
                                    HashSet <T> postClosure = epsilonClosure(followedState);
                                    nextState.UnionWith(postClosure);
                                }
                            }
                        }

                        if (nextState.Count() > 0)
                        {
                            next.Add(nextState);
                            // Add transition.
                            if (result.addTransition(terminal, currState, nextState))
                            {
                                addedNew = true;
                            }
                            // Add end state
                            if (EndStates.Intersect(nextState).Count() > 0)
                            {
                                result.addEndState(nextState);
                            }
                        }
                        else
                        {
                            /*
                             * . . . . . . . . . . . . . . . .   ____________
                             * . . . . . . . . . . . . . . . . / It’s a trap! \
                             * . . . . . . . . . . . . . . . . \ _____________/
                             * __...------------._
                             * ,-'                   `-.
                             * ,-'                         `.
                             * ,'                            ,-`.
                             * ;                              `-' `.
                             * ;                                 .-. \
                             * ;                           .-.    `-'  \
                             * ;                            `-'          \
                             * ;                                          `.
                             * ;                                           :
                             * ;                                            |
                             * ;                                             ;
                             * ;                            ___              ;
                             * ;                        ,-;-','.`.__          |
                             * _..;                      ,-' ;`,'.`,'.--`.        |
                             * ///;           ,-'   `. ,-'   ;` ;`,','_.--=:      /
                             |'':          ,'        :     ;` ;,;,,-'_.-._`.   ,'
                             * '  :         ;_.-.      `.    :' ;;;'.ee.    \|  /
                             \.'    _..-'/8o. `.     :    :! ' ':8888)   || /
                             ||`-''    \\88o\ :     :    :! :  :`""'    ;;/
                             ||         \"88o\;     `.    \ `. `.      ;,'
                             ||/)   ___    `."'/(--.._ `.    `.`.  `-..-' ;--.
                             \(.="""""==.. `'-'     `.|      `-`-..__.-' `. `.
                             |          `"==.__      )                    )  ;
                             |   ||           `"=== '                   .'  .'
                             | /\,,||||  | |           \                .'   .'
                             | |||'|' |'|'           \|             .'   _.' \
                             | |\' |  |           || ||           .'    .'    \
                             | ' | \ ' |'  .   ``-- `| ||         .'    .'       \
                             | '  |  ' |  .    ``-.._ |  ;    .'    .'          `.
                             | _.--,;`.       .  --  ...._,'   .'    .'              `.__
                             | ,'  ,';   `.     .   --..__..--'.'    .'                __/_\
                             | ,'   ; ;     |    .   --..__.._.'     .'                ,'     `.
                             | /    ; :     ;     .    -.. _.'     _.'                 /         `
                             | /     :  `-._ |    .    _.--'     _.'                   |
                             | /       `.    `--....--''       _.'                      |
                             | `._              _..-'                         |
                             | `-..____...-''                              |
                             |
                             |
                             */
                            result.addTransition(terminal, currState, trap);
                        }
                    }
                }
                curr = next;
                done = !addedNew;
            }

            return(result);
        }