Example #1
0
        public void SetRemoteChatNonProperty()
        {
            var code = "CHAT c1\nSET $fish.name=Mary\nSAY Hi $fish.name";

            code += "\nCHAT c2\nSET $c1.happiness=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(0));

            // throw b/c we only allow setting of persistent properties
            // (staleness, etc) on remote chats
            Assert.Throws <BindException>(() => chat2.Resolve(globals));
        }
Example #2
0
        public void SetBadRemoteChatProperty()
        {
            var code = "CHAT c1\nSET $fish.name=Mary\nSAY Hi $fish.name";

            code += "\nCHAT c2\nSET $WRONG.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(0));

            // throw b/c $WRONG.staleness doesn't exist in any scope
            Assert.Throws <BindException>(() => chat2.Resolve(globals));
        }
Example #3
0
        public void GroupsWithMissingSymbol()
        {
            ChatRuntime rt;
            string      txt;

            txt = "a | b | c";
            rt  = new ChatRuntime();
            rt.ParseText(txt);

            Resolver.DBUG = false;

            for (int i = 0; i < 5; i++)
            {
                var s = rt.InvokeImmediate(globals);
                //Console.WriteLine("#" + i + ": " + s);
                Assert.That(s.IsOneOf(new[] { "a", "b", "c" }));
            }

            txt = "CHAT c1\nSET a = $object | $object.Call() | honk\nSAY $a";
            rt  = new ChatRuntime();
            rt.ParseText(txt);
            //Resolver.DBUG = true;
            rt.strictMode      = false;
            ChatRuntime.SILENT = true;
            for (int i = 0; i < 5; i++)
            {
                var s = rt.InvokeImmediate(globals);
                //Console.WriteLine("#"+i+": "+s);
                Assert.That(s.IsOneOf(new[] { "$object", "$object.Call()", "honk" }));
            }
            ChatRuntime.SILENT = false;
        }
Example #4
0
        public void SetRemoteChatProperty()
        {
            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(0));

            chat2.Resolve(globals);

            Assert.That(chat.Staleness(), Is.EqualTo(2));
        }
Example #5
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();
 }
Example #6
0
        public void MultiTransformResolution()
        {
            var str = "(well|well).cap() don't you look (anger).emoadj().";
            var rt  = new ChatRuntime(Client.AppConfig.TAC);

            rt.ParseText(str);
            //rt.strictMode = false;
            var s = rt.InvokeImmediate(globals);

            //Console.WriteLine(s);
            Assert.That(s.StartsWith("Well don't you look ", Util.IC), Is.True);
            Assert.That(s.Contains("anger"), Is.False);
            Assert.That(s.Contains("emoadj"), Is.False);

            str = "The (dog|).Cap() ran.";
            rt  = new ChatRuntime(Client.AppConfig.TAC);
            rt.ParseText(str);

            //rt.strictMode = false;
            for (int i = 0; i < 5; i++)
            {
                s = rt.InvokeImmediate(globals);
                //Console.WriteLine(i + ") " + s);
                Assert.That(s.IsOneOf("The Dog ran.", "The ran."));
            }
        }
Example #7
0
        public void SearchFailWithConstraint()
        {
            var lines = new[] {
                "CHAT Test {type=a,stage=b}",
                "SAY Find",
                "FIND {type=a,x=b}",
                "CHAT next {type=a,stage=b}",
                "SAY Done",
            };

            ChatRuntime rt = new ChatRuntime(AppConfig.TAC);

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

            var s = rt.InvokeImmediate(null);

            Assert.That(s, Is.EqualTo("Find\nDone"));

            //FuzzySearch.DBUG = true;

            lines = new[] {
                "CHAT Test {type=a,stage=b}",
                "SAY Find",
                "FIND {type=a,staleness<5}",
                "CHAT next {type=a,stage=b,staleness=5}",
                "SAY Done",
            };

            rt = new ChatRuntime(AppConfig.TAC);
            rt.ParseText(String.Join("\n", lines));

            s = rt.InvokeImmediate(null);
            Assert.That(s, Is.EqualTo("Find\nDone"));
        }
