Beispiel #1
0
        public BrigitGraph CreateDecision(YamlMappingNode yamlNode)
        {
            BrigitGraph graph    = new BrigitGraph();
            Decision    decision = new Decision();
            Node        baseNode = new Node(decision);

            graph.AddNode(baseNode);

            foreach (YamlMappingNode choiceYamlNode in (YamlSequenceNode)yamlNode.Children[new YamlScalarNode("decision")])
            {
                Choice choice = DeserializeChoice(choiceYamlNode);
                choice.NextNode = -1;
                decision.Choices.Add(choice);

                // Node has inline branch
                if (choiceYamlNode.Children.ContainsKey(new YamlScalarNode("graph")))
                {
                    var inlineBranch = CreateGraph((YamlSequenceNode)choiceYamlNode.Children[new YamlScalarNode("graph")]);
                    graph.AddBranch(baseNode, inlineBranch);
                    choice.NextNode = baseNode.Next.Count - 1;
                }
            }

            foreach (Choice choice in decision.Choices.Where(x => x.NextNode == -1))
            {
                choice.NextNode = baseNode.Next.Count;
            }

            return(graph);
        }
Beispiel #2
0
        public BrigitGraph CreateFork(YamlMappingNode yamlNode)
        {
            var forkingGraph           = new BrigitGraph();
            var nonInteractiveDecision = new Decision();

            nonInteractiveDecision.Interactive = false;
            var baseNode = new Node(nonInteractiveDecision);

            forkingGraph.AddNode(baseNode);

            foreach (YamlMappingNode node in (YamlSequenceNode)yamlNode.Children[new YamlScalarNode("fork")])
            {
                var choice = new Choice();
                choice.NextNode = -1;
                string expression = GetScalarYamlNodeValue("path", node);
                choice.Attributes.Expression = BrigitExpressionParser.Parse(expression);

                if (node.Children.ContainsKey(new YamlScalarNode("graph")))
                {
                    var subGraph = CreateGraph((YamlSequenceNode)node.Children[new YamlScalarNode("graph")]);
                    forkingGraph.AddBranch(baseNode, subGraph);
                    choice.NextNode = baseNode.Next.Count - 1;
                }

                nonInteractiveDecision.Choices.Add(choice);
            }

            foreach (Choice choice in nonInteractiveDecision.Choices.Where(x => x.NextNode == -1))
            {
                choice.NextNode = baseNode.Next.Count;
            }

            return(forkingGraph);
        }
Beispiel #3
0
        public void Add_BranchingLinkedListToLinkedList_NoException()
        {
            BrigitGraph ll = new BrigitGraph();
            Node        n1 = new Node()
            {
                Data = 1
            };

            ll.AddNode(n1);

            Node n2 = new Node()
            {
                Data = 2
            };

            ll.AddNode(n2);

            Node n3 = new Node()
            {
                Data = 3
            };

            ll.AddNode(n3);

            Node nn1 = new Node()
            {
                Data = 4
            };
            Node nn2 = new Node()
            {
                Data = 5
            };
            Node nn3 = new Node()
            {
                Data = 6
            };
            BrigitGraph ll2 = new BrigitGraph();

            ll2.AddNode(nn1);
            ll2.AddNode(nn2);
            ll2.AddNode(nn3);

            ll.AddBranch(n2, ll2);
        }
Beispiel #4
0
        // The multiple lines to the tail node are being created
        // because of the recurisve nature of the ToString function i wrote
        public void Parse_TomeTest3()
        {
            var yaml = new YamlStream();

            yaml.Load(GetReader("TomeTest_3.yml"));
            var mapping    = (YamlMappingNode)yaml.Documents[0].RootNode;
            var yamlParser = new BrigitYamlParser(mapping);
            var conv       = yamlParser.CreateGraphFromYaml();

            var constructed = new BrigitGraph();

            constructed.AddNode(new Node
            {
                Data = new Dialog("Diana", "I didn't want to be the one to forget")
            });
            constructed.AddNode(new Node
            {
                Data = new Dialog("Diego", "I thought of everything I'd never regret")
            });

            // looks like they're routing to the wrong places
            var choice = new Node()
            {
                Data = new Decision()
                {
                    Choices = new List <Choice>()
                    {
                        new Choice("A little time with you is all that I get", 0),
                        new Choice("That's all we need because that's all we can take", 1),
                        new Choice("I don't believe in him - his lips on the ground", 2),
                        new Choice("I wanna take you back to the place by the rock", 2)
                    }
                }
            };

            constructed.AddNode(choice);

            // chorus creation and then addition
            var chorusSubGraph = new BrigitGraph();

            chorusSubGraph.AddNode(new Node
            {
                Data = new Dialog("Diego", "I gotta be in your arms baby", "But far away I seek for your light",
                                  "I hold on because for you my heart keeps beating")
            });
            constructed.AddBranch(choice, chorusSubGraph);


            var diegoChoiceSubGraph = new BrigitGraph();

            diegoChoiceSubGraph.AddNode(new Node()
            {
                Data = new Dialog("Diego", "One thing I never see the same when you're round")
            });

            // will probably check here to make sure this works
            // the error may happen some where around here
            constructed.AddBranch(choice, diegoChoiceSubGraph);

            // everything seems fine up to this point
            constructed.AddNode(new Node()
            {
                Data = new Dialog("Diana", "But no one gives us time anymore")
            });

            constructed.AddNode(new Node()
            {
                Data = new Dialog("Diego", "Will you be my light?")
            });


            bool checker = conv.Equals(constructed);

            Assert.AreEqual(true, checker);
        }
Beispiel #5
0
        public void Parse_TomeTest2()
        {
            var yaml = new YamlStream();

            yaml.Load(GetReader("TomeTest_2.yml"));
            var mapping    = (YamlMappingNode)yaml.Documents[0].RootNode;
            var yamlParser = new BrigitYamlParser(mapping);
            var conv       = yamlParser.CreateGraphFromYaml();

            BrigitGraph constructed = new BrigitGraph();

            constructed.AddNode(new Node()
            {
                Data = new Dialog("Yulia", "What the f**k is this", "What are you doing?")
            });

            // the choice sub graph
            BrigitGraph subGraph = new BrigitGraph();
            Decision    root     = new Decision()
            {
                Choices = new List <Choice>()
                {
                    new Choice("Nothing", 0),
                    new Choice("Everything", 2),
                    new Choice("Go away", 1),
                }
            };

            subGraph.AddNode(new Node()
            {
                Data = root
            });

            // the first branch
            BrigitGraph nothingBranch = new BrigitGraph();

            nothingBranch.AddNode(new Node()
            {
                Data = new Dialog("Yulia", "You're lying")
            });
            nothingBranch.AddNode(new Node()
            {
                Data = new Dialog("Diego", "Yeah she is")
            });

            subGraph.AddBranch(subGraph.Head, nothingBranch);

            // the second branch pointed to by the 3rd choice
            BrigitGraph goAwayBranch = new BrigitGraph();

            goAwayBranch.AddNode(new Node()
            {
                Data = new Dialog("Yulia", "NO")
            });
            subGraph.AddBranch(subGraph.Head, goAwayBranch);

            constructed.AddGraph(subGraph);
            constructed.AddNode(new Node()
            {
                Data = new Dialog("Diego", "There's a lot of yelling going on right now")
            });


            bool checker = conv.Equals(constructed);

            Assert.AreEqual(true, checker);
        }
Beispiel #6
0
        public BrigitGraph ParseDescision(TomeStream stream, Dictionary <string, OpenChoice> branchEndings)
        {
            Decision    descision = new Decision();
            BrigitGraph ll        = new BrigitGraph();
            Node        root      = new Node()
            {
                Data = descision
            };

            ll.AddNode(root);

            // first parse away the @player:
            AssertChar(stream, '@');
            AssertAlphaDigitString(stream, "player");
            AssertChar(stream, '{');
            EatWhitespace(stream);

            ParsingState state = ParsingState.ExpectingMore;

            while (state == ParsingState.ExpectingMore)
            {
                // parse the choice (same as parse text with esacape)
                // parse attributes if there are any
                Choice ch = ParseChoice(stream, ref state);
                // -1 is the place holder for now. will be set to an actual number
                // or to the size of list
                ch.NextNode = -1;
                descision.Choices.Add(ch);

                // at this point either the parsing is complete
                // or there is an arrow pointing to a sub branch or a branch name

                // Parseing.Expecting more implies that the arrow maybe still be here
                // Some branches have multiple "nexts" where the next either points to the same node
                // or to two different ones within the same branching pattern.
                //  It's the addition of the nodes that's wrong
                if (ParseArrow(stream))
                {
                    // whitespace
                    EatWhitespace(stream);
                    // either it's a branch
                    if (stream.PeekChar() == '{')
                    {
                        AssertChar(stream, '{');
                        // parse the subbranch if there is one
                        // parse the branch name is there is one, and we didn't parse a sub branch
                        // add the subbranch to the next list if there is none set their "next" to -1
                        BrigitGraph subGraph = ParseBrigitGraph(stream);

                        ll.AddBranch(root, subGraph);
                        ch.NextNode = root.Next.Count - 1;
                    }
                    // or a branch name
                    else
                    {
                        string     BranchName = ParseOnlyTextNoEscape(stream);
                        OpenChoice openCh     = new OpenChoice(root, ch);
                        branchEndings.Add(BranchName, openCh);
                    }

                    // this means it has reach the end
                    if (stream.PeekChar() == '}')
                    {
                        state = ParsingState.Complete;
                        stream.NextChar();
                    }
                }
            }

            // any next nodes in descision that are -1 should be set
            // to the count of the list
            foreach (Choice c in descision.Choices)
            {
                if (c.NextNode == -1)
                {
                    c.NextNode = root.Next.Count;
                }
            }

            return(ll);
        }
Beispiel #7
0
        // this will select a branch from a set given to it
        // this looks really similar to chioce with some smaller but important changes
        public BrigitGraph ParseBranchSelector(Dictionary <string, OpenChoice> branchEndings)
        {
            AssertChar(Stream, '{');
            // the loop consists of parsing expression, parsing branch
            // then check for a new expression or the end

            // TODO replace this fix with a more elegant solution
            // hot fix used for resetting the open choices
            List <string> branches = new List <string>();
            Decision      selector = new Decision();

            selector.Interactive = false;
            BrigitGraph graph = new BrigitGraph();
            Node        root  = new Node()
            {
                Data = selector
            };

            graph.AddNode(root);

            while (Stream.PeekChar() != '}')
            {
                // gets chars up to the *
                string      expression = FetchNonStarSubString();
                IExpression exp;
                if (expression == "_")
                {
                    //exp = new Tautalogy();
                    exp = new Variable("TRUE");
                }
                else
                {
                    exp = BrigitExpressionParser.Parse(expression);
                }
                // eat the star that ends the expresssion
                AssertChar(Stream, '*');
                Choice ch = new Choice();
                ch.Attributes.Expression = exp;
                ch.Text     = string.Empty;
                ch.NextNode = -1;

                EatWhitespace(Stream);

                // if there is no arrow then it should be considered a
                // "pass"
                // there can be multiple passes, but there can only be one default which is "_"
                if (Stream.PeekChar() == '-')
                {
                    // parse either a sub graph or parse a branchname
                    ParseArrow(Stream);
                    EatWhitespace(Stream);

                    if (Stream.PeekChar() == '{')
                    {
                        AssertChar(Stream, '{');
                        BrigitGraph subGraph = ParseBrigitGraph(Stream);
                        graph.AddBranch(root, subGraph);
                        ch.NextNode = root.Next.Count - 1;
                    }
                    else if (Char.IsLetterOrDigit(Stream.PeekChar()))
                    {
                        string BranchName = ParseOnlyTextNoEscape(Stream);
                        branches.Add(BranchName);
                        OpenChoice open = new OpenChoice(root, ch);
                        branchEndings.Add(BranchName, open);
                    }
                    else
                    {
                        // throw not recognized
                        string msg = String.Format("Expected beginning of branch name or sub graph but found {0} at {1}", Stream.PeekChar(), Stream.Position);
                        throw new Exception(msg);
                    }
                }
                // a pass
                else
                {
                }
                selector.Choices.Add(ch);
                EatWhitespace(Stream);
            }

            // to end this off
            AssertChar(Stream, '}');

            foreach (Choice ch in selector.Choices)
            {
                if (ch.NextNode == -1)
                {
                    ch.NextNode = root.Next.Count;
                }
            }

            foreach (string s in branches)
            {
                branchEndings[s].TailNode = null;
            }

            return(graph);
        }