diff --git a/rule-engines/jess/pom.xml b/rule-engines/jess/pom.xml new file mode 100644 index 0000000000..40d50fae70 --- /dev/null +++ b/rule-engines/jess/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + com.baeldung.rules.jess + jess + 1.0-SNAPSHOT + + + + + jsr94 + jsr94 + 1.1 + + + gov.sandia + jess + 7.1p2 + + + + \ No newline at end of file diff --git a/rule-engines/jess/src/main/java/com/baeldung/rules/jess/JessHello.java b/rule-engines/jess/src/main/java/com/baeldung/rules/jess/JessHello.java new file mode 100644 index 0000000000..1462996a4c --- /dev/null +++ b/rule-engines/jess/src/main/java/com/baeldung/rules/jess/JessHello.java @@ -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(); + } +} diff --git a/rule-engines/jess/src/main/java/com/baeldung/rules/jess/JessWithData.java b/rule-engines/jess/src/main/java/com/baeldung/rules/jess/JessWithData.java new file mode 100644 index 0000000000..5d5dc9b983 --- /dev/null +++ b/rule-engines/jess/src/main/java/com/baeldung/rules/jess/JessWithData.java @@ -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); + } +} diff --git a/rule-engines/jess/src/main/java/com/baeldung/rules/jess/JessWithJsr94.java b/rule-engines/jess/src/main/java/com/baeldung/rules/jess/JessWithJsr94.java new file mode 100644 index 0000000000..f07bf10a7a --- /dev/null +++ b/rule-engines/jess/src/main/java/com/baeldung/rules/jess/JessWithJsr94.java @@ -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); + } +} \ No newline at end of file diff --git a/rule-engines/jess/src/main/java/com/baeldung/rules/jess/model/Answer.java b/rule-engines/jess/src/main/java/com/baeldung/rules/jess/model/Answer.java new file mode 100644 index 0000000000..d690d04e13 --- /dev/null +++ b/rule-engines/jess/src/main/java/com/baeldung/rules/jess/model/Answer.java @@ -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 + ")"; + } +} diff --git a/rule-engines/jess/src/main/java/com/baeldung/rules/jess/model/Question.java b/rule-engines/jess/src/main/java/com/baeldung/rules/jess/model/Question.java new file mode 100644 index 0000000000..499113d0fc --- /dev/null +++ b/rule-engines/jess/src/main/java/com/baeldung/rules/jess/model/Question.java @@ -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 + ")"; + } + +} diff --git a/rule-engines/jess/src/main/resources/bonus.clp b/rule-engines/jess/src/main/resources/bonus.clp new file mode 100644 index 0000000000..7f430bc43f --- /dev/null +++ b/rule-engines/jess/src/main/resources/bonus.clp @@ -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)))) diff --git a/rule-engines/jess/src/main/resources/helloJess.clp b/rule-engines/jess/src/main/resources/helloJess.clp new file mode 100644 index 0000000000..547294eca6 --- /dev/null +++ b/rule-engines/jess/src/main/resources/helloJess.clp @@ -0,0 +1,3 @@ +;; Hello, world in Jess! + +(printout t "Hello from Jess!" crlf)