Drools7.5.0教程(5)-workbench规则开发与测试

本文介绍如何使用Drools 7.4.1进行考试评分规则的开发与测试。通过创建ScoreRule实体类及对应的评分规则,实现了不同分数段的成绩评定,并提供了测试场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Drools7.4.1教程(5)-workbench规则开发与测试

drools,是由JBOSS公司开源的一套基于JAVA的规则引擎系统

规则进行时

假设我们现在要做一个考试评分的规则,60-70分以上为及格,70-90分以上良好,90-100分为优秀

  1. 新建数据对象
    由于其实规则项目就是一个maven项目,所以我们先新建一个软件包,再建立一个数据对象

    package com.myteam.demo.entity;
    
    /**
     * This class was automatically generated by the data modeler tool.
     */
    
    @javax.persistence.Entity
    public class ScoreRule implements java.io.Serializable {
    
        static final long serialVersionUID = 1L;
    
        @javax.persistence.GeneratedValue(generator = "SCORERULE_ID_GENERATOR", strategy = javax.persistence.GenerationType.AUTO)
        @javax.persistence.Id
        @javax.persistence.SequenceGenerator(name = "SCORERULE_ID_GENERATOR", sequenceName = "SCORERULE_ID_SEQ")
            private java.lang.Long id;
    
            //分数
            private int score;
    
            //结果
            private String result;
    
            public ScoreRule() {
            }
    
            public ScoreRule(java.lang.Long id) {
                this.id = id;
            }
    
            public java.lang.Long getId() {
                return this.id;
            }
    
            public void setId(java.lang.Long id) {
                this.id = id;
            }
    
            public int getScore() {
                return score;
            }
    
            public void setScore(int score) {
                this.score = score;
            }
    
            public String getResult() {
                return result;
            }
    
            public void setResult(String result) {
                this.result = result;
            }
    }
  2. 再建立一个规则,直接贴代码

        package com.myteam.demo.rules;
    
    import com.myteam.demo.entity.ScoreRule;
    
    no-loop
    
    rule "excellent"
        when 
            s:ScoreRule(score>=90&&score<=100)
        then
            System.out.println("excellent");
            modify(s) { setResult("excellent") };
    end
    
    
    rule "good"
        when 
            s:ScoreRule(score>=70&&score<90)
        then
            System.out.println("good");
            modify(s) { setResult("good") };
    end
    
    
    rule "pass"
        when 
            s:ScoreRule(score>=60&&score<70)
        then
            System.out.println("pass");
            modify(s) { setResult("pass") };
    end
    
    rule "nopass"
        when 
            s:ScoreRule(score<60)
        then
            System.out.println("nopass");
            modify(s) { setResult("nopass") };
    end
  3. 新建一个测试场景
    失败截图
    成功截图

如果大家觉得好,哈哈,可以给我点鼓励,谢谢大家。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值