BAEL-812: List of Rules Engines in Java (#2319)

* spring beans DI examples

* fix-1: shortening examples

* List of Rules Engines in Java

* BAEL-812: Openl-Tablets example added

* BAEL-812: artifacts names changed
This commit is contained in:
felipeazv 2017-08-16 21:58:42 +02:00 committed by adamd1985
parent 4226100ea9
commit dc105bc6f2
16 changed files with 337 additions and 0 deletions

15
easy-rules/pom.xml Normal file
View File

@ -0,0 +1,15 @@
<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.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-core</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</project>

View File

@ -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");
}
}

View File

@ -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);
}
}

23
openl-tablets/pom.xml Normal file
View File

@ -0,0 +1,23 @@
<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>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openl</groupId>
<artifactId>org.openl.core</artifactId>
<version>5.19.4</version>
</dependency>
<dependency>
<groupId>org.openl.rules</groupId>
<artifactId>org.openl.rules</artifactId>
<version>5.19.4</version>
</dependency>
</dependencies>
</project>

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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());
}
}

View File

@ -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;
}
}

15
rulebook/pom.xml Normal file
View File

@ -0,0 +1,15 @@
<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.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.deliveredtechnologies</groupId>
<artifactId>rulebook-core</artifactId>
<version>0.6.2</version>
</dependency>
</dependencies>
</project>

View File

@ -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();
}
}

View File

@ -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<>());
}
}

View File

@ -0,0 +1,70 @@
package com.baeldung.autowired;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
@SpringBootApplication
public class TypesOfBeanInjectionSpring {
private final UserService userService;
@Autowired // the @Autowired can even be omitted, in case there's only one explicit constructor
public TypesOfBeanInjectionSpring(UserService userService) {
this.userService = userService;
}
public static void main(String[] args) {
SpringApplication.run(TypesOfBeanInjectionSpring.class, args);
}
@Bean
CommandLineRunner runIt() {
return args -> {
userService.listUsers()
.stream()
.forEach(System.out::println);
};
}
}
class User {
private String name;
public User(String name) {
this.name = name;
}
// getters and setters ...
public String getName() {
return this.name;
}
public String toString() {
return name;
}
}
interface UserService {
List<User> listUsers();
}
@Service
class UserServiceImpl implements UserService {
@Override
public List<User> listUsers() {
ArrayList<User> users = new ArrayList<>(3);
users.add(new User("Snoopy"));
users.add(new User("Woodstock"));
users.add(new User("Charlie Brown"));
return users;
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.autowired;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TypesOfBeanInjectionSpringIntegrationTest {
@Autowired
UserService userService;
private static final String[] expected = new String[] { "Snoopy", "Woodstock", "Charlie Brown" };
@Test
public void givenDI_whenInjectObject_thenUserNamesAreListed() {
Assert.assertArrayEquals(expected, userService.listUsers()
.stream()
.map(User::getName)
.toArray());
}
}