/// <summary> /// /// </summary> /// <param name="fromState"></param> /// <param name="toState"></param> /// <param name="guard"></param> /// <param name="action"></param> /// <param name="transName"></param> /// <param name="tt"></param> /// <param name="timeout">有設置TIMEOUT時間時,代表此transition 是timeout類型且在tostate timeout時切到這條TRANSITION,MAXVALUE被設定為不設定</param> /// <returns></returns> public virtual Transition NewTransition(State fromState, State toState , Func <bool> guard, Action action, string transName , TransitionType tt = TransitionType.Normal, int timeout = int.MaxValue) { Transition transition; if (tt == TransitionType.Deferral) { transition = new DeferralTransition(fromState, toState, guard, action, transName); } else if (tt == TransitionType.Internal) { transition = new InternalTransition(fromState, guard, action, transName); } else { transition = new Transition(fromState, toState, guard, action, transName); } if (timeout != int.MaxValue) { if (timeout > 0) { toState.timeoutExecuteTransition = transition; toState.TimeoutLimit = timeout; } else { throw new Exception("wrong transition timeout config"); } } AddTransition(transition); return(transition); }
public static void BuildStateMachineFromXML(ref Dictionary <string, StateMachine> sms, string document) { XmlDocument doc = new XmlDocument(); try { doc.Load(document); } catch (Exception ex) { MessageBox.Show(ex.ToString()); throw ex; } StateMachine tmp; XmlNode sm = doc.FirstChild; string smName = sm.Attributes["Name"].Value; string smClass = sm.Attributes["Class"].Value; XmlNodeList tempNodes = sm.SelectNodes("State"); if (!sms.ContainsKey(smName)) { Type type = null; //Assembly ass = Assembly.LoadFrom("MaskAutoCleaner.dll"); foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies()) { type = ass.GetType(smClass); if (type != null) { break; } } tmp = (StateMachine)Activator.CreateInstance(type); sms.Add(smName, tmp); } else { StateMachine findtmp = sms[smName]; tmp = findtmp; } foreach (XmlNode stateNode in tempNodes) { State s; if (stateNode.Attributes["Type"].Value.Equals("ExceptionState")) { s = new ExceptionState(stateNode.Attributes["Name"].Value); } else { s = new State(stateNode.Attributes["Name"].Value); } addEvent(ref sms, ref s, stateNode, "Entry"); addEvent(ref sms, ref s, stateNode, "Exit"); tmp.AddState(s); } tempNodes = sm.SelectNodes("Transition"); foreach (XmlNode transitionNode in tempNodes) { State FromState = tmp.States[transitionNode.SelectSingleNode("FromState").Attributes["Name"].Value]; State ToState = tmp.States[transitionNode.SelectSingleNode("ToState").Attributes["Name"].Value]; Transition s; if (transitionNode.Attributes["Type"].Value.Equals("Deferral")) { s = new DeferralTransition(FromState, ToState, tmp.GetType().GetMethod(transitionNode.SelectSingleNode("Guard").Attributes["Name"].Value), tmp.GetType().GetMethod(transitionNode.SelectSingleNode("Action").Attributes["Name"].Value), transitionNode.Attributes["Name"].Value); } else { s = new Transition(FromState, ToState, tmp.GetType().GetMethod(transitionNode.SelectSingleNode("Guard").Attributes["Name"].Value), tmp.GetType().GetMethod(transitionNode.SelectSingleNode("Action").Attributes["Name"].Value), transitionNode.Attributes["Name"].Value); } tmp.AddTransition(s); } }