drools中一个复杂场景的dmn

现在遇到一个问题,就是都知道规则引擎是个好东西,但怎么用它却没有一个直观的概念.
因为看到的example都是很简单的,简单的东西什么工具都能做,要体现优势就需要有个复杂场景,这个场景在dmn里可以可视化开发,可视化运行,与之相对比用传统语言代码非常复杂难懂不易维护. 那这就很有说服性了

首先DMN是 OMG这个组织发布的规范
https://www.omg.org/spec/DMN/1.4/Beta1/PDF
给了几个例子,对应pdf中的几个章节
https://www.omg.org/spec/DMN/20211108/examples.zip

在这里插入图片描述

例子不是给drools用的, drools实现dmn是有差异的所以不能直接打开,对比能打开的dmn, 对官方给的例子做了些调整(替换文字的时候正则表达式…真好用,表示任意字符) 现在起码是能打开了,后续还要做些调整.比如import现在是粗暴的直接注释了.

文件等下上传 .另外提一点 workbench里的项目可以git管理代码
在这里插入图片描述
非常好用,dmn在本地一上传, kie-wb里就看到打印出来的解析dmn的错误堆栈
有了它才有了进行格式转换的可能(人肉转换)

dmn文件见:
https://download.csdn.net/download/wangduqiang747/86404649
文件有几个版本,用最新的那个

发布之后的请求如下:
post http://127.0.0.1:8080/kie-server/services/rest/server/containers/dumpli_1.0.0-SNAPSHOT/dmn

{
    "model-namespace": "http://www.trisotech.com/definitions/_9d01a0c4-f529-4ad8-ad8e-ec5fb5d96ad4",
    "model-name": "Chapter 11 Example",
    "dmn-context": {
        "Applicant data": {
            "Age": 25,
            "MaritalStatus": "M",
            "EmploymentStatus": "EMPLOYED",
            "ExistingCustomer": false,
            "Monthly": {
                "Income": 10000,
                "Repayments": 2500,
                "Expenses": 100000
            }
        },
        "Requested product": {
            "ProductType": "STANDARD LOAN",
            "Rate": 0.08,
            "Term": 36,
            "Amount": 10000
        },
        "Bureau data": {
            "Bankrupt": false,
            "CreditScore": 100
        },
        "Supporting documents": {},
        "Loan default data": "aaaaa"
    }
}

请求的返回如下:

