This commit is contained in:
nheron 2014-02-14 00:00:49 +01:00
commit 4dbaba5e48
80 changed files with 4193 additions and 0 deletions

View file

@ -0,0 +1,80 @@
package com.sample;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
/**
* This is a sample class to launch a rule.
*/
public class DroolsTest {
public static final void main(String[] args) {
try {
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
// go !
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
ksession.insert(message);
ksession.fireAllRules();
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error: errors) {
System.err.println(error);
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
public static class Message {
public static final int HELLO = 0;
public static final int GOODBYE = 1;
private String message;
private int status;
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return this.status;
}
public void setStatus(int status) {
this.status = status;
}
}
}

View file

@ -0,0 +1,44 @@
package droolscours;
public class Account {
private long accountno;
private double balance;
public long getAccountno() {
return accountno;
}
public void setAccountno(long accountno) {
this.accountno = accountno;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString() {
// TODO Auto-generated method stub
StringBuffer buff = new StringBuffer();
buff.append("-----Account-----)\n");
buff.append("Account no " + this.accountno + "\n");
buff.append("Balance " + this.balance + "\n");
buff.append("-----End Account-)");
return buff.toString();
}
public Account(long accountno, double balance) {
super();
this.accountno = accountno;
this.balance = balance;
}
public Account() {
super();
// TODO Auto-generated constructor stub
}
}

View file

@ -0,0 +1,60 @@
package droolscours;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AccountingPeriod {
private Date startDate;
private Date endDate;
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date start) {
this.startDate = start;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date end) {
this.endDate = end;
}
public AccountingPeriod() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
// TODO Auto-generated method stub
StringBuffer buff = new StringBuffer();
buff.append("-----AccountingPeriod-----)\n");
if (this.startDate != null) {
buff.append("StartDate "
+ DateFormat.getDateInstance().format(this.startDate)
+ "\n");
}else{
buff.append("No start date was set\n");
}
if (this.endDate != null) {
buff.append("EndDate "
+ DateFormat.getDateInstance().format(this.endDate) + "\n");
}else {
buff.append("No ens date was set\n");
}
buff.append("-----End AccountingPeriod -)");
return buff.toString();
}
public AccountingPeriod(Date startDate, Date endDate) {
super();
this.startDate = startDate;
this.endDate = endDate;
}
}

View file

@ -0,0 +1,78 @@
package droolscours;
import java.text.DateFormat;
import java.util.Date;
public class CashFlow {
private Date mvtDate;
private double amount;
private int type;
private long accountNo;
public static int CREDIT = 1;
public static int DEBIT = 2;
public Date getMvtDate() {
return mvtDate;
}
public void setMvtDate(Date date) {
this.mvtDate = date;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public long getAccountNo() {
return accountNo;
}
public void setAccountNo(long accountNo) {
this.accountNo = accountNo;
}
@Override
public String toString() {
// TODO Auto-generated method stub
StringBuffer buff = new StringBuffer();
buff.append("-----CashFlow-----)\n");
buff.append("Account no="+this.accountNo+"\n");
if (this.mvtDate != null) {
buff.append("Mouvement Date= "
+ DateFormat.getDateInstance().format(this.mvtDate)
+ "\n");
}else{
buff.append("No Mouvement date was set\n");
}
buff.append("Mouvement Amount="+this.amount+"\n");
buff.append("-----CashFlow end--)");
return buff.toString();
}
public CashFlow(Date mvtDate, double amount, int type, long accountNo) {
super();
this.mvtDate = mvtDate;
this.amount = amount;
this.type = type;
this.accountNo = accountNo;
}
public CashFlow() {
super();
// TODO Auto-generated constructor stub
}
}

View file

@ -0,0 +1,46 @@
package droolscours;
public class Customer {
private String name;
private String surname;
private String country;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public String toString() {
StringBuffer buff = new StringBuffer();
buff.append("-----Customer-----)\n");
buff.append("Name=" + this.name + "\n");
buff.append("Surname Name=" + this.surname + "\n");
buff.append("Country=" + this.country + "\n");
buff.append("-----Customer end-)");
return buff.toString();
}
public Customer(String name, String surname, String country) {
super();
this.name = name;
this.surname = surname;
this.country = country;
}
public Customer() {
super();
// TODO Auto-generated constructor stub
}
}

View file

@ -0,0 +1,23 @@
package droolscours;
public class PrivateAccount extends Account {
private Customer owner;
public Customer getOwner() {
return owner;
}
public void setOwner(Customer owner) {
this.owner = owner;
}
@Override
public String toString() {
StringBuffer buff = new StringBuffer();
buff.append("-----Private Account-)");
buff.append(super.toString());
buff.append(this.owner.toString());
buff.append("-----Private Account end-)");
return buff.toString();
}
}

View file

@ -0,0 +1,18 @@
package droolscours.service;
import java.util.ArrayList;
import java.util.List;
import droolscours.Customer;
public class CustomerService {
public List<Customer> getListCustomer(){
List<Customer> result = new ArrayList<Customer>();
result.add(new Customer("Héron", "Nicolas", "Fr"));
result.add(new Customer("Héron", "James", "GB"));
result.add(new Customer("Héron", "Nicolas", "GB"));
return result;
}
}

View file

@ -0,0 +1,20 @@
package util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateHelper {
public static String sFormat = "yyyy-MM-dd";
public static Date getDate(String sDate) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
return sdf.parse(sDate);
}
public static Date getDate(String sDate, String anotherFormat)
throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(anotherFormat);
return sdf.parse(sDate);
}
}

