Book.java
package com.test.domain;
public class Book {
private int id = 0;
private String name = "";
private String author = "";
private String summary = "";
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
}
Drools.java
package com.test.drools;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
public class Drools {
private PackageFactory packageFactory = null;
private String location = "";
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public PackageFactory getPackageFactory() {
return packageFactory;
}
public void setPackageFactory(PackageFactory packageFactory) {
this.packageFactory = packageFactory;
}
public void run(int status) throws Exception {
final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage(this.packageFactory.run(location));
final StatefulSession session = ruleBase.newStatefulSession();
final Message message = new Message();
message.setMessage("Hello World");
message.setStatus(status);
session.insert(message);
session.fireAllRules();
session.dispose();
}
}
HelloWorld.drl
package test
import com.test.drools.Message;
rule "Hello World"
dialect "mvel"
when
m:Message( status == Message.HELLO, message : message)
then
System.out.println(message);
modify(m){ message = "Goodbye cruel world",
status = Message.GOODBYE};
end
rule "Good Bye"
dialect "java"
when
Message( status == Message.GOODBYE, message : message)
then
System.out.println(message);
end
Message.java
package com.test.drools;
public class Message {
public static final int HELLO = 0;
public static final int GOODBYE = 1;
private String message = "";
private int status = 0;
public String getMessage() {
return message;
}
public void setMessage(final String message) {
this.message = message;
}
public int getStatus() {
return status;
}
public void setStatus(final int status) {
this.status = status;
}
}
PackageFactory.java
package com.test.drools;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import org.drools.compiler.DroolsParserException;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
public class PackageFactory {
public Package run(String location) throws DroolsParserException, IOException {
final Reader source = new InputStreamReader( this.getClass
().getResourceAsStream(location));
final PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl(source);
if(builder.hasErrors()) {
System.out.println(builder.getErrors().toString());
throw new RuntimeException( "Unable to compile /"" + location +
"/"." );
}
final Package pkg = builder.getPackage();
return pkg;
}
}
Test.java
package com.test.drools;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PackageFactory pf = new PackageFactory();
Drools drools = new Drools();
drools.setLocation("HelloWorld.drl");
drools.setPackageFactory(pf);
try {
drools.run(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
BookManager.java
package com.test.logic.business;
import java.util.List;
import com.test.domain.Book;
import com.test.persistence.dao.impl.BookDao;
public class BookManager implements IBookManager {
private BookDao bookDao = null;
public BookDao getBookDao() {
return bookDao;
}
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public List selectAllBooks(int top, int max) {
return this.bookDao.selectAllBooks(top, max);
}
public List findBooks(String name) {
return this.bookDao.selectBookByName(name);
}
public int getBookTotal() {
return this.bookDao.getBookTotal();
}
public Book selectBookById(int id) {
return this.bookDao.selectBookById(id);
}
public List selectBookByName(String name) {
return this.bookDao.selectBookByName(name);
}
public void insertBook(Book book) {
this.bookDao.insertBook(book);
}
public void updateBook(Book book) {
this.bookDao.updateBook(book);
}
public void deleteBookById(int id) {
this.bookDao.deleteBookById(id);
}
}
BookService.java
package com.test.logic.service;
import java.util.List;
import com.test.domain.Book;
import com.test.logic.business.BookManager;
import com.test.logic.business.IBookManager;
public class BookService implements IBookService{
private BookManager bookManager = null;
public BookManager getBookManager() {
return bookManager;
}
public void setBookManager(BookManager bookManager) {
this.bookManager = bookManager;
}
public List selectAllBooks(int top, int max) {
return this.bookManager.selectAllBooks(top, max);
}
public List findBooks(String name) {
return this.bookManager.findBooks(name);
}
public int getBookTotal() {
return this.bookManager.getBookTotal();
}
public Book getBookById(int id) {
return this.bookManager.selectBookById(id);
}
public void save(Book book) {
if(book.getId() > 0) {
this.bookManager.updateBook(book);
} else {
this.bookManager.insertBook(book);
}
}
public void deleteBookById(int id) {
this.bookManager.deleteBookById(id);
}
}