{
    "type": "SUCCESS",
    "msg": "OK from container 'dumpli_1.0.0-SNAPSHOT'",
    "result": {
        "dmn-evaluation-result": {
            "messages": [],
            "model-namespace": "http://www.trisotech.com/definitions/_9d01a0c4-f529-4ad8-ad8e-ec5fb5d96ad4",
            "model-name": "Chapter 11 Example",
            "decision-name": [],
            "dmn-context": {
                "Installment calculation": "function Installment calculation( Product Type, Rate, Term, Amount )",
                "Bureau call type table": "function Bureau call type table( Pre-Bureau Risk Category )",
                "Eligibility": "INELIGIBLE",
                "Adjudication": 2,
                "Loan default data": "aaaaa",
                "Application risk score model": "function Application risk score model( Age, Marital Status, Employment Status )",
                "Credit contingency factor table": "function Credit contingency factor table( Risk Category )",
                "Application risk score": 125,
                "Pre-bureau risk category": "LOW",
                "Supporting documents": {},
                "Bureau data": {
                    "Bankrupt": false,
                    "CreditScore": 100
                },
                "Eligibility rules": "function Eligibility rules( Pre-Bureau Risk Category, Pre-Bureau Affordability, Age )",
                "Pre-bureau risk category table": "function Pre-bureau risk category table( Existing Customer, Application Risk Score )",
                "Routing rules": "function Routing rules( Post-bureau risk category, Post-bureau affordability, Bankrupt, Credit score )",
                "Post-bureau risk category": "HIGH",
                "Requested product": {
                    "Rate": 0.08,
                    "Amount": 10000,
                    "ProductType": "STANDARD LOAN",
                    "Term": 36
                },
                "Routing": "DECLINE",
                "Affordability calculation": "function Affordability calculation( Monthly Income, Monthly Repayments, Monthly Expenses, Risk Category, Required Monthly Installment )",
                "Post-bureau affordability": false,
                "Financial.PMT": "function Financial.PMT( Rate, Term, Amount )",
                "Post-bureau risk category table": "function Post-bureau risk category table( Existing Customer, Application Risk Score, Credit Score )",
                "Required monthly installment": 333.3636546143084985132842970339110,
                "Applicant data": {
                    "MaritalStatus": "M",
                    "Monthly": {
                        "Repayments": 2500,
                        "Expenses": 100000,
                        "Income": 10000
                    },
                    "ExistingCustomer": false,
                    "EmploymentStatus": "EMPLOYED",
                    "Age": 25
                },
                "Strategy": "DECLINE",
                "Bureau call type": "MINI",
                "Pre-bureau affordability": false
            },
            "decision-results": {
                "_5b8356f3-2cf2-40e8-8f80-324937e8b276": {
                    "messages": [],
                    "decision-id": "_5b8356f3-2cf2-40e8-8f80-324937e8b276",
                    "decision-name": "Bureau call type",
                    "result": "MINI",
                    "status": "SUCCEEDED"
                },
                "_b5e759df-f662-44cd-94f5-55c3c81f0ee3": {
                    "messages": [],
                    "decision-id": "_b5e759df-f662-44cd-94f5-55c3c81f0ee3",
                    "decision-name": "Eligibility",
                    "result": "INELIGIBLE",
                    "status": "SUCCEEDED"
                },
                "_4bd33d4a-741b-444a-968b-64e1841211e7": {
                    "messages": [],
                    "decision-id": "_4bd33d4a-741b-444a-968b-64e1841211e7",
                    "decision-name": "Adjudication",
                    "result": 2,
                    "status": "SUCCEEDED"
                },
                "_e905f02c-c5d9-4f2a-ba57-7912ff523b46": {
                    "messages": [],
                    "decision-id": "_e905f02c-c5d9-4f2a-ba57-7912ff523b46",
                    "decision-name": "Application risk score",
                    "result": 125,
                    "status": "SUCCEEDED"
                },
                "_ed60265c-25e2-400f-a99f-fafd3b489838": {
                    "messages": [],
                    "decision-id": "_ed60265c-25e2-400f-a99f-fafd3b489838",
                    "decision-name": "Pre-bureau affordability",
                    "result": false,
                    "status": "SUCCEEDED"
                },
                "_40b45659-9299-43a6-af30-04c948c5c0ec": {
                    "messages": [],
                    "decision-id": "_40b45659-9299-43a6-af30-04c948c5c0ec",
                    "decision-name": "Post-bureau risk category",
                    "result": "HIGH",
                    "status": "SUCCEEDED"
                },
                "_728e3a50-f00f-42c0-b3ee-1ee5aabd5474": {
                    "messages": [],
                    "decision-id": "_728e3a50-f00f-42c0-b3ee-1ee5aabd5474",
                    "decision-name": "Post-bureau affordability",
                    "result": false,
                    "status": "SUCCEEDED"
                },
                "_3c8cee68-99dd-418c-847d-0b54697354f2": {
                    "messages": [],
                    "decision-id": "_3c8cee68-99dd-418c-847d-0b54697354f2",
                    "decision-name": "Required monthly installment",
                    "result": 333.3636546143084985132842970339110,
                    "status": "SUCCEEDED"
                },
                "_ca1e6032-12eb-428a-a80b-49028a88c0b5": {
                    "messages": [],
                    "decision-id": "_ca1e6032-12eb-428a-a80b-49028a88c0b5",
                    "decision-name": "Routing",
                    "result": "DECLINE",
                    "status": "SUCCEEDED"
                },
                "_9997fcfd-0f50-4933-939e-88a235b5e2a0": {
                    "messages": [],
                    "decision-id": "_9997fcfd-0f50-4933-939e-88a235b5e2a0",
                    "decision-name": "Pre-bureau risk category",
                    "result": "LOW",
                    "status": "SUCCEEDED"
                },
                "_8b838f06-968a-4c66-875e-f5412fd692cf": {
                    "messages": [],
                    "decision-id": "_8b838f06-968a-4c66-875e-f5412fd692cf",
                    "decision-name": "Strategy",
                    "result": "DECLINE",
                    "status": "SUCCEEDED"
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Drools是一款规则引擎,可以用于规则的管理和应用。在Drools,function是一种自定义的函数,可以在规则调用。下面我将介绍如何编写一个复杂的function,并在实际场景应用它。 假设我们有一个场景,要对用户的订单进行风险评估,根据不同的风险级别进行不同的操作。我们需要编写一个复杂的function,计算订单的风险级别。 首先,我们定义一个Order类,包含订单的基本信息: ``` public class Order { private int orderId; private double amount; private int userId; // 省略getter和setter } ``` 接下来,我们定义一个函数,计算订单的风险级别。假设我们的风险评估算法如下: 1. 如果订单金额超过10000元,风险级别为高; 2. 如果订单金额超过5000元,风险级别为; 3. 如果订单金额超过1000元,并且订单用户的历史订单金额总和超过5000元,风险级别为; 4. 其他情况,风险级别为低。 根据上述算法,我们可以编写如下的函数: ``` public static String calculateRiskLevel(Order order, List<Order> historyOrders) { double amount = order.getAmount(); int userId = order.getUserId(); double totalAmount = historyOrders.stream() .filter(o -> o.getUserId() == userId) .mapToDouble(Order::getAmount) .sum(); if (amount > 10000) { return "HIGH"; } else if (amount > 5000 || (amount > 1000 && totalAmount > 5000)) { return "MEDIUM"; } else { return "LOW"; } } ``` 在上述函数,我们首先获取订单的金额和用户ID,然后根据历史订单计算用户的总订单金额,最后根据算法计算风险级别。 接下来,我们可以在Drools规则调用上述函数,进行风险评估。例如: ``` rule "Risk Assessment" when $order: Order(amount > 1000) then List<Order> historyOrders = getHistoryOrders($order.getUserId()); String riskLevel = calculateRiskLevel($order, historyOrders); if ("HIGH".equals(riskLevel)) { // 高风险操作 } else if ("MEDIUM".equals(riskLevel)) { // 风险操作 } else { // 低风险操作 } end ``` 在上述规则,我们首先获取订单的金额和用户ID,然后调用函数calculateRiskLevel计算风险级别,最后根据风险级别进行不同的操作。 以上就是一个复杂的function在实际场景的应用。通过自定义function,我们可以更灵活地应用Drools规则引擎,实现更复杂的业务逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值