View file

@ -0,0 +1,137 @@
package util;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.event.rule.DefaultAgendaEventListener;
import org.drools.event.rule.ActivationCancelledEvent;
import org.drools.event.rule.ActivationCreatedEvent;
import org.drools.event.rule.AfterActivationFiredEvent;
import org.drools.event.rule.AgendaGroupPoppedEvent;
import org.drools.event.rule.AgendaGroupPushedEvent;
import org.drools.event.rule.BeforeActivationFiredEvent;
import org.drools.event.rule.ObjectInsertedEvent;
import org.drools.event.rule.ObjectRetractedEvent;
import org.drools.event.rule.ObjectUpdatedEvent;
import org.drools.event.rule.WorkingMemoryEventListener;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.StatelessKnowledgeSession;
public class KnowledgeSessionHelper {
private static KnowledgeBase createRuleBase(String drlFile) {
KnowledgeBase ruleBase = null;
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource(drlFile),
ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
System.err.println(error);
}
throw new IllegalArgumentException(
"Probleme a la lecture du fichier.");
}
ruleBase = KnowledgeBaseFactory.newKnowledgeBase();
ruleBase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return ruleBase;
}
public static StatelessKnowledgeSession getStatelessKnowledgeSession(
String drlFile) {
KnowledgeBase ruleBase = createRuleBase(drlFile);
return ruleBase.newStatelessKnowledgeSession();
}
public static StatefulKnowledgeSession getStatefulKnowledgeSession(
String drlFile) {
KnowledgeBase ruleBase = createRuleBase(drlFile);
return ruleBase.newStatefulKnowledgeSession();
}
public static StatefulKnowledgeSession getStatefulKnowledgeSessionWithCallback(
String drlFile) {
StatefulKnowledgeSession session = getStatefulKnowledgeSession(drlFile);
session.addEventListener(new WorkingMemoryEventListener() {
@Override
public void objectUpdated(ObjectUpdatedEvent arg0) {
System.out.println("Object mise à jour \n"
+ "Nouvelles valeurs \n" + arg0.getObject().toString());
}
@Override
public void objectRetracted(ObjectRetractedEvent arg0) {
System.out.println("Object retiré \n"
+ arg0.getOldObject().toString());
}
@Override
public void objectInserted(ObjectInsertedEvent arg0) {
System.out.println("Object inséré \n"
+ arg0.getObject().toString());
}
});
session.addEventListener(new DefaultAgendaEventListener() {
@Override
public void beforeActivationFired(BeforeActivationFiredEvent arg0) {
System.out.println("La règle "
+ arg0.getActivation().getRule().getName()
+ " va être déclenchée");
}
@Override
public void agendaGroupPushed(AgendaGroupPushedEvent arg0) {
}
@Override
public void agendaGroupPopped(AgendaGroupPoppedEvent arg0) {
}
@Override
public void afterActivationFired(AfterActivationFiredEvent arg0) {
System.out.println("La règle "
+ arg0.getActivation().getRule().getName()
+ " a êté déclenchée");
}
@Override
public void activationCreated(ActivationCreatedEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void activationCancelled(ActivationCancelledEvent arg0) {
}
});
return session;
}
public static StatefulKnowledgeSession getStatefulKnowledgeSession(
String drlFile,String rfFile) {
KnowledgeBase ruleBase = null;
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource(drlFile),
ResourceType.DRL);
kbuilder.add(ResourceFactory.newClassPathResource(rfFile),
ResourceType.DRF);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
System.err.println(error);
}
throw new IllegalArgumentException(
"Probleme a la lecture du fichier.");
}
ruleBase = KnowledgeBaseFactory.newKnowledgeBase();
ruleBase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return ruleBase.newStatefulKnowledgeSession();
}
}

View file

@ -0,0 +1,10 @@
package util;
public class OutputDisplay {
public void showText(String someText) {
long time = System.currentTimeMillis();
System.out.println("time=" + time + "-" + someText);
}
}

View file

@ -0,0 +1,67 @@
package droolscours;
import org.drools.event.rule.ObjectInsertedEvent;
import org.drools.event.rule.ObjectRetractedEvent;
import org.drools.event.rule.ObjectUpdatedEvent;
import org.drools.event.rule.WorkingMemoryEventListener;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.StatelessKnowledgeSession;
import org.junit.Assert;
import org.junit.Test;
import util.DateHelper;
import util.KnowledgeSessionHelper;
public class PremierEssai {
StatelessKnowledgeSession sessionStateless = null;
StatefulKnowledgeSession sessionStatefull = null;
@Test
public void test1() {
sessionStateless = KnowledgeSessionHelper
.getStatelessKnowledgeSession("Sample.drl");
sessionStateless.execute(new String("Hello"));
}
@Test
public void testSimple() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSession("SimpleSample.drl");
sessionStatefull.addEventListener(new WorkingMemoryEventListener() {
@Override
public void objectUpdated(ObjectUpdatedEvent arg0) {
System.out.println("Object mise à jour \n"
+ "Nouvelles valeurs \n" + arg0.getObject().toString());
}
@Override
public void objectRetracted(ObjectRetractedEvent arg0) {
System.out.println("Object retiré \n"
+ arg0.getOldObject().toString());
}
@Override
public void objectInserted(ObjectInsertedEvent arg0) {
System.out.println("Object inséré \n"
+ arg0.getObject().toString());
}
});
Account account = new Account();
account.setAccountno(1);
sessionStatefull.insert(account);
AccountingPeriod period = new AccountingPeriod();
period.setStartDate(DateHelper.getDate("2010-01-01"));
period.setEndDate(DateHelper.getDate("2010-03-31"));
sessionStatefull.insert(period);
CashFlow action1 = new CashFlow();
action1.setAccountNo(1);
action1.setAmount(1000);
action1.setType(CashFlow.CREDIT);
action1.setMvtDate(DateHelper.getDate("2010-01-02"));
sessionStatefull.insert(action1);
sessionStatefull.fireAllRules();
Assert.assertTrue(account.getBalance() == 1000);
}
}

View file

@ -0,0 +1,64 @@
package droolscours;
import org.drools.runtime.StatefulKnowledgeSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import util.KnowledgeSessionHelper;
import util.OutputDisplay;
public class TestRuleFlow {
StatefulKnowledgeSession sessionStatefull = null;
@Before
public void setUp() throws Exception {
System.out.println("------------Before------------");
}
@After
public void tearDown() throws Exception {
System.out.println("------------Après ------------");
}
@Test
public void testdeuxFait1() {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSession("demo-ruleflow.drl","RuleFlow1.rf");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
Account a = new Account();
sessionStatefull.insert(a);
AccountingPeriod period = new AccountingPeriod();
sessionStatefull.insert(period);
sessionStatefull.startProcess("RF1");
sessionStatefull.fireAllRules();
}
@Test
public void testdeuxFait2() {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSession("demo-ruleflow2.drl","RuleFlow2.rf");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
Account a = new Account();
sessionStatefull.insert(a);
AccountingPeriod period = new AccountingPeriod();
sessionStatefull.insert(period);
sessionStatefull.fireAllRules();
}
@Test
public void testdeuxFait3() {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSession("demo-ruleflow3.drl","RuleFlow3.rf");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
Account a = new Account();
a.setBalance(1500);
sessionStatefull.insert(a);
AccountingPeriod period = new AccountingPeriod();
sessionStatefull.insert(period);
sessionStatefull.fireAllRules();
}
}

View file

@ -0,0 +1,138 @@
package droolscours;
import org.drools.event.rule.ObjectInsertedEvent;
import org.drools.event.rule.ObjectRetractedEvent;
import org.drools.event.rule.ObjectUpdatedEvent;
import org.drools.event.rule.WorkingMemoryEventListener;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.rule.FactHandle;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import util.KnowledgeSessionHelper;
import util.OutputDisplay;
public class TestUnFait {
StatefulKnowledgeSession sessionStatefull = null;
@Before
public void setUp() throws Exception {
System.out.println("------------Before------------");
}
@After
public void tearDown() throws Exception {
System.out.println("------------Après ------------");
}
@Test
public void testUnFaitSansFait() {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSession("demo.drl");
sessionStatefull.fireAllRules();
System.out.println("Avez-vous vu quelquechose ?");
}
@Test
public void testUnFaitAvecAccountCommeFait() {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSession("demo.drl");
Account a = new Account();
sessionStatefull.insert(a);
sessionStatefull.fireAllRules();
System.out.println("Vous avez donc vu quelquechose ;)");
}
@Test
public void testUnFaitAvecAccountCommeFaitGlobal() {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSession("demo-global.drl");
Account a = new Account();
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
sessionStatefull.insert(a);
sessionStatefull.fireAllRules();
System.out.println("Vous avez donc vu quelquechose ;)");
}
@Test
public void testUnFaitAvecAccountCommeFaitGlobalEtAvecCallBack() {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSession("demo.drl");
sessionStatefull.addEventListener(new WorkingMemoryEventListener() {
@Override
public void objectUpdated(ObjectUpdatedEvent arg0) {
System.out.println("Object mise à jour \n"
+ "Nouvelles valeurs \n" + arg0.getObject().toString());
}
@Override
public void objectRetracted(ObjectRetractedEvent arg0) {
System.out.println("Object retiré \n"
+ arg0.getOldObject().toString());
}
@Override
public void objectInserted(ObjectInsertedEvent arg0) {
System.out.println("Object inséré \n"
+ arg0.getObject().toString());
}
});
Account a = new Account();
a.setAccountno(0);
FactHandle handlea = sessionStatefull.insert(a);
a.setAccountno(100);
sessionStatefull.update(handlea, a);
sessionStatefull.retract(handlea);
sessionStatefull.fireAllRules();
System.out.println("Vous avez donc vu quelquechose ;)");
}
@Test
public void testUnFaitAvecAccountRDGDeclenchement1() {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSession("demo.drl");
Account a = new Account();
FactHandle handlea = sessionStatefull.insert(a);
sessionStatefull.fireAllRules();
sessionStatefull.fireAllRules();
}
@Test
public void testUnFaitAvecAccountRDGDeclenchement2() {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSession("demo.drl");
Account a = new Account();
FactHandle handlea = sessionStatefull.insert(a);
sessionStatefull.fireAllRules();
sessionStatefull.update(handlea, a);
sessionStatefull.fireAllRules();
}
@Test
public void testUnFaitAvecAccountInsert() {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSession("demo-insertupdatedelete.drl");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
sessionStatefull.addEventListener(new WorkingMemoryEventListener() {
@Override
public void objectUpdated(ObjectUpdatedEvent arg0) {
System.out.println("Object mise à jour \n"
+ "Nouvelles valeurs \n" + arg0.getObject().toString());
}
@Override
public void objectRetracted(ObjectRetractedEvent arg0) {
System.out.println("Object retiré \n"
+ arg0.getOldObject().toString());
}
@Override
public void objectInserted(ObjectInsertedEvent arg0) {
System.out.println("Object inséré \n"
+ arg0.getObject().toString());
}
});
Account a = new Account();
FactHandle handlea = sessionStatefull.insert(a);
sessionStatefull.fireAllRules();
}
}

View file

