コード例 #1
0
ファイル: ExecuteAction.cs プロジェクト: nasa03/Jasonity
        public ExecuteAction Clone()
        {
            ExecuteAction ae = new ExecuteAction((Pred)action.Clone(), intention.Clone());

            ae.result = result;
            return(ae);
        }
コード例 #2
0
        // Reads in a problem from a file.
        public static Problem GetProblem(string file)
        {
            // Create the problem object.
            Problem problem = new Problem();

            // Read the domain file into a string.
            string input = System.IO.File.ReadAllText(file);

            // Split the input string by space, line feed, character return, and tab.
            string[] words = input.Split(new char[] { ' ', '\r', '\n', '\t' });

            // Remove all empty elements of the word array.
            words = words.Where(x => !string.IsNullOrEmpty(x)).ToArray();

            // Loop through the word array.
            for (int i = 0; i < words.Length; i++)
            {
                // Set the problem name.
                if (words[i].Equals("(problem"))
                {
                    problem.Name = words[i + 1].Remove(words[i + 1].Length - 1);
                }

                // Set the domain name.
                if (words[i].Equals("(:domain"))
                {
                    problem.Domain = words[i + 1].Remove(words[i + 1].Length - 1);
                }

                // Fill in the problem's internal information.
                if (words[i].Equals("(:objects"))
                {
                    // A list of temporary objects to store before we know their type.
                    List <string> tempObjects = new List <string>();

                    // Add the problem objects.
                    while (!Regex.Replace(words[i++], @"\t|\n|\r", "").ToLower().Equals("(:init"))
                    {
                        if (!Regex.Replace(words[i], @"\t|\n|\r", "").ToLower().Equals("(:init"))
                        {
                            if (!Regex.Replace(words[i], @"\t|\n|\r", "").ToLower().Equals("-"))
                            {
                                tempObjects.Add(Regex.Replace(words[i], @"\t|\n|\r|[()]", "").ToLower());
                            }
                            else
                            {
                                // Store the specified type.
                                string type = Regex.Replace(words[++i], @"\t|\n|\r|[()]", "").ToLower();

                                // For all the stored objects...
                                foreach (string tempObj in tempObjects)
                                {
                                    if (tempObj != "")
                                    {
                                        // ... associate them with their type and add them to the problem.
                                        problem.Objects.Add(new Obj(tempObj, type));
                                    }
                                }

                                // Clear the temporary objects list.
                                tempObjects = new List <string>();
                            }
                        }
                    }

                    // Add objects with unspecified types to the problem.
                    foreach (string tempObj in tempObjects)
                    {
                        if (tempObj != "")
                        {
                            problem.Objects.Add(new Obj(tempObj, ""));
                        }
                    }

                    // Add the initial state.
                    while (!Regex.Replace(words[i], @"\t|\n|\r", "").ToLower().Equals("(:goal"))
                    {
                        if (words[i][0] == '(')
                        {
                            // Check for an intention predicate.
                            if (words[i].Equals("(intends"))
                            {
                                // Create a new intention object.
                                Intention intends = new Intention();

                                // Add the character to the intention object.
                                intends.Character = words[++i];

                                // Check for a negative predicate.
                                if (words[++i].Equals("(not"))
                                {
                                    // Iterate the counter.
                                    i++;

                                    // Set the predicate's sign to false.
                                    intends.Predicate.Sign = false;
                                }

                                // Set the predicate's name.
                                intends.Predicate.Name = Regex.Replace(words[i++], @"\t|\n|\r|[()]", "");

                                // Add the predicates's terms.
                                while (words[i][0] != '(')
                                {
                                    intends.Predicate.Terms.Add(new Term("", Regex.Replace(words[i++], @"\t|\n|\r|[()]", "")));
                                }

                                // Add the intention to the problem.
                                problem.Intentions.Add(intends.Clone() as Intention);
                            }
                            else
                            {
                                // Create a new predicate object.
                                Predicate pred = new Predicate();

                                // Check for a negative predicate.
                                if (words[i].Equals("(not"))
                                {
                                    // Iterate the counter.
                                    i++;

                                    // Set the predicate's sign to false.
                                    pred.Sign = false;
                                }

                                // Set the predicate's name.
                                pred.Name = Regex.Replace(words[i++], @"\t|\n|\r|[()]", "");

                                // Add the predicates's terms.
                                while (words[i][0] != '(')
                                {
                                    if (!Regex.Replace(words[i], @"\t|\n|\r|[()]", "").Equals(""))
                                    {
                                        pred.Terms.Add(new Term("", Regex.Replace(words[i++], @"\t|\n|\r|[()]", "")));
                                    }
                                    else
                                    {
                                        i++;
                                    }
                                }

                                // Add the predicate to the initial state.
                                problem.Initial.Add(pred);
                            }
                        }
                    }

                    // Add the goal state.
                    while (i++ < words.Length - 1)
                    {
                        if (words[i][0] == '(')
                        {
                            if (!words[i].ToLower().Equals("(and"))
                            {
                                // Create a new predicate object.
                                Predicate pred = new Predicate();

                                // Check for a negative predicate.
                                if (words[i].Equals("(not"))
                                {
                                    // Iterate the counter.
                                    i++;

                                    // Set the predicate's sign to false.
                                    pred.Sign = false;
                                }

                                // Set the predicate's name.
                                pred.Name = Regex.Replace(words[i], @"\t|\n|\r|[()]", "");

                                // Add the predicate to the goal state.
                                problem.Goal.Add(pred);
                            }
                        }
                        else
                        {
                            // Add the predicate's terms.
                            if (!words[i].Equals(")"))
                            {
                                problem.Goal.Last().Terms.Add(new Term("", Regex.Replace(words[i], @"\t|\n|\r|[()]", "")));
                            }
                        }
                    }
                }
            }

            // Kind of a hack.
            problem.OriginalName = problem.Name;

            return(problem);
        }