Ejemplo n.º 1
0
        public void SudokuTest()
        {
            try
            {
                Handler      handler      = new DesktopHandler(new DLVDesktopService(GetPath()));
                InputProgram inputProgram = new ASPInputProgram();

                for (int i = 0; i < N; i++)
                {
                    for (int j = 0; j < N; j++)
                    {
                        if (sudokuMatrix[i, j] != 0)
                        {
                            inputProgram.AddObjectInput(new Cell(i, j, sudokuMatrix[i, j]));
                        }
                    }
                }

                inputProgram.AddFilesPath(".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "test-resources" + Path.DirectorySeparatorChar + "asp" + Path.DirectorySeparatorChar + "sudoku");
                handler.AddProgram(inputProgram);
                handler.StartAsync(new CallbackAction(o =>
                {
                    if (!(o is AnswerSets))
                    {
                        return;
                    }

                    answerSets = (AnswerSets)o;

                    @lock.Signal();
                }));
                @lock.Wait(new TimeSpan(0, 0, 0, 0, 5000));
                Assert.IsNotNull(answerSets);
                Assert.IsTrue(String.IsNullOrEmpty(answerSets.ErrorsString), "Found error in the Plan\n" + answerSets.ErrorsString);

                if (answerSets.Answersets.Count == 0)
                {
                    return;
                }

                AnswerSet @as = answerSets.Answersets[0];

                foreach (object obj in @as.Atoms)
                {
                    Cell cell = (Cell)obj;
                    sudokuMatrix[cell.getRow(), cell.getColumn()] = cell.getValue();
                }

                for (int i = 0; i < N; i++)
                {
                    for (int j = 0; j < N; j++)
                    {
                        Console.Write(sudokuMatrix[i, j] + " ");

                        if (sudokuMatrix[i, j] == 0)
                        {
                            Assert.Fail("NumberNotValid");
                        }
                    }

                    Console.WriteLine();
                }
            }
            catch (Exception e)
            {
                Assert.Fail("Exception " + e.Message);
            }
        }
        private void Test(int[] results_sizes, string instance_name)
        {
            string instance_path = base_path + instance_name + Path.DirectorySeparatorChar;

            Console.WriteLine("Testing " + results_sizes.Length + " files for " + instance_path);

            for (int i = 1; i <= results_sizes.Length; i++)
            {
                try
                {
                    plan  = null;
                    @lock = new CountdownEvent(1);
                    Handler handler = new DesktopHandler(new SPDDesktopService());

                    Console.WriteLine();

                    InputProgram inputProgramDomain = new PDDLInputProgram(PDDLProgramType.DOMAIN);

                    inputProgramDomain.AddFilesPath(instance_path + "domain.pddl");

                    InputProgram inputProgramProblem = new PDDLInputProgram(PDDLProgramType.PROBLEM);
                    string       problem             = instance_path + "p" + (i < 10 ? "0" : "") + i + ".pddl";

                    Console.WriteLine(problem);
                    Assert.IsTrue(File.Exists(problem), "File not found: " + problem);
                    inputProgramProblem.AddFilesPath(problem);
                    handler.AddProgram(inputProgramDomain);
                    handler.AddProgram(inputProgramProblem);
                    PDDLMapper.Instance.RegisterClass(typeof(PickUp));
                    Assert.IsNull(plan);
                    handler.StartAsync(new CallbackAction(o =>
                    {
                        if (!(o is Plan))
                        {
                            return;
                        }

                        plan = (Plan)o;

                        foreach (embasp.languages.pddl.Action action in plan.Actions)
                        {
                            Console.Write(action.Name + ",");
                        }

                        @lock.Signal();
                    }));
                    @lock.Wait(new TimeSpan(0, 0, 0, 0, 15000));
                    Assert.IsNotNull(plan);

                    if (results_sizes[i - 1] != 0)
                    {
                        Assert.IsTrue(String.IsNullOrEmpty(plan.ErrorsString) || plan.ErrorsString.ToLower().Contains("server busy"), "Found error in the Plan " + problem + "\n" + plan.ErrorsString);
                    }

                    if (plan.ErrorsString.ToLower().Contains("server busy."))
                    {
                        Console.WriteLine("[WARNING] Server busy occurred!");
                    }

                    Assert.AreEqual(results_sizes[i - 1], plan.Actions.Count);

                    foreach (object obj in plan.ActionsObjects)
                    {
                        if (obj is PickUp)
                        {
                            PickUp pickUp = (PickUp)obj;
                            Console.WriteLine(pickUp.getBlock());
                        }
                    }

                    Thread.Sleep(500);
                }
                catch (Exception e)
                {
                    Assert.Fail(e.Message);
                }
            }

            Console.WriteLine();
        }