@ -0,0 +1,248 @@
package droolscours;
import junit.framework.Assert;
import org.drools.audit.WorkingMemoryFileLogger;
import org.drools.event.rule.ObjectInsertedEvent;
import org.drools.event.rule.ObjectRetractedEvent;
import org.drools.event.rule.ObjectUpdatedEvent;
import org.drools.event.rule.WorkingMemoryEventListener;
import org.drools.runtime.StatefulKnowledgeSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import droolscours.service.CustomerService;
import util.DateHelper;
import util.KnowledgeSessionHelper;
import util.OutputDisplay;
public class Testlanguage {
StatefulKnowledgeSession sessionStatefull = null;
@Before
public void setUp() throws Exception {
System.out.println("------------Before------------");
}
@After
public void tearDown() throws Exception {
System.out.println("------------Après ------------");
}
@Test
public void testdeuxFait1() {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback("demo-Language.drl");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
Account a = new Account();
sessionStatefull.insert(a);
AccountingPeriod period = new AccountingPeriod();
sessionStatefull.insert(period);
sessionStatefull.fireAllRules();
}
@Test
public void testdeuxFait2() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback("demo-Language.drl");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
Account a = new Account();
a.setAccountno(1);
a.setBalance(0);
sessionStatefull.insert(a);
CashFlow cash1 = new CashFlow();
cash1.setAccountNo(1);
cash1.setAmount(1000);
cash1.setMvtDate(DateHelper.getDate("2010-01-15"));
cash1.setType(CashFlow.CREDIT);
sessionStatefull.insert(cash1);
sessionStatefull.fireAllRules();
Assert.assertTrue(a.getBalance()==1000);
}
@Test
public void testdeuxFait3() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback("demo-Language.drl");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
Account a = new Account();
a.setAccountno(1);
a.setBalance(0);
sessionStatefull.insert(a);
CashFlow cash1 = new CashFlow();
cash1.setAccountNo(1);
cash1.setAmount(1000);
cash1.setMvtDate(DateHelper.getDate("2010-01-15"));
cash1.setType(CashFlow.CREDIT);
sessionStatefull.insert(cash1);
CashFlow cash2 = new CashFlow();
cash2.setAccountNo(2);
cash2.setAmount(1000);
cash2.setMvtDate(DateHelper.getDate("2010-01-15"));
cash2.setType(CashFlow.CREDIT);
sessionStatefull.insert(cash2);
sessionStatefull.fireAllRules();
Assert.assertTrue(a.getBalance()==1000);
}
@Test
public void testdeuxFait4() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback("demo-Language2.drl");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
Account a = new Account();
a.setAccountno(1);
a.setBalance(0);
sessionStatefull.insert(a);
CashFlow cash1 = new CashFlow();
cash1.setAccountNo(1);
cash1.setAmount(1000);
cash1.setMvtDate(DateHelper.getDate("2010-01-15"));
cash1.setType(CashFlow.CREDIT);
sessionStatefull.insert(cash1);
CashFlow cash2 = new CashFlow();
cash2.setAccountNo(1);
cash2.setAmount(500);
cash2.setMvtDate(DateHelper.getDate("2010-02-15"));
cash2.setType(CashFlow.DEBIT);
sessionStatefull.insert(cash2);
CashFlow cash3 = new CashFlow();
cash3.setAccountNo(1);
cash3.setAmount(1000);
cash3.setMvtDate(DateHelper.getDate("2010-04-15"));
cash3.setType(CashFlow.CREDIT);
sessionStatefull.insert(cash3);
AccountingPeriod period = new AccountingPeriod();
period.setStartDate(DateHelper.getDate("2010-01-01"));
period.setEndDate(DateHelper.getDate("2010-03-31"));
sessionStatefull.insert(period);
sessionStatefull.fireAllRules();
Assert.assertTrue(a.getBalance()==500);
}
@Test
public void testdeuxFait5() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback("demo-Language3.drl");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
Customer customer = new Customer();
customer.setName("Héron");
customer.setSurname("Nicolas");
PrivateAccount pAccount = new PrivateAccount();
pAccount.setOwner(customer);
sessionStatefull.insert(pAccount);
sessionStatefull.fireAllRules();
}
@Test
public void testdeuxFait6() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback("demo-Language3.drl");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
Customer customer = new Customer();
customer.setName("Héron");
customer.setSurname("Nicolas");
customer.setCountry("GB");
sessionStatefull.insert(customer);
PrivateAccount pAccount = new PrivateAccount();
pAccount.setOwner(customer);
sessionStatefull.insert(pAccount);
sessionStatefull.fireAllRules();
}
@Test
public void testdeuxFait7() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback("demo-Language3.drl");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
Account pAccount = new Account();
sessionStatefull.insert(pAccount);
sessionStatefull.fireAllRules();
}
@Test
public void testdeuxFait8() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback("demo-Language3.drl");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
Account pAccount = new Account();
sessionStatefull.insert(pAccount);
sessionStatefull.fireAllRules();
}
@Test
public void testdeuxFait9() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback("demo-Language4.drl");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
Account a = new Account();
a.setAccountno(1);
a.setBalance(0);
sessionStatefull.insert(a);
CashFlow cash1 = new CashFlow();
cash1.setAccountNo(1);
cash1.setAmount(1000);
cash1.setMvtDate(DateHelper.getDate("2010-01-15"));
cash1.setType(CashFlow.CREDIT);
sessionStatefull.insert(cash1);
CashFlow cash2 = new CashFlow();
cash2.setAccountNo(1);
cash2.setAmount(500);
cash2.setMvtDate(DateHelper.getDate("2010-02-15"));
cash2.setType(CashFlow.DEBIT);
sessionStatefull.insert(cash2);
CashFlow cash3 = new CashFlow();
cash3.setAccountNo(1);
cash3.setAmount(1000);
cash3.setMvtDate(DateHelper.getDate("2010-04-15"));
cash3.setType(CashFlow.CREDIT);
sessionStatefull.insert(cash3);
sessionStatefull.fireAllRules();
}
@Test
public void testdeuxFait10() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback("demo-Language5.drl");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
sessionStatefull.setGlobal("serviceCustomer", new CustomerService());
Customer c = new Customer("Héron","Nicolas","A");
sessionStatefull.insert(c);
sessionStatefull.fireAllRules();
}
@Test
public void testdeuxFait11() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback("demo-Language6.drl");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
Account a = new Account();
a.setAccountno(1);
a.setBalance(0);
sessionStatefull.insert(a);
sessionStatefull.insert(new CashFlow(DateHelper.getDate("2010-01-15"),1000,CashFlow.CREDIT,1));
sessionStatefull.insert(new CashFlow(DateHelper.getDate("2010-02-15"),500,CashFlow.DEBIT,1));
sessionStatefull.insert(new CashFlow(DateHelper.getDate("2010-04-15"),1000,CashFlow.CREDIT,1));
sessionStatefull.insert(new AccountingPeriod(DateHelper.getDate("2010-01-01"),DateHelper.getDate("2010-31-31")));
sessionStatefull.fireAllRules();
}
@Test
public void testdeuxFait12() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback("demo-Language7.drl");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("result", display);
sessionStatefull.insert(new Account(1,0));
sessionStatefull.insert(new CashFlow(DateHelper.getDate("2010-01-15"),1000,CashFlow.CREDIT,1));
sessionStatefull.insert(new CashFlow(DateHelper.getDate("2010-02-15"),500,CashFlow.DEBIT,1));
sessionStatefull.insert(new CashFlow(DateHelper.getDate("2010-04-15"),1000,CashFlow.CREDIT,1));
sessionStatefull.insert(new AccountingPeriod(DateHelper.getDate("2010-01-01"),DateHelper.getDate("2010-31-31")));
sessionStatefull.fireAllRules();
}
}

