简体   繁体   English

设计数据驱动的逻辑系统

[英]Designing a data-driven logic system

I'm developing a tax calculation system that applies various taxes based on a set of supplied criteria. 我正在开发一种税收计算系统,该系统可根据一组提供的标准应用各种税收。
The information frequently changes, so I'm trying to create a way to store all these logic rules in the database. 该信息经常更改,因此我试图创建一种将所有这些逻辑规则存储在数据库中的方法。

As you can imagine, there is a lot of compound logic involved in applying taxes. 可以想像,征税涉及很多复合逻辑。
For example, a tax might only apply if A is true, B is less than 100, and C equals 7 . 例如,仅当A为true,B小于100且C等于7时才适用税收。

My current design is terrible. 我当前的设计太糟糕了。
I have a few database columns for very common criteria filtering, such as location and tax year. 我有一些数据库列用于非常常见的条件过滤,例如位置和纳税年度。
For more complex logic, I have a column that holds JavaScript, and in code, I run an interpreter to filter the results. 对于更复杂的逻辑,我有一列包含JavaScript,并且在代码中运行解释器以过滤结果。 Performance and maintainability suck. 性能和可维护性很差。

I'd like to improve this design by making the logic entirely data-driven, but I'm having trouble figuring out how to correctly represent this logic within a relational database. 我想通过使逻辑完全由数据驱动来改进此设计,但是在弄清楚如何在关系数据库中正确表示此逻辑时遇到了麻烦。 What is a good way to model this logic in the database? 在数据库中对此逻辑建模的好方法是什么?

I have worked on this similar issue for over a year now for a manufacturing cost generation application. 我已经为生产成本生成应用程序解决了类似问题一年多了。 Similarly, it takes in loads of product design data input and base on the design, and other inventory considerations such as quantity, bulk purchase options, part supplier, electrical ratings etc. The result is a list of direct materials, labour and costs. 同样,它会根据设计以及其他库存考虑因素(例如数量,批量购买选项,零件供应商,电气额定值等)来接受产品设计数据输入和负载。结果是直接材料,人工和成本的清单。

I knew from the onset that what I need is some kind of query language instead of a computational one, and it has to be scripted, not compiled. 我从一开始就知道,我需要的是某种查询语言而不是计算语言,它必须是脚本编写的,而不是编译的。 But I have yet to find a perfect solution: 但是我还没有找到一个完美的解决方案:

METHOD 1 - SQL I created tables that represents my objects and columns that represents properties and then manually typed in the all the SQL SELECT statments required in an item_rules table. 方法1-SQL我创建了代表我的对象的表和代表属性的列,然后在item_rules表中手动键入所有SQL SELECT语句。 What I did was to first save the object into the database, then then I did 我要做的是先将对象保存到数据库中,然后再执行

rules = SELECT * FROM item_rules
foreach(rules as _rule)
{
   count = SELECT COUNT(*) FROM (_rule[select_statement]) as T1
   if(count > 1) itemlist.add(_rule[item_that_satisfy_rule])
}

What it does is it takes each rule in the item_rules table and run it against my object that is now in the tables. 它的作用是将item_rules表中的每个规则带到表中现在存在的我的对象上。 eg SELECT * FROM my_object WHERE A=5 AND B>10. 例如SELECT * FROM my_object,其中A = 5并且B> 10。 If I successfully pick it up, I get a positive count and then I know I should include the corresponding rule item to my items list. 如果我成功捡起它,我得到一个正数,然后知道我应该将相应的规则项目包括在我的项目列表中。

METHOD 2 - NCALC Instead of storing the queries in SQL format, I found the NCALC opensource expression parsing library. 方法2-NCALC我没有以SQL格式存储查询,而是找到了NCALC开源表达式解析库。 NCALC takes a string expression and option variable and computes a result. NCALC接受字符串表达式和选项变量并计算结果。 The string expressions can be stored in plain text on the filesystem. 字符串表达式可以以纯文本格式存储在文件系统上。

METHOD 3 - EXCEL EXCEL is actually a very good piece of software for doing data lookups. 方法3-EXCEL EXCEL实际上是用于执行数据查找的非常好的软件。 You can create the formulas in excel and then feed data from your application into excel and then let excel run the formulas to give you the results. 您可以在excel中创建公式,然后将应用程序中的数据输入excel,然后让excel运行公式以提供结果。 Advantage is that many people knows how to use excel, so different people can maintain it. 优点是许多人都知道如何使用excel,因此不同的人可以维护它。

But like I say, none of these are perfect for me. 但是就像我说的那样,这些都不适合我。 I am just sharing and hopefully we can get better recommedations. 我只是在分享,希望我们能得到更好的建议。

如果要使用Jake的方法,那么也可以使用Dynamic Sql。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM