BAEL-1258 Added example code for Jess and JSR94 (#9693)

* BAEL-1258 Added example code for Jess and JSR94

* BAEL-1258 Relocate to rule-modules

* BAEL-1258 Remove README as per PR review

* šBAEL-1258 Declared exceptions and replace System.out.println with log statements

* BAEL-1258 Removed Lombok

* BAEL-1258 Removed Lombok

* BAEL-1258 Rename artifactId to match directory name
This commit is contained in:
Roger 2020-07-25 23:45:41 +10:00 committed by GitHub
parent 9f5396e448
commit 4128525f00
8 changed files with 256 additions and 0 deletions

25
rule-engines/jess/pom.xml Normal file
View File

@ -0,0 +1,25 @@
<?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.baeldung.rules.jess</groupId>
<artifactId>jess</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- https://mvnrepository.com/artifact/jsr94/jsr94 -->
<dependencies>
<dependency>
<groupId>jsr94</groupId>
<artifactId>jsr94</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>gov.sandia</groupId>
<artifactId>jess</artifactId>
<version>7.1p2</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,17 @@
package com.baeldung.rules.jess;
import jess.JessException;
import jess.Rete;
public class JessHello {
public static final String RULES_FILE = "helloJess.clp";
public static void main(String[] args) throws JessException {
Rete engine = new Rete();
engine.reset();
engine.batch(RULES_FILE);
engine.run();
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.rules.jess;
import com.baeldung.rules.jess.model.Answer;
import com.baeldung.rules.jess.model.Question;
import jess.Filter;
import jess.JessException;
import jess.Rete;
import java.util.Iterator;
import java.util.logging.Logger;
public class JessWithData {
public static final String RULES_BONUS_FILE = "bonus.clp";
private static Logger log = Logger.getLogger("JessWithData");
public static void main(String[] args) throws JessException {
Rete engine = new Rete();
engine.reset();
engine.batch(RULES_BONUS_FILE);
prepareData(engine);
engine.run();
checkResults(engine);
}
private static void checkResults(Rete engine) {
Iterator results = engine.getObjects(new Filter.ByClass(Answer.class));
while (results.hasNext()) {
Answer answer = (Answer) results.next();
log.info(answer.toString());
}
}
private static void prepareData(Rete engine) throws JessException {
Question question = new Question("Can I have a bonus?", -5);
log.info(question.toString());
engine.add(question);
}
}

View File

@ -0,0 +1,98 @@
package com.baeldung.rules.jess;
import com.baeldung.rules.jess.model.Answer;
import com.baeldung.rules.jess.model.Question;
import javax.rules.*;
import javax.rules.admin.RuleAdministrator;
import javax.rules.admin.RuleExecutionSet;
import javax.rules.admin.RuleExecutionSetCreateException;
import javax.rules.admin.RuleExecutionSetRegisterException;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
public class JessWithJsr94 {
public static final String RULES_BONUS_FILE = "/bonus.clp";
public static final String RULES_URI = "com/baeldung/rules/bonus";
private static final String RULE_SERVICE_PROVIDER = "jess.jsr94";
private static Logger log = Logger.getLogger("JessWithData");
public static void main(String[] args) throws Exception {
RuleServiceProvider ruleServiceProvider = instantiateJessInstance();
// load our rules and register them with the rules provider
registerRules(RULES_BONUS_FILE, RULES_URI, ruleServiceProvider);
runRules(RULES_URI, ruleServiceProvider);
}
private static RuleServiceProvider instantiateJessInstance() throws ClassNotFoundException, ConfigurationException {
// Load the rule service provider of the reference implementation.
// Loading this class will automatically register this provider with the provider manager.
Class.forName(RULE_SERVICE_PROVIDER + ".RuleServiceProviderImpl");
// Get the rule service provider from the provider manager.
return RuleServiceProviderManager.getRuleServiceProvider(RULE_SERVICE_PROVIDER);
}
private static void runRules(String rulesURI, RuleServiceProvider ruleServiceProvider)
throws ConfigurationException, RuleSessionTypeUnsupportedException, RuleSessionCreateException, RuleExecutionSetNotFoundException, RemoteException, InvalidRuleSessionException {
// Get a RuleRuntime and invoke the rule engine.
RuleRuntime ruleRuntime = ruleServiceProvider.getRuleRuntime();
//Create a statelessRuleSession.
StatelessRuleSession statelessRuleSession = (StatelessRuleSession) ruleRuntime.createRuleSession(rulesURI, new HashMap(), RuleRuntime.STATELESS_SESSION_TYPE);
calculateResults(statelessRuleSession);
statelessRuleSession.release();
}
private static void calculateResults(StatelessRuleSession statelessRuleSession) throws InvalidRuleSessionException, RemoteException {
List data = prepareData();
// execute the rules
List results = statelessRuleSession.executeRules(data);
checkResults(results);
}
private static List prepareData() {
List data = new ArrayList();
data.add(new Question("Can I have a bonus?", -5));
return data;
}
private static void checkResults(List results) {
Iterator itr = results.iterator();
while (itr.hasNext()) {
Object obj = itr.next();
if (obj instanceof Answer) {
log.info(obj.toString());
}
}
}
private static void registerRules(String rulesFile, String rulesURI, RuleServiceProvider serviceProvider) throws ConfigurationException, RuleExecutionSetCreateException, IOException, RuleExecutionSetRegisterException {
// Get the rule administrator.
RuleAdministrator ruleAdministrator = serviceProvider.getRuleAdministrator();
// load the rules
InputStream ruleInput = JessWithJsr94.class.getResourceAsStream(rulesFile);
HashMap vendorProperties = new HashMap();
// Create the RuleExecutionSet
RuleExecutionSet ruleExecutionSet = ruleAdministrator
.getLocalRuleExecutionSetProvider(vendorProperties)
.createRuleExecutionSet(ruleInput, vendorProperties);
// Register the rule execution set.
ruleAdministrator.registerRuleExecutionSet(rulesURI, ruleExecutionSet, vendorProperties);
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.rules.jess.model;
public class Answer {
private String answer;
private int newBalance;
public Answer(String answer, int newBalance) {
this.answer = answer;
this.newBalance = newBalance;
}
public int getNewBalance() {
return newBalance;
}
public void setNewBalance(int newBalance) {
this.newBalance = newBalance;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String toString() {
return "Answer(answer=" + answer + ", newBalance=" + newBalance + ")";
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.rules.jess.model;
public class Question {
private String question;
private int balance;
public Question(String question, int balance) {
this.question = question;
this.balance = balance;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public String toString() {
return "Question(question=" + question + ", balance=" + balance + ")";
}
}

View File

@ -0,0 +1,8 @@
(import com.baeldung.rules.jess.model.*)
(deftemplate Question (declare (from-class Question)))
(deftemplate Answer (declare (from-class Answer)))
(defrule avoid-overdraft "Give $50 to anyone overdrawn"
?q <- (Question { balance < 0 })
=>
(add (new Answer "Overdrawn bonus" (+ ?q.balance 50))))

View File

@ -0,0 +1,3 @@
;; Hello, world in Jess!
(printout t "Hello from Jess!" crlf)