public CustomFunctionBot() { var lgFilePath = Path.Join(Directory.GetCurrentDirectory(), "Resources", "main.lg"); // prefix all functions with your namespace to avoid collisions. const string mySqrtFnName = "contoso.sqrt"; // define evaluator for the custom function. ExpressionEvaluator sqrtDelegate = new ExpressionEvaluator( mySqrtFnName, ExpressionFunctions.Apply( args => { object retValue = null; if (args[0] != null) { double dblValue; if (double.TryParse(args[0], out dblValue)) { retValue = Math.Sqrt(dblValue); } } return(retValue); }, ExpressionFunctions.VerifyStringOrNull), ReturnType.Number, ExpressionFunctions.ValidateUnary); // add the custom function Expression.Functions.Add(mySqrtFnName, sqrtDelegate); // by default this uses Expression.Functions which would include the custom function added above. _templates = Templates.ParseFile(lgFilePath); }
// Generate a new lookup function based on one lookup function private EvaluatorLookup CustomizedEvaluatorLookup(EvaluatorLookup baseLookup) => (string name) => { var standardFunction = baseLookup(name); if (standardFunction != null) { return(standardFunction); } if (name.StartsWith("lg.")) { name = name.Substring(3); } var templateName = ParseTemplateName(name).pureTemplateName; if (this.TemplateMap.ContainsKey(templateName)) { return(new ExpressionEvaluator(templateName, ExpressionFunctions.Apply(this.TemplateEvaluator(name)), ReturnType.Object, this.ValidTemplateReference)); } const string template = "template"; if (name.Equals(template)) { return(new ExpressionEvaluator(template, ExpressionFunctions.Apply(this.TemplateFunction()), ReturnType.Object, this.ValidateTemplateFunction)); } const string fromFile = "fromFile"; if (name.Equals(fromFile)) { return(new ExpressionEvaluator(fromFile, ExpressionFunctions.Apply(this.FromFile()), ReturnType.String, ExpressionFunctions.ValidateUnaryString)); } const string activityAttachment = "ActivityAttachment"; if (name.Equals(activityAttachment)) { return(new ExpressionEvaluator( activityAttachment, ExpressionFunctions.Apply(this.ActivityAttachment()), ReturnType.Object, (expr) => ExpressionFunctions.ValidateOrder(expr, null, ReturnType.Object, ReturnType.String))); } const string isTemplate = "isTemplate"; if (name.Equals(isTemplate)) { return(new ExpressionEvaluator(isTemplate, ExpressionFunctions.Apply(this.IsTemplate()), ReturnType.Boolean, ExpressionFunctions.ValidateUnaryString)); } return(null); };
// Generate a new lookup function based on one lookup function private EvaluatorLookup CustomizedEvaluatorLookup(EvaluatorLookup baseLookup, bool isExpander) => (string name) => { var prebuiltPrefix = "prebuilt."; if (name.StartsWith(prebuiltPrefix)) { return(baseLookup(name.Substring(prebuiltPrefix.Length))); } if (this.TemplateMap.ContainsKey(name)) { if (isExpander) { return(new ExpressionEvaluator(name, ExpressionFunctions.Apply(this.TemplateExpander(name)), ReturnType.String, this.ValidTemplateReference)); } else { return(new ExpressionEvaluator(name, ExpressionFunctions.Apply(this.TemplateEvaluator(name)), ReturnType.String, this.ValidTemplateReference)); } } return(baseLookup(name)); };
private Templates InjectToExpressionFunction() { var totalTempaltes = new List <Templates> { this }.Union(References); foreach (var curTemplates in totalTempaltes) { var globalFuncs = curTemplates.GetGlobalFunctionTable(curTemplates.Options); foreach (var templateName in globalFuncs) { if (curTemplates.Any(u => u.Name == templateName)) { var newGlobalName = $"{curTemplates.Namespace}.{templateName}"; Expression.Functions.Add(newGlobalName, new ExpressionEvaluator(newGlobalName, ExpressionFunctions.Apply(this.GlobalTemplateFunction(templateName)), ReturnType.Object)); } } } return(this); }