规则引擎 - 如何存储的规则,以避免解析的编辑? [英] Rule Engine - How to store rules to avoid parsing on edit?

查看:811
本文介绍了规则引擎 - 如何存储的规则,以避免解析的编辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.NET应用程序在运行时评估用户定义的规则。这些规则是由用户通过图形用户界面菜单进入到系统中。我产生对应于这一个逻辑语句并将其存储在数据库中。

例如:(名称='约翰'和姓='史密斯)或号码> 12

然而,当用户希望编辑的GUI由一个规则,我需要一个反向操作,以从所存储的规则,这是昂贵的和复杂的确定菜单状态。你会如何​​建议的方式存储的规则,它可以被逆转到菜单状态容易吗?

解决方案

您可以存储规则 AST 秒 - 实行了几类,再present树的节点:

 接口的inode
{
}

枚举BinaryOperator
{
    AND,OR,等于,大于较低;
}

类BinaryEx pression:INODE
{
    BinaryOperator操作{获得;组; }
    inode的左{获得;组; }
    inode的右{获得;组; }
}

类PropertyRerefence:INODE
{
    字符串属性名{获得;组; }
}

类常量:INODE
{
    字符串值{获得;组; }
}
 

该树为你的例子是这样的:

  BinaryEx pression(OR)
  左= BinaryEx pression(AND)
          左= ...
          右= ...
  右= BinaryEx pression(大)
          左= PropertyReference(数字)
          右=常数(12)
 

您可以再使用的序列化(最好JSON或XML,甚至二,如果你不关心的可读性的分贝)来保存这些树木。在反序列化,你不需要做任何分析,可以遍历树来填充菜单。

打印(名称='约翰'和姓='史密斯)或数字> 12也很容易,当你有AST - 为BinaryEx pression:打印左侧,打印操作员,打印右<。 / P>

您说您已经评价实施,所以我会离开了这一点。您还可以看看这个问题

My .NET application evaluates user defined rules at runtime. These rules are entered to system via GUI menus by user. I generate a logical statement that corresponds to it and store it in database.

For example: (Name = 'John' AND Surname = 'Smith') OR Number > 12

However, when the user wants to edit a rule by GUI, I need to make a reverse operation to determine menu states from the stored rule, which is costly and complex. How would you recommend to store rules in a way that it can be reversed to menu states easily?

解决方案

You could store the rules as ASTs - implement a few classes that represent the nodes of the tree:

interface INode
{
}

enum BinaryOperator 
{
    AND, OR, Equal, Greater, Lower;
}

class BinaryExpression : INode
{
    BinaryOperator Operator { get; set; }
    INode Left { get; set; }
    INode Right { get; set; } 
}

class PropertyRerefence : INode
{
    string PropertyName { get; set; }
}

class Constant : INode
{
    string Value { get; set; }
}

The tree for your example would look like this:

BinaryExpression(OR)
  Left=BinaryExpression(AND)
          Left=...
          Right=...
  Right=BinaryExpression(Greater)
          Left=PropertyReference("Number")
          Right=Constant("12")

You could then use serialization (best JSON, or XML, maybe even binary if you don't care about readability in the db) to save such trees. On deserialization, you don't need to do any parsing and can traverse the tree to populate the menus.

Printing "(Name = 'John' AND Surname = 'Smith') OR Number > 12" is also easy when you have the AST - for a BinaryExpression: print Left, print Operator, print Right.

You say you already have the evaluation implemented so I'll leave this out. You can also look at this question.

这篇关于规则引擎 - 如何存储的规则,以避免解析的编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