Exemplo n.º 1
0
        public void GenerateMoreOrLess()
        {
            CoreCommand.BinaryManager manager = new CoreCommand.BinaryManager();
            MemoryStream commands             = new MemoryStream();
            MemoryStream output  = new MemoryStream();
            uint         integer = (uint)EntityFactory.BASE_ID.INTEGER_TYPE;

            Declare.Reply moreOrLessContext = HandleCommand <Declare.Reply, Declare>(
                new Declare
            {
                EntityType  = EntityFactory.ENTITY.CONTEXT,
                ContainerID = 0,
                Name        = "moreOrLess",
                Visibility  = EntityFactory.VISIBILITY.PUBLIC
            }, manager);

            //int min = 0;
            Declare.Reply minVariable = HandleCommand <Declare.Reply, Declare>(
                new Declare
            {
                EntityType  = EntityFactory.ENTITY.VARIABLE,
                ContainerID = moreOrLessContext.EntityID,
                Name        = "min",
                Visibility  = EntityFactory.VISIBILITY.PRIVATE
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Variable.SetType>(
                new CoreCommand.Command.Variable.SetType
            {
                VariableID = minVariable.EntityID,
                TypeID     = integer
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Variable.SetValue>(
                new CoreCommand.Command.Variable.SetValue
            {
                VariableID = minVariable.EntityID,
                Value      = "0"
            }, manager);

            //int max = 100;
            Declare.Reply maxVariable = HandleCommand <Declare.Reply, Declare>(
                new Declare
            {
                EntityType  = EntityFactory.ENTITY.VARIABLE,
                ContainerID = moreOrLessContext.EntityID,
                Name        = "max",
                Visibility  = EntityFactory.VISIBILITY.PRIVATE
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Variable.SetType>(
                new CoreCommand.Command.Variable.SetType
            {
                VariableID = maxVariable.EntityID,
                TypeID     = integer
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Variable.SetValue>(
                new CoreCommand.Command.Variable.SetValue
            {
                VariableID = maxVariable.EntityID,
                Value      = "100"
            }, manager);

            //int lastGiven = -1;
            Declare.Reply lastGivenVariable = HandleCommand <Declare.Reply, Declare>(
                new Declare
            {
                EntityType  = EntityFactory.ENTITY.VARIABLE,
                ContainerID = moreOrLessContext.EntityID,
                Name        = "lastGiven",
                Visibility  = EntityFactory.VISIBILITY.PRIVATE
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Variable.SetType>(
                new CoreCommand.Command.Variable.SetType
            {
                VariableID = lastGivenVariable.EntityID,
                TypeID     = integer
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Variable.SetValue>(
                new CoreCommand.Command.Variable.SetValue
            {
                VariableID = lastGivenVariable.EntityID,
                Value      = "-1"
            }, manager);

            /*
             * enum COMPARISON
             * {
             *      MORE = 0,
             *      LESS = 1,
             *      NONE = 2
             * }
             */
            Declare.Reply COMPARISONenum = HandleCommand <Declare.Reply, Declare>(
                new Declare
            {
                EntityType  = EntityFactory.ENTITY.ENUM_TYPE,
                ContainerID = moreOrLessContext.EntityID,
                Name        = "COMPARISON",
                Visibility  = EntityFactory.VISIBILITY.PUBLIC
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Enum.SetValue>(
                new CoreCommand.Command.Enum.SetValue
            {
                EnumId = COMPARISONenum.EntityID,
                Name   = "MORE",
                Value  = "0"
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Enum.SetValue>(
                new CoreCommand.Command.Enum.SetValue
            {
                EnumId = COMPARISONenum.EntityID,
                Name   = "LESS",
                Value  = "1"
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Enum.SetValue>(
                new CoreCommand.Command.Enum.SetValue
            {
                EnumId = COMPARISONenum.EntityID,
                Name   = "NONE",
                Value  = "2"
            }, manager);

            /*
             * COMPARISON Play(COMPARISON lastResult = NONE);
             */
            Declare.Reply playFunction = HandleCommand <Declare.Reply, Declare>(
                new Declare
            {
                EntityType  = EntityFactory.ENTITY.FUNCTION,
                ContainerID = moreOrLessContext.EntityID,
                Name        = "Play",
                Visibility  = EntityFactory.VISIBILITY.PUBLIC
            }, manager);
            Declare.Reply play_lastResultVariable = HandleCommand <Declare.Reply, Declare>(
                new Declare
            {
                EntityType  = EntityFactory.ENTITY.VARIABLE,
                ContainerID = playFunction.EntityID,
                Name        = "lastResult",
                Visibility  = EntityFactory.VISIBILITY.PUBLIC
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Function.SetParameter>(
                new CoreCommand.Command.Function.SetParameter
            {
                FuncId          = playFunction.EntityID,
                ExternalVarName = "lastResult"
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Variable.SetType>(
                new CoreCommand.Command.Variable.SetType
            {
                VariableID = play_lastResultVariable.EntityID,
                TypeID     = COMPARISONenum.EntityID
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Variable.SetValue>(
                new CoreCommand.Command.Variable.SetValue
            {
                VariableID = play_lastResultVariable.EntityID,
                Value      = "2"
            }, manager);
            Declare.Reply play_resultVariable = HandleCommand <Declare.Reply, Declare>(
                new Declare
            {
                EntityType  = EntityFactory.ENTITY.VARIABLE,
                ContainerID = playFunction.EntityID,
                Name        = "result",
                Visibility  = EntityFactory.VISIBILITY.PUBLIC
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Function.SetReturn>(
                new CoreCommand.Command.Function.SetReturn
            {
                FuncId          = playFunction.EntityID,
                ExternalVarName = "result"
            }, manager);
            HandleCommand <EmptyReply, CoreCommand.Command.Variable.SetType>(
                new CoreCommand.Command.Variable.SetType
            {
                VariableID = play_resultVariable.EntityID,
                TypeID     = integer
            }, manager);

            AddInstruction.Reply splitCOMPARISON = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.ENUM_SPLITTER,
                Arguments  = new List <uint> {
                    COMPARISONenum.EntityID
                }
            }, manager);
            AddInstruction.Reply getLastResult = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.GETTER,
                Arguments  = new List <uint> {
                    play_lastResultVariable.EntityID
                }
            }, manager);

            //if (lastResult == COMPARISON::MORE)
            AddInstruction.Reply lr_eq_more = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.EQUAL,
                Arguments  = new List <uint> {
                    COMPARISONenum.EntityID, COMPARISONenum.EntityID
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = splitCOMPARISON.InstructionID,
                OutputName = "MORE",
                ToId       = lr_eq_more.InstructionID,
                InputName  = "LeftOperand"
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = getLastResult.InstructionID,
                OutputName = "reference",
                ToId       = lr_eq_more.InstructionID,
                InputName  = "RightOperand"
            }, manager);
            AddInstruction.Reply if_lr_eq_more = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.IF,
                Arguments  = new List <uint> {
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = lr_eq_more.InstructionID,
                OutputName = "result",
                ToId       = if_lr_eq_more.InstructionID,
                InputName  = "condition"
            }, manager);

            //min = lastGiven
            AddInstruction.Reply getLastGiven = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.GETTER,
                Arguments  = new List <uint> {
                    lastGivenVariable.EntityID
                }
            }, manager);
            AddInstruction.Reply setMin = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.SETTER,
                Arguments  = new List <uint> {
                    minVariable.EntityID
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = getLastGiven.InstructionID,
                OutputName = "reference",
                ToId       = setMin.InstructionID,
                InputName  = "value"
            }, manager);

            //if (lastResult == COMPARISON::LESS)
            AddInstruction.Reply lr_eq_less = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.EQUAL,
                Arguments  = new List <uint> {
                    COMPARISONenum.EntityID, COMPARISONenum.EntityID
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = getLastResult.InstructionID,
                OutputName = "reference",
                ToId       = lr_eq_less.InstructionID,
                InputName  = "LeftOperand"
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = splitCOMPARISON.InstructionID,
                OutputName = "LESS",
                ToId       = lr_eq_less.InstructionID,
                InputName  = "RightOperand"
            }, manager);
            AddInstruction.Reply if_lr_eq_less = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.IF,
                Arguments  = new List <uint> {
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = lr_eq_less.InstructionID,
                OutputName = "result",
                ToId       = if_lr_eq_less.InstructionID,
                InputName  = "condition"
            }, manager);

            //max = lastGiven
            AddInstruction.Reply set_max = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.SETTER,
                Arguments  = new List <uint> {
                    maxVariable.EntityID
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = getLastGiven.InstructionID,
                OutputName = "reference",
                ToId       = set_max.InstructionID,
                InputName  = "value"
            }, manager);

            //min / 2
            AddInstruction.Reply getMin = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.GETTER,
                Arguments  = new List <uint> {
                    minVariable.EntityID
                }
            }, manager);
            AddInstruction.Reply minHalf = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.DIV,
                Arguments  = new List <uint> {
                    integer, integer, integer
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = getMin.InstructionID,
                OutputName = "reference",
                ToId       = minHalf.InstructionID,
                InputName  = "LeftOperand"
            }, manager);
            HandleCommand <EmptyReply, SetInputValue>(
                new SetInputValue
            {
                FunctionID  = playFunction.EntityID,
                Instruction = minHalf.InstructionID,
                InputName   = "RightOperand",
                InputValue  = "2"
            }, manager);

            //max / 2
            AddInstruction.Reply getMax = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.GETTER,
                Arguments  = new List <uint> {
                    maxVariable.EntityID
                }
            }, manager);
            AddInstruction.Reply maxHalf = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.DIV,
                Arguments  = new List <uint> {
                    integer, integer, integer
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = getMax.InstructionID,
                OutputName = "reference",
                ToId       = maxHalf.InstructionID,
                InputName  = "LeftOperand"
            }, manager);
            HandleCommand <EmptyReply, SetInputValue>(
                new SetInputValue
            {
                FunctionID  = playFunction.EntityID,
                Instruction = maxHalf.InstructionID,
                InputName   = "RightOperand",
                InputValue  = "2"
            }, manager);

            //min / 2 + max / 2
            AddInstruction.Reply minHalfPlusMaxHalf = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.ADD,
                Arguments  = new List <uint> {
                    integer, integer, integer
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = minHalf.InstructionID,
                OutputName = "result",
                ToId       = minHalfPlusMaxHalf.InstructionID,
                InputName  = "LeftOperand"
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = maxHalf.InstructionID,
                OutputName = "result",
                ToId       = minHalfPlusMaxHalf.InstructionID,
                InputName  = "RightOperand"
            }, manager);

            //result = min / 2 + max / 2
            AddInstruction.Reply setResult = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.SETTER,
                Arguments  = new List <uint> {
                    play_resultVariable.EntityID
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = minHalfPlusMaxHalf.InstructionID,
                OutputName = "result",
                ToId       = setResult.InstructionID,
                InputName  = "value"
            }, manager);

            //result == lastGiven
            AddInstruction.Reply getResult = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.GETTER,
                Arguments  = new List <uint> {
                    play_resultVariable.EntityID
                }
            }, manager);
            AddInstruction.Reply resEqLastGiven = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.EQUAL,
                Arguments  = new List <uint> {
                    integer, integer
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = getLastGiven.InstructionID,
                OutputName = "reference",
                ToId       = resEqLastGiven.InstructionID,
                InputName  = "LeftOperand"
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = getResult.InstructionID,
                OutputName = "reference",
                ToId       = resEqLastGiven.InstructionID,
                InputName  = "RightOperand"
            }, manager);

            //if (result == lastGiven)
            AddInstruction.Reply ifResEqLastGiven = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.IF,
                Arguments  = new List <uint> {
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = resEqLastGiven.InstructionID,
                OutputName = "result",
                ToId       = ifResEqLastGiven.InstructionID,
                InputName  = "condition"
            }, manager);

            //lastResult == MORE
            AddInstruction.Reply lastResultEqMore = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.EQUAL,
                Arguments  = new List <uint> {
                    COMPARISONenum.EntityID, COMPARISONenum.EntityID
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = getLastResult.InstructionID,
                OutputName = "reference",
                ToId       = lastResultEqMore.InstructionID,
                InputName  = "LeftOperand"
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = splitCOMPARISON.InstructionID,
                OutputName = "MORE",
                ToId       = lastResultEqMore.InstructionID,
                InputName  = "RightOperand"
            }, manager);

            //if (lastResult == MORE)
            AddInstruction.Reply ifLastResultEqMore = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.IF,
                Arguments  = new List <uint> {
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = lastResultEqMore.InstructionID,
                OutputName = "result",
                ToId       = ifLastResultEqMore.InstructionID,
                InputName  = "condition"
            }, manager);

            //result + 1
            AddInstruction.Reply resultPP = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.ADD,
                Arguments  = new List <uint> {
                    integer, integer, integer
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = getResult.InstructionID,
                OutputName = "reference",
                ToId       = resultPP.InstructionID,
                InputName  = "LeftOperand"
            }, manager);
            HandleCommand <EmptyReply, SetInputValue>(
                new SetInputValue
            {
                FunctionID  = playFunction.EntityID,
                Instruction = resultPP.InstructionID,
                InputName   = "RightOperand",
                InputValue  = "1"
            }, manager);

            //result = result + 1
            AddInstruction.Reply setResultPP = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.SETTER,
                Arguments  = new List <uint> {
                    play_resultVariable.EntityID
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = resultPP.InstructionID,
                OutputName = "result",
                ToId       = setResultPP.InstructionID,
                InputName  = "value"
            }, manager);

            //result - 1
            AddInstruction.Reply resultMM = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.SUB,
                Arguments  = new List <uint> {
                    integer, integer, integer
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = getResult.InstructionID,
                OutputName = "reference",
                ToId       = resultMM.InstructionID,
                InputName  = "LeftOperand"
            }, manager);
            HandleCommand <EmptyReply, SetInputValue>(
                new SetInputValue
            {
                FunctionID  = playFunction.EntityID,
                Instruction = resultMM.InstructionID,
                InputName   = "RightOperand",
                InputValue  = "1"
            }, manager);

            //result = result - 1
            AddInstruction.Reply setResultMM = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.SETTER,
                Arguments  = new List <uint> {
                    play_resultVariable.EntityID
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = resultMM.InstructionID,
                OutputName = "result",
                ToId       = setResultMM.InstructionID,
                InputName  = "value"
            }, manager);

            //lastGiven = result
            AddInstruction.Reply setLastGiven = HandleCommand <AddInstruction.Reply, AddInstruction>(
                new AddInstruction
            {
                FunctionID = playFunction.EntityID,
                ToCreate   = InstructionFactory.INSTRUCTION_ID.SETTER,
                Arguments  = new List <uint> {
                    lastGivenVariable.EntityID
                }
            }, manager);
            HandleCommand <EmptyReply, LinkData>(
                new LinkData
            {
                FunctionID = playFunction.EntityID,
                FromId     = getResult.InstructionID,
                OutputName = "reference",
                ToId       = setLastGiven.InstructionID,
                InputName  = "value"
            }, manager);

            /*
             * if (lastResult == MORE)
             * {
             *      min = lastGiven
             * }
             * else
             * {
             *      if (lastResult == LESS)
             *      {
             *          max = lastGiven
             *      }
             * }
             *
             * result = min / 2 + max / 2
             *
             * if (number == lastGiven)
             * {
             *      if (lastResult == MORE)
             *      {
             *          number = number + 1
             *      }
             *      else
             *      {
             *          number = number - 1
             *      }
             * }
             *
             * lastGiven = number
             */

            HandleCommand <EmptyReply, LinkExecution>(
                new LinkExecution
            {
                FunctionID = playFunction.EntityID,
                FromId     = if_lr_eq_more.InstructionID,
                OutIndex   = 0,   //on true
                ToId       = setMin.InstructionID
            }, manager);
            HandleCommand <EmptyReply, LinkExecution>(
                new LinkExecution
            {
                FunctionID = playFunction.EntityID,
                FromId     = if_lr_eq_more.InstructionID,
                OutIndex   = 1,   //on false
                ToId       = if_lr_eq_less.InstructionID
            }, manager);

            HandleCommand <EmptyReply, LinkExecution>(
                new LinkExecution
            {
                FunctionID = playFunction.EntityID,
                FromId     = setMin.InstructionID,
                OutIndex   = 0,
                ToId       = setResult.InstructionID
            }, manager);

            HandleCommand <EmptyReply, LinkExecution>(
                new LinkExecution
            {
                FunctionID = playFunction.EntityID,
                FromId     = if_lr_eq_less.InstructionID,
                OutIndex   = 0,   //on true
                ToId       = set_max.InstructionID
            }, manager);
            HandleCommand <EmptyReply, LinkExecution>(
                new LinkExecution
            {
                FunctionID = playFunction.EntityID,
                FromId     = if_lr_eq_less.InstructionID,
                OutIndex   = 1,   //on false
                ToId       = setResult.InstructionID
            }, manager);

            HandleCommand <EmptyReply, LinkExecution>(
                new LinkExecution
            {
                FunctionID = playFunction.EntityID,
                FromId     = set_max.InstructionID,
                OutIndex   = 0,
                ToId       = setResult.InstructionID
            }, manager);

            HandleCommand <EmptyReply, LinkExecution>(
                new LinkExecution
            {
                FunctionID = playFunction.EntityID,
                FromId     = setResult.InstructionID,
                OutIndex   = 0,
                ToId       = ifResEqLastGiven.InstructionID
            }, manager);

            HandleCommand <EmptyReply, LinkExecution>(
                new LinkExecution
            {
                FunctionID = playFunction.EntityID,
                FromId     = ifResEqLastGiven.InstructionID,
                OutIndex   = 0,   //on true
                ToId       = ifLastResultEqMore.InstructionID
            }, manager);
            HandleCommand <EmptyReply, LinkExecution>(
                new LinkExecution
            {
                FunctionID = playFunction.EntityID,
                FromId     = ifResEqLastGiven.InstructionID,
                OutIndex   = 1,   //on false
                ToId       = setLastGiven.InstructionID
            }, manager);

            HandleCommand <EmptyReply, LinkExecution>(
                new LinkExecution
            {
                FunctionID = playFunction.EntityID,
                FromId     = ifLastResultEqMore.InstructionID,
                OutIndex   = 0,   //on true
                ToId       = setResultPP.InstructionID
            }, manager);
            HandleCommand <EmptyReply, LinkExecution>(
                new LinkExecution
            {
                FunctionID = playFunction.EntityID,
                FromId     = ifLastResultEqMore.InstructionID,
                OutIndex   = 1,   //on false
                ToId       = setResultMM.InstructionID
            }, manager);

            HandleCommand <EmptyReply, LinkExecution>(
                new LinkExecution
            {
                FunctionID = playFunction.EntityID,
                FromId     = setResultPP.InstructionID,
                OutIndex   = 0,
                ToId       = setLastGiven.InstructionID
            }, manager);
            HandleCommand <EmptyReply, LinkExecution>(
                new LinkExecution
            {
                FunctionID = playFunction.EntityID,
                FromId     = setResultMM.InstructionID,
                OutIndex   = 0,
                ToId       = setLastGiven.InstructionID
            }, manager);

            HandleCommand <EmptyReply, SetEntryPoint>(
                new SetEntryPoint
            {
                FunctionId  = playFunction.EntityID,
                Instruction = if_lr_eq_more.InstructionID
            }, manager);

            MoreOrLessExecuter(manager, playFunction.EntityID);

            HandleCommand <EmptyReply, Save>(new Save
            {
                Filename = "moreOrLess.duly"
            }, manager);

            CoreCommand.IManager witness = new CoreCommand.BinaryManager();

            HandleCommand <EmptyReply, Load>(new Load
            {
                Filename = "moreOrLess.duly"
            }, witness);

            MoreOrLessExecuter(witness, playFunction.EntityID);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generates the template source code file that will be understood by Unity.
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="variables"></param>
        /// <param name="functions"></param>
        /// <param name="dataTypes"></param>
        /// <returns></returns>
        internal string GenerateTemplateContent(CoreCommand.BinaryManager manager = null, List <CoreControl.EntityFactory.Entity> variables = null,
                                                List <CoreControl.EntityFactory.Entity> functions = null, List <CoreControl.EntityFactory.Entity> dataTypes = null)
        {
            var template      = new GeneratedCodeTemplate();
            var customObjects = new Queue <CustomObject>();

            enumNames.Clear();

            if (manager != null)
            {
                template.FilePath = Path.GetFileName(manager.FilePath);
                //Regex rgx = new Regex("[^a-zA-Z0-9 -]");
                //template.Namespace = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(rgx.Replace(Path.GetFileNameWithoutExtension(template.FilePath), ""));
                //template.Namespace = Path.GetFileNameWithoutExtension(template.FilePath).RemoveIllegalCharacters();
                template.Namespace = manager.Controller.GetMainContextName();
            }

            //if (functions?.Count > 0)
            //    template.ClassName = functions[0].Name;
            template.ClassName = template.Namespace;

            if (dataTypes != null)
            {
                foreach (var item in dataTypes)
                {
                    var type = manager.Controller.GetEntityType(item.Id);
                    if (type == CoreControl.EntityFactory.ENTITY.ENUM_TYPE)
                    {
                        var ret = "public ";
                        ret += $"enum {item.Name} {{";
                        foreach (var v in manager.Controller.GetEnumerationValues(item.Id))
                        {
                            ret += $"{v} = {manager.Controller.GetEnumerationValue(item.Id, v)},";
                        }
                        ret += "}";
                        template.DataTypes.Add(ret);
                        enumNames.Add(item.Id, item.Name);
                        template.EnumNames.Add(item.Name);
                    }
                    else if (type == CoreControl.EntityFactory.ENTITY.OBJECT_TYPE)
                    {
                        //template.DataTypes.Add(CreateObject(manager, item));
                        customObjects.Enqueue(new CustomObject(item, manager.Controller));
                    }
                }
                foreach (var obj in customObjects)
                {
                    template.DataTypes.Add(CreateObject(obj));
                }
            }

            if (variables != null)
            {
                template.Inputs.Clear();
                foreach (var item in variables)
                {
                    template.Inputs.Add(item.ToSerialString(manager.Controller));
                }
            }

            if (functions?.Count > 0)
            {
                template.Outputs.Clear();
                //template.FunctionId = functions[0].Id;
                foreach (var item in functions)
                {
                    var func = new Function();
                    func.Name       = item.Name;
                    func.FunctionId = item.Id;

                    template.Functions.Add(func);

                    //template.FunctionId = item.Id;
                    var pars = manager.Controller.GetFunctionParameters(item.Id);

                    // Gets the variables with the function container id
                    foreach (var v in pars)
                    {
                        var typeId   = manager.Controller.GetVariableType(v.Id);
                        var realType = manager.Controller.GetEntityType(typeId);
                        if (realType == CoreControl.EntityFactory.ENTITY.ENUM_TYPE)
                        {
                            var ret = $"{enumNames[typeId]} {v.Name}";
                            template.Inputs.Add(ret);
                        }
                        else
                        {
                            template.Inputs.Add(v.ToSerialString(manager.Controller));
                        }
                    }

                    for (int i = 0; i < pars.Count; i++)
                    {
                        func.FunctionArguments += $"{{\"{pars[i].Name}\", ({manager.Controller.GetVariableValue(pars[i].Id).GetType().ToString()}) {pars[i].Name}}},";
                    }
                    foreach (var ret in manager.Controller.GetFunctionReturns(item.Id))
                    {
                        template.Outputs.Add(ret.ToSerialString(manager.Controller));
                    }
                }
            }
            return(template.TransformText());
        }