This commit is contained in:
Jonathan Cook
2019-10-23 15:01:44 +02:00
parent db85c8f275
commit 684ec0d2e3
20486 changed files with 1642483 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
## Rule Engines
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)
+27
View File
@@ -0,0 +1,27 @@
<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>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../..</relativePath>
</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>
@@ -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>
+33
View File
@@ -0,0 +1,33 @@
<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>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../..</relativePath>
</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>
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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);
}
@@ -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());
}
}
@@ -0,0 +1,24 @@
package com.baeldung.openltablets.rules;
import java.util.HashMap;
import java.util.Map;
public class Response {
private String result;
private Map<String, String> map = new HashMap<>();
public Response() { }
public String getResult() {
return result;
}
public void setResult(final String s) {
result = s;
}
public Map<String, String> getMap() {
return map;
}
}
@@ -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>
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>
<artifactId>rule-engines</artifactId>
<name>rule-engines</name>
<packaging>pom</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<modules>
<module>easy-rules</module>
<module>openl-tablets</module>
<module>rulebook</module>
</modules>
</project>
+28
View File
@@ -0,0 +1,28 @@
<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.rulebook</groupId>
<artifactId>rulebook</artifactId>
<version>1.0</version>
<name>rulebook</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.deliveredtechnologies</groupId>
<artifactId>rulebook-core</artifactId>
<version>${rulebook.version}</version>
</dependency>
</dependencies>
<properties>
<rulebook.version>0.6.2</rulebook.version>
</properties>
</project>
@@ -0,0 +1,17 @@
package com.baeldung.rulebook;
import com.deliveredtechnologies.rulebook.lang.RuleBookBuilder;
import com.deliveredtechnologies.rulebook.model.RuleBook;
public class HelloWorldRule {
public RuleBook<Object> defineHelloWorldRules() {
return RuleBookBuilder.create()
.addRule(rule -> rule.withNoSpecifiedFactType()
.then(f -> System.out.print("Hello ")))
.addRule(rule -> rule.withNoSpecifiedFactType()
.then(f -> System.out.println("World")))
.build();
}
}
@@ -0,0 +1,12 @@
package com.baeldung.rulebook;
import com.deliveredtechnologies.rulebook.FactMap;
public class Launcher {
public static void main(String[] args) {
HelloWorldRule ruleBook = new HelloWorldRule();
ruleBook.defineHelloWorldRules()
.run(new FactMap<>());
}
}
@@ -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>