簡體   English   中英

評估復雜表達式樹

[英]Evaluating complex expression tree

我有一個基本的規則引擎,其構建方式與此處建議的路由非常相似:

如何實施規則引擎?

我已經根據進一步的要求擴展了它,現在我需要評估復雜的類,例如

EvaluateRule("Transaction.IsOpen", "Equals", "true")  

最基本形式的代碼是:

var param = inputMessageType;
left = Expression.Property(param, memberName);
tProp = typeof(T).GetProperty(r.MemberName).PropertyType;
right = Expression.Constant(Convert.ChangeType(r.TargetValue, tProp));
return Expression.MakeBinary(tBinary, left, right);    

為了評估復雜的類,我使用了一種類似於此處的方法:

https://stackoverflow.com/questions/16544672/dynamically-evaluating-a-property-string-with-expressions

我遇到的問題是,當我嘗試使用類的屬性(Transaction.IsOpen)評估規則時,使用表達式右側的根類型的類型但使用復雜類型的類型來獲取該規則對象位於表達式的左側。

這導致錯誤:

System.InvalidOperationException: The binary operator Equal is not defined for the types 'System.Func`2[Transaction,System.Boolean]' and 'System.Boolean'.

我該如何克服這個問題? 我不是使用表達式樹的專家,當示例偏離標准文檔時,許多概念都很難理解。

編輯:這是代碼(我省略了一些特定於環境的內容,以便關注問題)

public Actions EvaluateRulesFromMessage(ClientEventQueueMessage message)
    {            
        var ruleGroups = _ruleRepository.GetRuleList();

    var actions = new Actions();

    foreach (var ruleGroup  in ruleGroups)
    {
        if (message.MessageType == "UI_UPDATE")
        {
            // clean up json object
            JObject dsPayload = (JObject.Parse(message.Payload));

            var msgParams = JsonConvert.DeserializeObject<UiTransactionUpdate>(message.Payload);                    
            msgParams.RulesCompleted = msgParams.RulesCompleted ?? new List<int>(); 

            var conditionsMet = false;
            // process the rules filtering out the rules that have already been evaluated                    
            var filteredRules = ruleGroup.Rules.Where(item =>
                !msgParams.RulesCompleted.Any(r => r.Equals(item.Id)));                    

            foreach (var rule in filteredRules)                                                
            {                        
                Func<UiTransactionUpdate, bool> compiledRule = CompileRule<UiTransactionUpdate>(rule, msgParams);
                if (compiledRule(msgParams))
                {
                    conditionsMet = true;

                }
                else
                {
                    conditionsMet = false;
                    break;
                }                        

            }
            if (conditionsMet) 
            {                        
                actions = AddAction(message, ruleGroup);
                break;
            }
        }
    }                
    return actions;
}

public Func<UiTransactionUpdate, bool> CompileRule<T>(Rule r, UiTransactionUpdate msg)
{
    var expression = Expression.Parameter(typeof(UiTransactionUpdate));
    Expression expr = BuildExpr<UiTransactionUpdate>(r, expression, msg);
    // build a lambda function UiTransactionUpdate->bool and compile it
    return Expression.Lambda<Func<UiTransactionUpdate, bool>>(expr, expression).Compile();
}

static Expression Eval(object root, string propertyString, out Type tProp)
{
    Type type = null;
    var propertyNames = propertyString.Split('.');
    ParameterExpression param = Expression.Parameter(root.GetType());
    Expression property = param;
    string propName = "";
    foreach (var prop in propertyNames)
    {                           
        property = MemberExpression.PropertyOrField(property, prop);
        type = property.Type;
        propName = prop;
    }

    tProp = Type.GetType(type.UnderlyingSystemType.AssemblyQualifiedName);

    var param2 = MemberExpression.Parameter(tProp);

    var e = Expression.Lambda(property, param);

    return e;
}

static Expression BuildExpr<T>(Rule r, ParameterExpression param, UiTransactionUpdate msg)
{
    Expression left;
    Type tProp;
    string memberName = r.MemberName;
    if (memberName.Contains("."))
    {
        left = Eval(msg, memberName, out tProp);            
    }
    else
    {
        left = Expression.Property(param, memberName);
        tProp = typeof(T).GetProperty(r.MemberName).PropertyType;
    }

    ExpressionType tBinary;            
    if (ExpressionType.TryParse(r.Operator, out tBinary))
    {
        Expression right=null;
        switch (r.ValueType)  ///todo: this needs to be refactored to be type independent
        {
            case TargetValueType.Value:
                right = Expression.Constant(Convert.ChangeType(r.TargetValue, tProp));
                break;                    
        }
        // use a binary operation ie true/false
        return Expression.MakeBinary(tBinary, left, right);                
    }
    else
    {
        var method = tProp.GetMethod(r.Operator);
        var tParam = method.GetParameters()[0].ParameterType;
        var right = Expression.Constant(Convert.ChangeType(r.TargetValue, tParam));
        // use a method call, e.g. 'Contains' -> 'u.Tags.Contains(some_tag)'
        return Expression.Call(left, method, right);
    }
}

該示例代碼並未涵蓋您的方案中使用的所有數據類型,因此很難說出它到底在哪里中斷,但是除了System.Func'2[Transaction,System.Boolean]' and 'System.Boolean這很明顯,左手有一個接受Transaction並返回bool的委托( Func<Transaction, bool> ),右手只有bool

不可能將Func<Transaction, bool>bool進行比較,但是可以調用該函數並比較其結果:

Func<Transaction, bool> func = ...;
bool comparand = ...;
Transaction transaction = ...;
if (func(transaction) == comparand) { ... }

什么翻譯成表達式樹:

Expression funcExpression = ... /*LambdaExpression<Func<Transaction,bool>>*/;
Expression comparandExpression = Expression.Constant(true);
Expression transactionArg = /*e.g.*/Expression.Constant(transaction);
Expression funcResultExpression = Expression.Call(funcExpression, "Invoke", null, transactionArg);
Expression equalityTestExpression = Expression.Equal(funcResultExpression, comparandExpression);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM