示例#1
0
        public void EmptyGlobalScope()
        {
            //Resolver.DBUG = true;
            ChatRuntime rt = new ChatRuntime();
            Chat        c1 = rt.AddNewChat("c1");

            Assert.Throws <UnboundSymbol>(() =>
                                          rt.resolver.Bind("$animal", c1, null));
        }
示例#2
0
        public void SymbolBinding()
        {
            //Resolver.DBUG = true;
            ChatRuntime rt  = new ChatRuntime();
            Chat        c1  = rt.AddNewChat("c1");
            var         res = rt.resolver.Bind("Hello $$recur", c1, globals);

            Assert.That(res, Is.EqualTo("Hello A"));
        }
示例#3
0
        public void SaveMultipleAsync()
        {
            var blocker1 = new AutoResetEvent(false);
            var file1    = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + Util.EpochMs() + ".ser");
            var lines1   = new[] {
                "CHAT switch {type=a1,stage=b1,other=async1}",
                "SAY async1",
            };
            ChatRuntime rt1 = new ChatRuntime(Client.AppConfig.TAC);

            rt1.ParseText(String.Join("\n", lines1));


            //var blocker2 = new AutoResetEvent(false);
            var file2  = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + Util.EpochMs() + ".ser");
            var lines2 = new[] {
                "CHAT switch2 {type=a2,stage=2,other=async2}",
                "DO #flip",
                "SAY async2",
            };
            ChatRuntime rt2 = new ChatRuntime(Client.AppConfig.TAC);

            rt2.ParseText(String.Join("\n", lines2));


            rt1.SaveAsync(serializer, file1, (bytes) =>
            {
                Thread.Sleep(50);
                blocker1.Set();
                //Console.WriteLine("CB1: " + (bytes != null ? bytes.Length + " bytes" : "Failed"));
                Assert.That(bytes, Is.Not.Null);
                Assert.That(bytes.Length, Is.GreaterThan(0));

                // create a new runtime from the bytes
                var rtDeser1 = ChatRuntime.Create(serializer, bytes, AppConfig.TAC);

                // and verify they are the same
                CheckEquals(rt1, rtDeser1);
            });

            rt2.SaveAsync(serializer, file2, (bytes) =>
            {
                //blocker2.Set();
                //Console.WriteLine("CB2: " + (bytes != null ? bytes.Length + " bytes" : "Failed"));
                Assert.That(bytes, Is.Not.Null);
                Assert.That(bytes.Length, Is.GreaterThan(0));

                // create a new runtime from the bytes
                var rtDeser2 = ChatRuntime.Create(serializer, bytes, AppConfig.TAC);

                // and verify they are the same
                CheckEquals(rt2, rtDeser2);
            });

            blocker1.WaitOne();
            //blocker2.WaitOne();
        }
示例#4
0
        public void BoundedVarSolutions()
        {
            ChatRuntime rt;

            string[] lines;
            string   s;

            lines = new[] {
                "CHAT c1",
                "SET doop = bop",
                "SET deep = beep",
                "SAY [a=$deep][b=$doop]"
            };
            rt = new ChatRuntime();
            rt.ParseText(String.Join("\n", lines), true);

            s = rt.InvokeImmediate(globals);
            Assert.That(s, Is.EqualTo("beepbop"));

            lines = new[] {
                "CHAT c1",
                "SET doop = bop",
                "SET deep = beep",
                "SAY ($deep)-($doop)"
            };
            rt = new ChatRuntime();
            rt.ParseText(String.Join("\n", lines), true);

            s = rt.InvokeImmediate(globals);
            Assert.That(s, Is.EqualTo("beep-bop"));


            lines = new[] {
                "CHAT c1",
                "SET doop = bop",
                "SET deep = beep",
                "SAY ($deep)($doop)"
            };
            rt = new ChatRuntime();
            rt.ParseText(String.Join("\n", lines), true);

            s = rt.InvokeImmediate(globals);
            Assert.That(s, Is.EqualTo("beepbop"));


            lines = new[] {
                "CHAT c1",
                "SET doop = bop",
                "SET deep = beep",
                "SAY [a=$deep]+[b=$doop]"
            };
            rt = new ChatRuntime();
            rt.ParseText(String.Join("\n", lines), true);

            s = rt.InvokeImmediate(globals);
            Assert.That(s, Is.EqualTo("beep+bop"));
        }