Example #8
0
        public void ImmediateFindTest()
        {
            string[] lines =
            {
                "CHAT c",
                "SAY hello $animal.",
                "ASK are you a $animal?",
                "FIND {a=b}",
                "CHAT c1 {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."));

            s = rt.InvokeImmediate(globals, "c");
            Assert.That(s, Is.EqualTo("hello dog.\nare you a dog?\ngoodbye."));

            s = rt.InvokeImmediate(globals, "c1");
            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?\ngoodbye."));
        }
Example #9
0
        public void SearchWithHardConstraint()
        {
            string[] lines =
            {
                "CHAT CORE_Shake {type=shake, stage=CORE}",
                "SAY Core shake!",

                "CHAT CORE_Tap {type=tap, stage=CORE}",
                "SAY Core tap!",

                "CHAT CORE_Stale_Fast {type=critic, stage=CORE}",
                "SAY Core critic!",

                "CHAT NV_Shake {type=shake, stage=NV}",
                "SAY NV shake!",

                "CHAT NV_Tap {type=tap, stage=NV}",
                "SAY NV tap!",

                "CHAT NV_Stale_Fast {type=critic, stage=NV}",
                "SAY NV critic!",
            };
            string      contents = String.Join("\n", lines);
            ChatRuntime rt       = new ChatRuntime(AppConfig.TAC);

            rt.ParseText(contents);
            var finder = new Find().Init("{!!type=tap,!stage=CORE}");

            var chat = rt.DoFind((Dialogic.Find)finder);

            Assert.That(chat.text, Is.EqualTo("CORE_Tap"));
        }
Example #10
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); // ???
        }
Example #11
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());
        }
Example #12
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"));
        }
Example #13
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"));
        }
Example #14
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));
        }
Example #15
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));
        }
Example #16
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));
        }
Example #17
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"));
        }
Example #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" }));
            }
        }
Example #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);
        }
Example #20
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"));
        }
Example #21
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"));
        }
Example #22
0
        public void ImmediateModeResume()
        {
            string[] lines =
            {
                "CHAT c",
                "SAY hello",
                "GO #c1",
                "SAY ok",
                "CHAT c1",
                "SAY goodbye"
            };
            ChatRuntime rt = new ChatRuntime();

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

            var s = rt.InvokeImmediate(globals);

            Assert.That(s, Is.EqualTo("hello\ngoodbye\nok"));
        }
Example #23
0
        public void ImmediateTripleLoop()
        {
            string[] lines =
            {
                "CHAT c1",
                "SAY C1",
                "GO #c2",
                "CHAT c2",
                "SAY C2",
                "GO #c1",
            };

            ChatRuntime rt = new ChatRuntime();

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

            var s = rt.InvokeImmediate(globals);

            Assert.That(s, Is.EqualTo("C1\nC2\nC1\n\nGO #c2 looped"));
        }
Example #24
0
        public void PreloadingBindingFunc()
        {
            string[] lines = new[] {
                "CHAT c1",
                "SAY $d $e",

                "CHAT c2 {preload=true}",
                "SET $d = hello",
                "SET $e = $emotion.Cap()",
            };
            ChatRuntime rt = new ChatRuntime();

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

            globals.Add("emotion", "darkness");

            var s = rt.InvokeImmediate(globals);

            Assert.That(s, Is.EqualTo("hello Darkness"));
        }
Example #25
0
        public void ImmediateWithDynamicGo()
        {
            string[] lines =
            {
                "CHAT c1",
                "GO #(line1|line2)",
                "CHAT line1",
                "Done",
                "CHAT line2",
                "Done"
            };

            ChatRuntime rt = new ChatRuntime();

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

            var s = rt.InvokeImmediate(globals);

            //Console.WriteLine(s);
            Assert.That(s, Is.EqualTo("Done"));
        }
Example #26
0
        public void ImmediateTransformFails()
        {
            string[] lines =
            {
                "CHAT c",
                "SAY hello (a | a).noFun(),",
                "SAY ok.",
            };

            ChatRuntime rt = new ChatRuntime();

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

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

            //ChatRuntime.SILENT = true;
            rt.strictMode = false;
            var s = rt.InvokeImmediate(globals);

            //ChatRuntime.SILENT = false;
            Assert.That(s, Is.EqualTo("hello a.noFun(),\nok."));
        }