View file

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<process xmlns="http://drools.org/drools-5.0/process"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/drools-5.0/process drools-processes-5.0.xsd"
type="RuleFlow" name="RuleFlow1" id="RF1" package-name="cours" >
<header>
</header>
<nodes>
<start id="1" name="Start" x="62" y="79" width="80" height="40" />
<ruleSet id="2" name="Verify" x="162" y="77" width="80" height="40" ruleFlowGroup="Group1" />
<ruleSet id="3" name="Calculation" x="267" y="77" width="80" height="40" ruleFlowGroup="Group2" />
<end id="4" name="End" x="372" y="77" width="80" height="40" />
</nodes>
<connections>
<connection from="1" to="2" />
<connection from="2" to="3" />
<connection from="3" to="4" />
</connections>
</process>

View file

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<process xmlns="http://drools.org/drools-5.0/process"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/drools-5.0/process drools-processes-5.0.xsd"
type="RuleFlow" name="RuleFlow2" id="RF2" package-name="cours" >
<header>
</header>
<nodes>
<start id="1" name="Start" x="62" y="79" width="80" height="40" />
<ruleSet id="2" name="Verify" x="162" y="77" width="80" height="40" ruleFlowGroup="Group1" />
<ruleSet id="3" name="Calculation" x="267" y="77" width="80" height="40" ruleFlowGroup="Group2" />
<end id="4" name="End" x="372" y="77" width="80" height="40" />
</nodes>
<connections>
<connection from="1" to="2" />
<connection from="2" to="3" />
<connection from="3" to="4" />
</connections>
</process>

View file

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<process xmlns="http://drools.org/drools-5.0/process"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/drools-5.0/process drools-processes-5.0.xsd"
type="RuleFlow" name="RuleFlow3" id="RF2" package-name="cours" >
<header>
<imports>
<import name="droolscours.Account" />
</imports>
</header>
<nodes>
<start id="1" name="Start" x="27" y="78" width="80" height="40" />
<ruleSet id="2" name="Calculate2" x="264" y="135" width="80" height="40" ruleFlowGroup="Group2" />
<ruleSet id="3" name="Calculate1" x="266" y="28" width="80" height="40" ruleFlowGroup="Group1" />
<end id="4" name="End" x="487" y="85" width="80" height="40" />
<join id="6" name="Join" x="357" y="91" width="80" height="40" type="2" />
<split id="7" name="Split" x="131" y="93" width="80" height="40" type="3" >
<constraints>
<constraint toNodeId="2" toType="DROOLS_DEFAULT" name="constraint" priority="1" type="rule" dialect="mvel" >Account(balance &lt;= 1000 )</constraint>
<constraint toNodeId="3" toType="DROOLS_DEFAULT" name="constraint" priority="1" type="rule" dialect="mvel" >Account(balance &gt; 1000 )</constraint>
</constraints>
</split>
</nodes>
<connections>
<connection from="7" to="2" />
<connection from="7" to="3" />
<connection from="6" to="4" />
<connection from="3" to="6" />
<connection from="2" to="6" />
<connection from="1" to="7" />
</connections>
</process>

View file

@ -0,0 +1,20 @@
package com.sample
import com.sample.DroolsTest.Message;
rule "Hello World"
when
m : Message( status == Message.HELLO, myMessage : message )
then
System.out.println( myMessage );
m.setMessage( "Goodbye cruel world" );
m.setStatus( Message.GOODBYE );
update( m );
end
rule "GoodBye"
when
Message( status == Message.GOODBYE, myMessage : message )
then
System.out.println( myMessage );
end

View file

@ -0,0 +1,17 @@
package com.sample
import com.sample.DroolsTest.Message;
import droolscours.Account;
import droolscours.AccountingPeriod;
import droolscours.CashFlow;
import util.DateHelper;
rule "Credit Rule"
when
acc: Account( no : accountno )
period : AccountingPeriod(s : startDate , e: endDate )
act : CashFlow( type == CashFlow.CREDIT ,mvtDate >= s && mvtDate <=e ,accountNo ==no)
then
acc.setBalance(acc.getBalance()+act.getAmount());
end

View file

@ -0,0 +1,34 @@
//#created on: 30 oct. 2010
package cours
//#list any import classes here.
import droolscours.Account;
import droolscours.AccountingPeriod;
import droolscours.CashFlow;
import util.OutputDisplay;
//#declare any global variables here
global OutputDisplay result;
rule "Account and accounting period both exist"
when
Account( )
AccountingPeriod( )
then
result.showText("Un compte est un accountingperiod existe");
end
rule "Credit rule"
when
$cash :CashFlow( $no : accountNo ,type == CashFlow.CREDIT )
$acc : Account(accountno ==$no )
then
$acc.setBalance($acc.getBalance()+$cash.getAmount());
result.showText("le compte no "+$no+ " a maintenant une valeur de "+$acc.getBalance());
end

View file

@ -0,0 +1,31 @@
//#created on: 30 oct. 2010
package cours
import droolscours.Account;
import droolscours.AccountingPeriod;
import droolscours.CashFlow;
import util.OutputDisplay;
global OutputDisplay result;
rule "Credit rule"
when
$cash :CashFlow( $aDate : mvtDate, $no : accountNo ,type == CashFlow.CREDIT )
$acc : Account(accountno ==$no )
$period : AccountingPeriod( startDate <= $aDate && endDate >= $aDate)
then
$acc.setBalance($acc.getBalance()+$cash.getAmount());
result.showText("le compte no "+$no+ " a maintenant une valeur de "+$acc.getBalance());
end
rule "Debit rule"
when
$cash :CashFlow( $aDate : mvtDate, $no : accountNo ,type == CashFlow.DEBIT )
$acc : Account(accountno ==$no )
$period : AccountingPeriod( startDate <= $aDate && endDate >= $aDate)
then
$acc.setBalance($acc.getBalance()-$cash.getAmount());
result.showText("le compte no "+$no+ " a maintenant une valeur de "+$acc.getBalance());
end

View file

@ -0,0 +1,51 @@
//#created on: 30 oct. 2010
package cours
//#list any import classes here.
import droolscours.Account;
import droolscours.AccountingPeriod;
import droolscours.CashFlow;
import droolscours.PrivateAccount;
import droolscours.Customer;
import util.OutputDisplay;
//#declare any global variables here
global OutputDisplay result;
rule "Le cashFlow est du crédit ou du débit"
when
$cash :CashFlow(type in ( CashFlow.DEBIT,CashFlow.CREDIT) )
then
result.showText("Cash Flow est un crédit ou un débit");
end
rule "Accessor"
when
$cash :PrivateAccount( owner.name =="Héron" )
then
result.showText("Account is owned by Héron");
end
rule "infixAnd"
when
( $c1 : Customer ( country=="GB") and PrivateAccount( owner==$c1))
or
( $c1 : Customer (country=="US") and PrivateAccount( owner==$c1))
then
result.showText("Person lives in GB or US");
end
rule "no customer"
when
not Customer( )
then
result.showText("No customer");
end
rule "Exists"
when
exists Account( )
then
result.showText("Account exists");
end

View file

@ -0,0 +1,23 @@
//#created on: 30 oct. 2010
package cours
//#list any import classes here.
import droolscours.Account;
import droolscours.AccountingPeriod;
import droolscours.CashFlow;
import droolscours.PrivateAccount;
import droolscours.Customer;
import util.OutputDisplay;
//#declare any global variables here
global OutputDisplay result;
rule "ForAll"
when
$acc : Account()
forall ( Account( this==$acc, $no : accountno )
CashFlow( accountNo == $no)
)
then
result.showText("All mouvements concerns account no " + $acc.getAccountno());
end

View file

@ -0,0 +1,22 @@
//#created on: 30 oct. 2010
package cours
//#list any import classes here.
import droolscours.Account;
import droolscours.AccountingPeriod;
import droolscours.CashFlow;
import droolscours.PrivateAccount;
import droolscours.Customer;
import droolscours.service.CustomerService;
import util.OutputDisplay;
//#declare any global variables here
global OutputDisplay result;
global CustomerService serviceCustomer;
rule "ForAll"
when
$c : Customer()
$cc : Customer(name ==$c.name,surname==$c.surname,country !=$c.country) from serviceCustomer.getListCustomer();
then
result.showText("Found same customer in 2 countries");
end

View file

@ -0,0 +1,37 @@
//#created on: 30 oct. 2010
package cours
//#list any import classes here.
import droolscours.Account;
import droolscours.AccountingPeriod;
import droolscours.CashFlow;
import droolscours.PrivateAccount;
import droolscours.Customer;
import droolscours.service.CustomerService;
import java.util.ArrayList;
import util.OutputDisplay;
//#declare any global variables here
global OutputDisplay result;
global CustomerService serviceCustomer;
rule "More then 2 CashFlow Line"
when
$c : Account( $acc : accountno )
$p : AccountingPeriod ($sDate : startDate ,$eDate : endDate )
$number : ArrayList(size >= 2 )
from collect( CashFlow( mvtDate >= $sDate && mvtDate <= $eDate,accountNo == $acc ) )
then
result.showText("Found more than 2 CashFlow Lines");
end
rule "Numbers of CashFlow Line"
when
$c : Account( $acc : accountno )
$p : AccountingPeriod ($sDate : startDate ,$eDate : endDate )
$number : ArrayList( )
from collect( CashFlow( mvtDate >= $sDate && mvtDate <= $eDate,accountNo == $acc ) )
then
result.showText("Found "+$number+" more than 2 CashFlow Lines");
end

