Exemplo n.º 1
0
        public void Add_GraphInBetweenNodes_NoException()
        {
            BrigitGraph bg = new BrigitGraph();

            bg.AddNode(new Node()
            {
                Data = 1
            });
            bg.AddNode(new Node()
            {
                Data = 3
            });

            BrigitGraph bg2 = new BrigitGraph();

            bg2.AddNode(new Node()
            {
                Data = 6
            });
            bg2.AddNode(new Node()
            {
                Data = 7
            });

            bg.AddInBetween(bg.Head, bg2);
        }
Exemplo n.º 2
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);
        }