public void TestColumns()
        {
            Expect.Once.On(m_ResultViewHelper).Method("AddColumn").With("Determinate", "!Test");

            for (int i = 0; i < 10; i++)
            {
                Expect.Once.On(m_ResultViewHelper).Method("AddItem");
            }

            IRandom random = m_Mockery.NewMock<IRandom>();
            Project project = new Project(random);

            Rule rule = new Rule();

            Expect.AtLeastOnce.On(random).Method("NextDouble").Will(Return.Value((double)0.5));

            rule.Name = "test";
            rule.Probability = 1.0;
            rule.LineNumber = 1;

            LiteralCommand a = new LiteralCommand();
            a.Literal = "a";
            rule.Commands.Add(a);

            Rule rule2 = new Rule();

            rule2.Name = "test2";
            rule2.Probability = 1.0;
            rule2.LineNumber = 1;

            LiteralCommand b = new LiteralCommand();
            b.Literal = "b";
            rule2.Commands.Add(b);

            project.Rules.Add(rule);
            project.Rules.Add(rule2);

            MarkCommand m = new MarkCommand();
            m.Name = "Test";
            m.Value = "yes";

            rule2.Commands.Add(m);

            project.StartRules.Add("test", 5);
            project.StartRules.Add("test2", 5);

            project.Columns.Add("Determinate", "!Test");

            m_GeneratorController.Generate(project);

            Assert.AreEqual(2, project.StartRules.Count);
            m_Mockery.VerifyAllExpectationsHaveBeenMet();
        }
        public void TestGenerateOneStartingRule()
        {
            for (int i = 0; i < 10; i++)
            {
                Expect.Once.On(m_ResultViewHelper).Method("AddItem");
            }

            IRandom random = m_Mockery.NewMock<IRandom>();
            Project project = new Project(random);

            Rule rule = new Rule();

            rule.Name = "test";
            rule.Probability = 1.0;
            rule.LineNumber = 1;

            LiteralCommand a = new LiteralCommand();
            a.Literal = "a";
            rule.Commands.Add(a);

            project.Rules.Add(rule);

            project.StartRules.Add("test", 10);

            Expect.AtLeastOnce.On(random).Method("NextDouble").Will(Return.Value((double)0.5));

            m_GeneratorController.Generate(project);

            Assert.AreEqual(1, project.StartRules.Count);
            m_Mockery.VerifyAllExpectationsHaveBeenMet();
        }
示例#3
0
        private static bool ParseProject(object context, TextReader reader, string line, ref int lineNumber)
        {
            Project project = context as Project;

            int start = 0;

            try {
                if (!string.IsNullOrEmpty(line)) {
                    string command = ReadToken(line, ref start);

                    if (command.Equals("tokens", StringComparison.CurrentCultureIgnoreCase)) {
                        TokenSet tks = new TokenSet(null, Whee.WordBuilder.Helpers.Random.Instance);
                        int len = line.Length;
                        string token = ReadToken(line, ref start);
                        tks.Name = token;

                        List<string> expand = new List<string>();
                        bool didExpand = false;
                        bool @remove = false;
                        while (start > -1 && start < len) {
                            token = ReadToken(line, ref start, ref expand, ref didExpand, ref @remove);

                            if (!didExpand && !@remove) {
                                if (token.StartsWith("$") && token.Length > 1) {
                                    TokenSet refTks = project.TokenSets.FindByName(token.Substring(1));
                                    if (refTks != null) {
                                        tks.Tokens.AddRange(refTks.Tokens);
                                    }
                                    else {
                                        project.Warnings.Add(string.Format("Line {0}: The Tokens directive referenced another token set '{1}' which did not exist at the time of parsing.", lineNumber, token.Substring(1)));
                                    }
                                }
                                else {
                                    tks.Tokens.Add(token);
                                }
                            }
                        }

                        if (@remove) {
                            tks.Tokens.RemoveAll((string tok) => expand.Contains(tok));
                        }
                        else {
                            tks.Tokens.AddRange(expand);
                        }

                        if (tks.Tokens.Count == 0) {
                            project.Warnings.Add(string.Format("Line {0}: The Tokens directive requires at least 2 arguments.", lineNumber));
                        }

                        project.TokenSets.Add(tks);
                    }
                    else if (command.Equals("rule", StringComparison.CurrentCultureIgnoreCase)) {
                        Rule r = new Rule(null);
                        r.Name = ReadToken(line, ref start);
                        r.LineNumber = lineNumber;
                        double probability = 1;

                        string probstring = ReadToken(line, ref start);
                        if (probstring == "{") {
                            if (start > -1) {
                                throw new ApplicationException("The Rule directive must end with a {");
                            }
                        }
                        else {
                            if (!double.TryParse(probstring, out probability)) {
                                project.Warnings.Add(string.Format("Line {0}: The Rule directive requires the second argument to be numeric.", lineNumber));
                            }

                            probstring = ReadToken(line, ref start);

                            if (probstring != "{") {
                                throw new ApplicationException("The Rule directive must end with a {");
                            }
                        }

                        r.Probability = probability;

                        project.Rules.Add(r);
                        if (ReadLines(new object[] { project, r.Commands }, reader, ParseCommands, ref lineNumber)) {
                            project.Warnings.Add(string.Format("Line {1}: The rule '{0}' is not closed correctly.", r.Name, lineNumber));
                        }
                    }
                    else if (command.Equals("startingrule", StringComparison.CurrentCultureIgnoreCase)) {
                        string rulename = ReadToken(line, ref start);

                        if (start > -1) {
                            string amountstring = ReadToken(line, ref start);

                            if (start > -1) {
                                project.Warnings.Add(string.Format("Line {0}: The StartingRule directive requires 1 or 2 arguments.", lineNumber));
                            }

                            int val = 0;
                            if (int.TryParse(amountstring, out val)) {
                                project.StartRules[rulename] = val;
                            }
                            else {
                                project.Warnings.Add(string.Format("Line {0}: The StartingRule directive requires that the second argument is a number.", lineNumber));
                            }
                        }
                        else {
                            project.StartRules[rulename] = 100;
                        }
                    }
                    else if (command.Equals("Column", StringComparison.CurrentCultureIgnoreCase)) {
                        string name = ReadToken(line, ref start);
                        if (start > -1) {
                            string value = ReadToken(line, ref start);

                            project.Columns.Add(name, value);
                        }
                        else {
                            project.Warnings.Add(string.Format("Line {0}: The Column directive requires two arguments.", lineNumber));
                        }
                    }
                    else {
                        project.Warnings.Add(string.Format("Line {1}: The command '{0}' was not recognized.", command, lineNumber));
                    }
                }
            }
            catch (Exception ex) {
                project.Warnings.Add(string.Format("Line {0}: {1}", lineNumber, ex.Message));
            }

            return true;
        }