Ejemplo n.º 1
0
        /// <summary>
        /// Solves all the non-terminal symbols within the given sentence.
        /// </summary>
        /// <param name="sentence">A string with non-terminal symbols to replace.</param>
        /// <param name="rnd">Random number generator used to choose the
        /// productions and generate the sentence</param>
        /// <param name="stackCounter">A counter that indicates how many times
        /// this function has called itself. It is used to prevent a stack overflow.
        /// When it reach 1000 the production is aborted.</param>
        /// <returns>A string with terminal symbols only</returns>
        /// <remarks>Recursive function</remarks>
        private TaskNode SolveNonTerminals(string sentence, Random rnd, int stackCounter)
        {
            if (++stackCounter > 999)
            {
                throw new StackOverflowException();
            }

            TaskNode node = new TaskNode(null);

            string[] parts = sentence.SmartSplit(' ');
            foreach (string part in parts)
            {
                if (part.Contains("$"))
                {
                    string   replacement = FindReplacement(part, rnd);
                    TaskNode child       = SolveNonTerminals(replacement, rnd, stackCounter + 1);
                    child.Term = part;
                    node.Children.Add(child);
                }
                else
                {
                    TaskNode child = new TaskNode(node);
                    child.StringValue = part;
                    node.Children.Add(child);
                }
            }
            return(node);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates a random sentence.
        /// </summary>
        /// <param name="rnd">Random number generator used to choose the
        /// productions and generate the sentence</param>
        /// <returns>A randomly generated sentence.</returns>
        public TaskNode GenerateSentence(Random rnd)
        {
            string   option = FindReplacement("$Main", rnd);
            TaskNode root   = SolveNonTerminals(option, rnd);

            root.Term = "$Main";
            root.Tier = Tier;
            return(root);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Randomly generates a task with the given grammar
 /// </summary>
 /// <param name="grammarName">The name of the grammar to generate the task</param>
 /// <param name="tier">The maximum difficulty degree allowed to produce the task</param>
 /// <returns></returns>
 public Task GenerateTask(string grammarName, DifficultyDegree tier)
 {
     for (int i = 0; i < 3; ++i)
     {
         try
         {
             TaskNode         root          = GetTaskPrototype(grammarName);
             string           taskPrototype = root.Render();
             WildcardReplacer replacer      = new WildcardReplacer(this, tier);
             if (String.IsNullOrEmpty(taskPrototype))
             {
                 return(null);
             }
             Task t = replacer.GetTask(taskPrototype);
             t.Tree = root;
             return(t);
         }
         catch
         {
             continue;
         }
     }
     return(null);
 }