示例#5
0
        public void SymbolTraversalSimple()
        {
            Resolver.DBUG = false;
            ChatRuntime rt  = new ChatRuntime();
            Chat        c1  = rt.AddNewChat("c1");
            var         res = rt.resolver.Bind("Hello $fish.name", c1, globals);

            Assert.That(res, Is.EqualTo("Hello Fred"));
        }
示例#6
0
        public void SimpleLocalScope()
        {
            ChatRuntime rt = new ChatRuntime();
            Chat        c1 = rt.AddNewChat("c1");

            c1.scope.Add("a", "b");
            var res = rt.resolver.Bind("$a", c1, globals);

            Assert.That(res, Is.EqualTo("b"));
        }
示例#7
0
        public void SubstringIssue()
        {
            ChatRuntime rt = new ChatRuntime();

            //Resolver.DBUG = true;
            rt.ParseText("SET $a = A\nSET $b = $a1\nSET $a1 = B\nSAY $a $b\n", true);
            var s = rt.InvokeImmediate(globals);

            Assert.That(s, Is.EqualTo("A B"));
        }
示例#8
0
        public void FindAllMultiHard2()
        {
            string[] lines =
            {
                "CHAT c0",
                "FIND {emotion*=(hot|cool), !day =fri}",
                "CHAT c1",
                "CHAT c2 {emotion=hot,day=fri}",
                "CHAT c3 {emotion=dry}",
                "CHAT c4 {emotion=dry,day=fri}",
            };

            string contents = String.Join("\n", lines);

            List <Chat> chats = ChatParser.ParseText(contents, NO_VALIDATORS);
            //chats.ForEach((ch) => Console.WriteLine(ch.ToTree()));
            Command finder = chats[0].commands[0]; finder.Resolve(null);

            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Find)));

            chats = new ChatRuntime(chats).DoFindAll((Find)finder, null);
            //chats.ForEach((obj) => Console.WriteLine(obj.text));

            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(1));

            Assert.That(chats[0], Is.Not.Null);
            Assert.That(chats[0].text, Is.EqualTo("c2"));

            lines = new string[] {
                "CHAT c0 {day=sunday}",
                "FIND {emotion *= (hot|cool|blah), !day*=(fri|wed)}",
                "CHAT c1 {emotion=wet, day = wed }",
                "CHAT c2 {emotion=hot,day=fri}",
                "CHAT c3 {emotion=dry}",
                "CHAT c4 {emotion=cool,day=tues}",
            };

            contents = String.Join("\n", lines);

            chats = ChatParser.ParseText(contents, NO_VALIDATORS);
            //chats.ForEach((ch) => Console.WriteLine(ch.ToTree()));
            finder = chats[0].commands[0]; finder.Resolve(null);
            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Find)));


            chats = new ChatRuntime(chats).DoFindAll((Find)finder, null);
            //chats.ForEach((obj) => Console.WriteLine(obj.text));

            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(1));

            Assert.That(chats[0], Is.Not.Null);
            Assert.That(chats[0].text, Is.EqualTo("c2"));
        }
示例#9
0
        public void FindAllOpsHard()
        {
            string[] lines =
            {
                "CHAT c0",
                "FIND {!dev>1,day=fri}",
                "CHAT c1 {dev=2,day=fri}",
                "CHAT c2 {dev=1,day=fri}",
                "CHAT c3 {}"
            };

            string contents = String.Join("\n", lines);

            List <Chat> chats = ChatParser.ParseText(contents, NO_VALIDATORS);
            //chats.ForEach((ch) => Console.WriteLine(ch.ToTree()));
            Command finder = chats[0].commands[0]; finder.Resolve(null);

            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Find)));
            ChatRuntime cr = new ChatRuntime(chats);

            chats = cr.DoFindAll((Find)finder, null);
            //chats.ForEach((obj) => Console.WriteLine(obj.text));

            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(1));

            Assert.That(chats[0], Is.Not.Null);
            Assert.That(chats[0].text, Is.EqualTo("c1"));

            lines = new[] {
                "CHAT c0",
                "FIND {dev<2,!day=fri}",
                "CHAT c1 {dev=2,day=fri}",
                "CHAT c2 {dev=1,day=fri}",
                "CHAT c3 {}"
            };

            contents = String.Join("\n", lines);

            chats = ChatParser.ParseText(contents, NO_VALIDATORS);
            //chats.ForEach((ch) => Console.WriteLine(ch.ToTree()));
            finder = chats[0].commands[0]; finder.Resolve(null);
            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Find)));
            cr = new ChatRuntime(chats);

            chats = cr.DoFindAll((Find)finder, null);
            //chats.ForEach((obj) => Console.WriteLine(obj.text));

            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(1));

            Assert.That(chats[0], Is.Not.Null);
            Assert.That(chats[0].text, Is.EqualTo("c2"));
        }
示例#10
0
        public void ValidateParensTest()
        {
            string[] lines = new[] {
                "CHAT c1",
                "SET ab = (a | b))",
                "SAY $ab"
            };
            ChatRuntime rt = new ChatRuntime();

            rt.ParseText(String.Join("\n", lines), true);
            Assert.Throws <MismatchedParens>(() => rt.InvokeImmediate(globals));
        }
示例#11
0
        public void SetChatLocalPath()
        {
            var code = "CHAT c1\nSET $fish.name=Mary\nSAY Hi $fish.name";

            code += "\nCHAT c2\nSET $c1.staleness=2";

            var rt = new ChatRuntime(null);

            rt.ParseText(code, true);

            var chat = rt["c1"];

            Assert.That(chat, Is.Not.Null);
            Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Set)));
            Assert.That(chat.commands[1].GetType(), Is.EqualTo(typeof(Say)));

            chat.Resolve(globals);

            var chat2 = rt["c2"];

            Assert.That(chat2, Is.Not.Null);
            Assert.That(chat2.commands[0].GetType(), Is.EqualTo(typeof(Set)));

            Assert.That(chat.Staleness(), Is.EqualTo(Defaults.CHAT_STALENESS));
            Assert.That(Convert.ToDouble(chat.GetMeta(Meta.STALENESS)), Is.EqualTo(Defaults.CHAT_STALENESS));

            chat2.Resolve(globals);
            Assert.That(chat.Staleness(), Is.EqualTo(2));
            Assert.That(Convert.ToDouble(chat.GetMeta(Meta.STALENESS)), Is.EqualTo(2));


            code  = "CHAT c1\nSET $fish.name=Mary\nSAY Hi $fish.name";
            code += "\nCHAT c2\nSET $c1.stalenessIncr=2";

            rt = new ChatRuntime(null);
            rt.ParseText(code, true);

            chat = rt["c1"];
            Assert.That(chat, Is.Not.Null);
            Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Set)));
            Assert.That(chat.commands[1].GetType(), Is.EqualTo(typeof(Say)));

            chat.Resolve(globals);

            chat2 = rt["c2"];
            Assert.That(chat2, Is.Not.Null);
            Assert.That(chat2.commands[0].GetType(), Is.EqualTo(typeof(Set)));

            // no need to check metadata, except for staleness
            Assert.That(chat.StalenessIncr(), Is.EqualTo(Defaults.CHAT_STALENESS_INCR));
            chat2.Resolve(globals);
            Assert.That(chat.StalenessIncr(), Is.EqualTo(2));
        }
示例#12
0
        public void SaveAndRestoreChatWithAsk()
        {
            var lines = new[] {
                "CHAT Test {type=a,stage=b}",
                "ASK Is it ok?",
                "OPT yes #next ",
                "OPT no #next",
                "CHAT next {type=a,stage=b}",
                "SAY Done",
            };
            Chat        c1, c2;
            ChatRuntime rtOut, rtIn;

            var text = String.Join("\n", lines);

            rtIn = new ChatRuntime(Client.AppConfig.TAC);
            rtIn.ParseText(text);

            // serialize the runtime to bytes
            var bytes = serializer.ToBytes(rtIn);

            // create a new runtime from the bytes
            rtOut = ChatRuntime.Create(serializer, bytes, AppConfig.TAC);

            // check they are identical
            Assert.That(rtIn, Is.EqualTo(rtOut));

            // double-check the chats themselves
            c1 = rtIn.Chats().First();
            c2 = rtOut.Chats().First();

            //Console.WriteLine(c1.ToTree()+"\n\n"+c2.ToTree());

            Assert.That(c1, Is.EqualTo(c2));
            Assert.That(c1.ToTree(), Is.EqualTo(c2.ToTree()));
            Assert.That(c1.text, Is.EqualTo(c2.text));
            for (int i = 0; i < c1.commands.Count; i++)
            {
                var cmd1 = c1.commands[i];
                Assert.That(cmd1.parent, Is.Not.Null);

                var cmd2 = c2.commands[i];
                Assert.That(cmd2.parent, Is.Not.Null);

                Assert.That(c1.commands[i], Is.EqualTo(c2.commands[i]));
            }

            // no dynamics, so output should be the same
            var res1 = rtIn.InvokeImmediate(globals);
            var res2 = rtOut.InvokeImmediate(globals);

            Assert.That(res1, Is.EqualTo(res2));
        }
示例#13
0
        public void PartialTransformIssue()
        {
            ChatRuntime rt = new ChatRuntime();

            rt.ParseText("SET $test = (a) (b)\nSAY $test.Cap()", true);
            Assert.That(rt.InvokeImmediate(globals), Is.EqualTo("A b"));

            Resolver.DBUG = false;

            rt = new ChatRuntime();
            rt.ParseText("SET $test = (a | a) (b | b)\nSAY $test.Cap()", true);
            Assert.That(rt.InvokeImmediate(globals), Is.EqualTo("A b"));
        }
示例#14
0
        public void FindAll()
        {
            string[] lines =
            {
                "CHAT c1 {day=fri}",
                "CHAT c2 {dev=2,day=fri}",
                "CHAT c3 {}"
            };
            string      contents = String.Join("\n", lines);
            List <Chat> chats    = ChatParser.ParseText(contents, NO_VALIDATORS);
            List <Chat> result   = new ChatRuntime(chats).DoFindAll(null,
                                                                    new Constraint("dev", "1"),
                                                                    new Constraint("day", "fri")
                                                                    );

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Count, Is.EqualTo(2));
            Assert.That(result[0].text, Is.EqualTo("c1"));

            Chat c;

            chats       = new List <Chat>();
            chats.Add(c = Chat.Create("c1"));
            chats.Add(c = Chat.Create("c2"));
            c.SetMeta("dev", "1");
            c.SetMeta("day", "hello");
            chats.Add(c = Chat.Create("c3"));
            ChatRuntime cr = new ChatRuntime(chats);

            chats = cr.DoFindAll(null, new Constraint("dev", "1"));
            //chats.ForEach((obj) => Console.WriteLine(obj.text));
            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(3));
            Assert.That(chats[0].text, Is.EqualTo("c2"));

            chats       = new List <Chat>();
            chats.Add(c = Chat.Create("c1"));
            c.SetMeta("dev", "2");
            chats.Add(c = Chat.Create("c2"));
            c.SetMeta("dev", "1");
            c.SetMeta("day", "hello");
            chats.Add(c = Chat.Create("c3"));

            cr    = new ChatRuntime(chats);
            chats = cr.DoFindAll(null, new Constraint("dev", "1"));
            //chats.ForEach((obj) => Console.WriteLine(obj.text));
            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(2));
            Assert.That(chats[0].text, Is.EqualTo("c2"));
            Assert.That(chats[1].text, Is.EqualTo("c3"));
        }
