Пример #1
0
        private static void AddThrowException(SMCSharpGenerator gen, string evName, StringBuilder buff)
        {
            var exceptionName = gen.StateMap.ExceptionName;

            buff.Append($"        throw new {exceptionName}")
            .AppendLine($"(\"{evName}\", {ArgName}.CurrentStateName);");
        }
Пример #2
0
        private static void AddErrorFuncCall(SMCSharpGenerator gen, string evName, StringBuilder buff)
        {
            var errorFunctionName = gen.StateMap.ErrorFunctionName;

            buff.Append($"        {ArgName}.{errorFunctionName}")
            .AppendLine($"(\"{evName}\", {ArgName}.CurrentState);");
        }
Пример #3
0
        private static SMCSharpGenerator MakeGeneratorWith(Builder.FSMRepresentationBuilder fsmbld)
        {
            var fsm = new SMCSharpGenerator();

            fsm.FSMInit(fsmbld.StateMap, "fileName");
            fsm.Initialize();
            return(fsm);
        }
Пример #4
0
        private void AddOpenEventDeclaration(SMCSharpGenerator gen, string evName, StringBuilder buff)
        {
            var stateMachineClass = gen.StateMap.Name;
            var methodName        = this.CreateMethodName(evName);

            buff.AppendLine($"    public virtual void {methodName}({stateMachineClass} {ArgName})")
            .AppendLine("    {");
        }
Пример #5
0
        protected static SMCSharpGenerator InitWithBuilder(FSMRepresentationBuilder fsmbld)
        {
            var retval = new SMCSharpGenerator();

            retval.FSMInit(fsmbld.StateMap, "fileName");
            retval.Initialize();
            return(retval);
        }
Пример #6
0
        public void Paramaters()
        {
            FSMGenerator fsm = new SMCSharpGenerator();

            fsm.FSMInit(new MutableStateMap(), "TurnStyle");
            fsm.Initialize();
            Assert.AreEqual("TurnStyle", fsm.InputFileName);
            Assert.AreEqual("TurnStyle", fsm.FilePrefix);
        }
Пример #7
0
        public void ConcreteStates()
        {
            FSMRepresentationBuilder fsmbld = InitBuilderState();
            StateMap     map = fsmbld.StateMap;
            FSMGenerator fsm = new SMCSharpGenerator();

            fsm.FSMInit(map, "TurnStyle");
            fsm.Initialize();
            Assert.AreEqual(2, fsm.ConcreteStates.Count());
        }
Пример #8
0
        public static StringBuilder AddFromGenerators(SMCSharpGenerator gen, Func <IEnumerable <CSharpCodeGenerator> > generatorFactory, StringBuilder buff)
        {
            var generators = generatorFactory.Invoke();

            foreach (var code in generators)
            {
                buff.Append(code.GenerateCode(gen));
            }
            return(buff);
        }
Пример #9
0
        public string GenerateCode(SMCSharpGenerator gen, StringBuilder buff)
        {
            if (gen.HasNamespace)
            {
                buff.AppendLine($"namespace {gen.Namespace}")
                    .AppendLine("{");
            }

            return buff.ToString();
        }
Пример #10
0
 private static void AddEventBody(SMCSharpGenerator gen, string evName, StringBuilder buff)
 {
     if (gen.StateMap.UsesExceptions)
     {
         AddThrowException(gen, evName, buff);
     }
     else
     {
         AddErrorFuncCall(gen, evName, buff);
     }
 }
Пример #11
0
        public string GenerateCode(SMCSharpGenerator gen, StringBuilder buff)
        {
            if (gen.HasUsing)
            {
                foreach (var item in gen.Usings)
                {
                    buff.AppendLine($"using {item};");
                }

                buff.AppendLine();
            }

            return(buff.ToString());
        }
Пример #12
0
        private void AddConcreteStates(SMCSharpGenerator gen, StringBuilder buff)
        {
            buff.AppendLine("    #region Static Properties For Each State")
            .AppendLine();
            foreach (var cs in gen.ConcreteStates)
            {
                buff.Append($"    public static State {CreateMethodName(cs)} ")
                .AppendLine($"{{ get; }} = new {ClassNameFor(cs)}();");
            }

            buff.AppendLine()
            .AppendLine("    #endregion")
            .AppendLine();
        }
Пример #13
0
        public override string GenerateCode(SMCSharpGenerator gen)
        {
            var buff = new StringBuilder();

            AddClassHeader(buff);
            AddOpeningClassDeclaration(buff);

            AddConcreteStates(gen, buff);
            AddPublicProperties(buff);
            AddEventMethods(gen, buff);

            AddClosingClassDeclaration(buff);

            return(buff.ToString());
        }
Пример #14
0
        public override string GenerateCode(SMCSharpGenerator gen)
        {
            this.gen = gen;
            var buff = new StringBuilder();

            foreach (var cs in this.gen.ConcreteStates)
            {
                GenerateConcreteStateClass(cs, buff);
            }

            var length = Environment.NewLine.Length;

            buff.Remove(buff.Length - length - 1, length);

            return(buff.ToString());
        }
Пример #15
0
        public override string GenerateCode(SMCSharpGenerator gen)
        {
            var stateMap = gen.StateMap;

            var buff = new StringBuilder()
                       .AppendLine("// <auto-generated />")
                       .AppendLine("//-----------------------------------------------------------------------")
                       .AppendLine($"// FSM:       {stateMap.Name}")
                       .AppendLine($"// Context:   {stateMap.ContextName}")
                       .AppendLine(GetErrorStrategy(stateMap, stateMap.UsesExceptions))
                       .AppendLine($"// Version:   {stateMap.Version}")
                       .AppendLine("//-----------------------------------------------------------------------")
                       .AppendLine();

            return(buff.ToString());
        }
Пример #16
0
        public override string GenerateCode(SMCSharpGenerator gen)
        {
            var stateMap = gen.StateMap;
            var buff     = new StringBuilder();

            AddClassHeader(buff);
            BeginClassDeclaration(buff, stateMap);
            AddFields(buff, stateMap);
            AddClassMembers(gen, buff);

            AddStateClasses(gen, buff);

            CloseClassDeclaration(buff);

            return(buff.ToString());
        }
Пример #17
0
        private void AddEventMethods(SMCSharpGenerator gen, StringBuilder buff)
        {
            buff.AppendLine("    #region Default Event Methods")
            .AppendLine();

            var events = gen.StateMap.Events;

            foreach (var evName in events)
            {
                AddEventHeader(evName, buff);
                AddOpenEventDeclaration(gen, evName, buff);
                AddEventBody(gen, evName, buff);
                AddCloseEventDeclaration(buff);
            }

            buff.AppendLine("    #endregion");
        }
Пример #18
0
 public override string GenerateCode(SMCSharpGenerator gen)
 {
     return(new StringBuilder()
            .AppendLine("    #region Public Properties")
            .AppendLine()
            .AppendLine("    public string Version => version;")
            .AppendLine()
            .AppendLine("    public string CurrentStateName => this.currentState.Name;")
            .AppendLine()
            .AppendLine("    internal State CurrentState")
            .AppendLine("    {")
            .AppendLine("        get { return this.currentState; }")
            .AppendLine("        set { this.currentState = value; }")
            .AppendLine("    }")
            .AppendLine()
            .AppendLine("    #endregion")
            .AppendLine()
            .ToString());
 }
Пример #19
0
        public override string GenerateCode(SMCSharpGenerator gen)
        {
            var buff = new StringBuilder()
                       .AppendLine("    #region Event Methods - forward to the current State")
                       .AppendLine();

            var events = gen.StateMap.Events;

            foreach (var evi in events)
            {
                var evName = CreateMethodName(evi);
                buff.Append($"    public void {evName}() ")
                .AppendLine($"=> this.currentState.{evName}(this);")
                .AppendLine();
            }

            buff.AppendLine("    #endregion")
            .AppendLine();

            return(buff.ToString());
        }
Пример #20
0
        public override string GenerateCode(SMCSharpGenerator gen)
        {
            var buff = new StringBuilder()
                       .AppendLine("    #region Constructors & Destructors")
                       .AppendLine()
                       .AppendLine($"    public {gen.StateMap.Name}()")
                       .AppendLine("    {");

            var iName = CreateMethodName(gen.StateMap.InitialState);

            buff.AppendLine($"        this.currentState = State.{iName};");

            var initialHierarchy = new List <State>();

            gen.GetStateHierarchy(initialHierarchy, gen.StateMap.InitialState);

            foreach (var newState in initialHierarchy)
            {
                var eactions = newState.EntryActions;
                if (eactions.Any())
                {
                    buff.AppendLine()
                    .AppendLine($"        // Entry functions for: {newState.Name}");
                }

                foreach (var action in eactions)
                {
                    buff.AppendLine($"        {action}();");
                }
            }

            buff.AppendLine("    }")
            .AppendLine()
            .AppendLine("    #endregion")
            .AppendLine();

            return(buff.ToString());
        }
Пример #21
0
 public override string GenerateCode(SMCSharpGenerator gen)
 => GenerateCode(gen, new StringBuilder());
Пример #22
0
 public abstract string GenerateCode(SMCSharpGenerator gen);
Пример #23
0
 public static StringBuilder AddFromGenerators(SMCSharpGenerator gen, Func <IEnumerable <CSharpCodeGenerator> > generatorFactory)
 => AddFromGenerators(gen, generatorFactory, new StringBuilder());
Пример #24
0
 private static void AddStateClasses(SMCSharpGenerator gen, StringBuilder buff) => AddFromGenerators(gen, CSharpCodeGeneratorBuilder.Instance.StateGenerators, buff);