This commit is contained in:
Nicolas Héron 2016-06-03 11:33:48 +02:00
commit 3b7807aba6
13 changed files with 114 additions and 186 deletions

View file

@ -4,5 +4,7 @@
<kbase name="lesson2" packages="lesson2"> <kbase name="lesson2" packages="lesson2">
<ksession name="lesson2-session"/> <ksession name="lesson2-session"/>
</kbase> </kbase>
<kbase name="lesson2a" packages="lesson2a">
<ksession name="lesson2a-session"/>
</kbase>
</kmodule> </kmodule>

View file

@ -7,7 +7,7 @@ import droolscours.AccountingPeriod;
import droolscours.CashFlow; import droolscours.CashFlow;
import droolscours.util.OutputDisplay; import droolscours.util.OutputDisplay;
global OutputDisplay showResult; global OutputDisplay showResults;
rule "Credit rule" rule "Credit rule"
@ -17,7 +17,7 @@ rule "Credit rule"
$period : AccountingPeriod( startDate <= $aDate && endDate >= $aDate) $period : AccountingPeriod( startDate <= $aDate && endDate >= $aDate)
then then
$acc.setBalance($acc.getBalance()+$cash.getAmount()); $acc.setBalance($acc.getBalance()+$cash.getAmount());
showResult.showText("Account no "+$acc.getAccountno()+ " has now a balance of "+$acc.getBalance()); showResults.showText("Account no "+$acc.getAccountno()+ " has now a balance of "+$acc.getBalance());
end end
rule "Debit rule" rule "Debit rule"
@ -27,6 +27,6 @@ rule "Debit rule"
$period : AccountingPeriod( startDate <= $aDate && endDate >= $aDate) $period : AccountingPeriod( startDate <= $aDate && endDate >= $aDate)
then then
$acc.setBalance($acc.getBalance()-$cash.getAmount()); $acc.setBalance($acc.getBalance()-$cash.getAmount());
showResult.showText("Account no "+$acc.getAccountno()+ " has now a balance of "+$acc.getBalance()); showResults.showText("Account no "+$acc.getAccountno()+ " has now a balance of "+$acc.getBalance());
end end

View file

@ -0,0 +1,30 @@
package cours
import droolscours.Account;
import droolscours.AccountingPeriod;
import droolscours.CashFlow;
import droolscours.util.OutputDisplay;
global OutputDisplay showResults;
rule "Credit rule"
when
$cash :CashFlow( $aDate : mvtDate, $no : accountNo ,type == CashFlow.CREDIT )
$acc : Account(accountno ==$no )
then
$acc.setBalance($acc.getBalance()+$cash.getAmount());
showResults.showText("Account no "+$acc.getAccountno()+ " has now a balance of "+$acc.getBalance());
end
rule "Debit rule"
when
$cash :CashFlow( $aDate : mvtDate, $no : accountNo ,type == CashFlow.DEBIT )
$acc : Account(accountno ==$no )
then
$acc.setBalance($acc.getBalance()-$cash.getAmount());
showResults.showText("Account no "+$acc.getAccountno()+ " has now a balance of "+$acc.getBalance());
end

View file

@ -1,6 +1,7 @@
package droolscours; package droolscours;
import droolscours.util.OutputDisplay; import droolscours.util.OutputDisplay;
import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieContainer;
@ -20,25 +21,12 @@ public class Testlesson2 {
kieContainer=KnowledgeSessionHelper.createRuleBase(); kieContainer=KnowledgeSessionHelper.createRuleBase();
} }
@Test
public void testdeuxFait1() {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback(kieContainer,"lesson2-session");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("showResult", display);
Account a = new Account();
sessionStatefull.insert(a);
AccountingPeriod period = new AccountingPeriod();
sessionStatefull.insert(period);
sessionStatefull.fireAllRules();
}
@Test @Test
public void testdeuxFait2() throws Exception { public void testTwoFacts() {
sessionStatefull = KnowledgeSessionHelper sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback(kieContainer,"lesson2-session"); .getStatefulKnowledgeSessionWithCallback(kieContainer, "lesson2a-session");
OutputDisplay display = new OutputDisplay(); OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("showResult", display); sessionStatefull.setGlobal("showResults", display);
Account a = new Account(); Account a = new Account();
a.setAccountno(1); a.setAccountno(1);
a.setBalance(0); a.setBalance(0);
@ -46,18 +34,18 @@ public class Testlesson2 {
CashFlow cash1 = new CashFlow(); CashFlow cash1 = new CashFlow();
cash1.setAccountNo(1); cash1.setAccountNo(1);
cash1.setAmount(1000); cash1.setAmount(1000);
cash1.setMvtDate(DateHelper.getDate("2010-01-15"));
cash1.setType(CashFlow.CREDIT); cash1.setType(CashFlow.CREDIT);
sessionStatefull.insert(cash1); sessionStatefull.insert(cash1);
sessionStatefull.fireAllRules(); sessionStatefull.fireAllRules();
junit.framework.Assert.assertTrue(a.getBalance()==1000); Assert.assertEquals(a.getBalance(), 1000, 0);
} }
@Test @Test
public void testdeuxFait3() throws Exception { public void testTwofactsTwocashFlowMovement() throws Exception {
sessionStatefull = KnowledgeSessionHelper sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback(kieContainer,"lesson2-session"); .getStatefulKnowledgeSessionWithCallback(kieContainer, "lesson2a-session");
OutputDisplay display = new OutputDisplay(); OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("showResult", display); sessionStatefull.setGlobal("showResults", display);
Account a = new Account(); Account a = new Account();
a.setAccountno(1); a.setAccountno(1);
a.setBalance(0); a.setBalance(0);
@ -75,6 +63,42 @@ public class Testlesson2 {
cash2.setType(CashFlow.CREDIT); cash2.setType(CashFlow.CREDIT);
sessionStatefull.insert(cash2); sessionStatefull.insert(cash2);
sessionStatefull.fireAllRules(); sessionStatefull.fireAllRules();
junit.framework.Assert.assertTrue(a.getBalance()==1000); Assert.assertEquals(a.getBalance(), 1000, 0);
}
@Test
public void testcalculateBalance() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback(kieContainer, "lesson2-session");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("showResults", 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("2016-01-15"));
cash1.setType(CashFlow.CREDIT);
sessionStatefull.insert(cash1);
CashFlow cash2 = new CashFlow();
cash2.setAccountNo(1);
cash2.setAmount(500);
cash2.setMvtDate(DateHelper.getDate("2016-02-15"));
cash2.setType(CashFlow.DEBIT);
sessionStatefull.insert(cash2);
CashFlow cash3 = new CashFlow();
cash3.setAccountNo(1);
cash3.setAmount(1000);
cash3.setMvtDate(DateHelper.getDate("2016-04-15"));
cash3.setType(CashFlow.CREDIT);
sessionStatefull.insert(cash3);
AccountingPeriod period = new AccountingPeriod();
period.setStartDate(DateHelper.getDate("2016-01-01"));
period.setEndDate(DateHelper.getDate("2016-03-31"));
sessionStatefull.insert(period);
sessionStatefull.fireAllRules();
Assert.assertTrue(a.getBalance() == 500);
} }
} }

View file

@ -82,36 +82,7 @@ public class Testlesson31 {
sessionStatefull.fireAllRules(); sessionStatefull.fireAllRules();
} }
@Test
public void testForALl() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionWithCallback(kieContainer, "lesson31-session");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("showResult", display);
Account a = new Account();
a.setAccountno(1);
a.setBalance(0);
sessionStatefull.insert(a);
CashFlow cash1 = new CashFlow();
cash1.setAccountNo(1);
sessionStatefull.insert(cash1);
CashFlow cash2 = new CashFlow();
cash2.setAccountNo(1);
sessionStatefull.insert(cash2);
Account a2 = new Account();
a2.setAccountno(2);
a2.setBalance(0);
sessionStatefull.insert(a2);
CashFlow cash3 = new CashFlow();
cash3.setAccountNo(2);
sessionStatefull.insert(cash3);
sessionStatefull.fireAllRules();
}
} }

View file

