设计数据驱动的逻辑系统
我正在开发一个税收计算系统,该系统根据一组提供的标准应用各种税收。
信息经常变化,所以我试图创建一种方法来将所有这些逻辑规则存储在数据库中。
正如您可以想象的那样,征税涉及很多复合逻辑。
例如,如果 A 为真、B 小于 100、C 等于 7,则可能仅适用税收。
我目前的设计很糟糕。
我有一些数据库列用于非常常见的条件过滤,例如位置和纳税年度。
对于更复杂的逻辑,我有一个包含 JavaScript 的列,在代码中,我运行解释器来过滤结果。性能和可维护性很糟糕。
我想通过使逻辑完全由数据驱动来改进此设计,但我无法弄清楚如何在关系数据库中正确表示此逻辑。在数据库中建模此逻辑的好方法是什么?
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.
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. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经在制造成本生成应用程序中研究这个类似的问题一年多了。同样,它会输入大量的产品设计数据,并基于设计以及其他库存考虑因素,例如数量、批量采购选项、零件供应商、电气额定值等。结果是直接材料、劳动力和成本的列表。
我从一开始就知道我需要的是某种查询语言而不是计算语言,并且它必须是脚本化的,而不是编译的。但我还没有找到完美的解决方案:
方法1 - SQL
我创建了表示对象的表和表示属性的列,然后手动键入 item_rules 表中所需的所有 SQL SELECT 语句。我所做的是首先将对象保存到数据库中,然后我做了
它的作用是获取 item_rules 表中的每个规则并针对现在位于表中的对象运行它。例如 SELECT * FROM my_object WHERE A=5 AND B>10。如果我成功拾取它,我会得到一个正计数,然后我知道我应该将相应的规则项目包含到我的项目列表中。
方法 2 - NCALC
我没有以 SQL 格式存储查询,而是找到了 NCALC 开源表达式解析库。 NCALC 采用字符串表达式和选项变量并计算结果。字符串表达式可以以纯文本形式存储在文件系统上。
方法 3 - Excel
EXCEL其实是一个非常好的数据查找软件。您可以在 Excel 中创建公式,然后将应用程序中的数据输入 Excel,然后让 Excel 运行公式以给出结果。优点是很多人都知道如何使用excel,所以不同的人可以维护它。
但就像我说的,这些都不适合我。我只是分享,希望我们能得到更好的推荐。
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. What I did was to first save the object into the database, then then I did
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. e.g. SELECT * FROM my_object WHERE A=5 AND 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. NCALC takes a string expression and option variable and computes a result. 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. 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. Advantage is that many people knows how to use excel, so different people can maintain it.
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。
If you are to go with Jake's approach, You can use Dynamic Sql too.