Example #27
0
        public void ImmediateGoFails()
        {
            string[] lines =
            {
                "CHAT c",
                "SAY hello $animal.",
                "SAY ok.",
                "GO c2",
                "CHAT c1",
                "SAY goodbye."
            };

            ChatRuntime rt = new ChatRuntime();

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

            ChatRuntime.SILENT = true;
            rt.strictMode      = false;
            var s = rt.InvokeImmediate(globals);

            ChatRuntime.SILENT = false;
            Assert.That(s, Is.EqualTo("hello dog.\nok.\n\nGO #c2 failed"));
        }
Example #28
0
        public void ParensCheck()
        {
            string[]    lines;
            ChatRuntime rt;
            string      s;

            Resolver.DBUG = false;

            lines = new[] {
                "CHAT c",
                "SET $aa = Word",
                "SET $bb = (A | B) | (A | B)",
                "SAY $aa $bb"
            };
            rt = new ChatRuntime();
            rt.ParseText(String.Join("\n", lines), true);

            for (int i = 0; i < 10; i++)
            {
                s = rt.InvokeImmediate(globals);
                //Console.WriteLine(i+") "+s);
                Assert.That(s.IsOneOf("Word A", "Word B"));
            }
        }
Example #29
0
        public void AppendNewChats()
        {
            var lines = new[] {
                "CHAT Test {type=a,stage=b}",
                "SAY Find",
                "FIND {type=a,stage=b,other=c}",
                "CHAT next {type=a,stage=b}",
                "SAY Done",
            };

            ChatRuntime rt;

            rt = new ChatRuntime(Client.AppConfig.TAC);
            rt.ParseText(String.Join("\n", lines));

            var s = rt.InvokeImmediate(null);

            Assert.That(s, Is.EqualTo("Find\nDone"));


            // Add more chats via Update, with higher search score
            var lines2 = new[] {
                "CHAT switch {type=a,stage=b,other=c,statelness=}",
                "SAY Added",
            };

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

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

            // append the 2nd runtime to the first
            rt.UpdateFrom(serializer, rt2);

            s = rt.InvokeImmediate(null);
            Assert.That(s, Is.EqualTo("Find\nAdded"));
        }
Example #30
0
        public void DynamicRules()
        {
            string[]    lines;
            ChatRuntime rt;
            string      s;

            Resolver.DBUG = false;

            lines = new[] {
                "CHAT c",
                "SET $emo = anger",
                "SAY ($emo)_rule"
            };
            rt = new ChatRuntime();
            rt.ParseText(String.Join("\n", lines), true);
            s = rt.InvokeImmediate(globals);
            Assert.That(s, Is.EqualTo("anger_rule"));

            lines = new[] {
                "CHAT c",
                "SET $emo = a|a",
                "SAY $$emo"
            };
            rt            = new ChatRuntime();
            rt.strictMode = false;
            rt.ParseText(String.Join("\n", lines), true);
            s = rt.InvokeImmediate(globals);
            Assert.That(s, Is.EqualTo("A"));

            lines = new[] {
                "CHAT c",
                "SET $emo = a|a",
                "SAY $($emo)."
            };
            rt            = new ChatRuntime();
            rt.strictMode = false;
            rt.ParseText(String.Join("\n", lines), true);
            s = rt.InvokeImmediate(globals);
            Assert.That(s, Is.EqualTo("A."));

            lines = new[] {
                "CHAT c",
                "SET $emo = anger",
                "SAY $($emo)_rule"
            };

            rt            = new ChatRuntime();
            rt.strictMode = false;
            rt.ParseText(String.Join("\n", lines), true);
            ChatRuntime.SILENT = true;
            s = rt.InvokeImmediate(globals);
            ChatRuntime.SILENT = false;
            Assert.That(s, Is.EqualTo("$anger_rule"));

            lines = new[] {
                "CHAT c",
                "SET $emo = anger",
                "SET start = $($emo)_rule",
                "SET anger_rule = I am angry",
                "SET happy_rule = I am happy",
                "SAY $start"
            };
            rt = new ChatRuntime();
            rt.ParseText(String.Join("\n", lines), true);
            s = rt.InvokeImmediate(globals);
            Assert.That(s, Is.EqualTo("I am angry"));
        }