示例#15
0
        public void Staleness()
        {
            Chat        c;
            List <Chat> chats = new List <Chat>();

            chats.Add(c = Chat.Create("c1"));
            c.SetMeta("dev", "hello");
            c.Staleness(2);
            chats.Add(c = Chat.Create("c2"));
            c.SetMeta("dev", "2");
            c.Staleness(3);
            chats.Add(c = Chat.Create("c3"));
            c.SetMeta("dev", "3");
            c.Staleness(4);
            var res = new ChatRuntime(chats).DoFindAll
                          (null, new Constraint(Operator.LT, "staleness", "3"));

            Assert.That(res.Count, Is.EqualTo(1));
            Assert.That(res[0].text, Is.EqualTo("c1"));

            string[] lines =
            {
                "CHAT c0",
                "FIND {dev=1,day=fri,staleness<5}",
                "CHAT c1 {day=fri}",
                "CHAT c2 {dev=2,day=fri}",
                "CHAT c3 {}"
            };

            string contents = String.Join("\n", lines);

            chats = ChatParser.ParseText(contents, NO_VALIDATORS);

            Command finder = chats[0].commands[0]; finder.Resolve(null);

            Assert.That(finder.GetType(), Is.EqualTo(typeof(Find)));

            var crt = new ChatRuntime(chats);

            c = crt.FindChatByLabel("#c1");
            c.Staleness(6);
            Assert.That(c, Is.Not.Null);
            Assert.That(c.text, Is.EqualTo("c1"));

            chats = crt.DoFindAll((Find)finder, null);
            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(1));
            Assert.That(chats[0], Is.Not.Null);
            Assert.That(chats[0].text, Is.EqualTo("c3"));
        }
示例#16
0
        private void CheckEquals(ChatRuntime r1, ChatRuntime r2, bool hasDynamics = false)//, bool ignoreStaleness = false)
        {
            // check they are identical
            Assert.That(r2, Is.EqualTo(r1), "FAILED\n" + r1 + "\n" + r2);

            // if no dynamics, output should be the same
            var res1 = r2.InvokeImmediate(globals, null, true);
            var res2 = r1.InvokeImmediate(globals, null, true);

            if (!hasDynamics)
            {
                Assert.That(res1, Is.EqualTo(res2));
            }
        }
示例#17
0
        public void BasicFindAll()
        {
            Chat        c;
            List <Chat> chats = new List <Chat>();

            chats.Add(c = Chat.Create("c1"));
            chats.Add(c = Chat.Create("c2"));
            c.SetMeta("dev", "1");
            c.SetMeta("day", "hello");
            chats.Add(c = Chat.Create("c3"));
            Chat res = new ChatRuntime(chats).DoFindAll(null, new Constraint("dev", "1"))[0];

            Assert.That(res.text, Is.EqualTo("c2"));
        }
示例#18
0
        public void ATransformWithinAChoice()
        {
            var         txt = "CHAT c1\nSET a = a ($animal.Cap() | $prep.Cap())\nSAY $a";
            ChatRuntime rt  = new ChatRuntime();

            rt.ParseText(txt);
            Resolver.DBUG = false;
            rt.strictMode = false;
            for (int i = 0; i < 1; i++)
            {
                var s = rt.InvokeImmediate(globals);
                Assert.That(s.IsOneOf(new[] { "a Dog", "a Then" }));
            }
        }
示例#19
0
        private static string DoSay(ChatRuntime rt, string s)
        {
            var globs = new Dictionary <string, object> {
                { "ant", "hello" }
            };

            rt.chats = new Dictionary <string, Chat>();
            rt.ParseText(s);
            Say say = (Dialogic.Say)rt.Chats().First().commands.First();

            say.Resolve(globs);
            s = say.Text();
            //Console.WriteLine(s);
            return(s);
        }
示例#20
0
        /// <summary>
        /// Create an engine from a script file or folder script files
        /// </summary>
        /// <param name="fileOrFolder">File or folder.</param>
        public MockGameEngine(FileInfo fileOrFolder)
        {
            var config   = AppConfig.TAC;
            var saveFile = new FileInfo("./runtime.ser");

            ChatRuntime tmp = new ChatRuntime(config);

            tmp.ParseFile(fileOrFolder);

            serializer = new SerializerMessagePack();
            tmp.Save(serializer, saveFile);

            dialogic = ChatRuntime.Create(serializer, saveFile, config);
            dialogic.Run();
        }
示例#21
0
        public void SerializationPerformance()
        {
            if (!RUN_PROFILING_TESTS)
            {
                return;
            }

            ChatRuntime.VERIFY_UNIQUE_CHAT_LABELS = false;

            ChatRuntime rtOut, rtIn;

            byte[] bytes      = null;
            int    iterations = 10;

            var testfile = AppDomain.CurrentDomain.BaseDirectory;

            testfile += "../../../../dialogic/data/allchats.gs";

            rtIn = new ChatRuntime(Client.AppConfig.TAC);

            var watch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < iterations; i++)
            {
                rtIn.ParseFile(new FileInfo(testfile));
            }
            var numChats = rtIn.Chats().Count;

            watch.Stop(); Console.WriteLine("Parsed " + numChats
                                            + " chats in " + watch.ElapsedMilliseconds / 1000.0 + "s");

            for (int i = 0; i < iterations; i++)
            {
                watch = System.Diagnostics.Stopwatch.StartNew();
                bytes = serializer.ToBytes(rtIn);
                watch.Stop();
                Console.WriteLine("Serialize #" + i + ": "
                                  + watch.ElapsedMilliseconds / 1000.0 + "s");
            }

            for (int i = 0; i < iterations; i++)
            {
                watch = System.Diagnostics.Stopwatch.StartNew();
                rtOut = ChatRuntime.Create(serializer, bytes, AppConfig.TAC);
                watch.Stop(); Console.WriteLine("Deserialize #" + i + ": "
                                                + watch.ElapsedMilliseconds / 1000.0 + "s");
            }
        }
示例#22
0
        public void ImmediateSimpleLoop()
        {
            var lines = new[] {
                "CHAT Test {type=a,stage=b}",
                "SAY Find",
                "GO #Test"
            };

            ChatRuntime rt = new ChatRuntime(Client.AppConfig.TAC);

            rt.ParseText(String.Join("\n", lines));

            var s = rt.InvokeImmediate(null);

            // Note: exits before GO to avoid infinite loop
            Assert.That(s, Is.EqualTo("Find\nFind\n\nGO #Test looped"));
        }
示例#23
0
        public void ContinueAskTest()
        {
            string[] lines =
            {
                "CHAT c",
                "ASK Hello?",
                "OPT Yes",
                "OPT No",
                "SAY Ok"
            };
            ChatRuntime rt = new ChatRuntime();

            rt.ParseText(String.Join("\n", lines));
            var s = rt.InvokeImmediate(globals);

            Assert.That(s, Is.EqualTo("Hello?\n[No]\nOk").Or.EqualTo("Hello?\n[Yes]\nOk"));
        }
示例#24
0
        public static void Profiling(string[] args)
        {
            ChatRuntime.VERIFY_UNIQUE_CHAT_LABELS = false;

            AppConfig   config     = AppConfig.TAC;
            ISerializer serializer = new SerializerMessagePack();

            ChatRuntime rtOut, rtIn;

            byte[] bytes      = null;
            int    iterations = 10;

            var testfile = AppDomain.CurrentDomain.BaseDirectory;

            testfile += "../../../../dialogic/data/allchats.gs";

            rtIn = new ChatRuntime(config);

            var watch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < iterations; i++)
            {
                rtIn.ParseFile(new FileInfo(testfile));
            }
            var numChats = rtIn.Chats().Count;

            watch.Stop(); Console.WriteLine("Parsed " + numChats
                                            + " chats in " + watch.ElapsedMilliseconds / 1000.0 + "s");

            for (int i = 0; i < iterations; i++)
            {
                watch = System.Diagnostics.Stopwatch.StartNew();
                bytes = serializer.ToBytes(rtIn);
                watch.Stop();
                Console.WriteLine("Serialize #" + i + ": "
                                  + watch.ElapsedMilliseconds / 1000.0 + "s");
            }

            for (int i = 0; i < iterations; i++)
            {
                watch = System.Diagnostics.Stopwatch.StartNew();
                rtOut = ChatRuntime.Create(serializer, bytes, config);
                watch.Stop(); Console.WriteLine("Deserialize #" + i + ": "
                                                + watch.ElapsedMilliseconds / 1000.0 + "s");
            }
        }
