Inheritance: AbstractStore
 public void TestTemplateOneOf()
 {
     Random random = new Random();
     var document = new SimpleDocument("The letter is {OneOf(\"a\", \"b\", \"c\", \"d\", null)}.");
     var store = new BuiltinStore();
     store["OneOf"] = new NativeFunction((values) =>
     {
         return values[random.Next(values.Count)];
     });
     store["system"] = "Alrai";
     List<string> results = new List<string>();
     for (int i = 0; i < 1000; i++)
     {
         results.Add(document.Render(store));
     }
     Assert.IsTrue(results.Contains(@"The letter is a."));
     results.RemoveAll(result => result == @"The letter is a.");
     Assert.IsTrue(results.Contains(@"The letter is b."));
     results.RemoveAll(result => result == @"The letter is b.");
     Assert.IsTrue(results.Contains(@"The letter is c."));
     results.RemoveAll(result => result == @"The letter is c.");
     Assert.IsTrue(results.Contains(@"The letter is d."));
     results.RemoveAll(result => result == @"The letter is d.");
     Assert.IsTrue(results.Contains(@"The letter is ."));
     results.RemoveAll(result => result == @"The letter is .");
     Assert.IsTrue(results.Count == 0);
 }
Beispiel #2
0
        private void buttonEvaluate_Click(object sender, EventArgs e)
        {
            SimpleDocument	document;
            ISetting		setting;
            IStore			store;

            setting = this.SettingCreate ();

            try
            {
                document = new SimpleDocument (this.textBoxInput.Text, setting);
                store = new BuiltinStore ();

                foreach (TreeNode root in this.treeViewValue.Nodes)
                {
                    foreach (KeyValuePair<Value, Value> pair in this.ValuesBuild (root.Nodes))
                        store.Set (pair.Key, pair.Value, StoreMode.Global);
                }

                this.DisplayText (document.Render (store));
            }
            catch (ParseException exception)
            {
                this.DisplayError (exception);
            }
        }
 public void TestTemplateSimple()
 {
     var document = new SimpleDocument(@"Hello {name}!");
     var store = new BuiltinStore();
     store["name"] = "world";
     var result = document.Render(store);
     Assert.AreEqual("Hello world!", result);
 }
 public void TestTemplateFunctional()
 {
     var document = new SimpleDocument(@"You are entering the {System(system)} system.");
     var store = new BuiltinStore();
     store["System"] = new NativeFunction((values) =>
     {
         return Translations.StarSystem(values[0].AsString);
     }, 1);
     store["system"] = "Alrai";
     var result = document.Render(store);
     Assert.AreEqual("You are entering the <phoneme alphabet=\"ipa\" ph=\"ˈalraɪ\">Alrai</phoneme> system.", result);
 }
 public void TestTemplateConditional()
 {
     var document = new SimpleDocument("{if value = 1:foo|else:{if value = 2:bar|else:baz}}");
     var store = new BuiltinStore();
     store["value"] = 1;
     var result = document.Render(store);
     Assert.AreEqual("foo", result);
     store["value"] = 2;
     result = document.Render(store);
     Assert.AreEqual("bar", result);
     store["value"] = 3;
     result = document.Render(store);
     Assert.AreEqual("baz", result);
 }
 /// <summary>
 /// Resolve a script with an existing store
 /// </summary>
 public string resolveScript(string script, BuiltinStore store)
 {
     try
     {
         var document = new SimpleDocument(script, setting);
         var result = document.Render(store);
         Logging.Debug("Turned script " + script + " in to speech " + result);
         return result.Trim() == "" ? null : result.Trim();
     }
     catch (Exception e)
     {
         Logging.Warn("Failed to resolve script: " + e.ToString());
         return "There is a problem with the script: " + e.Message.Replace("'", "");
     }
 }
        public string resolve(string name, BuiltinStore store)
        {
            Logging.Debug("Resolving script " + name);
            Script script;
            scripts.TryGetValue(name, out script);
            if (script == null || script.Value == null)
            {
                Logging.Debug("No script");
                return null;
            }
            Logging.Debug("Found script");
            if (script.Enabled == false)
            {
                Logging.Debug("Script disabled");
                return null;
            }

            return resolveScript(script.Value, store);
        }
Beispiel #8
0
        private static void AssertResult(string expression, bool expected)
        {
            IDocument	document = new SimpleDocument ("{" + (expected ? "" : "!") + "(" + expression + ")}");
            IStore		store = new BuiltinStore ();

            Assert.AreEqual ("true", document.Render (store));
        }
        /// <summary>
        /// Build a store from a list of variables
        /// </summary>
        private BuiltinStore buildStore(Dictionary<string, Cottle.Value> vars)
        {
            BuiltinStore store = new BuiltinStore();

            // Function to call another script
            store["F"] = new NativeFunction((values) =>
            {
                return new ScriptResolver(scripts).resolve(values[0].AsString, store);
            }, 1);

            // Translation functions
            store["P"] = new NativeFunction((values) =>
            {
                string val = values[0].AsString;
                string translation = val;
                if (translation == val)
                {
                    translation = Translations.Faction(val);
                }
                if (translation == val)
                {
                    translation = Translations.Power(val);
                }
                if (translation == val)
                {
                    translation = Translations.StarSystem(val);
                }
                return translation;
            }, 1);

            // Helper functions
            store["OneOf"] = new NativeFunction((values) =>
            {
                return new ScriptResolver(scripts).resolveScript(values[random.Next(values.Count)].AsString, store);
            });

            store["Occasionally"] = new NativeFunction((values) =>
            {
                if (random.Next((int)values[0].AsNumber) == 0)
                {
                    return new ScriptResolver(scripts).resolveScript(values[1].AsString, store);
                }
                else
                {
                    return "";
                }
            }, 2);

            store["Humanise"] = new NativeFunction((values) =>
            {
                return Translations.Humanize(values[0].AsNumber);
            }, 1);

            store["Pause"] = new NativeFunction((values) =>
            {
                return @"<break time =""" + values[0].AsNumber + @"ms"" />";
            }, 1);

            //
            // Commander-specific functions
            //
            store["ShipName"] = new NativeFunction((values) =>
            {
                int? localId = (values.Count == 0 ? (int?)null : (int)values[0].AsNumber);
                string model = (values.Count == 2 ? values[1].AsString : null);
                Ship ship = findShip(localId, model);
                string result = (ship == null ? "your ship" : ship.SpokenName());
                return result;
            }, 0, 2);

            store["ShipCallsign"] = new NativeFunction((values) =>
            {
                int? localId = (values.Count == 0 ? (int?)null : (int)values[0].AsNumber);
                Ship ship = findShip(localId, null);

                string result;
                if (ship != null)
                {
                    if (EDDI.Instance.Cmdr != null && EDDI.Instance.Cmdr.name != null)
                    {
                        // Obtain the first three characters
                        string chars = new Regex("[^a-zA-Z0-9]").Replace(EDDI.Instance.Cmdr.name, "").ToUpperInvariant().Substring(0, 3);
                        result = ship.SpokenManufacturer() + " " + Translations.CallSign(chars);
                    }
                    else
                    {
                        if (ship.SpokenManufacturer() == null)
                        {
                            result = "unidentified ship";
                        }
                        else
                        {
                            result = "unidentified " + ship.SpokenManufacturer() + " " + ship.SpokenModel();
                        }
                    }
                }
                else
                {
                    result = "unidentified ship";
                }
                return result;
            }, 0, 1);

            //
            // Obtain definition objects for various items
            //

            store["ShipDetails"] = new NativeFunction((values) =>
            {
                int? localId = (values.Count == 0 ? (int?)null : (int)values[0].AsNumber);
                Ship result = findShip(localId, null);
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 0, 1);

            store["CombatRatingDetails"] = new NativeFunction((values) =>
            {
                CombatRating result = CombatRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = CombatRating.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["TradeRatingDetails"] = new NativeFunction((values) =>
            {
                TradeRating result = TradeRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = TradeRating.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["ExplorationRatingDetails"] = new NativeFunction((values) =>
            {
                ExplorationRating result = ExplorationRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = ExplorationRating.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["EmpireRatingDetails"] = new NativeFunction((values) =>
            {
                EmpireRating result = EmpireRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = EmpireRating.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["FederationRatingDetails"] = new NativeFunction((values) =>
            {
                FederationRating result = FederationRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = FederationRating.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["SystemDetails"] = new NativeFunction((values) =>
            {
                StarSystem result = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(values[0].AsString, true);
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["StationDetails"] = new NativeFunction((values) =>
            {
                if (values.Count == 0)
                {
                    return null;
                }
                StarSystem system;
                if (values.Count == 1)
                {
                    // Current system
                    system = EDDI.Instance.CurrentStarSystem;
                }
                else
                {
                    system = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(values[1].AsString, true);
                }
                Station result = system != null && system.stations != null ? system.stations.FirstOrDefault(v => v.name == values[0].AsString) : null;
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1, 2);

            store["SuperpowerDetails"] = new NativeFunction((values) =>
            {
                Superpower result = Superpower.FromName(values[0].AsString);
                if (result == null)
                {
                    result = Superpower.From(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["StateDetails"] = new NativeFunction((values) =>
            {
                State result = State.FromName(values[0].AsString);
                if (result == null)
                {
                    result = State.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["EconomyDetails"] = new NativeFunction((values) =>
            {
                Economy result = Economy.FromName(values[0].AsString);
                if (result == null)
                {
                    result = Economy.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["GovernmentDetails"] = new NativeFunction((values) =>
            {
                Government result = Government.FromName(values[0].AsString);
                if (result == null)
                {
                    result = Government.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["SecurityLevelDetails"] = new NativeFunction((values) =>
            {
                SecurityLevel result = SecurityLevel.FromName(values[0].AsString);
                if (result == null)
                {
                    result = SecurityLevel.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["MaterialDetails"] = new NativeFunction((values) =>
            {
                Material result = Material.FromName(values[0].AsString);
                if (result == null)
                {
                    result = Material.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            // Variables
            foreach (KeyValuePair<string, Cottle.Value> entry in vars)
            {
                store[entry.Key] = entry.Value;
            }

            return store;
        }
Beispiel #10
0
        private static void AssertPrint(string expression, string expected)
        {
            IDocument	document = new SimpleDocument ("{echo " + expression + "}");
            IStore		store = new BuiltinStore ();

            Assert.AreEqual (expected, document.Render (store), "'{0}' doesn't render to '{1}'", expression, expected);
        }
Beispiel #11
0
        private static void AssertEqual(string expression, string expected)
        {
            IDocument	document = new SimpleDocument ("{eq(" + expression + ", " + expected + ")}");
            IStore		store = new BuiltinStore ();

            Assert.AreEqual ("true", document.Render (store), "'{0}' doesn't evaluate to '{1}'", expression, expected);
        }
Beispiel #12
0
 public BuiltinStore()
 {
     this.store = new FallbackStore(BuiltinStore.GetConstant(), new SimpleStore());
 }