@ -1,5 +1,6 @@
package droolscours; package droolscours;
import droolscours.util.OutputDisplay;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieContainer;
@ -19,11 +20,34 @@ public class Testlesson32 {
} }
@Test @Test
public void testUnFaitSansFait() { public void testForALl() throws Exception {
sessionStatefull = KnowledgeSessionHelper sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSession(kieContainer, "lesson1-session"); .getStatefulKnowledgeSessionWithCallback(kieContainer, "lesson32-session");
OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("showResult", display);
Account a = new Account();
a.setAccountno(1);
a.setBalance(0);
sessionStatefull.insert(a);
CashFlow cash1 = new CashFlow();
cash1.setAccountNo(1);
sessionStatefull.insert(cash1);
CashFlow cash2 = new CashFlow();
cash2.setAccountNo(1);
sessionStatefull.insert(cash2);
Account a2 = new Account();
a2.setAccountno(2);
a2.setBalance(0);
sessionStatefull.insert(a2);
CashFlow cash3 = new CashFlow();
cash3.setAccountNo(2);
sessionStatefull.insert(cash3);
sessionStatefull.fireAllRules(); sessionStatefull.fireAllRules();
System.out.println("Did you see something ?");
} }
} }

View file

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false"> <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/../drools-lesson8/target/classes" /> <output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/../drools-lesson8/target/test-classes" /> <output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />

View file

@ -9,7 +9,7 @@
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>drools-lesson8</artifactId> <artifactId>drools-lesson4</artifactId>
<dependencies> <dependencies>
<dependency> <dependency>

View file

@ -32,7 +32,7 @@ public class Testlesson4 {
@Test @Test
public void testRuleFlow1() { public void testRuleFlow1() {
sessionStatefull = KnowledgeSessionHelper sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionForJBPM(kieContainer, "ksession-lesson4"); .getStatefulKnowledgeSessionForJBPM(kieContainer, "lesson4-session");
OutputDisplay display = new OutputDisplay(); OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("showResult", display); sessionStatefull.setGlobal("showResult", display);
Account a = new Account(); Account a = new Account();
@ -43,7 +43,7 @@ public class Testlesson4 {
@Test @Test
public void testRuleFlow2() { public void testRuleFlow2() {
sessionStatefull = KnowledgeSessionHelper sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSessionForJBPM(kieContainer, "ksession-lesson4"); .getStatefulKnowledgeSessionForJBPM(kieContainer, "lesson4-session");
OutputDisplay display = new OutputDisplay(); OutputDisplay display = new OutputDisplay();
sessionStatefull.setGlobal("showResult", display); sessionStatefull.setGlobal("showResult", display);
Account a = new Account(); Account a = new Account();

View file

@ -1,64 +0,0 @@
package droolscours;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.kie.api.event.rule.ObjectDeletedEvent;
import org.kie.api.event.rule.ObjectInsertedEvent;
import org.kie.api.event.rule.ObjectUpdatedEvent;
import org.kie.api.event.rule.RuleRuntimeEventListener;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.StatelessKieSession;
import util.DateHelper;
import util.KnowledgeSessionHelper;
public class PremierEssai {
static KieContainer kieContainer;
StatelessKieSession sessionStateless = null;
KieSession sessionStatefull = null;
@BeforeClass
public static void beforeClass() {
kieContainer = KnowledgeSessionHelper.createRuleBase();
}
@Test
public void testSimple() throws Exception {
sessionStatefull = KnowledgeSessionHelper
.getStatefulKnowledgeSession(kieContainer, "simplesample-session");
sessionStatefull.addEventListener(new RuleRuntimeEventListener() {
public void objectInserted(ObjectInsertedEvent event) {
System.out.println("Object inserted \n"
+ event.getObject().toString());
}
public void objectUpdated(ObjectUpdatedEvent event) {
System.out.println("Object was updated \n"
+ "new Content \n" + event.getObject().toString());
}
public void objectDeleted(ObjectDeletedEvent event) {
System.out.println("Object retracted \n"
+ event.getOldObject().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

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

View file

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

View file

@ -1,15 +0,0 @@
package com.sample
import droolscours.Account;
import droolscours.AccountingPeriod;
import droolscours.CashFlow;
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