rulebook rule engine example

Keywords: Java Maven Apache xml

Forced distribution is often used in performance management, such as:

As shown in the figure, this table is applicable to the number of departments with more than five people, for example:

Zhang San's department performance is A,
It belongs to A department,
There are 20 people in the Department,
Zhang San ranks the 8th in the Department;

According to this table, Zhang San belongs to class B employees and can be converted to 95 points;

 

However, if the number of departments is less than 6, the ranking rules are as follows:

 

Introduce rulebook rule engine, code structure:

pom.xml
 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.youway</groupId>
	<artifactId>kpi-rulebook</artifactId>
	<version>1.0-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<easy-rules.version>3.1.0</easy-rules.version>
		<slf4j.version>1.7.25</slf4j.version>
		<rulebook.version>0.9.1</rulebook.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>com.deliveredtechnologies</groupId>
			<artifactId>rulebook-core</artifactId>
			<version>${rulebook.version}</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>${slf4j.version}</version>
		</dependency>


		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.16.8</version>
			<scope>provided</scope>
		</dependency>

	</dependencies>


</project>

Rule execution test code Launcher.java

package com.youway;

import com.deliveredtechnologies.rulebook.NameValueReferableMap;
import com.deliveredtechnologies.rulebook.FactMap;
import com.deliveredtechnologies.rulebook.model.runner.RuleBookRunner;

/**
 * Rule execution test code
 * @author youway
 *
 */

public class Launcher {
  public static void main(String args[]) {
    RuleBookRunner ruleBook = new RuleBookRunner("com.youway.rules");  //Load all rules in the development package
    NameValueReferableMap facts = new FactMap();
    facts.setValue("num", 5);  //Department number: 5
    facts.setValue("deptLevel", "B");  //Department performance rating: B
    facts.setValue("rank", 3); //Rank of the employee in the department evaluation: 3
    ruleBook.run(facts);
    ruleBook.getResult().ifPresent(System.out::println);  //Print output of rule execution
  }
}

Result type of rule execution output Response.java

package com.youway.pojos;

import lombok.Data;

/**
 * Force distribution results based on the output of rules
 * @author youway
 *
 */
@Data
public class Response {
	
	public Response() {
		
	}
	
	public Response(String level, double score) {
		this.level = level;
		this.score = score;
	}

	//Grade
	private String level;
	//Conversion fraction
	private double score;
}

Forced distribution rule RuleParams.java

package com.youway.rules;

/**
 * Forced distribution rule
 * 
 * @author youway
 *
 */
public interface RuleParams {
	//Conversion fraction
	double SCORE_A = 100.00;
	double SCORE_B = 95.00;
	double SCORE_C = 95.00;
	double SCORE_D = 90.00;
	double SCORE_E = 85.00;
	
}

One person mandatory distribution rule LessRule1

package com.youway.rules;

import com.deliveredtechnologies.rulebook.RuleState;
import com.deliveredtechnologies.rulebook.annotation.Given;
import com.deliveredtechnologies.rulebook.annotation.Result;
import com.deliveredtechnologies.rulebook.annotation.Rule;
import com.deliveredtechnologies.rulebook.annotation.Then;
import com.deliveredtechnologies.rulebook.annotation.When;
import com.youway.pojos.Response;

/**
 * 1 Rules of forced distribution of people
 * @author youway
 *
 */
@Rule
public class LessRule1 implements RuleParams {
	// Number of employees in the Department
	@Given("num")
	private int num;
	// Department level
	@Given("deptLevel")
	private String deptLevel;
	// Department ranking
	@Given("rank")
	private int rank;

	@Result
	private Response response;

	@When
	public boolean when() {
		return num==1;
	}

	@Then
	public RuleState then() {
		System.out.println("LessRule1 Be executed......");
		if (deptLevel == "A") {
			this.response = new Response("A", SCORE_A);
		}
		if (deptLevel == "B") {
			this.response = new Response("B", SCORE_B);
		}
		if (deptLevel == "C") {
			this.response = new Response("C", SCORE_C);
		}
		if (deptLevel == "D") {
			this.response = new Response("D", SCORE_D);
		}
		if (deptLevel == "E") {
			this.response = new Response("E", SCORE_E);
		}
		
		return RuleState.BREAK;
	}
}

... other forced distribution rule structures are similar to this

Operation result:

LessRule5 Be executed......
Response(level=C, score=95.0)

Posted by jackthebookie on Thu, 02 Apr 2020 21:53:21 -0700