public void TestForeach() { CorePackage.Entity.Function whileTester = new CorePackage.Entity.Function(); CorePackage.Entity.Variable l = new CorePackage.Entity.Variable(new CorePackage.Entity.Type.ListType(CorePackage.Entity.Type.Scalar.Floating), new List <double> { 1.0, 2.0, 42.0 }); //while(i != 10) CorePackage.Execution.Foreach loop = new CorePackage.Execution.Foreach(); loop.ContainerType = CorePackage.Entity.Type.Scalar.Floating; //CorePackage.Execution.Operators.Different whcondition = new CorePackage.Execution.Operators.Different(CorePackage.Entity.Type.Scalar.Integer, CorePackage.Entity.Type.Scalar.Integer); //whcondition.GetInput("LeftOperand").LinkTo(new CorePackage.Execution.Getter(i), "reference"); //whcondition.SetInputValue("RightOperand", 10); //loop.GetInput("condition").LinkTo(whcondition, "result"); loop.GetInput("array").LinkTo(new CorePackage.Execution.Getter(l), "reference"); //// i = i + 1; CorePackage.Entity.Variable i = new CorePackage.Entity.Variable(CorePackage.Entity.Type.Scalar.Integer); CorePackage.Entity.Variable j = new CorePackage.Entity.Variable(CorePackage.Entity.Type.Scalar.Floating); CorePackage.Execution.Setter ipp = new CorePackage.Execution.Setter(i); CorePackage.Execution.Setter ipp2 = new CorePackage.Execution.Setter(j); //CorePackage.Execution.Operators.Add ipone = new CorePackage.Execution.Operators.Add(CorePackage.Entity.Type.Scalar.Integer, CorePackage.Entity.Type.Scalar.Integer, CorePackage.Entity.Type.Scalar.Integer); //ipone.GetInput("LeftOperand").LinkTo(new CorePackage.Execution.Getter(i), "reference"); //ipone.SetInputValue("RightOperand", 1); //ipp.GetInput("value").LinkTo(ipone, "result"); ipp.GetInput("value").LinkTo(loop, "index"); ipp2.GetInput("value").LinkTo(loop, "element"); //loop.Do(ipp); loop.Do(ipp); ////i = 42; //CorePackage.Execution.Setter finalset = new CorePackage.Execution.Setter(i); //finalset.SetInputValue("value", 42); //loop.Done(finalset); ////=============================== whileTester.setEntryPoint(whileTester.addInstruction(loop)); whileTester.Call(); whileTester.Call(); Assert.IsTrue(i.Value == 3); //Assert.IsTrue(j.Value == 42); }
public void Test_while() { CorePackage.Entity.Function whileTester = new CorePackage.Entity.Function(); CorePackage.Entity.Variable i = new CorePackage.Entity.Variable(CorePackage.Entity.Type.Scalar.Integer, 0); //while(i != 10) CorePackage.Execution.While loop = new CorePackage.Execution.While(); CorePackage.Execution.Operators.Different whcondition = new CorePackage.Execution.Operators.Different(CorePackage.Entity.Type.Scalar.Integer, CorePackage.Entity.Type.Scalar.Integer); whcondition.GetInput("LeftOperand").LinkTo(new CorePackage.Execution.Getter(i), "reference"); whcondition.SetInputValue("RightOperand", 10); loop.GetInput("condition").LinkTo(whcondition, "result"); // i = i + 1; CorePackage.Execution.Setter ipp = new CorePackage.Execution.Setter(i); CorePackage.Execution.Operators.Add ipone = new CorePackage.Execution.Operators.Add(CorePackage.Entity.Type.Scalar.Integer, CorePackage.Entity.Type.Scalar.Integer, CorePackage.Entity.Type.Scalar.Integer); ipone.GetInput("LeftOperand").LinkTo(new CorePackage.Execution.Getter(i), "reference"); ipone.SetInputValue("RightOperand", 1); ipp.GetInput("value").LinkTo(ipone, "result"); loop.Do(ipp); //i = 42; CorePackage.Execution.Setter finalset = new CorePackage.Execution.Setter(i); finalset.SetInputValue("value", 42); loop.Done(finalset); //=============================== whileTester.setEntryPoint(whileTester.addInstruction(loop)); whileTester.Call(); Assert.IsTrue(i.Value == 42); }
public void TestSize() { CorePackage.Entity.Function whileTester = new CorePackage.Entity.Function(); CorePackage.Entity.Variable idx = new CorePackage.Entity.Variable(CorePackage.Entity.Type.Scalar.Integer); CorePackage.Entity.Variable l = new CorePackage.Entity.Variable(new CorePackage.Entity.Type.ListType(CorePackage.Entity.Type.Scalar.Floating), new List <double> { 1.0, 2.0, 42.0 }); CorePackage.Execution.Size size = new CorePackage.Execution.Size(); CorePackage.Execution.Setter setIdx = new CorePackage.Execution.Setter(idx); size.ContainerType = CorePackage.Entity.Type.Scalar.Floating; size.GetInput("array").LinkTo(new CorePackage.Execution.Getter(l), "reference"); setIdx.GetInput("value").LinkTo(size, "count"); whileTester.setEntryPoint(whileTester.addInstruction(setIdx)); whileTester.Call(); Assert.IsTrue(size.GetOutputValue("count") == 3); }
public void moreOrLess() { Context moreOrLessCtx = new Context(); EnumType cmp = new EnumType(Scalar.Integer); cmp.SetValue("MORE", new Variable(Scalar.Integer, 0)); cmp.SetValue("LESS", new Variable(Scalar.Integer, 1)); cmp.SetValue("NONE", new Variable(Scalar.Integer, 2)); Variable ctx_min = new Variable(Scalar.Integer, 0); Variable ctx_max = new Variable(Scalar.Integer, 100); Variable ctx_lastGiven = new Variable(Scalar.Integer, -1); moreOrLessCtx.Declare(cmp, "COMPARISON", AccessMode.EXTERNAL); moreOrLessCtx.Declare(ctx_min, "min", AccessMode.INTERNAL); moreOrLessCtx.Declare(ctx_max, "max", AccessMode.INTERNAL); moreOrLessCtx.Declare(ctx_lastGiven, "lastGiven", AccessMode.INTERNAL); Function play = new Function(); moreOrLessCtx.Declare(play, "Play", AccessMode.EXTERNAL); play.Declare(new Variable(cmp, cmp.GetValue("NONE").Value), "lastResult", AccessMode.EXTERNAL); play.SetVariableAs("lastResult", Function.VariableRole.PARAMETER); play.Declare(new Variable(Scalar.Integer, -1), "number", AccessMode.EXTERNAL); play.SetVariableAs("number", Function.VariableRole.RETURN); //if (lastResult == MORE) CorePackage.Execution.Operators.Equal lastResEqqMore = new CorePackage.Execution.Operators.Equal(cmp, cmp); lastResEqqMore.GetInput("LeftOperand").LinkTo(new CorePackage.Execution.Getter(cmp.GetValue("MORE")), "reference"); lastResEqqMore.GetInput("RightOperand").LinkTo(new CorePackage.Execution.Getter(play.GetParameter("lastResult")), "reference"); CorePackage.Execution.If play_entrypoint = new CorePackage.Execution.If(); play.setEntryPoint(play.addInstruction(play_entrypoint)); play_entrypoint.GetInput("condition").LinkTo(lastResEqqMore, "result"); //min = lastGiven CorePackage.Execution.Setter minEqLastGiven = new CorePackage.Execution.Setter(ctx_min); minEqLastGiven.GetInput("value").LinkTo(new CorePackage.Execution.Getter(ctx_lastGiven), "reference"); play_entrypoint.LinkTo(0, minEqLastGiven); //"set" if true //if (lastResult == LESS) CorePackage.Execution.Operators.Equal lastResEqqLess = new CorePackage.Execution.Operators.Equal(cmp, cmp); lastResEqqLess.GetInput("LeftOperand").LinkTo(new CorePackage.Execution.Getter(play.GetParameter("lastResult")), "reference"); lastResEqqLess.GetInput("RightOperand").LinkTo(new CorePackage.Execution.Getter(cmp.GetValue("LESS")), "reference"); CorePackage.Execution.If condition = new CorePackage.Execution.If(); condition.GetInput("condition").LinkTo(lastResEqqLess, "result"); play_entrypoint.LinkTo(1, condition); //"if" if false //max = lastGiven CorePackage.Execution.Setter maxEqLastGiven = new CorePackage.Execution.Setter(ctx_max); maxEqLastGiven.GetInput("value").LinkTo(new CorePackage.Execution.Getter(ctx_lastGiven), "reference"); condition.LinkTo(0, maxEqLastGiven); //number = min/2 + max / 2 CorePackage.Execution.Setter numCalculation = new CorePackage.Execution.Setter(play.GetReturn("number")); minEqLastGiven.LinkTo(0, numCalculation); condition.LinkTo(1, numCalculation); CorePackage.Execution.Operators.Add bigOp = new CorePackage.Execution.Operators.Add( Scalar.Integer, Scalar.Integer, Scalar.Integer); //min / 2 CorePackage.Execution.Operators.Divide minmid = new CorePackage.Execution.Operators.Divide( Scalar.Integer, Scalar.Integer, Scalar.Integer); minmid.GetInput("LeftOperand").LinkTo(new CorePackage.Execution.Getter(ctx_min), "reference"); minmid.SetInputValue("RightOperand", 2); bigOp.GetInput("LeftOperand").LinkTo(minmid, "result"); //max / 2 CorePackage.Execution.Operators.Divide maxmid = new CorePackage.Execution.Operators.Divide( Scalar.Integer, Scalar.Integer, Scalar.Integer); maxmid.GetInput("LeftOperand").LinkTo(new CorePackage.Execution.Getter(ctx_max), "reference"); maxmid.SetInputValue("RightOperand", 2); bigOp.GetInput("RightOperand").LinkTo(maxmid, "result"); numCalculation.GetInput("value").LinkTo(bigOp, "result"); //if (number == lastGiven) condition = new CorePackage.Execution.If(); CorePackage.Execution.Operators.Equal numberEqqLastGiven = new CorePackage.Execution.Operators.Equal(Scalar.Integer, Scalar.Integer); numberEqqLastGiven.GetInput("LeftOperand").LinkTo(new CorePackage.Execution.Getter(play.GetReturn("number")), "reference"); numberEqqLastGiven.GetInput("RightOperand").LinkTo(new CorePackage.Execution.Getter(ctx_lastGiven), "reference"); condition.GetInput("condition").LinkTo(numberEqqLastGiven, "result"); maxEqLastGiven.LinkTo(0, numCalculation); numCalculation.LinkTo(0, condition); //lastGiven = number CorePackage.Execution.Setter lastGivenEqNumber = new CorePackage.Execution.Setter(ctx_lastGiven); lastGivenEqNumber.GetInput("value").LinkTo(new CorePackage.Execution.Getter(play.GetReturn("number")), "reference"); condition.LinkTo(1, lastGivenEqNumber); //if (lastResult == MORE) CorePackage.Execution.If last = new CorePackage.Execution.If(); last.GetInput("condition").LinkTo(lastResEqqMore, "result"); condition.LinkTo(0, last); //number += 1 CorePackage.Execution.Setter numberPlusOne = new CorePackage.Execution.Setter(play.GetReturn("number")); CorePackage.Execution.Operators.Add numAddOne = new CorePackage.Execution.Operators.Add(Scalar.Integer, Scalar.Integer, Scalar.Integer); numAddOne.GetInput("LeftOperand").LinkTo(new CorePackage.Execution.Getter(play.GetReturn("number")), "reference"); numAddOne.SetInputValue("RightOperand", 1); numberPlusOne.GetInput("value").LinkTo(numAddOne, "result"); last.LinkTo(0, numberPlusOne); //number -= 1 CorePackage.Execution.Setter numberMinusOne = new CorePackage.Execution.Setter(play.GetReturn("number")); CorePackage.Execution.Operators.Substract numMinOne = new CorePackage.Execution.Operators.Substract(Scalar.Integer, Scalar.Integer, Scalar.Integer); numMinOne.GetInput("LeftOperand").LinkTo(new CorePackage.Execution.Getter(play.GetReturn("number")), "reference"); numMinOne.SetInputValue("RightOperand", 1); numberMinusOne.GetInput("value").LinkTo(numMinOne, "result"); last.LinkTo(1, numberMinusOne); //lastGiven = number (in any case) numberPlusOne.LinkTo(0, lastGivenEqNumber); numberMinusOne.LinkTo(0, lastGivenEqNumber); play.SetParameterValue("lastResult", cmp.GetValue("NONE").Value); System.Diagnostics.Debug.Write(play.ToDotFile()); int mystery_number = 47; int i = 0; do { play.Call(); string toprint = "IA give: " + play.GetReturn("number").Value.ToString(); Debug.WriteLine(toprint); if (play.GetReturn("number").Value > mystery_number) { play.SetParameterValue("lastResult", cmp.GetValue("LESS").Value); Debug.WriteLine("==> It's less"); } else if (play.GetReturn("number").Value < mystery_number) { play.SetParameterValue("lastResult", cmp.GetValue("MORE").Value); Debug.WriteLine("==> It's more"); } else { break; } ++i; } while (play.GetReturn("number").Value != mystery_number && i < 10); if (i == 10) { throw new Exception("Failed to reach mystery number in less that 10 times"); } else { Debug.Write("AI found the mystery number: " + mystery_number.ToString()); } }
public void Test_if_getter_setter_debug_functionCall_Instructions() { //Function that will be executed CorePackage.Entity.Function test = new CorePackage.Entity.Function(); //Variable used to check function validity CorePackage.Entity.Variable witness = new CorePackage.Entity.Variable(CorePackage.Entity.Type.Scalar.Integer, 42); //if (4 == 5) CorePackage.Execution.If f_cond = new CorePackage.Execution.If(); CorePackage.Execution.Operators.Equal condition = new CorePackage.Execution.Operators.Equal(CorePackage.Entity.Type.Scalar.Integer, CorePackage.Entity.Type.Scalar.Integer); condition.SetInputValue("LeftOperand", 4); condition.SetInputValue("RightOperand", 5); f_cond.GetInput("condition").LinkTo(condition, "result"); CorePackage.Entity.Function say_hello = new CorePackage.Entity.Function(); //print("Hello World !") CorePackage.Execution.Debug print_hello = new CorePackage.Execution.Debug(new CorePackage.Entity.Variable(CorePackage.Entity.Type.Scalar.String, "Hello World !")); print_hello.SetInputValue("to_print", "Hello"); //witness = 84 CorePackage.Execution.Setter true_change = new CorePackage.Execution.Setter(witness); true_change.SetInputValue("value", 84); print_hello.LinkTo(0, true_change); say_hello.setEntryPoint(say_hello.addInstruction(print_hello)); //If the condition is true, then do print_hello f_cond.Then(new CorePackage.Execution.FunctionCall(say_hello)); CorePackage.Entity.Function say_bye = new CorePackage.Entity.Function(); //print("Goodbye World !") CorePackage.Execution.Debug print_goodbye = new CorePackage.Execution.Debug(new CorePackage.Entity.Variable(CorePackage.Entity.Type.Scalar.String, "Goodbye World !")); //witness = 0 CorePackage.Execution.Setter false_change = new CorePackage.Execution.Setter(witness); false_change.SetInputValue("value", 0); print_goodbye.LinkTo(0, false_change); say_bye.setEntryPoint(say_bye.addInstruction(print_goodbye)); //Else, do print_goodbye f_cond.Else(new CorePackage.Execution.FunctionCall(say_bye)); //Set the function entry point before calling it test.setEntryPoint(test.addInstruction(f_cond)); //In this call, it will check that 4 is equal to 5, then it will execute print_goodbye and false_change test.Call(); //So the witness value is expected to be 0 if (witness.Value != 0) { throw new Exception("Failed: Witness have to be equal to 0"); } //To change the condition result, we will set the right operand of the operation to 4 condition.SetInputValue("RightOperand", 4); //Then, function call will check if 4 is equal to 4 in order to execute print_hello test.Call(); //So the witness value is exepected to be 84 if (witness.Value != 84) { throw new Exception("Failed: Witness have to be equal to 84"); } System.Diagnostics.Debug.Write(test.ToDotFile()); }