示例#25
0
        private static IDictionary <string, string> ParsePostData(HttpListenerRequest request)
        {
            var result = new Dictionary <string, string>();

            try
            {
                if (request.HasEntityBody)
                {
                    Stream       body     = request.InputStream;
                    Encoding     encoding = request.ContentEncoding;
                    StreamReader reader   = new System.IO.StreamReader(body, encoding);

                    if (request.ContentType == "application/x-www-form-urlencoded")
                    {
                        string   s     = reader.ReadToEnd();
                        string[] pairs = s.Split('&');

                        foreach (var p in pairs)
                        {
                            var pair = p.Split('=');
                            if (pair.Length == 2)
                            {
                                //Console.WriteLine(pair[0] + ": " + pair[1]);
                                result.Add(WebUtility.UrlDecode(pair[0]),
                                           WebUtility.UrlDecode(pair[1]));
                            }
                            else
                            {
                                ChatRuntime.Warn("BAD KV-PAIR: " + p);
                                //throw new Exception("BAD-PAIR: " + p);
                            }
                        }
                    }

                    body.Close();
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                ChatRuntime.Warn(ex);
            }

            return(result);
        }
示例#26
0
        public void FindAllHard()
        {
            Chat        c;
            List <Chat> chats = new List <Chat>();

            chats.Add(c = Chat.Create("c1"));
            chats.Add(c = Chat.Create("c2"));
            c.SetMeta("dev", "1");
            c.SetMeta("day", "hello");
            chats.Add(c = Chat.Create("c3"));
            ChatRuntime cr = new ChatRuntime(chats);

            chats = cr.DoFindAll(null, new Constraint("dev", "1", ConstraintType.Hard));
            //chats.ForEach((obj) => Console.WriteLine(obj.text));
            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(1));
            Assert.That(chats[0].text, Is.EqualTo("c2"));
        }
示例#27
0
        public void ImmediateAskTest()
        {
            string[] lines =
            {
                "CHAT c",
                "SAY hello $animal.",
                "ASK are you a $animal?",
                "OPT Yes #c2",
                "OPT No #c2",
                "CHAT c1 {a=b}",
                "SAY nope.",
                "CHAT c2 {a=b}",
                "SAY goodbye."
            };
            ChatRuntime rt = new ChatRuntime();

            rt.ParseText(String.Join("\n", lines));

            var s = rt.InvokeImmediate(globals);

            //Assert.That(s, Is.EqualTo("hello dog.\nare you a dog?\ngoodbye."));
            //Assert.That(s, Is.EqualTo("hello dog.\nare you a dog?\ngoodbye."));
            Assert.That(s, Is.EqualTo("hello dog.\nare you a dog?\n[No]\ngoodbye.")
                        .Or.EqualTo("hello dog.\nare you a dog?\n[Yes]\ngoodbye."));


            s = rt.InvokeImmediate(globals, "c");
            Assert.That(s, Is.EqualTo("hello dog.\nare you a dog?\n[No]\ngoodbye.")
                        .Or.EqualTo("hello dog.\nare you a dog?\n[Yes]\ngoodbye."));
            //Assert.That(s, Is.EqualTo("hello dog.\nare you a dog?\ngoodbye."));

            s = rt.InvokeImmediate(globals, "c2");
            Assert.That(s, Is.EqualTo("goodbye."));

            Assert.Throws <UnboundSymbol>(() => rt.InvokeImmediate(null));


            ChatRuntime.SILENT = true;
            rt.strictMode      = false;
            s = rt.InvokeImmediate(null);
            ChatRuntime.SILENT = false;
            Assert.That(s, Is.EqualTo("hello $animal.\nare you a $animal?\n[No]\ngoodbye.")
                        .Or.EqualTo("hello $animal.\nare you a $animal?\n[Yes]\ngoodbye."));
        }
