Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
35a90d9fe6
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.exceptions;
|
||||
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
||||
public class StackTraceToString {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Convert a StackTrace to String using core java
|
||||
try {
|
||||
throw new NullPointerException();
|
||||
} catch (Exception e) {
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
e.printStackTrace(pw);
|
||||
|
||||
System.out.println(sw.toString());
|
||||
}
|
||||
|
||||
// Convert a StackTrace to String using Apache Commons
|
||||
try {
|
||||
throw new IndexOutOfBoundsException();
|
||||
} catch (Exception e) {
|
||||
System.out.println(ExceptionUtils.getStackTrace(e));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,4 @@
|
|||
package com.baeldung.java8.comparator;
|
||||
/*
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
@ -22,4 +20,3 @@ public class Employee implements Comparable<Employee>{
|
|||
}
|
||||
|
||||
}
|
||||
*/
|
|
@ -1,5 +1,4 @@
|
|||
package com.baeldung.java8.comparator;
|
||||
/*
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
|
@ -164,5 +163,3 @@ public class Java8ComparatorTest {
|
|||
|
||||
|
||||
}
|
||||
|
||||
*/
|
|
@ -0,0 +1,70 @@
|
|||
package org.baeldung.guava;
|
||||
import com.google.common.collect.ClassToInstanceMap;
|
||||
import com.google.common.collect.ImmutableClassToInstanceMap;
|
||||
import com.google.common.collect.MutableClassToInstanceMap;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ClassToInstanceMapTests {
|
||||
@Test
|
||||
public void createEmptyImmutableMap() {
|
||||
ClassToInstanceMap<Action> map = ImmutableClassToInstanceMap.of();
|
||||
assertTrue(map.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createEmptyMutableMap() {
|
||||
ClassToInstanceMap<Action> map = MutableClassToInstanceMap.create();
|
||||
assertTrue(map.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createSingleEntryMap() {
|
||||
ClassToInstanceMap<Action> map = ImmutableClassToInstanceMap.of(Save.class, new Save());
|
||||
assertEquals(1, map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createMapWithBuilder() {
|
||||
ClassToInstanceMap<Action> map = ImmutableClassToInstanceMap.<Action>builder()
|
||||
.put(Save.class, new Save())
|
||||
.put(Open.class, new Open())
|
||||
.build();
|
||||
assertEquals(2, map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnElement() {
|
||||
ClassToInstanceMap<Action> map = ImmutableClassToInstanceMap.of(Save.class, new Save());
|
||||
Action action = map.get(Save.class);
|
||||
assertTrue(action instanceof Save);
|
||||
|
||||
// Use getInstance to avoid casting
|
||||
Save save = map.getInstance(Save.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPutElement() {
|
||||
ClassToInstanceMap<Action> map = MutableClassToInstanceMap.create();
|
||||
map.put(Save.class, new Save());
|
||||
// Put again to get previous value returned
|
||||
Action action = map.put(Save.class, new Save());
|
||||
assertTrue(action instanceof Save);
|
||||
|
||||
// Use putInstance to avoid casting
|
||||
Save save = map.putInstance(Save.class, new Save());
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Action {
|
||||
}
|
||||
|
||||
class Save extends Action {
|
||||
}
|
||||
|
||||
class Open extends Action {
|
||||
}
|
||||
|
||||
class Delete extends Action {
|
||||
}
|
|
@ -9,7 +9,7 @@ import java.util.stream.IntStream;
|
|||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class GauavaStreamsTests {
|
||||
public class GuavaStreamsTests {
|
||||
|
||||
List<Integer> numbers;
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?><root>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</root>
|
|
@ -251,6 +251,11 @@
|
|||
<artifactId>h2</artifactId>
|
||||
<version>1.4.194</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.datanucleus</groupId>
|
||||
<artifactId>datanucleus-xml</artifactId>
|
||||
<version>5.0.0-release</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -20,22 +20,29 @@ public class GuideToJDO {
|
|||
private static final Logger LOGGER = Logger.getLogger(GuideToJDO.class.getName());
|
||||
private Random rnd = new Random();
|
||||
private PersistenceUnitMetaData pumd;
|
||||
private PersistenceUnitMetaData pumdXML;
|
||||
|
||||
public static void main(String[] args) {
|
||||
new GuideToJDO();
|
||||
}
|
||||
|
||||
public GuideToJDO() {
|
||||
CreateProperties();
|
||||
CreateH2Properties();
|
||||
CreateXMLProperties();
|
||||
CreateProducts();
|
||||
ListProducts();
|
||||
QueryJDOQL();
|
||||
QuerySQL();
|
||||
QueryJPQL();
|
||||
UpdateProducts();
|
||||
ListProducts();
|
||||
DeleteProducts();
|
||||
ListProducts();
|
||||
persistXML();
|
||||
listXMLProducts();
|
||||
}
|
||||
|
||||
public void CreateProperties(){
|
||||
public void CreateH2Properties(){
|
||||
|
||||
pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
|
||||
pumd.addClassName("com.baeldung.jdo.Product");
|
||||
|
@ -48,6 +55,14 @@ public class GuideToJDO {
|
|||
|
||||
}
|
||||
|
||||
public void CreateXMLProperties(){
|
||||
pumdXML = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
|
||||
pumdXML.addClassName("com.baeldung.jdo.ProductXML");
|
||||
pumdXML.setExcludeUnlistedClasses();
|
||||
pumdXML.addProperty("javax.jdo.option.ConnectionURL", "xml:file:myPersistence.xml");
|
||||
pumdXML.addProperty("datanucleus.autoCreateSchema", "true");
|
||||
}
|
||||
|
||||
public void CreateProducts() {
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
|
@ -140,4 +155,145 @@ public class GuideToJDO {
|
|||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void QueryJDOQL (){
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
|
||||
// Declarative JDOQL :
|
||||
LOGGER.log(Level.INFO, "Declarative JDOQL --------------------------------------------------------------");
|
||||
Query qDJDOQL = pm.newQuery(Product.class);
|
||||
qDJDOQL.setFilter("name == 'Tablet' && price == price_value");
|
||||
qDJDOQL.declareParameters("double price_value");
|
||||
List<Product> resultsqDJDOQL = qDJDOQL.setParameters(80.0).executeList();
|
||||
|
||||
Iterator<Product> iterDJDOQL = resultsqDJDOQL.iterator();
|
||||
while (iterDJDOQL.hasNext()) {
|
||||
Product p = iterDJDOQL.next();
|
||||
LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price });
|
||||
}
|
||||
LOGGER.log(Level.INFO, "--------------------------------------------------------------");
|
||||
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void QuerySQL (){
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
|
||||
//SQL :
|
||||
LOGGER.log(Level.INFO, "SQL --------------------------------------------------------------");
|
||||
Query query = pm.newQuery("javax.jdo.query.SQL", "SELECT * FROM PRODUCT");
|
||||
query.setClass(Product.class);
|
||||
List<Product> results = query.executeList();
|
||||
|
||||
Iterator<Product> iter = results.iterator();
|
||||
while (iter.hasNext()) {
|
||||
Product p = iter.next();
|
||||
LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price });
|
||||
}
|
||||
LOGGER.log(Level.INFO, "--------------------------------------------------------------");
|
||||
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void QueryJPQL (){
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
|
||||
//JPQL :
|
||||
LOGGER.log(Level.INFO, "JPQL --------------------------------------------------------------");
|
||||
Query q = pm.newQuery("JPQL", "SELECT p FROM "+Product.class.getName()+" p WHERE p.name = 'Laptop'");
|
||||
List results = (List)q.execute();
|
||||
|
||||
Iterator<Product> iter = results.iterator();
|
||||
while (iter.hasNext()) {
|
||||
Product p = iter.next();
|
||||
LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price });
|
||||
}
|
||||
LOGGER.log(Level.INFO, "--------------------------------------------------------------");
|
||||
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void persistXML(){
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumdXML, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
ProductXML productXML = new ProductXML(0,"Tablet", 80.0);
|
||||
pm.makePersistent(productXML);
|
||||
ProductXML productXML2 = new ProductXML(1,"Phone", 20.0);
|
||||
pm.makePersistent(productXML2);
|
||||
ProductXML productXML3 = new ProductXML(2,"Laptop", 200.0);
|
||||
pm.makePersistent(productXML3);
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void listXMLProducts(){
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumdXML, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
|
||||
Query q = pm.newQuery("SELECT FROM " + ProductXML.class.getName());
|
||||
List<ProductXML> products = (List<ProductXML>) q.execute();
|
||||
Iterator<ProductXML> iter = products.iterator();
|
||||
while (iter.hasNext()) {
|
||||
ProductXML p = iter.next();
|
||||
LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.getName(), p.getPrice() });
|
||||
pm.deletePersistent(p);
|
||||
}
|
||||
LOGGER.log(Level.INFO, "--------------------------------------------------------------");
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.jdo;
|
||||
|
||||
import javax.jdo.annotations.PersistenceCapable;
|
||||
import javax.jdo.annotations.PrimaryKey;
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
|
||||
@PersistenceCapable()
|
||||
public class ProductXML {
|
||||
|
||||
@XmlAttribute
|
||||
private long productNumber = 0;
|
||||
@PrimaryKey
|
||||
private String name = null;
|
||||
private Double price = 0.0;
|
||||
|
||||
public ProductXML() {
|
||||
this.productNumber = 0;
|
||||
this.name = null;
|
||||
this.price = 0.0;
|
||||
}
|
||||
|
||||
public ProductXML(long productNumber, String name, Double price) {
|
||||
this.productNumber = productNumber;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
}
|
1
pom.xml
1
pom.xml
|
@ -212,6 +212,7 @@
|
|||
<module>spring-data-gemfire</module>
|
||||
<module>cucumber</module>
|
||||
<module>mybatis</module>
|
||||
<module>spring-drools</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,130 @@
|
|||
<?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</groupId>
|
||||
<artifactId>spring-drools</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring-boot-version>1.1.1.RELEASE</spring-boot-version>
|
||||
<http-component-version>4.0-alpha6</http-component-version>
|
||||
<drools-version>7.0.0.CR1</drools-version>
|
||||
<apache-poi-version>3.13</apache-poi-version>
|
||||
<surefire-plugin-version>2.19.1</surefire-plugin-version>
|
||||
</properties>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.spring.platform</groupId>
|
||||
<artifactId>platform-bom</artifactId>
|
||||
<version>${spring-boot-version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>${http-component-version}</version>
|
||||
</dependency>
|
||||
<!-- ... -->
|
||||
<dependency>
|
||||
<groupId>org.kie</groupId>
|
||||
<artifactId>kie-ci</artifactId>
|
||||
<version>${drools-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.drools</groupId>
|
||||
<artifactId>drools-decisiontables</artifactId>
|
||||
<version>${drools-version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.drools</groupId>
|
||||
<artifactId>drools-core</artifactId>
|
||||
<version>${drools-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.drools</groupId>
|
||||
<artifactId>drools-compiler</artifactId>
|
||||
<version>${drools-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi</artifactId>
|
||||
<version>${apache-poi-version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<version>${apache-poi-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.kie</groupId>
|
||||
<artifactId>kie-spring</artifactId>
|
||||
<version>6.4.0.Final</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire-plugin-version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.spring.drools;
|
||||
|
||||
|
||||
import com.baeldung.spring.drools.model.Applicant;
|
||||
import com.baeldung.spring.drools.model.Product;
|
||||
import com.baeldung.spring.drools.model.SuggestedRole;
|
||||
import com.baeldung.spring.drools.service.ApplicantService;
|
||||
import com.baeldung.spring.drools.service.ProductService;
|
||||
import org.kie.api.KieServices;
|
||||
import org.kie.api.runtime.KieContainer;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@SpringBootApplication
|
||||
@Import(DroolConfiguration.class)
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext ctx = SpringApplication.run(Application.class, args);
|
||||
ApplicantService applicantService=(ApplicantService)ctx.getBean("applicantService");
|
||||
applicantService.suggestARoleForApplicant(new Applicant("Baljeet",37,1200000.0,8),new SuggestedRole());
|
||||
|
||||
ProductService productService=(ProductService)ctx.getBean("productService");
|
||||
Product returnedProduct=productService.applyLabelToProduct(new Product("Microwave","Book"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
package com.baeldung.spring.drools;
|
||||
|
||||
import com.baeldung.spring.drools.service.ApplicantService;
|
||||
import com.baeldung.spring.drools.service.ProductService;
|
||||
import org.kie.api.KieBase;
|
||||
import org.kie.api.KieServices;
|
||||
import org.kie.api.builder.*;
|
||||
import org.kie.api.runtime.KieContainer;
|
||||
import org.kie.api.runtime.KieSession;
|
||||
import org.kie.internal.io.ResourceFactory;
|
||||
import org.kie.spring.KModuleBeanFactoryPostProcessor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Configuration
|
||||
public class DroolConfiguration {
|
||||
|
||||
private static final String RULES_PATH = "com/baeldung/spring/drools/rules/";
|
||||
|
||||
@Bean
|
||||
public KieFileSystem kieFileSystem() throws IOException {
|
||||
KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
|
||||
for (Resource file : getRuleFiles()) {
|
||||
kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.getFilename(), "UTF-8"));
|
||||
}
|
||||
return kieFileSystem;
|
||||
}
|
||||
|
||||
private Resource[] getRuleFiles() throws IOException {
|
||||
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
|
||||
return resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public KieContainer kieContainer() throws IOException {
|
||||
final KieRepository kieRepository = getKieServices().getRepository();
|
||||
|
||||
kieRepository.addKieModule(new KieModule() {
|
||||
public ReleaseId getReleaseId() {
|
||||
return kieRepository.getDefaultReleaseId();
|
||||
}
|
||||
});
|
||||
|
||||
KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());
|
||||
kieBuilder.buildAll();
|
||||
|
||||
return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());
|
||||
}
|
||||
|
||||
private KieServices getKieServices() {
|
||||
return KieServices.Factory.get();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public KieBase kieBase() throws IOException {
|
||||
return kieContainer().getKieBase();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public KieSession kieSession() throws IOException {
|
||||
return kieContainer().newKieSession();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ApplicantService getApplicantService(){
|
||||
return new ApplicantService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ProductService getProductService(KieContainer kieContainer){
|
||||
return new ProductService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public KModuleBeanFactoryPostProcessor kiePostProcessor() {
|
||||
return new KModuleBeanFactoryPostProcessor();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.spring.drools.model;
|
||||
|
||||
public class Applicant {
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
private double currentSalary;
|
||||
private int experienceInYears;
|
||||
|
||||
public Applicant(String name, int age, Double currentSalary, int experienceInYears) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.currentSalary = currentSalary;
|
||||
this.experienceInYears = experienceInYears;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Double getCurrentSalary() {
|
||||
return currentSalary;
|
||||
}
|
||||
|
||||
public void setCurrentSalary(Double currentSalary) {
|
||||
this.currentSalary = currentSalary;
|
||||
}
|
||||
|
||||
public int getExperienceInYears() {
|
||||
return experienceInYears;
|
||||
}
|
||||
|
||||
public void setExperienceInYears(int experienceInYears) {
|
||||
this.experienceInYears = experienceInYears;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.spring.drools.model;
|
||||
|
||||
public class Product {
|
||||
private String name;
|
||||
private String type;
|
||||
|
||||
private String label;
|
||||
|
||||
public Product(String name, String type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.spring.drools.model;
|
||||
|
||||
public class SuggestedRole {
|
||||
|
||||
private String role;
|
||||
|
||||
public String getRole() {
|
||||
return role;
|
||||
}
|
||||
|
||||
public void setRole(String role) {
|
||||
this.role = role;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.spring.drools.service;
|
||||
|
||||
import com.baeldung.spring.drools.model.Applicant;
|
||||
import com.baeldung.spring.drools.model.SuggestedRole;
|
||||
import org.kie.api.runtime.KieContainer;
|
||||
import org.kie.api.runtime.KieSession;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ApplicantService {
|
||||
|
||||
@Autowired
|
||||
private KieContainer kieContainer;
|
||||
|
||||
public SuggestedRole suggestARoleForApplicant(Applicant applicant,SuggestedRole suggestedRole){
|
||||
KieSession kieSession = kieContainer.newKieSession();
|
||||
kieSession.insert(applicant);
|
||||
kieSession.setGlobal("suggestedRole",suggestedRole);
|
||||
kieSession.fireAllRules();
|
||||
System.out.println(suggestedRole.getRole());
|
||||
return suggestedRole;
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.spring.drools.service;
|
||||
|
||||
import com.baeldung.spring.drools.model.Product;
|
||||
import org.kie.api.runtime.KieContainer;
|
||||
import org.kie.api.runtime.KieSession;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ProductService {
|
||||
|
||||
@Autowired
|
||||
private KieContainer kieContainer;
|
||||
|
||||
public Product applyLabelToProduct(Product product){
|
||||
KieSession kieSession = kieContainer.newKieSession();
|
||||
kieSession.insert(product);
|
||||
kieSession.fireAllRules();
|
||||
System.out.println(product.getLabel());
|
||||
return product;
|
||||
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.spring.drools.rules;
|
||||
|
||||
import com.baeldung.spring.drools.model.Applicant;
|
||||
|
||||
global com.baeldung.spring.drools.model.SuggestedRole suggestedRole;
|
||||
|
||||
dialect "mvel"
|
||||
|
||||
rule "Suggest Manager Role"
|
||||
when
|
||||
Applicant(experienceInYears > 10)
|
||||
Applicant(currentSalary > 1000000 && currentSalary <= 2500000)
|
||||
then
|
||||
suggestedRole.setRole("Manager");
|
||||
end
|
||||
|
||||
rule "Suggest Senior developer Role"
|
||||
when
|
||||
Applicant(experienceInYears > 5 && experienceInYears <= 10)
|
||||
Applicant(currentSalary > 500000 && currentSalary <= 1500000)
|
||||
then
|
||||
suggestedRole.setRole("Senior developer");
|
||||
end
|
||||
|
||||
rule "Suggest Developer Role"
|
||||
when
|
||||
Applicant(experienceInYears > 0 && experienceInYears <= 5)
|
||||
Applicant(currentSalary > 200000 && currentSalary <= 1000000)
|
||||
then
|
||||
suggestedRole.setRole("Developer");
|
||||
end
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.spring.drools.service;
|
||||
|
||||
import com.baeldung.spring.drools.Application;
|
||||
import com.baeldung.spring.drools.DroolConfiguration;
|
||||
import com.baeldung.spring.drools.model.Applicant;
|
||||
import com.baeldung.spring.drools.model.SuggestedRole;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = {DroolConfiguration.class})
|
||||
public class ApplicantServiceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ApplicantService applicantService;
|
||||
|
||||
@Test
|
||||
public void whenCriteriaMatching_ThenSuggestManagerRole(){
|
||||
Applicant applicant=new Applicant("Davis",37,1600000.0,11);
|
||||
SuggestedRole suggestedRole=new SuggestedRole();
|
||||
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
|
||||
assertEquals("Manager",suggestedRole.getRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCriteriaMatching_ThenSuggestSeniorDeveloperRole(){
|
||||
Applicant applicant=new Applicant("John",37,1200000.0,8);
|
||||
SuggestedRole suggestedRole=new SuggestedRole();
|
||||
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
|
||||
assertEquals("Senior developer",suggestedRole.getRole());
|
||||
}
|
||||
@Test
|
||||
public void whenCriteriaMatching_ThenSuggestDeveloperRole(){
|
||||
Applicant applicant=new Applicant("Davis",37,800000.0,3);
|
||||
SuggestedRole suggestedRole=new SuggestedRole();
|
||||
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
|
||||
assertEquals("Developer",suggestedRole.getRole());
|
||||
}
|
||||
@Test
|
||||
public void whenCriteriaNotMatching_ThenNoRole(){
|
||||
Applicant applicant=new Applicant("John",37,1200000.0,5);
|
||||
SuggestedRole suggestedRole=new SuggestedRole();
|
||||
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
|
||||
assertNull(suggestedRole.getRole());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.spring.drools.service;
|
||||
|
||||
import com.baeldung.spring.drools.Application;
|
||||
import com.baeldung.spring.drools.DroolConfiguration;
|
||||
import com.baeldung.spring.drools.model.Product;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = {DroolConfiguration.class})
|
||||
public class ProductServiceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ProductService productService;
|
||||
|
||||
@Test
|
||||
public void whenProductTypeElectronic_ThenLabelBarcode(){
|
||||
Product product=new Product("Microwave","Electronic");
|
||||
product=productService.applyLabelToProduct(product);
|
||||
assertEquals("BarCode",product.getLabel());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenProductTypeBook_ThenLabelIsbn(){
|
||||
Product product=new Product("AutoBiography","Book");
|
||||
product=productService.applyLabelToProduct(product);
|
||||
assertEquals("ISBN",product.getLabel());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue