Exemple #1
0
        public BrigitGraph CreateGraph(YamlSequenceNode rootGraphNode)
        {
            var graph = new BrigitGraph();

            foreach (YamlNode node in rootGraphNode)
            {
                string brigitNodeType = ((YamlMappingNode)node).Children.Keys.First().ToString();

                switch (brigitNodeType)
                {
                case "dialog":
                    var brigitNode = new Node(CreateDialog((YamlMappingNode)node));
                    graph.AddNode(brigitNode);
                    break;

                case "decision":
                    var subGraph = CreateDecision((YamlMappingNode)node);
                    graph.AddGraph(subGraph);
                    break;

                case "fork":
                    var forkGraph = CreateFork((YamlMappingNode)node);
                    graph.AddGraph(forkGraph);
                    break;

                default:
                    throw new Exception(String.Format("Node Type {0} not recognized", brigitNodeType));
                }
            }

            return(graph);
        }
Exemple #2
0
        public void Add_LinkedListToLinkedList_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.AddGraph(ll2);
        }
Exemple #3
0
        // public for testing purpose
        // this chooses what is parsed next, IE a branch, a dialog or descision
        public BrigitGraph ParseBrigitGraph(TomeStream stream)
        {
            BrigitGraph ll = new BrigitGraph();

            while (!(stream.Complete() || stream.PeekChar() == '}'))
            {
                // getting rid of some beginning whitespace
                EatWhitespace(stream);

                // the real parsing starts here
                // can't use switch need to use if elses
                char c = stream.PeekChar();
                if (Char.IsLetterOrDigit(c))
                {
                    // start parsing as a dialog
                    // this one is simple. parse the dialog. then add
                    // it to the list
                    Node n = ParseDialog(stream);

                    // for the new AddInBetween function
                    // this will only work for whatever comes next. This isn't very good
                    // if there's multiple branches to place
                    // TODO make this functionality work better with dummy tail in the subgraphs
                    foreach (KeyValuePair <string, OpenChoice> kvp in BranchesToPlace)
                    {
                        if (kvp.Value.TailNode == null)
                        {
                            kvp.Value.TailNode = n;
                            break;
                        }
                    }

                    ll.AddNode(n);
                }
                else if (c == '@')
                {
                    // naybe use a struct here?
                    Dictionary <string, OpenChoice> branchesToNode = new Dictionary <string, OpenChoice>();
                    // TODO change signature of ParseDescision to (obj stream, Dict brachesToNode)
                    BrigitGraph subGraph = ParseDescision(stream, branchesToNode);

                    foreach (KeyValuePair <string, OpenChoice> kvp in branchesToNode)
                    {
                        BranchesToPlace.Add(kvp.Key, kvp.Value);
                    }

                    // adding the dictionary entries to this
                    ll.AddGraph(subGraph);
                }
                else if (c == '{')
                {
                    // this is a branch selector
                    // we can just pass in the big dictionary
                    BrigitGraph subGraph = ParseBranchSelector(BranchesToPlace);
                    ll.AddGraph(subGraph);
                }
                else if (c == '>')
                {
                    // this is a branch name
                    // TODO change signature of ParseBranch to ParseBranch(stream, ref string branchName)
                    // TODO i'll probably need a wrapper for the Node and Ch entires
                    string      branchName = String.Empty;
                    BrigitGraph subGraph   = ParseBranch(stream, ref branchName);
                    if (BranchesToPlace.ContainsKey(branchName))
                    {
                        OpenChoice openCh = BranchesToPlace[branchName];
                        Node       n      = openCh.EnclosingNode;
                        Choice     ch     = openCh.BranchingChoice;

                        ll.AddInBetween(n, new List <Node>()
                        {
                            openCh.TailNode
                        }, subGraph);
                        ch.NextNode = n.Next.Count - 1;
                    }
                    else
                    {
                        String msg = String.Format("{0} not found in dictionary, could the name be mispelled?",
                                                   branchName);
                        throw new Exception(msg);
                    }
                }
                else
                {
                    // panic here
                    String msg = String.Format("Expected beginning of character name, branch or chioce but found {0} at {1}",
                                               stream.PeekChar(), stream.Position);
                    throw new Exception(msg);
                }

                EatWhitespace(stream);
            }

            if (!stream.Complete())
            {
                AssertChar(stream, '}');
            }

            return(ll);
        }
Exemple #4
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);
        }