示例#28
0
        public void SaveAsyncAndRepeat()
        {
            var blocker  = new AutoResetEvent(false);
            var blocker2 = new AutoResetEvent(false);

            var file  = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + Util.EpochMs() + ".ser");
            var lines = new[] {
                "CHAT switch {type=a,stage=b,other=c}",
                "SAY async",
            };

            ChatRuntime rt = new ChatRuntime(Client.AppConfig.TAC);

            rt.ParseText(String.Join("\n", lines));
            rt.SaveAsync(serializer, file, (bytes) =>
            {
                blocker.Set();
                //Console.WriteLine(file + ": " + (bytes != null ? bytes.Length + " bytes" : "Failed"));
                Assert.That(bytes, Is.Not.Null);
                Assert.That(bytes.Length, Is.GreaterThan(0));

                // create a new runtime from the bytes
                var rt2 = ChatRuntime.Create(serializer, bytes, AppConfig.TAC);

                // and verify they are the same
                CheckEquals(rt, rt2);

                file = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + Util.EpochMs() + ".ser");
                rt2.SaveAsync(serializer, file, (bytes2) =>
                {
                    blocker2.Set();
                    //Console.WriteLine(file + ": " + (bytes != null ? bytes.Length + " bytes" : "Failed"));
                    Assert.That(bytes, Is.Not.Null);
                    Assert.That(bytes.Length, Is.GreaterThan(0));

                    // create a new runtime from the bytes
                    var rt3 = ChatRuntime.Create(serializer, bytes, AppConfig.TAC);
                    CheckEquals(rt, rt3);
                });
            });

            blocker2.WaitOne();
            blocker.WaitOne();
        }
示例#29
0
        public void SubstringSymbolResolve()
        {
            ChatRuntime rt = new ChatRuntime();

            rt.strictMode = false;

            ChatRuntime.SILENT = true;
            Assert.That(DoSay(rt, "SAY $ant $antelope"), Is.EqualTo("hello $antelope"));
            Assert.That(DoSay(rt, "SAY $ant$antelope"), Is.EqualTo("hello$antelope"));
            Assert.That(DoSay(rt, "SAY $ant. $antelope"), Is.EqualTo("hello. $antelope"));
            Assert.That(DoSay(rt, "SAY $ant! $antelope"), Is.EqualTo("hello! $antelope"));
            Assert.That(DoSay(rt, "SAY $ant? $antelope"), Is.EqualTo("hello? $antelope"));
            Assert.That(DoSay(rt, "SAY $ant, $antelope"), Is.EqualTo("hello, $antelope"));
            Assert.That(DoSay(rt, "SAY $ant; $antelope"), Is.EqualTo("hello; $antelope"));
            Assert.That(DoSay(rt, "SAY $ant: $antelope"), Is.EqualTo("hello: $antelope"));
            Assert.That(DoSay(rt, "SAY $ant $ant-"), Is.EqualTo("hello $ant-"));
            Assert.That(DoSay(rt, "SAY $ant $ant_"), Is.EqualTo("hello $ant_"));
            ChatRuntime.SILENT = false;
        }
示例#30
0
        public void PreloadingTest()
        {
            string[] lines = new[] {
                "CHAT c1",
                "SET ab = hello",
                "SAY $ab $de",

                "CHAT c2 {preload=true}",
                "SET $de = preload",
            };
            ChatRuntime rt = new ChatRuntime();

            rt.ParseText(String.Join("\n", lines), true);
            rt.Preload(globals);

            var s = rt.InvokeImmediate(globals);

            Assert.That(s, Is.EqualTo("hello preload"));
        }