View file

@ -0,0 +1,38 @@
//#created on: 30 oct. 2010
package cours
//#list any import classes here.
import droolscours.Account;
import droolscours.AccountingPeriod;
import droolscours.CashFlow;
import droolscours.PrivateAccount;
import droolscours.Customer;
import droolscours.service.CustomerService;
import java.util.ArrayList;
import util.OutputDisplay;
//#declare any global variables here
global OutputDisplay result;
global CustomerService serviceCustomer;
rule "Credit and Debit Rule"
when
$c : Account( $acc : accountno )
$p : AccountingPeriod ($sDate : startDate ,$eDate : endDate )
$totalCredit : Number( doubleValue > 100 )
from accumulate( CashFlow( type ==CashFlow.CREDIT,$value : amount, mvtDate >= $sDate && mvtDate <= $eDate,accountNo == $acc ),
init( double total = 0; ),
action( total += $value; ),
reverse( total -= $value; ),
result( total ) )
$totalDebit : Number( doubleValue > 100 )
from accumulate( CashFlow( type ==CashFlow.DEBIT,$value : amount, mvtDate >= $sDate && mvtDate <= $eDate,accountNo == $acc ),
init( double total = 0; ),
action( total += $value; ),
reverse( total -= $value; ),
result( total ) )
then
result.showText(" Found "+$totalCredit+" as a credit");
result.showText(" Found "+$totalDebit+" as a debit");
end

View file

@ -0,0 +1,30 @@
//#created on: 30 oct. 2010
package cours
//list any import classes here.
import droolscours.Account;
import util.OutputDisplay;
//#declare any global variables here
global OutputDisplay result;
rule "Your First Rule revisited"
when
Account( )
then
result.showText("Le compte existe donc déjà");
end
rule "Your Second Rule"
//#include attributes such as "salience" here...
when
//#conditions
then
//#actions
end

View file

@ -0,0 +1,23 @@
//#created on: 30 oct. 2010
package cours
//#list any import classes here.
import droolscours.Account;
import droolscours.AccountingPeriod;
import util.OutputDisplay;
//#declare any global variables here
global OutputDisplay result;
rule "Your First Rule revisited"
when
Account( )
then
result.showText("Le compte existe donc déjà");
AccountingPeriod newPeriod = new AccountingPeriod();
insert (newPeriod);
end
rule "Rule on Period"
when
AccountingPeriod ()
then
result.showText("Période de compte existe");
end

View file

@ -0,0 +1,32 @@
//#created on: 30 oct. 2010
package cours
//#list any import classes here.
import droolscours.Account;
import droolscours.AccountingPeriod;
import droolscours.CashFlow;
import util.OutputDisplay;
//#declare any global variables here
global OutputDisplay result;
rule "Account group1"
ruleflow-group "Group1"
when
Account( )
then
result.showText("Account in Group1");
end
rule "Account group2"
ruleflow-group "Group2"
when
Account( )
then
result.showText("Account in Group2");
end

View file

@ -0,0 +1,37 @@
//#created on: 30 oct. 2010
package cours
//#list any import classes here.
import droolscours.Account;
import droolscours.AccountingPeriod;
import droolscours.CashFlow;
import util.OutputDisplay;
//#declare any global variables here
global OutputDisplay result;
rule "start process"
when
then
kcontext.getKnowledgeRuntime().startProcess("RF2");
end
rule "Account group1"
ruleflow-group "Group1"
when
Account( )
then
result.showText("Account in Group1");
end
rule "Account group2"
ruleflow-group "Group2"
when
Account( )
then
result.showText("Account in Group2");
end

View file

@ -0,0 +1,37 @@
//#created on: 30 oct. 2010
package cours
//#list any import classes here.
import droolscours.Account;
import droolscours.AccountingPeriod;
import droolscours.CashFlow;
import util.OutputDisplay;
//#declare any global variables here
global OutputDisplay result;
rule "start process"
when
then
kcontext.getKnowledgeRuntime().startProcess("RF2");
end
rule "Account group1"
ruleflow-group "Group1"
when
Account(balance > 0 )
then
result.showText("Account in Group1 > 1000 ");
end
rule "Account group2"
ruleflow-group "Group2"
when
Account( )
then
result.showText("Account in Group2 < 1000");
end

View file

@ -0,0 +1,21 @@
//#created on: 30 oct. 2010
package cours
//list any import classes here.
import droolscours.Account;
import util.OutputDisplay;
//declare any global variables here
global OutputDisplay result;
rule "Your First Rule"
when
Account( )
then
System.out.println("Le compte existe");
end