src/EasyFrameWork/RuleEngine/Scripting/ScriptExpressionEvaluator.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

/* http://www.zkea.net/ 
 * Copyright (c) ZKEASOFT. All rights reserved. 
 * http://www.zkea.net/licenses */

using System;
using System.Collections.Generic;
using System.Linq;
using Easy.Cache;
using Easy.RuleEngine.Scripting.Ast;
using Easy.RuleEngine.Scripting.Compiler;

namespace Easy.RuleEngine.Scripting
{
    public class ScriptExpressionEvaluator : IScriptExpressionEvaluator
    {
        private readonly ICacheManager<ScriptExpressionResult> _cacheManager;
        public ScriptExpressionEvaluator(ICacheManager<ScriptExpressionResult> cacheManager)
        {
            _cacheManager = cacheManager;
        }

        public object Evaluate(string expression, IEnumerable<IGlobalMethodProvider> providers)
        {
            var expr = _cacheManager.GetOrAdd(expression, ctx =>
            {
                var ast = ParseExpression(expression);
                return new ScriptExpressionResult { Tree = ast, Errors = ast.GetErrors().ToList() };
            });

            if (expr.Errors.Any())
            {
                //TODO: Collect all errors
                throw new Exception($"Syntax error: {expr.Errors.First().Message}");
            }

            var result = EvaluateExpression(expr.Tree, providers);
            if (result.IsError)
            {
                throw new ApplicationException(result.ErrorValue.Message);
            }

            return result.Value;
        }

        private AbstractSyntaxTree ParseExpression(string expression)
        {
            return new Parser(expression).Parse();
        }

        private EvaluationResult EvaluateExpression(AbstractSyntaxTree tree, IEnumerable<IGlobalMethodProvider> providers)
        {
            var context = new EvaluationContext
            {
                Tree = tree,
                MethodInvocationCallback = (m, args) => Evaluate(providers, m, args)
            };
            return new Interpreter().Evalutate(context);
        }

        private object Evaluate(IEnumerable<IGlobalMethodProvider> globalMethodProviders, string name, IEnumerable<object> args)
后面还有 17 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。