Example #1
0
 private bool IsValidDefinition(StateMachineDefinition def, out string error)
 {
     error = "";
     if (def.ActionPackage == null || !def.ActionPackage.Contains(","))
     {
         error = "ActionPacakge not set or format invalid";
         return(false);
     }
     if (def.States == null || def.Transitions == null)
     {
         error = "States or Transactions not set";
         return(false);
     }
     foreach (StateDefinition stateDef in def.States)
     {
         if (string.IsNullOrWhiteSpace(stateDef.ID))
         {
             error = "State Id not set or format invalid";
             return(false);
         }
     }
     foreach (TransitionDefinition tranDef in def.Transitions)
     {
         if (string.IsNullOrWhiteSpace(tranDef.ID) ||
             string.IsNullOrWhiteSpace(tranDef.From) ||
             string.IsNullOrWhiteSpace(tranDef.To))
         {
             error = "invalid TransactionDefinition";
             return(false);
         }
     }
     return(true);
 }
Example #2
0
        public override IStateMachine CreateStateMachineFromXml(Stream xmlStream)
        {
            if (xmlStream == null)
            {
                throw new StateMachineException("xmlStream");
            }
            XmlSerializer          xmlSerializer = new XmlSerializer(typeof(StateMachineDefinition));
            StateMachineDefinition def           = xmlSerializer.Deserialize(xmlStream) as StateMachineDefinition;
            string error;

            if (!IsValidDefinition(def, out error))
            {
                throw new StateMachineException("Invalid StateMachineDefinition for " + error);
            }
            DefaultStateMachine sm = new DefaultStateMachine();

            sm.Initialize(def, InitializeAction);
            return(sm);
        }
Example #3
0
 /// <summary>
 /// Initialize by StateMachineDefinition instance.
 /// </summary>
 /// <param name="def">StateMachineDefinition instance</param>
 /// <param name="logger">the logger used to</param>
 /// <param name="actionInitializer"></param>
 public void Initialize(StateMachineDefinition def, Func <IAction, IAction> actionInitializer)
 {
     _actionBuilder = new ActionBuilder(def.ActionPackage, actionInitializer);
     foreach (StateDefinition stateDef in def.States)
     {
         CLRLogger.GetInstance().LogDevInfo(string.Format("Load State {0}", stateDef.ID));
         State state = new State(_actionBuilder);
         state.Initialize(stateDef);
         if (States.Contains(state))
         {
             throw new StateMachineException(string.Format("try to add an existed state {0}", state.ID));
         }
         States.Add(state);
         if (state.Category == StateCategory.INITIAL)
         {
             CurrentState = state;
         }
     }
     foreach (TransitionDefinition tranDef in def.Transitions)
     {
         LoadTransaction(tranDef);
     }
 }