Example #1
0
 private Func <IReadOnlyList <object>, object> GlobalTemplateFunction(string templateName)
 => (IReadOnlyList <object> args) =>
 {
     var evaluator = new Evaluator(AllTemplates.ToList(), ExpressionParser, LgOptions);
     var newScope  = evaluator.ConstructScope(templateName, args.ToList());
     return(evaluator.EvaluateTemplate(templateName, newScope));
 };
Example #2
0
 /// <summary>
 /// Expand a template with given name and scope.
 /// Return all possible responses instead of random one.
 /// </summary>
 /// <param name="templateName">Template name to be evaluated.</param>
 /// <param name="scope">The state visible in the evaluation.</param>
 /// <param name="opt">The evaluation option for current expander.</param>
 /// <returns>Expand result.</returns>
 public IList<object> ExpandTemplate(string templateName, object scope = null, EvaluationOptions opt = null)
 {
     CheckErrors();
     var evalOpt = opt ?? LgOptions;
     var expander = new Expander(AllTemplates.ToList(), ExpressionParser, evalOpt);
     return expander.ExpandTemplate(templateName, scope);
 }
Example #3
0
        /// <summary>
        /// (experimental)
        /// Analyze a template to get the static analyzer results including variables and template references.
        /// </summary>
        /// <param name="templateName">Template name to be evaluated.</param>
        /// <returns>Analyzer result.</returns>
        public AnalyzerResult AnalyzeTemplate(string templateName)
        {
            CheckErrors();
            var analyzer = new Analyzer(AllTemplates.ToList(), ExpressionParser);

            return(analyzer.AnalyzeTemplate(templateName));
        }
        /// <summary>
        /// Expand a template with given name and scope.
        /// Return all possible responses instead of random one.
        /// </summary>
        /// <param name="templateName">Template name to be evaluated.</param>
        /// <param name="scope">The state visible in the evaluation.</param>
        /// <returns>Expand result.</returns>
        public IList <object> ExpandTemplate(string templateName, object scope = null)
        {
            CheckErrors();
            var expander = new Expander(AllTemplates.ToList(), ExpressionParser, StrictMode);

            return(expander.ExpandTemplate(templateName, scope));
        }
Example #5
0
        /// <summary>
        /// Expand a template with given name and scope.
        /// Return all possible responses instead of random one.
        /// </summary>
        /// <param name="templateName">Template name to be evaluated.</param>
        /// <param name="scope">The state visible in the evaluation.</param>
        /// <returns>Expand result.</returns>
        public IList <string> ExpandTemplate(string templateName, object scope = null)
        {
            CheckErrors();
            var expander = new Expander(AllTemplates.ToList(), ExpressionEngine);

            return(expander.EvaluateTemplate(templateName, new CustomizedMemory(scope)));
        }
        /// <summary>
        /// Evaluate a template with given name and scope.
        /// </summary>
        /// <param name="templateName">Template name to be evaluated.</param>
        /// <param name="scope">The state visible in the evaluation.</param>
        /// <returns>Evaluate result.</returns>
        public object Evaluate(string templateName, object scope = null)
        {
            CheckErrors();

            var evaluator = new Evaluator(AllTemplates.ToList(), ExpressionParser, StrictMode);

            return(evaluator.EvaluateTemplate(templateName, scope));
        }
        /// <summary>
        /// Evaluate a template with given name and scope.
        /// </summary>
        /// <param name="templateName">Template name to be evaluated.</param>
        /// <param name="scope">The state visible in the evaluation.</param>
        /// <param name="opt">The EvaluationOptions in evaluating a template.</param>
        /// <returns>Evaluate result.</returns>
        public object Evaluate(string templateName, object scope = null, EvaluationOptions opt = null)
        {
            CheckErrors();
            var evalOpt = opt != null?opt.Merge(LgOptions) : LgOptions;

            var evaluator = new Evaluator(AllTemplates.ToList(), ExpressionParser, evalOpt);

            return(evaluator.EvaluateTemplate(templateName, scope));
        }
Example #8
0
        /// <summary>
        /// Evaluate a template with given name and scope.
        /// </summary>
        /// <param name="templateName">Template name to be evaluated.</param>
        /// <param name="scope">The state visible in the evaluation.</param>
        /// <returns>Evaluate result.</returns>
        public object EvaluateTemplate(string templateName, object scope = null)
        {
            CheckErrors();

            var memory    = SimpleObjectMemory.Wrap(scope);
            var evaluator = new Evaluator(AllTemplates.ToList(), ExpressionEngine);

            return(evaluator.EvaluateTemplate(templateName, new CustomizedMemory(memory)));
        }
Example #9
0
        /// <summary>
        /// Evaluate a template with given name and scope.
        /// </summary>
        /// <param name="templateName">Template name to be evaluated.</param>
        /// <param name="scope">The state visible in the evaluation.</param>
        /// <param name="opt">The EvaluationOptions in evaluating a template.</param>
        /// <returns>Evaluate result.</returns>
        public object Evaluate(string templateName, object scope = null, EvaluationOptions opt = null)
        {
            CheckErrors();
            var evalOpt = opt != null ? opt.Merge(LgOptions) : LgOptions;
            var evaluator = new Evaluator(AllTemplates.ToList(), ExpressionParser, evalOpt);
            var result = evaluator.EvaluateTemplate(templateName, scope);
            if (evalOpt.LineBreakStyle == LGLineBreakStyle.Markdown && result is string str)
            {
                result = newLineRegex.Replace(str, "$1$1");
            }

            return result;
        }
Example #10
0
        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 prefix        = string.IsNullOrWhiteSpace(curTemplates.Namespace) ? string.Empty : curTemplates.Namespace + ".";
                        var newGlobalName = prefix + templateName;
                        Expression.Functions.Add(newGlobalName, new ExpressionEvaluator(
                                                     newGlobalName,
                                                     (expression, state, options) =>
                        {
                            object result    = null;
                            var evaluator    = new Evaluator(AllTemplates.ToList(), ExpressionParser, LgOptions);
                            var(args, error) = FunctionUtils.EvaluateChildren(expression, state, options);
                            if (error == null)
                            {
                                var parameters = evaluator.TemplateMap[templateName].Parameters;
                                var newScope   = parameters.Zip(args, (k, v) => new { k, v })
                                                 .ToDictionary(x => x.k, x => x.v);
                                var scope = new CustomizedMemory(state, new SimpleObjectMemory(newScope));
                                try
                                {
                                    result = evaluator.EvaluateTemplate(templateName, scope);
                                }
#pragma warning disable CA1031 // Do not catch general exception types
                                catch (Exception err)
#pragma warning restore CA1031 // Do not catch general exception types
                                {
                                    error = err.Message;
                                }
                            }

                            return(result, error);
                        },
                                                     ReturnType.Object));
                    }
                }
            }

            return(this);
        }