Exemple #1
0
        /// <summary>
        /// Create a new snapshot from the specified runtime
        /// </summary>
        public static Snapshot Create(ChatRuntime rt)
        {
            Snapshot snap = new Snapshot();

            snap.firstChat = rt.firstChat;
            snap.chatData  = new List <ChatData>(rt.Chats().Count);
            rt.Chats().ForEach(c => snap.chatData.Add(ChatData.Create(c)));
            return(snap);
        }
Exemple #2
0
        public void FindAll2()
        {
            string[] lines =
            {
                "CHAT c0",
                "FIND {dev=1,day=fri}",
                "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);
            //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);

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

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

            lines = new[] {
                "CHAT c0",
                "FIND {!dev=1,day=fri}",
                "CHAT c1 {day=fri}",
                "CHAT c2 {dev=2,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);
            var mdev = finder.GetMeta("dev");

            Assert.That(mdev is Constraint, Is.True);
            Constraint cons = (Constraint)mdev;

            Assert.That(cons.type, Is.EqualTo(ConstraintType.Hard));
            Assert.That(cons.IsStrict(), Is.True);
            Assert.That(finder.GetType(), Is.EqualTo(typeof(Find)));

            var rt = new ChatRuntime(chats);

            rt.Chats().ForEach(c => c.Resolve(null));
            chats = rt.DoFindAll((Find)finder, null);

            //chats.ForEach((obj) => Console.WriteLine(obj.text));

            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(0));
        }
Exemple #3
0
        public void SaveRestoreChats()
        {
            ChatRuntime rtOut, rtIn;

            var testfile = AppDomain.CurrentDomain.BaseDirectory;

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

            rtIn = new ChatRuntime(Client.AppConfig.TAC);
            rtIn.ParseFile(new FileInfo(testfile));

            var bytes = serializer.ToBytes(rtIn);

            rtOut = ChatRuntime.Create(serializer, bytes, AppConfig.TAC);

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

            var inCmds  = rtIn.Chats();
            var outCmds = rtOut.Chats();

            Assert.That(rtOut.ToString(), Is.EqualTo(rtIn.ToString()));

            Assert.That(inCmds.Count, Is.EqualTo(outCmds.Count));
            for (int i = 0; i < inCmds.Count; i++)
            {
                var chat1 = inCmds.ElementAt(i);
                var chat2 = outCmds.ElementAt(i);
                Assert.That(chat1.text, Is.EqualTo(chat2.text));
                Assert.That(chat1.commands.Count, Is.EqualTo(chat2.commands.Count));
                Assert.That(chat1.ToTree(), Is.EqualTo(chat2.ToTree()));
            }
        }
Exemple #4
0
 internal static List<Chat> ParseText(string s, 
     IAppConfig config = null, bool disableValidators = false)
 {
     ChatRuntime rt = new ChatRuntime(config);
     rt.ParseText(s, disableValidators);
     return rt.Chats();
 }
Exemple #5
0
        internal static List <Chat> ParseText(string s, IAppConfig config = null)
        {
            //ChatRuntime rt = new ChatRuntime(Tendar.TendarConfig.Actors); // tmp: testing
            ChatRuntime rt = new ChatRuntime(config);

            rt.ParseText(s, false);
            return(rt.Chats());
        }
Exemple #6
0
        public void RuntimeIndexTest()
        {
            var rt = new ChatRuntime();

            rt.ParseText("CHAT c1\nSAY OK");
            Assert.That(rt.Chats()[0], Is.Not.Null);
            Assert.That(rt["c1"], Is.Not.Null); // ???
        }
Exemple #7
0
        public void SaveRestoreChatWithAsk()
        {
            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));
        }
Exemple #8
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);
        }
Exemple #9
0
        public void SerializationPerformance()
        {
            if (!RUN_PROFILING_TESTS)
            {
                return;
            }

            ChatRuntime.DISABLE_UNIQUE_CHAT_LABELS = true;

            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");
            }
        }
Exemple #10
0
        private IUpdateEvent ChatUpdateHandler(ref EventArgs ea, IDictionary <string, object> globals)
        {
            IChatUpdate evt    = (IChatUpdate)ea;
            var         findBy = evt.FindByCriteria();
            var         action = evt.GetAction();

            ea = null;

            if (String.IsNullOrEmpty(findBy))        // apply action to all chats
            {
                runtime.Chats().ForEach(action);
            }
            else if (findBy.StartsWith(Util.LABEL_IDENT, Util.IC)) // to one chat
            {
                action.Invoke(runtime.FindChatByLabel(findBy));
            }
            else                                  // to all those matching findBy
            {
                UpdateFinder(ref updateDelegate, findBy);
                runtime.FindAllAsync(updateDelegate, action, globals);
            }

            return(null);
        }
Exemple #11
0
        public void TransformIssues()
        {
            ChatRuntime rt;
            string      txt;
            Say         say;
            Chat        chat;

            Resolver.DBUG = false;

            txt = "SET $x = (a|a|a|a)\nSET test = (ok $x.Cap() | ok $x.Cap())\nSAY ($test).Cap()";
            rt  = new ChatRuntime();
            rt.ParseText(txt);
            chat = rt.Chats().First();
            say  = (Say)chat.commands[2];
            chat.Resolve(globals);
            //Console.WriteLine(res);
            Assert.That(say.Text(), Is.EqualTo("Ok A"));

            txt = "SET $x = (a|a|a|a)\nSET test = (ok $x.Cap() | ok $x.Cap())\nSAY $test.Cap()";
            rt  = new ChatRuntime();
            rt.ParseText(txt);
            chat = rt.Chats().First();
            say  = (Say)chat.commands[2];
            chat.Resolve(globals);
            //Console.WriteLine(res);
            Assert.That(say.Text(), Is.EqualTo("Ok A"));

            txt = "SAY (ok (a).Cap()).Cap()";
            rt  = new ChatRuntime();
            rt.ParseText(txt);
            chat = rt.Chats().First();
            say  = (Say)chat.commands[0];
            chat.Resolve(globals);
            //Console.WriteLine(res);
            Assert.That(say.Text(), Is.EqualTo("Ok A"));


            /////////////////////////////////////////////////////////////////

            txt = "SET $thing1 = (cat | cat)\nSAY A $thing1, many $thing1.Pluralize()";
            rt  = new ChatRuntime();
            rt.ParseText(txt);
            chat = rt.Chats().First();
            say  = (Say)chat.commands[1];
            chat.Resolve(globals);
            //Console.WriteLine(res);
            Assert.That(say.Text(), Is.EqualTo("A cat, many cats"));

            txt = "SET $thing1 = (cat | cat | cat)\nSAY A $thing1 $thing1";
            rt  = new ChatRuntime();
            rt.ParseText(txt);
            chat = rt.Chats().First();
            say  = (Say)chat.commands[1];
            chat.Resolve(globals);
            Assert.That(say.Text(), Is.EqualTo("A cat cat"));


            txt = "SET $thing1 = (cat | crow | cow)\nSAY A [save=$thing1], many $save.Pluralize()";
            rt  = new ChatRuntime();
            rt.ParseText(txt);
            chat = rt.Chats().First();
            say  = (Say)chat.commands[1];
            chat.Resolve(globals);
            Assert.That(say.Text(), Is.EqualTo("A cat, many cats").Or.EqualTo("A crow, many crows").Or.EqualTo("A cow, many cows"));
        }