Esempio n. 1
0
 public void SaveStateMachine(string file)
 {
     try
     {
         XmlDocument doc           = new XmlDocument();
         XmlElement  xStateMachine = doc.CreateElement("StateMachine");
         doc.AppendChild(xStateMachine);
         xStateMachine.SetAttribute("Name", this.Name);
         xStateMachine.SetAttribute("Class", this.GetType().ToString());
         //匯出STATE 狀態
         foreach (KeyValuePair <string, State> s in states)
         {
             s.Value.GetElements(ref doc, ref xStateMachine);
         }
         //匯出Transition 狀態
         //foreach (KeyValuePair<Enum, Transition> t in transitions)
         //{
         //    t.Value.GetElements(ref doc, ref xStateMachine);
         //}
         foreach (Transition t in transitions)
         {
             t.GetElements(ref doc, ref xStateMachine);
         }
         doc.Save(file);
     }
     catch (Exception ex)
     {
         MvLog.Write(ex, MvLoggerEnumLevel.Warn);
     }
 }
Esempio n. 2
0
 public void CleanEvent()
 {
     try
     {
         foreach (Delegate d in this.evtDataReceive.GetInvocationList())
         {
             this.evtDataReceive -= (EventHandler <WacohForceMessageEventArgs>)d;
         }
     }
     catch (Exception ex) { MvLog.Write(ex); }
 }
Esempio n. 3
0
        /// <summary>
        /// transitions runTrigger with step name
        /// </summary>
        /// <param name="param"></param>
        /// <returns>-1 is action can't run</returns>
        public EnumStateMachineMsgType SmProcTransition(IStateParam param)
        {
            try
            {
                bool executeFlag = false;

                Transition runTriggerSingle = null;

                //第一步檢查有沒有此TRANSITION
                if (transitions.Find(x => x.TransName.Equals(param.TransName)) == null)
                {
                    throw new StateMachineException(ID + " No Find Transition: " + param.TransName);
                }


                //找出同名Transition
                var trans = (from t in transitions
                             where t.TransName == param.TransName
                             select t).ToList();
                var currTrans = trans.Where(t => t.FromState == this.CurrentState).ToList();//過濾from state

                //確認所有Guard是否成立
                foreach (var tran in currTrans)
                {
                    if (tran.RunGuard(this))
                    {
                        //若確實只找到一個成立, 記錄下來, 超過一個不可執行
                        if (runTriggerSingle == null)
                        {
                            runTriggerSingle = tran;
                        }
                        else
                        {
                            throw new PluralTriggerSameTimeException(this.GetType().ToString() + " : current is " + this.CurrentState.StateName + " with Transition : " + param.TransName + ", trigger more than one same time");//只能有一個transition成立
                        }
                    }
                }


                if (runTriggerSingle != null)
                {
                    executeFlag = true;
                    runTriggerSingle.RunTrigger(param, this);
                }
                else if (currTrans.Count == 0 && trans.Where(t => t is DeferralTransition).Count() == 0)
                {
                    //如果要求的transaction不在current state, 且不具有Deferral Transaction, 就拋出例外
                    //不應存在無法trigger的transition
                    throw new NotCurrentStateException(this.GetType().ToString() + " : current is " + this.CurrentState.StateName + " does not have Transition : " + param.TransName);
                }
                else
                {
                    //否則: 這個State Machine 的 Deferral Transition 具有 param.TranName
                    //在這裡代表沒有transition被觸發, 因此需要判斷是否要存回Queue裡
                    //State Machine中, 並沒有定義 Defferal Guard 失敗時的行為, 因此暫以 Normal Transition 的行為作準則
                    // 不回存(or, 其中一個成立)
                    // 1. Trigger 成功
                    // 2. Current State 具同名 Transition (不論 Guard 成功與否)
                    // 即=>回存(and, 全部成立)
                    // 1. Trigger失敗
                    // 2. Curr State 不具同名 Transition
                    // 3. 其它State具有同名 DeferralTransition



                    //找出同名且不在Current State裡的Defferal的數量
                    var countDefferalOfNotInCurr = (from row in trans
                                                    where row is DeferralTransition
                                                    select row).Count();

                    if (currTrans.Count == 0 && countDefferalOfNotInCurr > 0)
                    {
                        SaveDeferralEvent(param);//TODO: 確認加回最後一個是否合理
                    }
                    //Deferral 請先確認是否有在CURRENT STATE有在的話,不管GUARD的狀況與否都不會重新放回JOB LIST
                    //bool noTransitionInCurrentState = true;
                    //foreach (Transition t in trans.Where(t => t is DeferralTransition))
                    //{
                    //    if (((DeferralTransition)t).IsInCurrentState(this))
                    //    {
                    //        noTransitionInCurrentState = false;
                    //    }
                    //}


                    //if (noTransitionInCurrentState)
                    //{
                    //    SaveDeferralEvent(param);
                    //}
                    //else
                    //{
                    //    //執行Defferal transition on not current state (save to event queue)
                    //    foreach (Transition t in trans.Where(t => t is DeferralTransition))
                    //    {
                    //        if (t.RunGuard(this))
                    //        {
                    //            if (runTriggerSingle == null)
                    //            {
                    //                runTriggerSingle = (DeferralTransition)t;
                    //            }
                    //            else
                    //            {
                    //                throw new PluralTriggerSameTimeException(this.GetType().ToString() + " : current is " + this.CurrentState.StateName + " with Transition : " + param.TransName + ", trigger more than one same time");
                    //            }
                    //        }
                    //    }
                    //    runTriggerSingle.RunTrigger(param, this);
                    //}
                }

                if (executeFlag)
                {
                    return(EnumStateMachineMsgType.CommandSuccessful);
                }
                else
                {
                    //run deferral or strange transition

                    /*foreach(Transition t in (from transObj in transitions where transObj.TransName.Equals(param.TransName.ToString()) && !currentState.transs.Contains(transObj) select transObj))
                     * {
                     *  t.RunTrigger(param, this);
                     * }*/
                    return(EnumStateMachineMsgType.GuardFail);
                }
            }
            catch (PluralTriggerSameTimeException ex)
            {
                PopupAlarm(this, EnumStateMachineMsgType.PluralTriggerSameTime, "TransitionPluralTrigger");
                MvLog.Write(ex, MvLoggerEnumLevel.Warn);
                return(EnumStateMachineMsgType.PluralTriggerSameTime);
            }
            catch (NotCurrentStateException ex)
            {
                //Transition nocurTrans = GetDynamicExceptionTransition(CurrentState, EnumStateMachineMsgType.NotCurrentStateTransition);
                //nocurTrans.RunTrigger(new StateParameter(), this);
                MvLog.Write(ex, MvLoggerEnumLevel.Error);
                return(EnumStateMachineMsgType.StateMachineException);
            }
            catch (StateMachineException ex)
            {
                System.Diagnostics.Debug.WriteLine(this.ID + ":Error");
                MvLog.Write(ex, MvLoggerEnumLevel.Error);
                throw ex;
            }
            catch (Exception ex)
            {
                MvLog.Write(ex, MvLoggerEnumLevel.Error);
                throw ex;
            }
        }