[JAVA-14177] Renamed rule-engines to rule-enfines-modules (#12723)

Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
panos-kakos
2022-09-14 15:25:00 +01:00
committed by GitHub
parent 30f56aede0
commit b8a97cbebd
39 changed files with 9 additions and 9 deletions
@@ -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();
}
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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 + ")";
}
}
@@ -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 + ")";
}
}
@@ -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))))
@@ -0,0 +1,3 @@
;; Hello, world in Jess!
(printout t "Hello from Jess!" crlf)