[JAVA-14177] Renamed rule-engines to rule-enfines-modules (#12723)
Co-authored-by: panagiotiskakos <[email protected]>
This commit is contained in:
committed by
GitHub
co-authored by
panagiotiskakos
parent
30f56aede0
commit
b8a97cbebd
@@ -0,0 +1,7 @@
|
||||
## Rule Engines Modules
|
||||
|
||||
This module contains articles about rule engines. Articles specific to a given rule engine go in the relevant submodule.
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [List of Rules Engines in Java](https://www.baeldung.com/java-rule-engines)
|
||||
@@ -0,0 +1,29 @@
|
||||
<?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.easyrules</groupId>
|
||||
<artifactId>easy-rules</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>easy-rules</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>rule-engines-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jeasy</groupId>
|
||||
<artifactId>easy-rules-core</artifactId>
|
||||
<version>${easy-rules.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<easy-rules.version>3.0.0</easy-rules.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.easyrules;
|
||||
|
||||
import org.jeasy.rules.annotation.Action;
|
||||
import org.jeasy.rules.annotation.Condition;
|
||||
import org.jeasy.rules.annotation.Rule;
|
||||
|
||||
@Rule(name = "Hello World rule", description = "Always say hello world")
|
||||
public class HelloWorldRule {
|
||||
|
||||
@Condition
|
||||
public boolean when() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Action
|
||||
public void then() throws Exception {
|
||||
System.out.println("hello world");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.easyrules;
|
||||
|
||||
import org.jeasy.rules.api.Facts;
|
||||
import org.jeasy.rules.api.Rules;
|
||||
import org.jeasy.rules.api.RulesEngine;
|
||||
import org.jeasy.rules.core.DefaultRulesEngine;
|
||||
|
||||
public class Launcher {
|
||||
public static void main(String... args) {
|
||||
// create facts
|
||||
Facts facts = new Facts();
|
||||
|
||||
// create rules
|
||||
Rules rules = new Rules();
|
||||
rules.register(new HelloWorldRule());
|
||||
|
||||
// create a rules engine and fire rules on known facts
|
||||
RulesEngine rulesEngine = new DefaultRulesEngine();
|
||||
rulesEngine.fire(rules, facts);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,3 @@
|
||||
## Relevant Articles:
|
||||
|
||||
- [Introduction to the Evrete Rule Engine](https://www.baeldung.com/java-evrete-rule-engine)
|
||||
@@ -0,0 +1,36 @@
|
||||
<?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.evrete</groupId>
|
||||
<artifactId>evrete</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>evrete</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>rule-engines-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- Core library -->
|
||||
<dependency>
|
||||
<groupId>org.evrete</groupId>
|
||||
<artifactId>evrete-core</artifactId>
|
||||
<version>${evrete.version}</version>
|
||||
</dependency>
|
||||
<!-- Annotated Java classes -->
|
||||
<dependency>
|
||||
<groupId>org.evrete</groupId>
|
||||
<artifactId>evrete-dsl-java</artifactId>
|
||||
<version>${evrete.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<evrete.version>2.1.04</evrete.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.evrete.introduction;
|
||||
|
||||
import com.baeldung.evrete.introduction.model.Customer;
|
||||
import com.baeldung.evrete.introduction.model.Invoice;
|
||||
import org.evrete.KnowledgeService;
|
||||
import org.evrete.api.Knowledge;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
|
||||
public class IntroductionAJR {
|
||||
public static void main(String[] args) throws IOException {
|
||||
ClassLoader classLoader = IntroductionAJR.class.getClassLoader();
|
||||
KnowledgeService service = new KnowledgeService();
|
||||
URL rulesetUrl = classLoader.getResource("rules/SalesRuleset.java");
|
||||
Knowledge knowledge = service.newKnowledge(
|
||||
"JAVA-SOURCE",
|
||||
rulesetUrl
|
||||
);
|
||||
|
||||
List<Customer> customers = Arrays.asList(
|
||||
new Customer("Customer A"),
|
||||
new Customer("Customer B"),
|
||||
new Customer("Customer C")
|
||||
);
|
||||
|
||||
Random random = new Random();
|
||||
Collection<Object> sessionData = new LinkedList<>(customers);
|
||||
for (int i = 0; i < 100_000; i++) {
|
||||
Customer randomCustomer = customers.get(random.nextInt(customers.size()));
|
||||
Invoice invoice = new Invoice(randomCustomer, 100 * random.nextDouble());
|
||||
sessionData.add(invoice);
|
||||
}
|
||||
|
||||
knowledge
|
||||
.newStatelessSession()
|
||||
.insert(sessionData)
|
||||
.fire();
|
||||
|
||||
for (Customer c : customers) {
|
||||
System.out.printf("%s:\t$%,.2f%n", c.getName(), c.getTotal());
|
||||
}
|
||||
|
||||
service.shutdown();
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.evrete.introduction;
|
||||
|
||||
import org.evrete.KnowledgeService;
|
||||
import org.evrete.api.Knowledge;
|
||||
import com.baeldung.evrete.introduction.model.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class IntroductionInline {
|
||||
public static void main(String[] args) {
|
||||
KnowledgeService service = new KnowledgeService();
|
||||
Knowledge knowledge = service
|
||||
.newKnowledge()
|
||||
.newRule("Clear total sales")
|
||||
.forEach("$c", Customer.class)
|
||||
.execute(ctx -> {
|
||||
Customer c = ctx.get("$c");
|
||||
c.setTotal(0.0);
|
||||
})
|
||||
.newRule("Compute totals")
|
||||
.forEach(
|
||||
"$c", Customer.class,
|
||||
"$i", Invoice.class
|
||||
)
|
||||
.where("$i.customer == $c")
|
||||
.execute(ctx -> {
|
||||
Customer c = ctx.get("$c");
|
||||
Invoice i = ctx.get("$i");
|
||||
c.addToTotal(i.getAmount());
|
||||
});
|
||||
|
||||
|
||||
List<Customer> customers = Arrays.asList(
|
||||
new Customer("Customer A"),
|
||||
new Customer("Customer B"),
|
||||
new Customer("Customer C")
|
||||
);
|
||||
|
||||
Random random = new Random();
|
||||
Collection<Object> sessionData = new LinkedList<>(customers);
|
||||
for (int i = 0; i < 100_000; i++) {
|
||||
Customer randomCustomer = customers.get(random.nextInt(customers.size()));
|
||||
Invoice invoice = new Invoice(randomCustomer, 100 * random.nextDouble());
|
||||
sessionData.add(invoice);
|
||||
}
|
||||
|
||||
knowledge
|
||||
.newStatelessSession()
|
||||
.insert(sessionData)
|
||||
.fire();
|
||||
|
||||
for (Customer c : customers) {
|
||||
System.out.printf("%s:\t$%,.2f%n", c.getName(), c.getTotal());
|
||||
}
|
||||
|
||||
service.shutdown();
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.evrete.introduction.model;
|
||||
|
||||
public class Customer {
|
||||
private double total = 0.0;
|
||||
private final String name;
|
||||
|
||||
public Customer(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void addToTotal(double amount) {
|
||||
this.total += amount;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public double getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(double total) {
|
||||
this.total = total;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.evrete.introduction.model;
|
||||
|
||||
public class Invoice {
|
||||
private final Customer customer;
|
||||
private final double amount;
|
||||
|
||||
public Invoice(Customer customer, double amount) {
|
||||
this.customer = customer;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public Customer getCustomer() {
|
||||
return customer;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.abc.author1;
|
||||
|
||||
import com.baeldung.evrete.introduction.model.Customer;
|
||||
import com.baeldung.evrete.introduction.model.Invoice;
|
||||
import org.evrete.dsl.annotation.Rule;
|
||||
import org.evrete.dsl.annotation.Where;
|
||||
|
||||
public class SalesRuleset {
|
||||
|
||||
@Rule
|
||||
public void rule1(Customer $c) {
|
||||
$c.setTotal(0.0);
|
||||
}
|
||||
|
||||
@Rule
|
||||
@Where("$i.customer == $c")
|
||||
public void rule2(Customer $c, Invoice $i) {
|
||||
$c.addToTotal($i.getAmount());
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.evrete.introduction;
|
||||
|
||||
import com.baeldung.evrete.introduction.model.Customer;
|
||||
import com.baeldung.evrete.introduction.model.Invoice;
|
||||
import org.evrete.KnowledgeService;
|
||||
import org.evrete.api.Knowledge;
|
||||
import org.evrete.api.RuleSession;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
class IntroductionAJRUnitTest {
|
||||
private static KnowledgeService service;
|
||||
|
||||
@BeforeAll
|
||||
static void setUpClass() {
|
||||
service = new KnowledgeService();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void shutDownClass() {
|
||||
service.shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* This test makes sure that each customer's actual total sales is equal to the amount
|
||||
* computed by the rule engine
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"true", "false"})
|
||||
void sessionTotalsTest(String type) throws IOException {
|
||||
boolean stateful = Boolean.parseBoolean(type);
|
||||
ClassLoader classLoader = IntroductionAJR.class.getClassLoader();
|
||||
KnowledgeService service = new KnowledgeService();
|
||||
Knowledge knowledge = service
|
||||
.newKnowledge(
|
||||
"JAVA-SOURCE",
|
||||
classLoader.getResource("rules/SalesRuleset.java")
|
||||
);
|
||||
|
||||
|
||||
List<Customer> customers = Arrays.asList(
|
||||
new Customer("Customer A"),
|
||||
new Customer("Customer B"),
|
||||
new Customer("Customer C")
|
||||
);
|
||||
Collection<Object> sessionData = new LinkedList<>(customers);
|
||||
|
||||
HashMap<Customer, Double> actualTotals = new HashMap<>();
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < 1_000; i++) {
|
||||
Customer randomCustomer = customers.get(random.nextInt(customers.size()));
|
||||
Invoice invoice = new Invoice(randomCustomer, random.nextInt(100));
|
||||
sessionData.add(invoice);
|
||||
|
||||
Double d = actualTotals.get(randomCustomer);
|
||||
if(d == null) {
|
||||
d = 0.0;
|
||||
}
|
||||
d = d + invoice.getAmount();
|
||||
actualTotals.put(randomCustomer, d);
|
||||
}
|
||||
|
||||
RuleSession<?> session = stateful ? knowledge.newStatefulSession() : knowledge.newStatelessSession();
|
||||
session
|
||||
.insert(sessionData)
|
||||
.fire();
|
||||
|
||||
for(Customer c : customers) {
|
||||
double d1 = c.getTotal();
|
||||
double d2 = actualTotals.get(c);
|
||||
assert d1 == d2;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package com.baeldung.evrete.introduction;
|
||||
|
||||
import com.baeldung.evrete.introduction.model.Customer;
|
||||
import com.baeldung.evrete.introduction.model.Invoice;
|
||||
import org.evrete.KnowledgeService;
|
||||
import org.evrete.api.Knowledge;
|
||||
import org.evrete.api.RuleSession;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class IntroductionInlineUnitTest {
|
||||
private static KnowledgeService service;
|
||||
|
||||
@BeforeAll
|
||||
static void setUpClass() {
|
||||
service = new KnowledgeService();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void shutDownClass() {
|
||||
service.shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* This test makes sure that each customer's actual total sales is equal to the amount
|
||||
* computed by the rule engine
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"true", "false"})
|
||||
void sessionTotalsTest(String type) {
|
||||
boolean stateful = Boolean.parseBoolean(type);
|
||||
Knowledge knowledge = service
|
||||
.newKnowledge()
|
||||
.newRule("Clear customer's total sales")
|
||||
.forEach("$c", Customer.class)
|
||||
.execute(ctx -> {
|
||||
Customer c = ctx.get("$c");
|
||||
c.setTotal(0.0);
|
||||
})
|
||||
.newRule("Compute totals")
|
||||
.forEach(
|
||||
"$c", Customer.class,
|
||||
"$i", Invoice.class
|
||||
)
|
||||
.where("$i.customer == $c")
|
||||
.execute(ctx -> {
|
||||
Customer c = ctx.get("$c");
|
||||
Invoice i = ctx.get("$i");
|
||||
c.addToTotal(i.getAmount());
|
||||
});
|
||||
|
||||
|
||||
List<Customer> customers = Arrays.asList(
|
||||
new Customer("Customer A"),
|
||||
new Customer("Customer B"),
|
||||
new Customer("Customer C")
|
||||
);
|
||||
Collection<Object> sessionData = new LinkedList<>(customers);
|
||||
|
||||
HashMap<Customer, Double> actualTotals = new HashMap<>();
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < 1_000; i++) {
|
||||
Customer randomCustomer = customers.get(random.nextInt(customers.size()));
|
||||
Invoice invoice = new Invoice(randomCustomer, random.nextInt(100));
|
||||
sessionData.add(invoice);
|
||||
|
||||
Double d = actualTotals.get(randomCustomer);
|
||||
if(d == null) {
|
||||
d = 0.0;
|
||||
}
|
||||
d = d + invoice.getAmount();
|
||||
actualTotals.put(randomCustomer, d);
|
||||
}
|
||||
|
||||
RuleSession<?> session = stateful ? knowledge.newStatefulSession() : knowledge.newStatelessSession();
|
||||
session
|
||||
.insert(sessionData)
|
||||
.fire();
|
||||
|
||||
for(Customer c : customers) {
|
||||
double d1 = c.getTotal();
|
||||
double d2 = actualTotals.get(c);
|
||||
assert d1 == d2;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Jess Rule Engine and JSR 94](https://www.baeldung.com/java-rule-engine-jess-jsr-94)
|
||||
@@ -0,0 +1,30 @@
|
||||
<?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>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>jsr94</groupId>
|
||||
<artifactId>jsr94</artifactId>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
<!-- the below be available after we download and unpack jess.jar and mvn install it into our
|
||||
local m2, compile errors with this project would go away then <dependency> <groupId>gov.sandia</groupId>
|
||||
<artifactId>jess</artifactId> <version>7.1p2</version> </dependency> -->
|
||||
</dependencies>
|
||||
|
||||
<!-- include repository http://dist.wso2.org/maven2 for jsr94 -->
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>wso2</id>
|
||||
<name>Numerical Method's Maven Repository</name>
|
||||
<url>http://dist.wso2.org/maven2/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
||||
@@ -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)
|
||||
@@ -0,0 +1,34 @@
|
||||
<?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.openltablets</groupId>
|
||||
<artifactId>openl-tablets</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>openl-tablets</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>rule-engines-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.openl</groupId>
|
||||
<artifactId>org.openl.core</artifactId>
|
||||
<version>${openl.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openl.rules</groupId>
|
||||
<artifactId>org.openl.rules</artifactId>
|
||||
<version>${openl.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<openl.version>5.19.4</openl.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.openltablets.model;
|
||||
|
||||
public class Case {
|
||||
|
||||
private User user;
|
||||
private int hourOfDay;
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(final User user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public int getHourOfDay() {
|
||||
return hourOfDay;
|
||||
}
|
||||
|
||||
public void setHourOfDay(final int hourOfDay) {
|
||||
this.hourOfDay = hourOfDay;
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.openltablets.model;
|
||||
|
||||
public enum Greeting {
|
||||
|
||||
GOOD_MORNING("Good Morning"),
|
||||
GOOD_AFTERNOON("Good Afternoon"),
|
||||
GOOD_EVENING("Good Evening"),
|
||||
GOOD_NIGHT("Good Night");
|
||||
|
||||
private final String literal;
|
||||
|
||||
private Greeting(final String literal) {
|
||||
this.literal = literal;
|
||||
}
|
||||
|
||||
public String getLiteral() {
|
||||
return literal;
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.openltablets.model;
|
||||
|
||||
public class User {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.openltablets.rules;
|
||||
|
||||
import com.baeldung.openltablets.model.Case;
|
||||
|
||||
public interface IRule {
|
||||
void helloUser(final Case aCase, final Response response);
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.openltablets.rules;
|
||||
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.openl.rules.runtime.RulesEngineFactory;
|
||||
import org.openl.runtime.EngineFactory;
|
||||
|
||||
import com.baeldung.openltablets.model.Case;
|
||||
import com.baeldung.openltablets.model.User;
|
||||
|
||||
public class Main {
|
||||
private IRule instance;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Main rules = new Main();
|
||||
User user = new User();
|
||||
user.setName("Donald Duck");
|
||||
Case aCase = new Case();
|
||||
aCase.setUser(user);
|
||||
aCase.setHourOfDay(23);
|
||||
rules.process(aCase);
|
||||
}
|
||||
|
||||
public void process(Case aCase) {
|
||||
final EngineFactory<IRule> engineFactory = new RulesEngineFactory<IRule>(getClass().getClassLoader()
|
||||
.getResource("openltablets/HelloUser.xls"), IRule.class);
|
||||
instance = engineFactory.newEngineInstance();
|
||||
instance.helloUser(aCase, new Response());
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user